Malloc_Allocator.cpp

Go to the documentation of this file.
00001 // Malloc_Allocator.cpp,v 4.13 2005/10/28 16:14:53 ossama Exp
00002 
00003 #include "ace/Malloc_Allocator.h"
00004 #include "ace/Object_Manager.h"
00005 
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/Malloc_Allocator.inl"
00008 #endif /* __ACE_INLINE__ */
00009 
00010 #include "ace/Guard_T.h"
00011 #include "ace/Recursive_Thread_Mutex.h"
00012 #include "ace/Log_Msg.h"  // for ACE_ASSERT
00013 #include "ace/OS_NS_string.h"
00014 
00015 ACE_RCSID (ace, Malloc_Allocator, "Malloc_Allocator.cpp,v 4.13 2005/10/28 16:14:53 ossama Exp")
00016 
00017 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 ACE_Allocator *
00020 ACE_Allocator::instance (void)
00021 {
00022   //  ACE_TRACE ("ACE_Allocator::instance");
00023 
00024   if (ACE_Allocator::allocator_ == 0)
00025     {
00026       // Perform Double-Checked Locking Optimization.
00027       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00028                                 *ACE_Static_Object_Lock::instance (), 0));
00029 
00030       if (ACE_Allocator::allocator_ == 0)
00031         {
00032           // Have a seat.  We want to avoid ever having to delete the
00033           // ACE_Allocator instance, to avoid shutdown order
00034           // dependencies.  ACE_New_Allocator never needs to be
00035           // destroyed:  its destructor is empty and its instance
00036           // doesn't have any state.  Therefore, sizeof
00037           // ACE_New_Allocator is equal to sizeof void *.  It's
00038           // instance just contains a pointer to its virtual function
00039           // table.
00040           //
00041           // So, we allocate space for the ACE_New_Allocator instance
00042           // in the data segment.  Because its size is the same as
00043           // that of a pointer, we allocate it as a pointer so that it
00044           // doesn't get constructed statically.  We never bother to
00045           // destroy it.
00046           static void *allocator_instance = 0;
00047 
00048           // Check this critical assumption.  We put it in a variable
00049           // first to avoid stupid compiler warnings that the
00050           // condition may always be true/false.
00051 #         if !defined (ACE_NDEBUG)
00052           int assertion = (sizeof allocator_instance ==
00053                            sizeof (ACE_New_Allocator));
00054           ACE_ASSERT (assertion);
00055 #         endif /* !ACE_NDEBUG */
00056 
00057           // Initialize the allocator_instance by using a placement
00058           // new.
00059           ACE_Allocator::allocator_ =
00060             new (&allocator_instance) ACE_New_Allocator;
00061         }
00062     }
00063 
00064   return ACE_Allocator::allocator_;
00065 }
00066 
00067 ACE_Allocator *
00068 ACE_Allocator::instance (ACE_Allocator *r)
00069 {
00070   ACE_TRACE ("ACE_Allocator::instance");
00071   ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00072                             *ACE_Static_Object_Lock::instance (), 0));
00073   ACE_Allocator *t = ACE_Allocator::allocator_;
00074 
00075   // We can't safely delete it since we don't know who created it!
00076   ACE_Allocator::delete_allocator_ = 0;
00077 
00078   ACE_Allocator::allocator_ = r;
00079   return t;
00080 }
00081 
00082 void
00083 ACE_Allocator::close_singleton (void)
00084 {
00085   ACE_TRACE ("ACE_Allocator::close_singleton");
00086 
00087   ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00088                      *ACE_Static_Object_Lock::instance ()));
00089 
00090   if (ACE_Allocator::delete_allocator_)
00091     {
00092       // This should never be executed....  See the
00093       // ACE_Allocator::instance (void) method for an explanation.
00094       delete ACE_Allocator::allocator_;
00095       ACE_Allocator::allocator_ = 0;
00096       ACE_Allocator::delete_allocator_ = 0;
00097     }
00098 }
00099 
00100 ACE_Allocator::~ACE_Allocator (void)
00101 {
00102   ACE_TRACE ("ACE_Allocator::~ACE_Allocator");
00103 }
00104 
00105 ACE_Allocator::ACE_Allocator (void)
00106 {
00107   ACE_TRACE ("ACE_Allocator::ACE_Allocator");
00108 }
00109 
00110 /******************************************************************************/
00111 
00112 void *
00113 ACE_New_Allocator::malloc (size_t nbytes)
00114 {
00115   char *ptr = 0;
00116 
00117   if (nbytes > 0)
00118     ACE_NEW_RETURN (ptr, char[nbytes], 0);
00119   return (void *) ptr;
00120 }
00121 
00122 void *
00123 ACE_New_Allocator::calloc (size_t nbytes,
00124                            char initial_value)
00125 {
00126   char *ptr = 0;
00127 
00128   ACE_NEW_RETURN (ptr, char[nbytes], 0);
00129 
00130   ACE_OS::memset (ptr, initial_value, nbytes);
00131   return (void *) ptr;
00132 }
00133 
00134 void *
00135 ACE_New_Allocator::calloc (size_t n_elem, size_t elem_size, char initial_value)
00136 {
00137   return ACE_New_Allocator::calloc (n_elem * elem_size, initial_value);
00138 }
00139 
00140 void
00141 ACE_New_Allocator::free (void *ptr)
00142 {
00143   delete [] (char *) ptr;
00144 }
00145 
00146 int
00147 ACE_New_Allocator::remove (void)
00148 {
00149   ACE_NOTSUP_RETURN (-1);
00150 }
00151 
00152 int
00153 ACE_New_Allocator::bind (const char *, void *, int)
00154 {
00155   ACE_NOTSUP_RETURN (-1);
00156 }
00157 
00158 int
00159 ACE_New_Allocator::trybind (const char *, void *&)
00160 {
00161   ACE_NOTSUP_RETURN (-1);
00162 }
00163 
00164 int
00165 ACE_New_Allocator::find (const char *, void *&)
00166 {
00167   ACE_NOTSUP_RETURN (-1);
00168 }
00169 
00170 int
00171 ACE_New_Allocator::find (const char *)
00172 {
00173   ACE_NOTSUP_RETURN (-1);
00174 }
00175 
00176 int
00177 ACE_New_Allocator::unbind (const char *)
00178 {
00179   ACE_NOTSUP_RETURN (-1);
00180 }
00181 
00182 int
00183 ACE_New_Allocator::unbind (const char *, void *&)
00184 {
00185   ACE_NOTSUP_RETURN (-1);
00186 }
00187 
00188 int
00189 ACE_New_Allocator::sync (ssize_t, int)
00190 {
00191   ACE_NOTSUP_RETURN (-1);
00192 }
00193 
00194 int
00195 ACE_New_Allocator::sync (void *, size_t, int)
00196 {
00197   ACE_NOTSUP_RETURN (-1);
00198 }
00199 
00200 int
00201 ACE_New_Allocator::protect (ssize_t, int)
00202 {
00203   ACE_NOTSUP_RETURN (-1);
00204 }
00205 
00206 int
00207 ACE_New_Allocator::protect (void *, size_t, int)
00208 {
00209   ACE_NOTSUP_RETURN (-1);
00210 }
00211 
00212 #if defined (ACE_HAS_MALLOC_STATS)
00213 void
00214 ACE_New_Allocator::print_stats (void) const
00215 {
00216 }
00217 #endif /* ACE_HAS_MALLOC_STATS */
00218 
00219 void
00220 ACE_New_Allocator::dump (void) const
00221 {
00222 #if defined (ACE_HAS_DUMP)
00223 #endif /* ACE_HAS_DUMP */
00224 }
00225 
00226 /******************************************************************************/
00227 
00228 void *
00229 ACE_Static_Allocator_Base::malloc (size_t nbytes)
00230 {
00231   if (this->offset_ + nbytes > this->size_)
00232     {
00233       errno = ENOMEM;
00234       return 0;
00235     }
00236   else
00237     {
00238       // Record the current offset, increment the offset by the number
00239       // of bytes requested, and return the original offset.
00240       char *ptr = &this->buffer_[this->offset_];
00241       this->offset_ += nbytes;
00242       return (void *) ptr;
00243     }
00244 }
00245 
00246 void *
00247 ACE_Static_Allocator_Base::calloc (size_t nbytes,
00248                                    char initial_value)
00249 {
00250   void *ptr = this->malloc (nbytes);
00251 
00252   ACE_OS::memset (ptr, initial_value, nbytes);
00253   return (void *) ptr;
00254 }
00255 
00256 void *
00257 ACE_Static_Allocator_Base::calloc (size_t n_elem,
00258                                    size_t elem_size,
00259                                    char initial_value)
00260 {
00261   return this->calloc (n_elem * elem_size, initial_value);
00262 }
00263 
00264 void
00265 ACE_Static_Allocator_Base::free (void *ptr)
00266 {
00267   // Check to see if ptr is within our pool?!
00268   ACE_UNUSED_ARG (ptr);
00269   ACE_ASSERT (ptr >= this->buffer_ && ptr < this->buffer_ + this->size_);
00270 }
00271 
00272 int
00273 ACE_Static_Allocator_Base::remove (void)
00274 {
00275   return -1;
00276 }
00277 
00278 int
00279 ACE_Static_Allocator_Base::bind (const char *, void *, int)
00280 {
00281   return -1;
00282 }
00283 
00284 int
00285 ACE_Static_Allocator_Base::trybind (const char *, void *&)
00286 {
00287   return -1;
00288 }
00289 
00290 int
00291 ACE_Static_Allocator_Base::find (const char *, void *&)
00292 {
00293   return -1;
00294 }
00295 
00296 int
00297 ACE_Static_Allocator_Base::find (const char *)
00298 {
00299   return -1;
00300 }
00301 
00302 int
00303 ACE_Static_Allocator_Base::unbind (const char *)
00304 {
00305   return -1;
00306 }
00307 
00308 int
00309 ACE_Static_Allocator_Base::unbind (const char *, void *&)
00310 {
00311   return -1;
00312 }
00313 
00314 int
00315 ACE_Static_Allocator_Base::sync (ssize_t, int)
00316 {
00317   return -1;
00318 }
00319 
00320 int
00321 ACE_Static_Allocator_Base::sync (void *, size_t, int)
00322 {
00323   return -1;
00324 }
00325 
00326 int
00327 ACE_Static_Allocator_Base::protect (ssize_t, int)
00328 {
00329   return -1;
00330 }
00331 
00332 int
00333 ACE_Static_Allocator_Base::protect (void *, size_t, int)
00334 {
00335   return -1;
00336 }
00337 
00338 #if defined (ACE_HAS_MALLOC_STATS)
00339 void
00340 ACE_Static_Allocator_Base::print_stats (void) const
00341 {
00342 }
00343 #endif /* ACE_HAS_MALLOC_STATS */
00344 
00345 void
00346 ACE_Static_Allocator_Base::dump (void) const
00347 {
00348 #if defined (ACE_HAS_DUMP)
00349   ACE_TRACE ("ACE_Static_Allocator_Base::dump");
00350 
00351   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00352   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\noffset_ = %d"), this->offset_));
00353   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nsize_ = %d\n"), this->size_));
00354   ACE_HEX_DUMP ((LM_DEBUG, this->buffer_, this->size_));
00355   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00356 
00357   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00358 #endif /* ACE_HAS_DUMP */
00359 }
00360 
00361 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:54 2006 for ACE by doxygen 1.3.6