ACE中的很多类使用了单例模式,为了便于管理单例对象,ACE使用了一个组件——ACE_Framework_Component来专门管理。

我们以ACE_Reactor这个单例类的创建和释放为例。

1、Reactor.cpp中,包括了类的创建释放。其中,单例模式的接口有两个instance函数提供——有参和无参。

首先来看无参instance:

 ACE_Reactor *
ACE_Reactor::instance (void)
{
ACE_TRACE ("ACE_Reactor::instance"); if (ACE_Reactor::reactor_ == )
{
// Perform Double-Checked Locking Optimization.
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Static_Object_Lock::instance (), )); if (ACE_Reactor::reactor_ == )
{
ACE_NEW_RETURN (ACE_Reactor::reactor_,
ACE_Reactor,
);
ACE_Reactor::delete_reactor_ = true; //由于动态生成了一个ACE_Reactor对象,所以将delete_reactor_字段设为true
ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Reactor, ACE_Reactor::reactor_) //将Singleton对象注册到一个统一的管理器中
}
}
return ACE_Reactor::reactor_;
}

这里使用了经典的双检锁的实现方式。ACE_MT宏会根据系统是否启用多线程来采取相应的操作:如果系统没有启用多线程,那么它的定义为空,这样可以避免给单线程系统添加额外的负担;否则它会执行宏定义的操作,实现方式如下:

 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != )
# define ACE_MT(X) X
# else
# define ACE_MT(X)
# endif /* ACE_MT_SAFE */

有参instance:

 ACE_Reactor *
ACE_Reactor::instance (ACE_Reactor *r, bool delete_reactor)
{
ACE_TRACE ("ACE_Reactor::instance"); ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Static_Object_Lock::instance (), ));
ACE_Reactor *t = ACE_Reactor::reactor_;
ACE_Reactor::delete_reactor_ = delete_reactor; ACE_Reactor::reactor_ = r; // We can't register the Reactor singleton as a framework component twice.
// Therefore we test to see if we had an existing reactor instance, which
// if so means it must have already been registered.
if (t == )
ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Reactor, ACE_Reactor::reactor_); return t;
}

这里是为了保证灵活性,我们可以创建一个需要的定制reactor并传入ACE_Reactor。具体使用方式如下:

 Ace_Reactor_Impl *impl=;
impl = new ACE_Select_Reactor();
ACE_Reactor *reactor=;
reactor=new ACE_Reactor(impl);
ACE_Reactor::instance(reactor);

2、生成了单例对象后,通过宏ACE_REGISTER_FRAMEWORK_COMPONENT将其注册到ACE_Framework_Component中,其定义如下:

/// This macro should be called in the instance() method
/// of the Concrete class that will be managed. Along
/// with the appropriate template instantiation.
#define ACE_REGISTER_FRAMEWORK_COMPONENT(CLASS, INSTANCE) \
ACE_Framework_Repository::instance ()->register_component \
(new ACE_Framework_Component_T<CLASS> (INSTANCE));

可以看到其将Singleton对象封装进了一个接口类ACE_Framework_Component_T并注册到ACE_Framework_Repository中进行统一管理。

3、下面来看一下ACE_Framework_Component_T类的实现。

ACE_Framework_Component_T.h

 ACE_BEGIN_VERSIONED_NAMESPACE_DECL

 /**
* @class ACE_Framework_Component_T
*
* @brief This class inherits the interface of the abstract
* ACE_Framework_Component class and is instantiated with the
* implementation of the concrete component class @c class Concrete.
*
* This design is similar to the Adapter and Decorator patterns
* from the ``Gang of Four'' book. Note that @c class Concrete
* need not inherit from a common class since ACE_Framework_Component
* provides the uniform virtual interface! (implementation based on
* ACE_Dumpable_Adapter in <ace/Dump_T.h>.
*/
template <class Concrete>
class ACE_Framework_Component_T : public ACE_Framework_Component
{
public:
// = Initialization and termination methods. /// Constructor.
ACE_Framework_Component_T (Concrete *concrete); /// Destructor.
~ACE_Framework_Component_T (void); /// Close the contained singleton.
void close_singleton (void); ACE_ALLOC_HOOK_DECLARE;
}; ACE_END_VERSIONED_NAMESPACE_DECL

ACE_Framework_Component_T.cpp

 ACE_BEGIN_VERSIONED_NAMESPACE_DECL

 template <class Concrete>
