00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Malloc_Base.h 00006 * 00007 * Malloc_Base.h,v 4.18 2006/04/19 11:51:53 jwillemsen Exp 00008 * 00009 * @author Doug Schmidt and Irfan Pyarali 00010 */ 00011 //============================================================================= 00012 00013 00014 #ifndef ACE_MALLOC_BASE_H 00015 #define ACE_MALLOC_BASE_H 00016 #include /**/ "ace/pre.h" 00017 00018 #include "ace/ACE_export.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 #include "ace/os_include/sys/os_types.h" 00025 #include "ace/os_include/sys/os_mman.h" 00026 #include "ace/os_include/sys/os_types.h" 00027 00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00029 00030 // The definition of this class is located in Malloc.cpp. 00031 00032 /** 00033 * @class ACE_Allocator 00034 * 00035 * @brief Interface for a dynamic memory allocator that uses inheritance 00036 * and dynamic binding to provide extensible mechanisms for 00037 * allocating and deallocating memory. 00038 */ 00039 class ACE_Export ACE_Allocator 00040 { 00041 public: 00042 // = Memory Management 00043 00044 /// Get pointer to a default ACE_Allocator. 00045 static ACE_Allocator *instance (void); 00046 00047 /// Set pointer to a process-wide ACE_Allocator and return existing 00048 /// pointer. 00049 static ACE_Allocator *instance (ACE_Allocator *); 00050 00051 /// Delete the dynamically allocated Singleton 00052 static void close_singleton (void); 00053 00054 /// "No-op" constructor (needed to make certain compilers happy). 00055 ACE_Allocator (void); 00056 00057 /// Virtual destructor 00058 virtual ~ACE_Allocator (void); 00059 00060 /// Allocate <nbytes>, but don't give them any initial value. 00061 virtual void *malloc (size_t nbytes) = 0; 00062 00063 /// Allocate <nbytes>, giving them <initial_value>. 00064 virtual void *calloc (size_t nbytes, char initial_value = '\0') = 0; 00065 00066 /// Allocate <n_elem> each of size <elem_size>, giving them 00067 /// <initial_value>. 00068 virtual void *calloc (size_t n_elem, 00069 size_t elem_size, 00070 char initial_value = '\0') = 0; 00071 00072 /// Free <ptr> (must have been allocated by <ACE_Allocator::malloc>). 00073 virtual void free (void *ptr) = 0; 00074 00075 /// Remove any resources associated with this memory manager. 00076 virtual int remove (void) = 0; 00077 00078 // = Map manager like functions 00079 00080 /** 00081 * Associate <name> with <pointer>. If <duplicates> == 0 then do 00082 * not allow duplicate <name>/<pointer> associations, else if 00083 * <duplicates> != 0 then allow duplicate <name>/<pointer> 00084 * assocations. Returns 0 if successfully binds (1) a previously 00085 * unbound <name> or (2) <duplicates> != 0, returns 1 if trying to 00086 * bind a previously bound <name> and <duplicates> == 0, else 00087 * returns -1 if a resource failure occurs. 00088 */ 00089 virtual int bind (const char *name, void *pointer, int duplicates = 0) = 0; 00090 00091 /** 00092 * Associate <name> with <pointer>. Does not allow duplicate 00093 * <name>/<pointer> associations. Returns 0 if successfully binds 00094 * (1) a previously unbound <name>, 1 if trying to bind a previously 00095 * bound <name>, or returns -1 if a resource failure occurs. When 00096 * this call returns <pointer>'s value will always reference the 00097 * void * that <name> is associated with. Thus, if the caller needs 00098 * to use <pointer> (e.g., to free it) a copy must be maintained by 00099 * the caller. 00100 */ 00101 virtual int trybind (const char *name, void *&pointer) = 0; 00102 00103 /// Locate <name> and pass out parameter via pointer. If found, 00104 /// return 0, returns -1 if failure occurs. 00105 virtual int find (const char *name, void *&pointer) = 0; 00106 00107 /// Returns 0 if the name is in the mapping. -1, otherwise. 00108 virtual int find (const char *name) = 0; 00109 00110 /// Unbind (remove) the name from the map. Don't return the pointer 00111 /// to the caller 00112 virtual int unbind (const char *name) = 0; 00113 00114 /// Break any association of name. Returns the value of pointer in 00115 /// case the caller needs to deallocate memory. 00116 virtual int unbind (const char *name, void *&pointer) = 0; 00117 00118 // = Protection and "sync" (i.e., flushing memory to persistent 00119 // backing store). 00120 00121 /** 00122 * Sync @a len bytes of the memory region to the backing store 00123 * starting at @c this->base_addr_. If @a len == -1 then sync the 00124 * whole region. 00125 */ 00126 virtual int sync (ssize_t len = -1, int flags = MS_SYNC) = 0; 00127 00128 /// Sync @a len bytes of the memory region to the backing store 00129 /// starting at @a addr. 00130 virtual int sync (void *addr, size_t len, int flags = MS_SYNC) = 0; 00131 00132 /** 00133 * Change the protection of the pages of the mapped region to <prot> 00134 * starting at <this->base_addr_> up to <len> bytes. If <len> == -1 00135 * then change protection of all pages in the mapped region. 00136 */ 00137 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR) = 0; 00138 00139 /// Change the protection of the pages of the mapped region to <prot> 00140 /// starting at <addr> up to <len> bytes. 00141 virtual int protect (void *addr, size_t len, int prot = PROT_RDWR) = 0; 00142 00143 #if defined (ACE_HAS_MALLOC_STATS) 00144 /// Dump statistics of how malloc is behaving. 00145 virtual void print_stats (void) const = 0; 00146 #endif /* ACE_HAS_MALLOC_STATS */ 00147 00148 /// Dump the state of the object. 00149 virtual void dump (void) const = 0; 00150 private: 00151 // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!! See the 00152 // <ACE_Allocator::instance> implementation for explanation. 00153 00154 /// Pointer to a process-wide ACE_Allocator instance. 00155 static ACE_Allocator *allocator_; 00156 00157 /// Must delete the <allocator_> if non-0. 00158 static int delete_allocator_; 00159 }; 00160 00161 ACE_END_VERSIONED_NAMESPACE_DECL 00162 00163 #include /**/ "ace/post.h" 00164 #endif /* ACE_MALLOC_BASE_H */