00001
00002
00003 #include "ace/Cleanup.h"
00004
00005 ACE_RCSID (ace,
00006 Cleanup,
00007 "$Id: Cleanup.cpp 73483 2006-07-13 10:43:30Z olli $")
00008
00009 #if !defined (ACE_HAS_INLINED_OSCALLS)
00010 # include "ace/Cleanup.inl"
00011 #endif
00012
00013 #include "ace/OS_Memory.h"
00014
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016
00017 void
00018 ACE_Cleanup::cleanup (void *)
00019 {
00020 delete this;
00021 }
00022
00023
00024 ACE_Cleanup::~ACE_Cleanup (void)
00025 {
00026 }
00027
00028
00029
00030 extern "C" void
00031 ACE_CLEANUP_DESTROYER_NAME (ACE_Cleanup *object, void *param)
00032 {
00033 object->cleanup (param);
00034 }
00035
00036
00037
00038 ACE_Cleanup_Info::ACE_Cleanup_Info (void)
00039 : object_ (0),
00040 cleanup_hook_ (0),
00041 param_ (0)
00042 {
00043 }
00044
00045 bool
00046 ACE_Cleanup_Info::operator== (const ACE_Cleanup_Info &o) const
00047 {
00048 return o.object_ == this->object_
00049 && o.cleanup_hook_ == this->cleanup_hook_
00050 && o.param_ == this->param_;
00051 }
00052
00053 bool
00054 ACE_Cleanup_Info::operator!= (const ACE_Cleanup_Info &o) const
00055 {
00056 return !(*this == o);
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 class ACE_Cleanup_Info_Node
00069 {
00070 public:
00071 ACE_Cleanup_Info_Node (void);
00072 ACE_Cleanup_Info_Node (const ACE_Cleanup_Info &new_info,
00073 ACE_Cleanup_Info_Node *next);
00074 ~ACE_Cleanup_Info_Node (void);
00075 ACE_Cleanup_Info_Node *insert (const ACE_Cleanup_Info &);
00076 private:
00077 ACE_Cleanup_Info cleanup_info_;
00078 ACE_Cleanup_Info_Node *next_;
00079
00080 friend class ACE_OS_Exit_Info;
00081 };
00082
00083 ACE_Cleanup_Info_Node::ACE_Cleanup_Info_Node (void)
00084 : cleanup_info_ (),
00085 next_ (0)
00086 {
00087 }
00088
00089 ACE_Cleanup_Info_Node::ACE_Cleanup_Info_Node (const ACE_Cleanup_Info &new_info,
00090 ACE_Cleanup_Info_Node *next)
00091 : cleanup_info_ (new_info),
00092 next_ (next)
00093 {
00094 }
00095
00096 ACE_Cleanup_Info_Node::~ACE_Cleanup_Info_Node (void)
00097 {
00098 delete next_;
00099 }
00100
00101 ACE_Cleanup_Info_Node *
00102 ACE_Cleanup_Info_Node::insert (const ACE_Cleanup_Info &new_info)
00103 {
00104 ACE_Cleanup_Info_Node *new_node = 0;
00105
00106 ACE_NEW_RETURN (new_node,
00107 ACE_Cleanup_Info_Node (new_info, this),
00108 0);
00109
00110 return new_node;
00111 }
00112
00113
00114
00115 ACE_OS_Exit_Info::ACE_OS_Exit_Info (void)
00116 {
00117 ACE_NEW (registered_objects_, ACE_Cleanup_Info_Node);
00118 }
00119
00120 ACE_OS_Exit_Info::~ACE_OS_Exit_Info (void)
00121 {
00122 delete registered_objects_;
00123 registered_objects_ = 0;
00124 }
00125
00126 int
00127 ACE_OS_Exit_Info::at_exit_i (void *object,
00128 ACE_CLEANUP_FUNC cleanup_hook,
00129 void *param)
00130 {
00131 ACE_Cleanup_Info new_info;
00132 new_info.object_ = object;
00133 new_info.cleanup_hook_ = cleanup_hook;
00134 new_info.param_ = param;
00135
00136
00137
00138
00139 ACE_Cleanup_Info_Node *new_node = 0;
00140
00141 if ((new_node = registered_objects_->insert (new_info)) == 0)
00142 return -1;
00143 else
00144 {
00145 registered_objects_ = new_node;
00146 return 0;
00147 }
00148 }
00149
00150 int
00151 ACE_OS_Exit_Info::find (void *object)
00152 {
00153
00154 for (ACE_Cleanup_Info_Node *iter = registered_objects_;
00155 iter && iter->next_ != 0;
00156 iter = iter->next_)
00157 {
00158 if (iter->cleanup_info_.object_ == object)
00159 {
00160
00161 return 1;
00162 }
00163 }
00164
00165 return 0;
00166 }
00167
00168 void
00169 ACE_OS_Exit_Info::call_hooks (void)
00170 {
00171
00172
00173 for (ACE_Cleanup_Info_Node *iter = registered_objects_;
00174 iter && iter->next_ != 0;
00175 iter = iter->next_)
00176 {
00177 ACE_Cleanup_Info &info = iter->cleanup_info_;
00178 if (info.cleanup_hook_ == reinterpret_cast<ACE_CLEANUP_FUNC> (
00179 ACE_CLEANUP_DESTROYER_NAME))
00180
00181 ACE_CLEANUP_DESTROYER_NAME (
00182 reinterpret_cast<ACE_Cleanup *> (info.object_),
00183 info.param_);
00184 else if (info.object_ == &ace_exit_hook_marker)
00185
00186 (* reinterpret_cast<ACE_EXIT_HOOK> (info.cleanup_hook_)) ();
00187 else
00188 (*info.cleanup_hook_) (info.object_, info.param_);
00189 }
00190 }
00191
00192 ACE_END_VERSIONED_NAMESPACE_DECL