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