ACE中静态实例管理方式

时间:2021-07-30 20:22:43

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框架来管理内存,便于对系统进行管理与调试。