PI_Malloc.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file   PI_Malloc.h
00006  *
00007  *  PI_Malloc.h,v 4.14 2006/04/19 11:54:16 jwillemsen Exp
00008  *
00009  *  @author Priyanka Gontla <pgontla@ece.uci.edu>
00010  *  @author Based on code that existed in other ACE files.
00011  */
00012 //==========================================================================
00013 
00014 #ifndef ACE_PI_MALLOC_H
00015 #define ACE_PI_MALLOC_H
00016 
00017 #include /**/ "ace/pre.h"
00018 
00019 #include "ace/ACE_export.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
00026 
00027 #include "ace/Malloc.h"
00028 #include "ace/Based_Pointer_T.h"
00029 
00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00031 
00032 // prepare for position independent malloc
00033 /**
00034  * @class ACE_PI_Control_Block
00035  *
00036  * @brief This information is stored in memory allocated by the Memory_Pool.
00037  *
00038  * This class implements the control block structure that can be
00039  * used in a "position indepent" fashion, i.e., you don't need to
00040  * "map" the underlying memory pool to the same address in
00041  * processes sharing the memory.  The tradoff of this flexibility
00042  * is more expensive malloc/free operations.
00043  */
00044 class ACE_Export ACE_PI_Control_Block
00045 {
00046 public:
00047   class ACE_Malloc_Header;
00048   class ACE_Name_Node;
00049 
00050   typedef ACE_Based_Pointer<ACE_Malloc_Header> MALLOC_HEADER_PTR;
00051   typedef ACE_Based_Pointer<ACE_Name_Node> NAME_NODE_PTR;
00052   typedef ACE_Based_Pointer_Basic<char> CHAR_PTR;
00053 
00054   /**
00055    * @class ACE_Malloc_Header
00056    *
00057    * @brief This is the control block header.  It's used by ACE_Malloc
00058    * to keep track of each chunk of data when it's in the free
00059    * list or in use.
00060    */
00061   class ACE_Export ACE_Malloc_Header
00062   {
00063   public:
00064     ACE_Malloc_Header (void);
00065 
00066     /// Points to next block if on free list.
00067     MALLOC_HEADER_PTR next_block_;
00068 
00069     /// Initialize a malloc header pointer.
00070     static void init_ptr (MALLOC_HEADER_PTR *ptr,
00071                           ACE_Malloc_Header *init,
00072                           void *base_addr);
00073 
00074     /// Size of this header control block.
00075     size_t size_;
00076 
00077 # if !defined (ACE_PI_MALLOC_PADDING_SIZE)
00078 #   define ACE_PI_MALLOC_PADDING_SIZE ACE_MALLOC_ROUNDUP (ACE_MALLOC_HEADER_SIZE + sizeof (MALLOC_HEADER_PTR) + sizeof (size_t), ACE_MALLOC_ALIGN) - (sizeof (MALLOC_HEADER_PTR) + sizeof (size_t))
00079 # endif /* !ACE_PI_MALLOC_PADDING_SIZE */
00080     char padding_[(ACE_PI_MALLOC_PADDING_SIZE) ? ACE_PI_MALLOC_PADDING_SIZE : ACE_MALLOC_ALIGN];
00081 
00082     /// Dump the state of the object.
00083     void dump (void) const;
00084 
00085   private:
00086 
00087     // Disallow copy construction and assignment.
00088     ACE_Malloc_Header (ACE_Malloc_Header const &);
00089     void operator= (ACE_Malloc_Header const &);
00090 
00091   };
00092 
00093   /**
00094    * @class ACE_Name_Node
00095    *
00096    * @brief This class supports "named memory regions" within ACE_Malloc.
00097    *
00098    * Internally, the named memory regions are stored as a
00099    * doubly-linked list within the Memory_Pool.  This makes
00100    * it easy to iterate over the items in the list in both FIFO
00101    * and LIFO order.
00102    */
00103   class ACE_Export ACE_Name_Node
00104   {
00105   public:
00106     // = Initialization methods.
00107     /// Constructor.
00108     ACE_Name_Node (const char *name,
00109                    char *name_ptr,
00110                    char *pointer,
00111                    ACE_Name_Node *head);
00112 
00113     /// Copy constructor.
00114     ACE_Name_Node (const ACE_Name_Node &);
00115 
00116     /// Constructor.
00117     ACE_Name_Node (void);
00118 
00119     /// Constructor.
00120     ~ACE_Name_Node (void);
00121 
00122     /// Initialize a name node pointer.
00123     static void init_ptr (NAME_NODE_PTR *ptr,
00124                           ACE_Name_Node *init,
00125                           void *base_addr);
00126 
00127     /// Return a pointer to the name of this node.
00128     const char *name (void) const;
00129 
00130     /// Assign a name;
00131     void name (const char *);
00132 
00133     /// Name of the Node.
00134     CHAR_PTR name_;
00135 
00136     /// Pointer to the contents.
00137     CHAR_PTR pointer_;
00138 
00139     /// Pointer to the next node in the doubly-linked list.
00140     NAME_NODE_PTR next_;
00141 
00142     /// Pointer to the previous node in the doubly-linked list.
00143     NAME_NODE_PTR prev_;
00144 
00145     /// Dump the state of the object.
00146     void dump (void) const;
00147 
00148   private:
00149 
00150     // Disallow assignment.
00151     void operator= (const ACE_Name_Node &);
00152   };
00153 
00154   /// Print out a bunch of size info for debugging.
00155   static void print_alignment_info (void);
00156 
00157   /// Reference counter.
00158   int ref_counter_;
00159 
00160   /// Head of the linked list of Name Nodes.
00161   NAME_NODE_PTR name_head_;
00162 
00163   /// Current head of the freelist.
00164   MALLOC_HEADER_PTR freep_;
00165 
00166   /// Name of lock thats ensures mutual exclusion.
00167   char lock_name_[MAXNAMELEN];
00168 
00169 #if defined (ACE_HAS_MALLOC_STATS)
00170   /// Keep statistics about ACE_Malloc state and performance.
00171   ACE_Malloc_Stats malloc_stats_;
00172 #define ACE_PI_CONTROL_BLOCK_SIZE ((int)(sizeof (NAME_NODE_PTR) \
00173                                          + sizeof (MALLOC_HEADER_PTR) \
00174                                          + sizeof (int) \
00175                                          + MAXNAMELEN  \
00176                                          + sizeof (ACE_Malloc_Stats)))
00177 #else
00178 #define ACE_PI_CONTROL_BLOCK_SIZE ((int)(sizeof (NAME_NODE_PTR) \
00179                                          + sizeof (MALLOC_HEADER_PTR) \
00180                                          + sizeof (int) \
00181                                          + MAXNAMELEN))
00182 #endif /* ACE_HAS_MALLOC_STATS */
00183 
00184 # if !defined (ACE_PI_CONTROL_BLOCK_ALIGN_BYTES)
00185 #   define ACE_PI_CONTROL_BLOCK_ALIGN_BYTES \
00186         ACE_MALLOC_ROUNDUP (ACE_PI_CONTROL_BLOCK_SIZE, ACE_MALLOC_ALIGN) - ACE_PI_CONTROL_BLOCK_SIZE
00187 # endif /* !ACE_PI_CONTROL_BLOCK_ALIGN_LONGS */
00188   /// Force alignment.
00189   char align_[(ACE_PI_CONTROL_BLOCK_ALIGN_BYTES) ? ACE_PI_CONTROL_BLOCK_ALIGN_BYTES : ACE_MALLOC_ALIGN];
00190 
00191   /// Dummy node used to anchor the freelist.  This needs to come last...
00192   ACE_Malloc_Header base_;
00193 
00194   /// Dump the state of the object.
00195   void dump (void) const;
00196 
00197 private:
00198 
00199   // Disallow assignment.
00200   void operator= (const ACE_Control_Block &);
00201 };
00202 
00203 ACE_END_VERSIONED_NAMESPACE_DECL
00204 
00205 #if defined (__ACE_INLINE__)
00206 #include "ace/PI_Malloc.inl"
00207 #endif /* __ACE_INLINE__ */
00208 
00209 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */
00210 
00211 #include /**/ "ace/post.h"
00212 
00213 #endif /* ACE_PI_MALLOC_H */

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