Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ace / Malloc_Base.h
blob6f3ac40375852ff517e46d29de3a0a224c54fd9c
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Malloc_Base.h
7 * @author Doug Schmidt and Irfan Pyarali
8 */
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)
19 # 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"
25 #include <limits>
26 #include <new>
28 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 // The definition of this class is located in Malloc.cpp.
32 /**
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
41 public:
42 /// Unsigned integer type used for specifying memory block lengths.
43 typedef size_t size_type;
45 // = Memory Management
47 /// Get pointer to a default ACE_Allocator.
48 static ACE_Allocator *instance ();
50 /// Set pointer to a process-wide ACE_Allocator and return existing
51 /// pointer.
52 static ACE_Allocator *instance (ACE_Allocator *);
54 /// Delete the dynamically allocated Singleton
55 static void close_singleton ();
57 /// "No-op" constructor (needed to make certain compilers happy).
58 ACE_Allocator ();
60 /// Virtual destructor
61 virtual ~ACE_Allocator ();
63 /// Allocate @a nbytes, but don't give them any initial value.
64 virtual void *malloc (size_type nbytes) = 0;
66 /// Allocate @a nbytes, giving them @a initial_value.
67 virtual void *calloc (size_type nbytes, char initial_value = '\0') = 0;
69 /// Allocate @a n_elem each of size @a elem_size, giving them
70 /// @a initial_value.
71 virtual void *calloc (size_type n_elem,
72 size_type elem_size,
73 char initial_value = '\0') = 0;
75 /// Free @a ptr (must have been allocated by ACE_Allocator::malloc()).
76 virtual void free (void *ptr) = 0;
78 /// Remove any resources associated with this memory manager.
79 virtual int remove () = 0;
81 // = Map manager like functions
83 /**
84 * Associate @a name with @a pointer. If @a duplicates == 0 then do
85 * not allow duplicate @a name/@a pointer associations, else if
86 * @a duplicates != 0 then allow duplicate @a name/@a pointer
87 * associations. Returns 0 if successfully binds (1) a previously
88 * unbound @a name or (2) @a duplicates != 0, returns 1 if trying to
89 * bind a previously bound @a name and @a duplicates == 0, else
90 * returns -1 if a resource failure occurs.
92 virtual int bind (const char *name, void *pointer, int duplicates = 0) = 0;
94 /**
95 * Associate @a name with @a pointer. Does not allow duplicate
96 * @a name/@a pointer associations. Returns 0 if successfully binds
97 * (1) a previously unbound @a name, 1 if trying to bind a previously
98 * bound @a name, or returns -1 if a resource failure occurs. When
99 * this call returns @a pointer's value will always reference the
100 * void * that @a name is associated with. Thus, if the caller needs
101 * to use @a pointer (e.g., to free it) a copy must be maintained by
102 * the caller.
104 virtual int trybind (const char *name, void *&pointer) = 0;
106 /// Locate @a name and pass out parameter via pointer. If found,
107 /// return 0, returns -1 if failure occurs.
108 virtual int find (const char *name, void *&pointer) = 0;
110 /// Returns 0 if the name is in the mapping. -1, otherwise.
111 virtual int find (const char *name) = 0;
113 /// Unbind (remove) the name from the map. Don't return the pointer
114 /// to the caller
115 virtual int unbind (const char *name) = 0;
117 /// Break any association of name. Returns the value of pointer in
118 /// case the caller needs to deallocate memory.
119 virtual int unbind (const char *name, void *&pointer) = 0;
121 // = Protection and "sync" (i.e., flushing memory to persistent
122 // backing store).
125 * Sync @a len bytes of the memory region to the backing store
126 * starting at @c this->base_addr_. If @a len == -1 then sync the
127 * whole region.
129 virtual int sync (ssize_t len = -1, int flags = MS_SYNC) = 0;
131 /// Sync @a len bytes of the memory region to the backing store
132 /// starting at @a addr.
133 virtual int sync (void *addr, size_type len, int flags = MS_SYNC) = 0;
136 * Change the protection of the pages of the mapped region to @a prot
137 * starting at <this->base_addr_> up to @a len bytes. If @a len == -1
138 * then change protection of all pages in the mapped region.
140 virtual int protect (ssize_t len = -1, int prot = PROT_RDWR) = 0;
142 /// Change the protection of the pages of the mapped region to @a prot
143 /// starting at @a addr up to @a len bytes.
144 virtual int protect (void *addr, size_type len, int prot = PROT_RDWR) = 0;
146 #if defined (ACE_HAS_MALLOC_STATS)
147 /// Dump statistics of how malloc is behaving.
148 virtual void print_stats () const = 0;
149 #endif /* ACE_HAS_MALLOC_STATS */
151 /// Dump the state of the object.
152 virtual void dump () const = 0;
153 private:
154 // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!! See the
155 // <ACE_Allocator::instance> implementation for explanation.
157 /// Pointer to a process-wide ACE_Allocator instance.
158 static ACE_Allocator *allocator_;
162 * @class ACE_Allocator_Std_Adapter
164 * @brief Model of std::allocator that forwards requests to
165 # ACE_Allocator::instance. To be used with STL containers.
168 template <typename T>
169 class ACE_Export ACE_Allocator_Std_Adapter
171 public:
172 typedef T value_type;
173 typedef T* pointer;
174 typedef const T* const_pointer;
175 typedef T& reference;
176 typedef const T& const_reference;
177 typedef std::size_t size_type;
178 typedef std::ptrdiff_t difference_type;
179 template <typename U> struct rebind { typedef ACE_Allocator_Std_Adapter<U> other; };
181 ACE_Allocator_Std_Adapter() = default;
183 template <typename U>
184 ACE_Allocator_Std_Adapter(const ACE_Allocator_Std_Adapter<U>&) {}
186 static T* allocate(std::size_t n)
188 void* raw_mem = ACE_Allocator::instance()->malloc(n * sizeof(T));
189 if (!raw_mem) throw std::bad_alloc();
190 return static_cast<T*>(raw_mem);
193 static void deallocate(T* ptr, std::size_t)
195 ACE_Allocator::instance()->free(ptr);
198 static void construct(T* ptr, const T& value)
200 new (static_cast<void*>(ptr)) T(value);
203 static void destroy(T* ptr)
205 ptr->~T();
208 static size_type max_size()
210 return (std::numeric_limits<size_type>::max)();
214 template <typename T, typename U>
215 bool operator==(const ACE_Allocator_Std_Adapter<T>&, const ACE_Allocator_Std_Adapter<U>&)
217 return true;
220 template <typename T, typename U>
221 bool operator!=(const ACE_Allocator_Std_Adapter<T>&, const ACE_Allocator_Std_Adapter<U>&)
223 return false;
226 ACE_END_VERSIONED_NAMESPACE_DECL
228 #include /**/ "ace/post.h"
229 #endif /* ACE_MALLOC_BASE_H */