ACE_Framework_Component_T<Concrete>::ACE_Framework_Component_T (Concrete *concrete)
: ACE_Framework_Component ((void *) concrete, concrete->dll_name (), concrete->name ())
{
ACE_TRACE ("ACE_Framework_Component_T<Concrete>::ctor");
} template <class Concrete>
ACE_Framework_Component_T<Concrete>::~ACE_Framework_Component_T (void)
{
ACE_TRACE ("ACE_Framework_Component_T<Concrete>::~ACE_Framework_Component_T");
Concrete::close_singleton ();
} ACE_ALLOC_HOOK_DEFINE_Tt(ACE_Framework_Component_T) template <class Concrete> void
ACE_Framework_Component_T<Concrete>::close_singleton (void)
{
ACE_TRACE ("ACE_Framework_Component_T<Concrete>::close_singleton");
Concrete::close_singleton ();
} ACE_END_VERSIONED_NAMESPACE_DECL

可以看到该类主要有构造函数、析构函数、和close_singleton。其中close_singleton会调用模板concrete即传入的单例对象的静态close_singleton函数。

 void
ACE_Reactor::close_singleton (void)
{
ACE_TRACE ("ACE_Reactor::close_singleton"); ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Static_Object_Lock::instance ())); if (ACE_Reactor::delete_reactor_)
{
delete ACE_Reactor::reactor_;
ACE_Reactor::reactor_ = ;
ACE_Reactor::delete_reactor_ = false;
}
}

4、对于ACE_Framework_Repository,register_component函数首先会遍历component查找该singleton对象是否已存在,而后会将其加入列表。

 int
ACE_Framework_Repository::register_component (ACE_Framework_Component *fc)
{
ACE_TRACE ("ACE_Framework_Repository::register_component");
ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -);
int i; // Check to see if it's already registered
for (i = ; i < this->current_size_; i++)
if (this->component_vector_[i] &&
fc->this_ == this->component_vector_[i]->this_)
{
ACELIB_ERROR_RETURN ((LM_ERROR,
"AFR::register_component: error, compenent already registered\n"),
-);
} if (i < this->total_size_)
{
this->component_vector_[i] = fc;
++this->current_size_;
return ;
} return -;
}

5、补充:对于ACE_Framework_Component_T有很有意思的地方,在声明中有这样一个宏:ACE_ALLOC_HOOK_DECLARE,其定义如下:

 # if defined (ACE_HAS_ALLOC_HOOKS)
# define ACE_ALLOC_HOOK_DECLARE \
void *operator new (size_t bytes); \
void *operator new (size_t bytes, void *ptr); \
void *operator new (size_t bytes, const std::nothrow_t &) throw (); \
void operator delete (void *ptr); \
void operator delete (void *ptr, const std::nothrow_t &); \
void *operator new[] (size_t size); \
void operator delete[] (void *ptr); \
void *operator new[] (size_t size, const std::nothrow_t &) throw (); \
void operator delete[] (void *ptr, const std::nothrow_t &)

定义中有宏:ACE_ALLOC_HOOK_DEFINE_Tt(ACE_Framework_Component_T):

 # if defined (ACE_HAS_ALLOC_HOOKS)
……
# define ACE_ALLOC_HOOK_DEFINE_Tt(CLASS) \
ACE_GENERIC_ALLOCS (ACE_ALLOC_HOOK_HELPER_Tt, CLASS)
 #  define ACE_GENERIC_ALLOCS(MAKE_PREFIX, CLASS) \
MAKE_PREFIX (void *, CLASS)::operator new (size_t bytes) \
{ \
void *const ptr = ACE_Allocator::instance ()->malloc (bytes); \
if (ptr == ) \
throw std::bad_alloc (); \
return ptr; \
} \
MAKE_PREFIX (void *, CLASS)::operator new (size_t, void *ptr) { return ptr; }\
MAKE_PREFIX (void *, CLASS)::operator new (size_t bytes, \
const std::nothrow_t &) throw () \
{ return ACE_Allocator::instance ()->malloc (bytes); } \
MAKE_PREFIX (void, CLASS)::operator delete (void *ptr) \
{ if (ptr) ACE_Allocator::instance ()->free (ptr); } \
MAKE_PREFIX (void, CLASS)::operator delete (void *ptr, \
const std::nothrow_t &) \
{ if (ptr) ACE_Allocator::instance ()->free (ptr); } \
MAKE_PREFIX (void *, CLASS)::operator new[] (size_t size) \
{ \
void *const ptr = ACE_Allocator::instance ()->malloc (size); \
if (ptr == ) \
throw std::bad_alloc (); \
return ptr; \
} \
MAKE_PREFIX (void, CLASS)::operator delete[] (void *ptr) \
{ if (ptr) ACE_Allocator::instance ()->free (ptr); } \
MAKE_PREFIX (void *, CLASS)::operator new[] (size_t size, \
const std::nothrow_t &) throw ()\
{ return ACE_Allocator::instance ()->malloc (size); } \
MAKE_PREFIX (void, CLASS)::operator delete[] (void *ptr, \
const std::nothrow_t &) \
{ if (ptr) ACE_Allocator::instance ()->free (ptr); }

