3 //=============================================================================
7 * @author Doug Schmidt and Irfan Pyarali
9 //=============================================================================
12 #ifndef ACE_MALLOC_BASE_H
13 #define ACE_MALLOC_BASE_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/os_include/sys/os_types.h"
23 #include "ace/os_include/sys/os_mman.h"
24 #include "ace/os_include/sys/os_types.h"
28 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 // The definition of this class is located in Malloc.cpp.
33 * @class ACE_Allocator
35 * @brief Interface for a dynamic memory allocator that uses inheritance
36 * and dynamic binding to provide extensible mechanisms for
37 * allocating and deallocating memory.
39 class ACE_Export ACE_Allocator
43 /// Unsigned integer type used for specifying memory block lengths.
44 typedef size_t size_type
;
46 // = Memory Management
48 /// Get pointer to a default ACE_Allocator.
49 static ACE_Allocator
*instance (void);
51 /// Set pointer to a process-wide ACE_Allocator and return existing
53 static ACE_Allocator
*instance (ACE_Allocator
*);
55 /// Delete the dynamically allocated Singleton
56 static void close_singleton (void);
58 /// "No-op" constructor (needed to make certain compilers happy).
61 /// Virtual destructor
62 virtual ~ACE_Allocator (void);
64 /// Allocate @a nbytes, but don't give them any initial value.
65 virtual void *malloc (size_type nbytes
) = 0;
67 /// Allocate @a nbytes, giving them @a initial_value.
68 virtual void *calloc (size_type nbytes
, char initial_value
= '\0') = 0;
70 /// Allocate @a n_elem each of size @a elem_size, giving them
72 virtual void *calloc (size_type n_elem
,
74 char initial_value
= '\0') = 0;
76 /// Free @a ptr (must have been allocated by ACE_Allocator::malloc()).
77 virtual void free (void *ptr
) = 0;
79 /// Remove any resources associated with this memory manager.
80 virtual int remove (void) = 0;
82 // = Map manager like functions
85 * Associate @a name with @a pointer. If @a duplicates == 0 then do
86 * not allow duplicate @a name/@a pointer associations, else if
87 * @a duplicates != 0 then allow duplicate @a name/@a pointer
88 * associations. Returns 0 if successfully binds (1) a previously
89 * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
90 * bind a previously bound @a name and @a duplicates == 0, else
91 * returns -1 if a resource failure occurs.
93 virtual int bind (const char *name
, void *pointer
, int duplicates
= 0) = 0;
96 * Associate @a name with @a pointer. Does not allow duplicate
97 * @a name/@a pointer associations. Returns 0 if successfully binds
98 * (1) a previously unbound @a name, 1 if trying to bind a previously
99 * bound @a name, or returns -1 if a resource failure occurs. When
100 * this call returns @a pointer's value will always reference the
101 * void * that @a name is associated with. Thus, if the caller needs
102 * to use @a pointer (e.g., to free it) a copy must be maintained by
105 virtual int trybind (const char *name
, void *&pointer
) = 0;
107 /// Locate @a name and pass out parameter via pointer. If found,
108 /// return 0, returns -1 if failure occurs.
109 virtual int find (const char *name
, void *&pointer
) = 0;
111 /// Returns 0 if the name is in the mapping. -1, otherwise.
112 virtual int find (const char *name
) = 0;
114 /// Unbind (remove) the name from the map. Don't return the pointer
116 virtual int unbind (const char *name
) = 0;
118 /// Break any association of name. Returns the value of pointer in
119 /// case the caller needs to deallocate memory.
120 virtual int unbind (const char *name
, void *&pointer
) = 0;
122 // = Protection and "sync" (i.e., flushing memory to persistent
126 * Sync @a len bytes of the memory region to the backing store
127 * starting at @c this->base_addr_. If @a len == -1 then sync the
130 virtual int sync (ssize_t len
= -1, int flags
= MS_SYNC
) = 0;
132 /// Sync @a len bytes of the memory region to the backing store
133 /// starting at @a addr.
134 virtual int sync (void *addr
, size_type len
, int flags
= MS_SYNC
) = 0;
137 * Change the protection of the pages of the mapped region to @a prot
138 * starting at <this->base_addr_> up to @a len bytes. If @a len == -1
139 * then change protection of all pages in the mapped region.
141 virtual int protect (ssize_t len
= -1, int prot
= PROT_RDWR
) = 0;
143 /// Change the protection of the pages of the mapped region to @a prot
144 /// starting at @a addr up to @a len bytes.
145 virtual int protect (void *addr
, size_type len
, int prot
= PROT_RDWR
) = 0;
147 #if defined (ACE_HAS_MALLOC_STATS)
148 /// Dump statistics of how malloc is behaving.
149 virtual void print_stats (void) const = 0;
150 #endif /* ACE_HAS_MALLOC_STATS */
152 /// Dump the state of the object.
153 virtual void dump (void) const = 0;
155 // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!! See the
156 // <ACE_Allocator::instance> implementation for explanation.
158 /// Pointer to a process-wide ACE_Allocator instance.
159 static ACE_Allocator
*allocator_
;
163 * @class ACE_Allocator_Std_Adapter
165 * @brief Model of std::allocator that forwards requests to
166 # ACE_Allocator::instance. To be used with STL containers.
169 template <typename T
>
170 class ACE_Export ACE_Allocator_Std_Adapter
173 typedef T value_type
;
175 typedef const T
* const_pointer
;
176 typedef T
& reference
;
177 typedef const T
& const_reference
;
178 typedef std::size_t size_type
;
179 typedef std::ptrdiff_t difference_type
;
180 template <typename U
> struct rebind
{ typedef ACE_Allocator_Std_Adapter
<U
> other
; };
182 ACE_Allocator_Std_Adapter() {}
184 template <typename U
>
185 ACE_Allocator_Std_Adapter(const ACE_Allocator_Std_Adapter
<U
>&) {}
187 static T
* allocate(std::size_t n
)
189 void* raw_mem
= ACE_Allocator::instance()->malloc(n
* sizeof(T
));
190 if (!raw_mem
) throw std::bad_alloc();
191 return static_cast<T
*>(raw_mem
);
194 static void deallocate(T
* ptr
, std::size_t)
196 ACE_Allocator::instance()->free(ptr
);
199 static void construct(T
* ptr
, const T
& value
)
201 new (static_cast<void*>(ptr
)) T(value
);
204 static void destroy(T
* ptr
)
209 static size_type
max_size()
211 return (std::numeric_limits
<size_type
>::max
)();
215 template <typename T
, typename U
>
216 bool operator==(const ACE_Allocator_Std_Adapter
<T
>&, const ACE_Allocator_Std_Adapter
<U
>&)
221 template <typename T
, typename U
>
222 bool operator!=(const ACE_Allocator_Std_Adapter
<T
>&, const ACE_Allocator_Std_Adapter
<U
>&)
227 ACE_END_VERSIONED_NAMESPACE_DECL
229 #include /**/ "ace/post.h"
230 #endif /* ACE_MALLOC_BASE_H */