Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Malloc_Allocator.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Malloc_Allocator.cpp,v 1.1.1.2.2.1 2003/03/13 19:44:21 chad Exp $
00003 
00004 // FILENAME
00005 //   Malloc_Allocator.cpp
00006 //
00007 // AUTHOR
00008 //   Priyanka Gontla <pgontla@uci.edu>
00009 //
00010 // ==========================================================================
00011 
00012 #if !defined (ACE_MALLOC_ALLOCATOR_CPP)
00013 #define ACE_MALLOC_ALLOCATOR_CPP
00014 
00015 #include "ace/Malloc_Allocator.h"
00016 #include "ace/Object_Manager.h"
00017 
00018 #if !defined (__ACE_INLINE__)
00019 #include "ace/Malloc_Allocator.i"
00020 #endif /* __ACE_INLINE__ */
00021 
00022 #include "ace/Synch_T.h"
00023 
00024 ACE_RCSID (ace, Malloc_Allocator, "$Id: Malloc_Allocator.cpp,v 1.1.1.2.2.1 2003/03/13 19:44:21 chad Exp $")
00025 
00026 ACE_Allocator *
00027 ACE_Allocator::instance (void)
00028 {
00029   //  ACE_TRACE ("ACE_Allocator::instance");
00030 
00031   if (ACE_Allocator::allocator_ == 0)
00032     {
00033       // Perform Double-Checked Locking Optimization.
00034       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00035                                 *ACE_Static_Object_Lock::instance (), 0));
00036 
00037       if (ACE_Allocator::allocator_ == 0)
00038         {
00039           // Have a seat.  We want to avoid ever having to delete the
00040           // ACE_Allocator instance, to avoid shutdown order
00041           // dependencies.  ACE_New_Allocator never needs to be
00042           // destroyed:  its destructor is empty and its instance
00043           // doesn't have any state.  Therefore, sizeof
00044           // ACE_New_Allocator is equal to sizeof void *.  It's
00045           // instance just contains a pointer to its virtual function
00046           // table.
00047           //
00048           // So, we allocate space for the ACE_New_Allocator instance
00049           // in the data segment.  Because its size is the same as
00050           // that of a pointer, we allocate it as a pointer so that it
00051           // doesn't get constructed statically.  We never bother to
00052           // destroy it.
00053           static void *allocator_instance = 0;
00054 
00055           // Check this critical assumption.  We put it in a variable
00056           // first to avoid stupid compiler warnings that the
00057           // condition may always be true/false.
00058 #         if !defined (ACE_NDEBUG)
00059           int assertion = (sizeof allocator_instance ==
00060                            sizeof (ACE_New_Allocator));
00061           ACE_ASSERT (assertion);
00062 #         endif /* !ACE_NDEBUG */
00063 
00064           // Initialize the allocator_instance by using a placement
00065           // new.
00066           ACE_Allocator::allocator_ =
00067             new (&allocator_instance) ACE_New_Allocator;
00068         }
00069     }
00070 
00071   return ACE_Allocator::allocator_;
00072 }
00073 
00074 ACE_Allocator *
00075 ACE_Allocator::instance (ACE_Allocator *r)
00076 {
00077   ACE_TRACE ("ACE_Allocator::instance");
00078   ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00079                             *ACE_Static_Object_Lock::instance (), 0));
00080   ACE_Allocator *t = ACE_Allocator::allocator_;
00081 
00082   // We can't safely delete it since we don't know who created it!
00083   ACE_Allocator::delete_allocator_ = 0;
00084 
00085   ACE_Allocator::allocator_ = r;
00086   return t;
00087 }
00088 
00089 void
00090 ACE_Allocator::close_singleton (void)
00091 {
00092   ACE_TRACE ("ACE_Allocator::close_singleton");
00093 
00094   ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00095                      *ACE_Static_Object_Lock::instance ()));
00096 
00097   if (ACE_Allocator::delete_allocator_)
00098     {
00099       // This should never be executed....  See the
00100       // ACE_Allocator::instance (void) method for an explanation.
00101       delete ACE_Allocator::allocator_;
00102       ACE_Allocator::allocator_ = 0;
00103       ACE_Allocator::delete_allocator_ = 0;
00104     }
00105 }
00106 
00107 ACE_Allocator::~ACE_Allocator (void)
00108 {
00109   ACE_TRACE ("ACE_Allocator::~ACE_Allocator");
00110 }
00111 
00112 ACE_Allocator::ACE_Allocator (void)
00113 {
00114   ACE_TRACE ("ACE_Allocator::ACE_Allocator");
00115 }
00116 
00117 void
00118 ACE_Static_Allocator_Base::dump (void) const
00119 {
00120   ACE_TRACE ("ACE_Static_Allocator_Base::dump");
00121 
00122   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00123   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\noffset_ = %d"), this->offset_));
00124   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nsize_ = %d\n"), this->size_));
00125   ACE_HEX_DUMP ((LM_DEBUG, this->buffer_, this->size_));
00126   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00127 
00128   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00129 }
00130 
00131 
00132 
00133 #endif /* ACE_MALLOC_ALLOCATOR_CPP */

Generated on Mon Jun 16 11:20:04 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002