而如果宏定义ACE_HAS_ALLOC_HOOKS未定义,则全部宏为空。

 # else
# define ACE_ALLOC_HOOK_DECLARE struct Ace_ {} /* Just need a dummy... */
# define ACE_ALLOC_HOOK_DEFINE(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tt(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tcc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tccc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tccct(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tc4(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tc5(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tc6(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tc7(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Ty(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tyc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tycc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tcy(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tcyc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tca(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tco(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tcoccc(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tcs(CLASS)
# define ACE_ALLOC_HOOK_DEFINE_Tmcc(CLASS)
# endif /* ACE_HAS_ALLOC_HOOKS */

这里可以在编译时选择是否添加内存管理钩子hook,通过ACE框架来管理内存,便于对系统进行管理与调试。

ACE中静态实例管理方式的更多相关文章

  1. linux驱动程序之电源管理之新版linux系统设备架构中关于电源管理方式的变更

    新版linux系统设备架构中关于电源管理方式的变更 based on linux-2.6.32 一.设备模型各数据结构中电源管理的部分 linux的设备模型通过诸多结构体来联合描述,如struct d ...

  2. 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用

    7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...

  3. cocos2dx中的内存管理方式

    转载:http://www.cocoachina.com/bbs/read.php?tid=195219 今天看了一下cocos2dx的内存管理机制,有些地方不太好理解搞了挺长的时间,现在感觉自己理解 ...

  4. Opencv在视频中静态、动态方式绘制矩形框ROI

    Opencv视频处理中的目标跟踪经常用到要在视频上画一个矩形框ROI,标注出要跟踪的物体,这里介绍两种在视频中绘制矩形框的方法,一种是"静态的",一种是"动态的" ...

  5. hibernate中session的管理方式

    package loaderman.c_session; import loaderman.b_second_cache.Dept; import loaderman.b_second_cache.E ...

  6. 跟我一起学WCF(8)——WCF中Session、实例管理详解

    一.引言 由前面几篇博文我们知道,WCF是微软基于SOA建立的一套在分布式环境中各个相对独立的应用进行交流(Communication)的框架,它实现了最新的基于WS-*规范.按照SOA的原则,相对独 ...

  7. glusterfs 内存管理方式

    glusterfs中的内存管理方式: 首先来看看glusterfs的内存管理结构吧: struct mem_pool { struct list_head list; int hot_count; i ...

  8. objective-C 中的内存管理解说

    初学objectice-C的朋友都有一个困惑,总觉得对objective-C的内存管理机制琢磨不透,程序经常内存泄漏或莫名其妙的崩溃.我在这里总结了自己对objective-C内存管理机制的研究成果和 ...

  9. EJB3 EntityBean中EntityManager的管理类型

    EJB中EntityManager的管理方式有两种:Container-managed EntityManager和Application-managed EntityManager 即容器管理的En ...

随机推荐

  1. js中的property和attribute

    javascript中的property和attribute虽然都被翻译为“属性”,但是他们还是有区别的. 前两天写网页时用到checkbox,就被property和attribute弄晕了好久.后来 ...

  2. 问题: Packet for query is too large (1786 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.

    错误描述: 今天在手机端查看之前上线的项目时,突然报了下面的错误.再之后用电脑登陆,其他设备登陆都一直报这个错误. 错误信息: ### Error querying database. Cause: ...

  3. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  4. Codeforces Round #127 (Div. 1) B. Guess That Car! 扫描线

    B. Guess That Car! 题目连接: http://codeforces.com/contest/201/problem/B Description A widely known amon ...

  5. opensuse搜狐镜像

    openSUSE-13.1-Update-sohu-mirror http://mirrors.sohu.com/opensuse/update/13.1/ openSUSE-13.1-Oss-soh ...

  6. Oracle10g 创建一个DataBase实例

    Oracle10g创建DataBase实例如下:第一步:Oracle - OraDb10g_home1 -> 配置和移植工具 -> 打开Database Configuration Ass ...

  7. UITabBarControlller 和 UINavigationController

  8. iOS中 三种随机数方法详解

    ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...

  9. mac远程链接 windows

    https://bbs.feng.com/read-htm-tid-10516042.html 一.利用电脑系统自带的远程(喜欢懒人版的方法,或者小白用户,可以跳过这个方法看下一个) 1.打开mac, ...

  10. Qt 5.7 亮瞎眼的更新

    Qt 5.7的beta版已经出来了,这将是一个超级重大的更新,主要有几个商业版的模块在GPLv3 open source 版的用户也可以用了,其中包括了两个很炫酷的模块: Qt Charts Qt D ...