00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Malloc_Base.h 00006 * 00007 * $Id: Malloc_Base.h 80826 2008-03-04 14:51:23Z wotte $ 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 00043 /// Unsigned integer type used for specifying memory block lengths. 00044 typedef size_t size_type; 00045 00046 // = Memory Management 00047 00048 /// Get pointer to a default ACE_Allocator. 00049 static ACE_Allocator *instance (void); 00050 00051 /// Set pointer to a process-wide ACE_Allocator and return existing 00052 /// pointer. 00053 static ACE_Allocator *instance (ACE_Allocator *); 00054 00055 /// Delete the dynamically allocated Singleton 00056 static void close_singleton (void); 00057 00058 /// "No-op" constructor (needed to make certain compilers happy). 00059 ACE_Allocator (void); 00060 00061 /// Virtual destructor 00062 virtual ~ACE_Allocator (void); 00063 00064 /// Allocate @a nbytes, but don't give them any initial value. 00065 virtual void *malloc (size_type nbytes) = 0; 00066 00067 /// Allocate @a nbytes, giving them @a initial_value. 00068 virtual void *calloc (size_type nbytes, char initial_value = '\0') = 0; 00069 00070 /// Allocate <n_elem> each of size @a elem_size, giving them 00071 /// @a initial_value. 00072 virtual void *calloc (size_type n_elem, 00073 size_type elem_size, 00074 char initial_value = '\0') = 0; 00075 00076 /// Free <ptr> (must have been allocated by <ACE_Allocator::malloc>). 00077 virtual void free (void *ptr) = 0; 00078 00079 /// Remove any resources associated with this memory manager. 00080 virtual int remove (void) = 0; 00081 00082 // = Map manager like functions 00083 00084 /** 00085 * Associate @a name with @a pointer. If @a duplicates == 0 then do 00086 * not allow duplicate @a name/@a pointer associations, else if 00087 * @a duplicates != 0 then allow duplicate @a name/@a pointer 00088 * assocations. Returns 0 if successfully binds (1) a previously 00089 * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to 00090 * bind a previously bound @a name and @a duplicates == 0, else 00091 * returns -1 if a resource failure occurs. 00092 */ 00093 virtual int bind (const char *name, void *pointer, int duplicates = 0) = 0; 00094 00095 /** 00096 * Associate @a name with @a pointer. Does not allow duplicate 00097 * @a name/@a pointer associations. Returns 0 if successfully binds 00098 * (1) a previously unbound @a name, 1 if trying to bind a previously 00099 * bound @a name, or returns -1 if a resource failure occurs. When 00100 * this call returns @a pointer's value will always reference the 00101 * void * that @a name is associated with. Thus, if the caller needs 00102 * to use @a pointer (e.g., to free it) a copy must be maintained by 00103 * the caller. 00104 */ 00105 virtual int trybind (const char *name, void *&pointer) = 0; 00106 00107 /// Locate @a name and pass out parameter via pointer. If found, 00108 /// return 0, returns -1 if failure occurs. 00109 virtual int find (const char *name, void *&pointer) = 0; 00110 00111 /// Returns 0 if the name is in the mapping. -1, otherwise. 00112 virtual int find (const char *name) = 0; 00113 00114 /// Unbind (remove) the name from the map. Don't return the pointer 00115 /// to the caller 00116 virtual int unbind (const char *name) = 0; 00117 00118 /// Break any association of name. Returns the value of pointer in 00119 /// case the caller needs to deallocate memory. 00120 virtual int unbind (const char *name, void *&pointer) = 0; 00121 00122 // = Protection and "sync" (i.e., flushing memory to persistent 00123 // backing store). 00124 00125 /** 00126 * Sync @a len bytes of the memory region to the backing store 00127 * starting at @c this->base_addr_. If @a len == -1 then sync the 00128 * whole region. 00129 */ 00130 virtual int sync (ssize_t len = -1, int flags = MS_SYNC) = 0; 00131 00132 /// Sync @a len bytes of the memory region to the backing store 00133 /// starting at @a addr. 00134 virtual int sync (void *addr, size_type len, int flags = MS_SYNC) = 0; 00135 00136 /** 00137 * Change the protection of the pages of the mapped region to @a prot 00138 * starting at <this->base_addr_> up to @a len bytes. If @a len == -1 00139 * then change protection of all pages in the mapped region. 00140 */ 00141 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR) = 0; 00142 00143 /// Change the protection of the pages of the mapped region to @a prot 00144 /// starting at @a addr up to @a len bytes. 00145 virtual int protect (void *addr, size_type len, int prot = PROT_RDWR) = 0; 00146 00147 #if defined (ACE_HAS_MALLOC_STATS) 00148 /// Dump statistics of how malloc is behaving. 00149 virtual void print_stats (void) const = 0; 00150 #endif /* ACE_HAS_MALLOC_STATS */ 00151 00152 /// Dump the state of the object. 00153 virtual void dump (void) const = 0; 00154 private: 00155 // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!! See the 00156 // <ACE_Allocator::instance> implementation for explanation. 00157 00158 /// Pointer to a process-wide ACE_Allocator instance. 00159 static ACE_Allocator *allocator_; 00160 00161 /// Must delete the <allocator_> if non-0. 00162 static int delete_allocator_; 00163 }; 00164 00165 ACE_END_VERSIONED_NAMESPACE_DECL 00166 00167 #include /**/ "ace/post.h" 00168 #endif /* ACE_MALLOC_BASE_H */