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

Malloc_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Malloc_T.h
00006  *
00007  *  $Id: Malloc_T.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu> and
00010  *          Irfan Pyarali <irfan@cs.wustl.edu>
00011  */
00012 //==========================================================================
00013 
00014 #ifndef ACE_MALLOC_T_H
00015 #define ACE_MALLOC_T_H
00016 #include "ace/pre.h"
00017 
00018 #include "ace/OS.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Synch.h"
00025 #include "ace/Malloc.h"               /* Need ACE_Control_Block */
00026 #include "ace/Malloc_Allocator.h"
00027 #include "ace/Free_List.h"
00028 
00029 /**
00030  * @class ACE_Cached_Mem_Pool_Node
00031  *
00032  * @brief ACE_Cached_Mem_Pool_Node keeps unused memory within a free
00033  * list.
00034  *
00035  * The length of a piece of unused memory must be greater than
00036  * sizeof (void*).  This makes sense because we'll waste even
00037  * more memory if we keep them in a separate data structure.
00038  * This class should really be placed within the ACE_Cached_Allocator
00039  * class but this can't be done due to C++ compiler portability problems.
00040  */
00041 template <class T>
00042 class ACE_Cached_Mem_Pool_Node
00043 {
00044 public:
00045   /// Return the address of free memory.
00046   T *addr (void);
00047 
00048   /// Get the next ACE_Cached_Mem_Pool_Node in a list.
00049   ACE_Cached_Mem_Pool_Node<T> *get_next (void);
00050 
00051   /// Set the next ACE_Cached_Mem_Pool_Node.
00052   void set_next (ACE_Cached_Mem_Pool_Node<T> *ptr);
00053 
00054 private:
00055   /**
00056    * Since memory is not used when placed in a free list,
00057    * we can use it to maintain the structure of free list.
00058    * I was using union to hide the fact of overlapping memory
00059    * usage.  However, that cause problem on MSVC.  So, I now turn
00060    * back to hack this with casting.
00061    */
00062   ACE_Cached_Mem_Pool_Node<T> *next_;
00063 };
00064 
00065 /**
00066  * @class ACE_Cached_Allocator
00067  *
00068  * @brief A fixed-size allocator that caches items for quicker access.
00069  *
00070  * This class enables caching of dynamically allocated,
00071  * fixed-sized classes.  Notice that the <code>sizeof (TYPE)</code>
00072  * must be greater than or equal to <code> sizeof (void*) </code> for
00073  * this to work properly.
00074  *
00075  * @sa ACE_Dynamic_Cached_Allocator
00076  */
00077 template <class T, class ACE_LOCK>
00078 class ACE_Cached_Allocator : public ACE_New_Allocator
00079 {
00080 public:
00081   /// Create a cached memory pool with @a n_chunks chunks
00082   /// each with sizeof (TYPE) size.
00083   ACE_Cached_Allocator (size_t n_chunks);
00084 
00085   /// Clear things up.
00086   ~ACE_Cached_Allocator (void);
00087 
00088   /**
00089    * Get a chunk of memory from free list cache.  Note that @a nbytes is
00090    * only checked to make sure that it's less or equal to sizeof T, and is
00091    * otherwise ignored since @c malloc() always returns a pointer to an
00092    * item of sizeof (T).
00093    */
00094   void *malloc (size_t nbytes = sizeof (T));
00095 
00096   /**
00097    * Get a chunk of memory from free list cache, giving them
00098    * @a initial_value.  Note that @a nbytes is only checked to make sure
00099    * that it's less or equal to sizeof T, and is otherwise ignored since
00100    * calloc() always returns a pointer to an item of sizeof (T).
00101    */
00102   virtual void *calloc (size_t nbytes,
00103                         char initial_value = '\0');
00104 
00105   /// This method is a no-op and just returns 0 since the free list
00106   /// only works with fixed sized entities.
00107   virtual void *calloc (size_t n_elem,
00108                         size_t elem_size,
00109                         char initial_value = '\0');
00110 
00111   /// Return a chunk of memory back to free list cache.
00112   void free (void *);
00113 
00114 private:
00115   /// Remember how we allocate the memory in the first place so
00116   /// we can clear things up later.
00117   char *pool_;
00118 
00119   /// Maintain a cached memory free list.
00120   ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<T>, ACE_LOCK> free_list_;
00121 };
00122 
00123 /**
00124  * @class ACE_Dynamic_Cached_Allocator
00125  *
00126  * @brief A size-based allocator that caches blocks for quicker access.
00127  *
00128  * This class enables caching of dynamically allocated,
00129  * fixed-size chunks.  Notice that the <code>chunk_size</code>
00130  * must be greater than or equal to <code> sizeof (void*) </code> for
00131  * this to work properly.
00132  *
00133  * @sa ACE_Cached_Allocator
00134  */
00135 template <class ACE_LOCK>
00136 class ACE_Dynamic_Cached_Allocator : public ACE_New_Allocator
00137 {
00138 public:
00139   /// Create a cached memory pool with @a n_chunks chunks
00140   /// each with @a chunk_size size.
00141   ACE_Dynamic_Cached_Allocator (size_t n_chunks, size_t chunk_size);
00142 
00143   /// Clear things up.
00144   ~ACE_Dynamic_Cached_Allocator (void);
00145 
00146   /**
00147    * Get a chunk of memory from free list cache.  Note that @a nbytes is
00148    * only checked to make sure that it's less or equal to @a chunk_size,
00149    * and is otherwise ignored since malloc() always returns a pointer to an
00150    * item of @a chunk_size size.
00151    */
00152   void *malloc (size_t nbytes = 0);
00153 
00154   /**
00155    * Get a chunk of memory from free list cache, giving them
00156    * @a initial_value.  Note that @a nbytes is only checked to make sure
00157    * that it's less or equal to @a chunk_size, and is otherwise ignored
00158    * since calloc() always returns a pointer to an item of @a chunk_size.
00159    */
00160   virtual void *calloc (size_t nbytes,
00161                         char initial_value = '\0');
00162 
00163   /// This method is a no-op and just returns 0 since the free list
00164   /// only works with fixed sized entities.
00165   virtual void *calloc (size_t n_elem,
00166                         size_t elem_size,
00167                         char initial_value = '\0');
00168 
00169   /// Return a chunk of memory back to free list cache.
00170   void free (void *);
00171 
00172 private:
00173   /// Remember how we allocate the memory in the first place so
00174   /// we can clear things up later.
00175   char *pool_;
00176 
00177   /// Maintain a cached memory free list. We use @c char as template
00178   /// parameter, although sizeof(char) is usually less than
00179   /// sizeof(void*). Really important is that @a chunk_size
00180   /// must be greater or equal to sizeof(void*).
00181   ACE_Locked_Free_List<ACE_Cached_Mem_Pool_Node<char>, ACE_LOCK> free_list_;
00182 
00183   /// Remember the size of our chunks.
00184   size_t chunk_size_;
00185 };
00186 
00187 /**
00188  * @class ACE_Allocator_Adapter
00189  *
00190  * @brief This class is an Adapter that allows the ACE_Allocator to
00191  * use the ACE_Malloc class below.
00192  */
00193 template <class MALLOC>
00194 class ACE_Allocator_Adapter : public ACE_Allocator
00195 {
00196 public:
00197   // Trait.
00198   typedef MALLOC ALLOCATOR;
00199 
00200 #if defined (ACE_HAS_TEMPLATE_TYPEDEFS)
00201   // The following code will break C++ compilers that don't support
00202   // template typedefs correctly.
00203   typedef const ACE_TYPENAME MALLOC::MEMORY_POOL_OPTIONS *MEMORY_POOL_OPTIONS;
00204 #else
00205   typedef const void *MEMORY_POOL_OPTIONS;
00206 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
00207 
00208   // = Initialization.
00209   /**
00210    * Note that @a pool_name should be located in
00211    * a directory with the appropriate visibility and protection so
00212    * that all processes that need to access it can do so. */
00213   ACE_Allocator_Adapter (const char *pool_name = 0);
00214 
00215   /**
00216    * Note that @a pool_name should be located in
00217    * a directory with the appropriate visibility and protection so
00218    * that all processes that need to access it can do so.
00219    * This constructor must be inline to avoid bugs with some C++
00220    * compilers. */
00221   ACE_Allocator_Adapter (const char *pool_name,
00222                          const char *lock_name,
00223                          MEMORY_POOL_OPTIONS options = 0)
00224       : allocator_ (ACE_TEXT_CHAR_TO_TCHAR (pool_name),
00225                     ACE_TEXT_CHAR_TO_TCHAR (lock_name),
00226                     options)
00227     {
00228       ACE_TRACE ("ACE_Allocator_Adapter<MALLOC>::ACE_Allocator_Adapter");
00229     }
00230 
00231 #if defined (ACE_HAS_WCHAR)
00232   /**
00233    * Note that @a pool_name should be located in
00234    * a directory with the appropriate visibility and protection so
00235    * that all processes that need to access it can do so. */
00236   ACE_Allocator_Adapter (const wchar_t *pool_name);
00237 
00238   /**
00239    * Note that @a pool_name should be located in
00240    * a directory with the appropriate visibility and protection so
00241    * that all processes that need to access it can do so.
00242    * This constructor must be inline to avoid bugs with some C++
00243    * compilers. */
00244   ACE_Allocator_Adapter (const wchar_t *pool_name,
00245                          const wchar_t *lock_name,
00246                          MEMORY_POOL_OPTIONS options = 0)
00247       : allocator_ (ACE_TEXT_WCHAR_TO_TCHAR (pool_name),
00248                     ACE_TEXT_WCHAR_TO_TCHAR (lock_name),
00249                     options)
00250     {
00251       ACE_TRACE ("ACE_Allocator_Adapter<MALLOC>::ACE_Allocator_Adapter");
00252     }
00253 #endif /* ACE_HAS_WCHAR */
00254 
00255   /// Destructor.
00256   virtual ~ACE_Allocator_Adapter (void);
00257 
00258   // = Memory Management
00259 
00260   /// Allocate @a nbytes, but don't give them any initial value.
00261   virtual void *malloc (size_t nbytes);
00262 
00263   /// Allocate @a nbytes, giving them all an @a initial_value.
00264   virtual void *calloc (size_t nbytes, char initial_value = '\0');
00265 
00266   /// Allocate @a n_elem each of size @a elem_size, giving them
00267   /// @a initial_value.
00268   virtual void *calloc (size_t n_elem,
00269                         size_t elem_size,
00270                         char initial_value = '\0');
00271 
00272   /// Free @a ptr (must have been allocated by ACE_Allocator::malloc()).
00273   virtual void free (void *ptr);
00274 
00275   /// Remove any resources associated with this memory manager.
00276   virtual int remove (void);
00277 
00278   // = Map manager like functions
00279 
00280   /**
00281    * Associate @a name with @a pointer.  If @a duplicates == 0 then do
00282    * not allow duplicate @a name/pointer associations, else if
00283    * @a duplicates> != 0 then allow duplicate @a name/pointer
00284    * assocations.  Returns 0 if successfully binds (1) a previously
00285    * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
00286    * bind a previously bound @a name and @a duplicates == 0, else
00287    * returns -1 if a resource failure occurs.
00288    */
00289   virtual int bind (const char *name, void *pointer, int duplicates = 0);
00290 
00291   /**
00292    * Associate @a name with @a pointer.  Does not allow duplicate
00293    * name/pointer associations.  Returns 0 if successfully binds
00294    * (1) a previously unbound @a name, 1 if trying to bind a previously
00295    * bound @a name, or returns -1 if a resource failure occurs.  When
00296    * this call returns, @a pointer's value will always reference the
00297    * void * that @a name is associated with.  Thus, if the caller needs
00298    * to use @a pointer (e.g., to free it) a copy must be maintained by
00299    * the caller.
00300    */
00301   virtual int trybind (const char *name, void *&pointer);
00302 
00303   /// Locate @a name and pass out parameter via pointer.  If found,
00304   /// return 0, returns -1 if @a name isn't found.
00305   virtual int find (const char *name, void *&pointer);
00306 
00307   /// Returns 0 if the name is in the mapping and -1 if not.
00308   virtual int find (const char *name);
00309 
00310   /// Unbind (remove) the name from the map.  Don't return the pointer
00311   /// to the caller
00312   virtual int unbind (const char *name);
00313 
00314   /// Break any association of name.  Returns the value of pointer in
00315   /// case the caller needs to deallocate memory.
00316   virtual int unbind (const char *name, void *&pointer);
00317 
00318   // = Protection and "sync" (i.e., flushing data to backing store).
00319 
00320   /**
00321    * Sync @a len bytes of the memory region to the backing store
00322    * starting at @c this->base_addr_.  If @a len == -1 then sync the
00323    * whole region.
00324    */
00325   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00326 
00327   /// Sync @a len bytes of the memory region to the backing store
00328   /// starting at @c addr_.
00329   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00330 
00331   /**
00332    * Change the protection of the pages of the mapped region to @a prot
00333    * starting at @c this->base_addr_ up to @a len bytes.  If @a len == -1
00334    * then change protection of all pages in the mapped region.
00335    */
00336   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
00337 
00338   /// Change the protection of the pages of the mapped region to @a prot
00339   /// starting at @a addr up to @a len bytes.
00340   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
00341 
00342   /// Returns the underlying allocator.
00343   ALLOCATOR &alloc (void);
00344 
00345 #if defined (ACE_HAS_MALLOC_STATS)
00346   /// Dump statistics of how malloc is behaving.
00347   virtual void print_stats (void) const;
00348 #endif /* ACE_HAS_MALLOC_STATS */
00349 
00350   /// Dump the state of the object.
00351   virtual void dump (void) const;
00352 
00353 private:
00354   /// ALLOCATOR instance, which is owned by the adapter.
00355   ALLOCATOR allocator_;
00356 };
00357 
00358 /**
00359  * @class ACE_Static_Allocator
00360  *
00361  * @brief Defines a class that provided a highly optimized memory
00362  * management scheme for allocating memory statically.
00363  *
00364  * This class allocates a fixed-size @c POOL_SIZE of memory and
00365  * uses the ACE_Static_Allocator_Base class implementations of
00366  * malloc() and calloc() to optimize memory allocation from this
00367  * pool.
00368  */
00369 template <size_t POOL_SIZE>
00370 class ACE_Static_Allocator : public ACE_Static_Allocator_Base
00371 {
00372 public:
00373   ACE_Static_Allocator (void)
00374     : ACE_Static_Allocator_Base (pool_, POOL_SIZE)
00375   {
00376     // This function <{must}> be inlined!!!
00377   }
00378 
00379 private:
00380   /// Pool contents.
00381   char pool_[POOL_SIZE];
00382 };
00383 
00384 // Forward declaration.
00385 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
00386 class ACE_Malloc_LIFO_Iterator_T;
00387 
00388 // Ensure backwards compatibility...
00389 #define ACE_Malloc_Iterator ACE_Malloc_LIFO_Iterator
00390 
00391 // Forward declaration.
00392 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
00393 class ACE_Malloc_FIFO_Iterator_T;
00394 
00395 /**
00396  * @class ACE_Malloc_T
00397  *
00398  * @brief Define a C++ class that uses parameterized types to provide
00399  * an extensible mechanism for encapsulating various of dynamic
00400  * memory management strategies.
00401  *
00402  * This class can be configured flexibly with different
00403  * MEMORY_POOL strategies and different types of ACE_LOCK
00404  * strategies.
00405  */
00406 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
00407 class ACE_Malloc_T
00408 {
00409 public:
00410   friend class ACE_Malloc_LIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>;
00411   friend class ACE_Malloc_FIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB>;
00412   typedef ACE_MEM_POOL MEMORY_POOL;
00413   typedef ACE_MEM_POOL_OPTIONS MEMORY_POOL_OPTIONS;
00414   typedef ACE_TYPENAME ACE_CB::ACE_Name_Node NAME_NODE;
00415   typedef ACE_TYPENAME ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
00416 
00417   // = Initialization and termination methods.
00418   /**
00419    * Initialize ACE_Malloc.  This constructor passes @a pool_name to
00420    * initialize the memory pool, and uses ACE::basename() to
00421    * automatically extract out the name used for the underlying lock
00422    * name (if necessary).
00423    *
00424    * Note that @a pool_name should be located in
00425    * a directory with the appropriate visibility and protection so
00426    * that all processes that need to access it can do so.
00427    */
00428   ACE_Malloc_T (const ACE_TCHAR *pool_name = 0);
00429 
00430   /**
00431    * Initialize ACE_Malloc.  This constructor passes @a pool_name to
00432    * initialize the memory pool, and uses @a lock_name to automatically
00433    * extract out the name used for the underlying lock name (if
00434    * necessary).  In addition, @a options is passed through to
00435    * initialize the underlying memory pool.
00436    *
00437    * Note that @a pool_name should be located in
00438    * a directory with the appropriate visibility and protection so
00439    * that all processes that need to access it can do so.
00440    */
00441   ACE_Malloc_T (const ACE_TCHAR *pool_name,
00442                 const ACE_TCHAR *lock_name,
00443                 const ACE_MEM_POOL_OPTIONS *options = 0);
00444 
00445   /**
00446    * Initialize an ACE_Malloc with an external ACE_LOCK.
00447    * This constructor passes @a pool_name and @a options to initialize
00448    * the memory pool. @a lock is used as the pool lock, and must be
00449    * properly set up and ready for use before being passed to this method.
00450    */
00451   ACE_Malloc_T (const ACE_TCHAR *pool_name,
00452                 const ACE_MEM_POOL_OPTIONS *options,
00453                 ACE_LOCK *lock);
00454 
00455 #if !defined (ACE_HAS_TEMPLATE_TYPEDEFS)
00456   /// This is necessary to work around template bugs with certain C++
00457   /// compilers.
00458   ACE_Malloc_T (const ACE_TCHAR *pool_name,
00459                 const ACE_TCHAR *lock_name,
00460                 const void *options = 0);
00461 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
00462 
00463   /// Destructor
00464   ~ACE_Malloc_T (void);
00465 
00466   /// Get Reference counter.
00467   int ref_counter (void);
00468 
00469   /// Release ref counter.
00470   int release (int close = 0);
00471 
00472   /// Releases resources allocated by this object.
00473   int remove (void);
00474 
00475   // = Memory management
00476 
00477   /// Allocate @a nbytes, but don't give them any initial value.
00478   void *malloc (size_t nbytes);
00479 
00480   /// Allocate @a nbytes, giving them @a initial_value.
00481   void *calloc (size_t nbytes, char initial_value = '\0');
00482 
00483   /// Allocate @a n_elem each of size @a elem_size, giving them
00484   /// @a initial_value.
00485   void *calloc (size_t n_elem,
00486                 size_t elem_size,
00487                 char initial_value = '\0');
00488 
00489   /// Deallocate memory pointed to by @a ptr, which must have been
00490   /// allocated previously by malloc().
00491   void free (void *ptr);
00492 
00493   /// Returns a reference to the underlying memory pool.
00494   MEMORY_POOL &memory_pool (void);
00495 
00496   // = Map manager like functions
00497 
00498   /**
00499    * Associate @a name with @a pointer.  If @a duplicates == 0 then do
00500    * not allow duplicate name/pointer associations, else if
00501    * @a duplicates != 0 then allow duplicate name/pointer
00502    * assocations.  Returns 0 if successfully binds (1) a previously
00503    * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
00504    * bind a previously bound @a name and @a duplicates == 0, else
00505    * returns -1 if a resource failure occurs.
00506    */
00507   int bind (const char *name, void *pointer, int duplicates = 0);
00508 
00509   /**
00510    * Associate @a name with @a pointer.  Does not allow duplicate
00511    * name/pointer associations.  Returns 0 if successfully binds
00512    * (1) a previously unbound @a name, 1 if trying to bind a previously
00513    * bound @a name, or returns -1 if a resource failure occurs.  When
00514    * this call returns @a pointer's value will always reference the
00515    * void * that @a name is associated with.  Thus, if the caller needs
00516    * to use @a pointer (e.g., to free it) a copy must be maintained by
00517    * the caller.
00518    */
00519   int trybind (const char *name, void *&pointer);
00520 
00521   /// Locate @a name and pass out parameter via @a pointer.  If found,
00522   /// return 0, returns -1 if failure occurs.
00523   int find (const char *name, void *&pointer);
00524 
00525   /// Returns 0 if @a name is in the mapping. -1, otherwise.
00526   int find (const char *name);
00527 
00528   /**
00529    * Unbind (remove) the name from the map.  Don't return the pointer
00530    * to the caller.  If you want to remove all occurrences of @a name
00531    * you'll need to call this method multiple times until it fails...
00532    */
00533   int unbind (const char *name);
00534 
00535   /**
00536    * Unbind (remove) one association of @a name to @a pointer.  Returns
00537    * the value of pointer in case the caller needs to deallocate
00538    * memory.  If you want to remove all occurrences of @a name you'll
00539    * need to call this method multiple times until it fails...
00540    */
00541   int unbind (const char *name, void *&pointer);
00542 
00543   // = Protection and "sync" (i.e., flushing data to backing store).
00544 
00545   /**
00546    * Sync @a len bytes of the memory region to the backing store
00547    * starting at @c this->base_addr_.  If @a len == -1 then sync the
00548    * whole region.
00549    */
00550   int sync (ssize_t len = -1, int flags = MS_SYNC);
00551 
00552   /// Sync @a len bytes of the memory region to the backing store
00553   /// starting at @c addr_.
00554   int sync (void *addr, size_t len, int flags = MS_SYNC);
00555 
00556   /**
00557    * Change the protection of the pages of the mapped region to @a prot
00558    * starting at @c this->base_addr_ up to @a len bytes.  If @a len == -1
00559    * then change protection of all pages in the mapped region.
00560    */
00561   int protect (ssize_t len = -1, int prot = PROT_RDWR);
00562 
00563   /// Change the protection of the pages of the mapped region to @a prot
00564   /// starting at @a addr up to @a len bytes.
00565   int protect (void *addr, size_t len, int prot = PROT_RDWR);
00566 
00567   /**
00568    * Returns a count of the number of available chunks that can hold
00569    * @a size byte allocations.  Function can be used to determine if you
00570    * have reached a water mark. This implies a fixed amount of allocated
00571    * memory.
00572    *
00573    * @param size  The chunk size of that you would like a count of
00574    * @return Function returns the number of chunks of the given size
00575    *          that would fit in the currently allocated memory.
00576    */
00577   ssize_t avail_chunks (size_t size) const;
00578 
00579 #if defined (ACE_HAS_MALLOC_STATS)
00580   /// Dump statistics of how malloc is behaving.
00581   void print_stats (void) const;
00582 #endif /* ACE_HAS_MALLOC_STATS */
00583 
00584   /// Returns a pointer to the lock used to provide mutual exclusion to
00585   /// an ACE_Malloc allocator.
00586   ACE_LOCK &mutex (void);
00587 
00588   /// Dump the state of an object.
00589   void dump (void) const;
00590 
00591   /// Declare the dynamic allocation hooks.
00592   ACE_ALLOC_HOOK_DECLARE;
00593 
00594   /// Return cb_ptr value.
00595   void *base_addr (void);
00596 
00597   /**
00598    * Bad flag.  This operation should be called immediately after the
00599    * construction of the Malloc object to query whether the object was
00600    * constructed successfully.  If not, the user should invoke @c
00601    * remove and release the object (it is not usable.)
00602    * @retval 0 if all is fine.  non-zero if this malloc object is
00603    *         unuable.
00604    */
00605   int bad (void);
00606 
00607 private:
00608   /// Initialize the Malloc pool.
00609   int open (void);
00610 
00611   /// Associate @a name with @a pointer.  Assumes that locks are held by
00612   /// callers.
00613   int shared_bind (const char *name,
00614                    void *pointer);
00615 
00616   /**
00617    * Try to locate @a name.  If found, return the associated
00618    * ACE_Name_Node, else returns 0 if can't find the @a name.
00619    * Assumes that locks are held by callers.  Remember to cast the
00620    * return value to ACE_CB::ACE_Name_Node*.
00621    */
00622   void *shared_find (const char *name);
00623 
00624   /// Allocate memory.  Assumes that locks are held by callers.
00625   void *shared_malloc (size_t nbytes);
00626 
00627   /// Deallocate memory.  Assumes that locks are held by callers.
00628   void shared_free (void *ptr);
00629 
00630   /// Pointer to the control block that is stored in memory controlled
00631   /// by <MEMORY_POOL>.
00632   ACE_CB *cb_ptr_;
00633 
00634   /// Pool of memory used by ACE_Malloc to manage its freestore.
00635   MEMORY_POOL memory_pool_;
00636 
00637   /// Lock that ensures mutual exclusion for the memory pool.
00638   ACE_LOCK *lock_;
00639   int delete_lock_;       // True if destructor should delete the lock
00640 
00641   /// Keep track of failure in constructor.
00642   int bad_flag_;
00643 };
00644 
00645 /**
00646  * @class ACE_Malloc_LIFO_Iterator_T
00647  *
00648  * @brief LIFO iterator for names stored in Malloc'd memory.
00649  *
00650  * Does not support deletions while iteration is occurring.
00651  */
00652 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
00653 class ACE_Malloc_LIFO_Iterator_T
00654 {
00655 public:
00656   typedef ACE_TYPENAME ACE_CB::ACE_Name_Node NAME_NODE;
00657   typedef ACE_TYPENAME ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
00658 
00659   // = Initialization method.
00660   /// If @a name = 0 it will iterate through everything else only
00661   /// through those entries whose @a name match.
00662   ACE_Malloc_LIFO_Iterator_T (ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc,
00663                               const char *name = 0);
00664 
00665   /// Destructor.
00666   ~ACE_Malloc_LIFO_Iterator_T (void);
00667 
00668   // = Iteration methods.
00669 
00670   /// Returns 1 when all items have been seen, else 0.
00671   int done (void) const;
00672 
00673   /// Pass back the next entry in the set that hasn't yet been
00674   /// visited.  Returns 0 when all items have been seen, else 1.
00675   int next (void *&next_entry);
00676 
00677   /**
00678    * Pass back the next entry (and the name associated with it) in
00679    * the set that hasn't yet been visited.  Returns 0 when all items
00680    * have been seen, else 1.
00681    */
00682   int next (void *&next_entry,
00683             const char *&name);
00684 
00685   /// Move forward by one element in the set.  Returns 0 when all the
00686   /// items in the set have been seen, else 1.
00687   int advance (void);
00688 
00689   /// Dump the state of an object.
00690   void dump (void) const;
00691 
00692   /// Declare the dynamic allocation hooks.
00693   ACE_ALLOC_HOOK_DECLARE;
00694 
00695 private:
00696   /// Malloc we are iterating over.
00697   ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc_;
00698 
00699   /// Keeps track of how far we've advanced...
00700   NAME_NODE *curr_;
00701 
00702   /// Lock Malloc for the lifetime of the iterator.
00703   ACE_Read_Guard<ACE_LOCK> guard_;
00704 
00705   /// Name that we are searching for.
00706   const char *name_;
00707 };
00708 
00709 /**
00710  * @class ACE_Malloc_FIFO_Iterator_T
00711  *
00712  * @brief FIFO iterator for names stored in Malloc'd memory.
00713  *
00714  * Does not support deletions while iteration is occurring.
00715  */
00716 template <ACE_MEM_POOL_1, class ACE_LOCK, class ACE_CB>
00717 class ACE_Malloc_FIFO_Iterator_T
00718 {
00719 public:
00720   typedef ACE_TYPENAME ACE_CB::ACE_Name_Node NAME_NODE;
00721   typedef ACE_TYPENAME ACE_CB::ACE_Malloc_Header MALLOC_HEADER;
00722 
00723   // = Initialization method.
00724   /// If @a name = 0 it will iterate through everything else only
00725   /// through those entries whose @a name match.
00726   ACE_Malloc_FIFO_Iterator_T (ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc,
00727                               const char *name = 0);
00728 
00729   /// Destructor.
00730   ~ACE_Malloc_FIFO_Iterator_T (void);
00731 
00732   // = Iteration methods.
00733 
00734   /// Returns 1 when all items have been seen, else 0.
00735   int done (void) const;
00736 
00737   /// Pass back the next entry in the set that hasn't yet been
00738   /// visited.  Returns 0 when all items have been seen, else 1.
00739   int next (void *&next_entry);
00740 
00741   /**
00742    * Pass back the next entry (and the name associated with it) in
00743    * the set that hasn't yet been visited.  Returns 0 when all items
00744    * have been seen, else 1.
00745    */
00746   int next (void *&next_entry,
00747             const char *&name);
00748 
00749   /// Move forward by one element in the set.  Returns 0 when all the
00750   /// items in the set have been seen, else 1.
00751   int advance (void);
00752 
00753   /// Go to the starting element that was inserted first. Returns 0
00754   /// when there is no item in the set, else 1.
00755   int start (void);
00756 
00757   /// Dump the state of an object.
00758   void dump (void) const;
00759 
00760   /// Declare the dynamic allocation hooks.
00761   ACE_ALLOC_HOOK_DECLARE;
00762 
00763 private:
00764   /// Malloc we are iterating over.
00765   ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_CB> &malloc_;
00766 
00767   /// Keeps track of how far we've advanced...
00768   NAME_NODE *curr_;
00769 
00770   /// Lock Malloc for the lifetime of the iterator.
00771   ACE_Read_Guard<ACE_LOCK> guard_;
00772 
00773   /// Name that we are searching for.
00774   const char *name_;
00775 };
00776 
00777 template <ACE_MEM_POOL_1, class ACE_LOCK>
00778 class ACE_Malloc : public ACE_Malloc_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
00779 {
00780 public:
00781   // = Initialization and termination methods.
00782   /**
00783    * Initialize ACE_Malloc.  This constructor passes @a pool_name to
00784    * initialize the memory pool, and uses ACE::basename() to
00785    * automatically extract out the name used for the underlying lock
00786    * name (if necessary).  Note that @a pool_name should be located in
00787    * a directory with the appropriate visibility and protection so
00788    * that all processes that need to access it can do so.
00789    */
00790   ACE_Malloc (const ACE_TCHAR *pool_name = 0);
00791 
00792   /**
00793    * Initialize ACE_Malloc.  This constructor passes @a pool_name to
00794    * initialize the memory pool, and uses @a lock_name to automatically
00795    * extract out the name used for the underlying lock name (if
00796    * necessary).  In addition, @a options is passed through to
00797    * initialize the underlying memory pool.  Note that @a pool_name
00798    * should be located in a directory with the appropriate visibility
00799    * and protection so that all processes that need to access it can
00800    * do so.
00801    */
00802   ACE_Malloc (const ACE_TCHAR *pool_name,
00803               const ACE_TCHAR *lock_name,
00804               const ACE_MEM_POOL_OPTIONS *options = 0);
00805 
00806 #if !defined (ACE_HAS_TEMPLATE_TYPEDEFS)
00807   /// This is necessary to work around template bugs with certain C++
00808   /// compilers.
00809   ACE_Malloc (const ACE_TCHAR *pool_name,
00810               const ACE_TCHAR *lock_name,
00811               const void *options = 0);
00812 #endif /* ACE_HAS_TEMPLATE_TYPEDEFS */
00813 };
00814 
00815 template <ACE_MEM_POOL_1, class ACE_LOCK>
00816 class ACE_Malloc_LIFO_Iterator : public ACE_Malloc_LIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
00817 {
00818 public:
00819   // = Initialization method.
00820   /// If @a name = 0 it will iterate through everything else only
00821   /// through those entries whose @a name match.
00822   ACE_Malloc_LIFO_Iterator (ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK> &malloc,
00823                             const char *name = 0);
00824 };
00825 
00826 template <ACE_MEM_POOL_1, class ACE_LOCK>
00827 class ACE_Malloc_FIFO_Iterator : public ACE_Malloc_FIFO_Iterator_T<ACE_MEM_POOL_2, ACE_LOCK, ACE_Control_Block>
00828 {
00829 public:
00830   // = Initialization method.
00831   /// If @a name = 0 it will iterate through everything else only
00832   /// through those entries whose @a name match.
00833   ACE_Malloc_FIFO_Iterator (ACE_Malloc<ACE_MEM_POOL_2, ACE_LOCK> &malloc,
00834                             const char *name = 0);
00835 };
00836 
00837 #if defined (__ACE_INLINE__)
00838 #include "ace/Malloc_T.i"
00839 #endif /* __ACE_INLINE__ */
00840 
00841 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00842 #include "ace/Malloc_T.cpp"
00843 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00844 
00845 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00846 #pragma implementation ("Malloc_T.cpp")
00847 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00848 
00849 #include "ace/post.h"
00850 #endif /* ACE_MALLOC_H */

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