2 * @brief Memory allocation functions.
12 /** MEMALLOC declares the allocators for a class to allocate
13 * memory in the non-snapshotting heap. */
15 void * operator new(size_t size) { \
16 return model_malloc(size); \
18 void operator delete(void *p, size_t size) { \
21 void * operator new[](size_t size) { \
22 return model_malloc(size); \
24 void operator delete[](void *p, size_t size) { \
27 void * operator new(size_t size, void *p) { /* placement new */ \
31 /** SNAPSHOTALLOC declares the allocators for a class to allocate
32 * memory in the snapshotting heap. */
33 #define SNAPSHOTALLOC \
34 void * operator new(size_t size) { \
35 return snapshot_malloc(size); \
37 void operator delete(void *p, size_t size) { \
40 void * operator new[](size_t size) { \
41 return snapshot_malloc(size); \
43 void operator delete[](void *p, size_t size) { \
46 void * operator new(size_t size, void *p) { /* placement new */ \
50 void *model_malloc(size_t size);
51 void *model_calloc(size_t count, size_t size);
52 void model_free(void *ptr);
53 void * model_realloc(void *ptr, size_t size);
55 void * snapshot_malloc(size_t size);
56 void * snapshot_calloc(size_t count, size_t size);
57 void * snapshot_realloc(void *ptr, size_t size);
58 void snapshot_free(void *ptr);
60 void * Thread_malloc(size_t size);
61 void Thread_free(void *ptr);
63 /** @brief Provides a non-snapshotting allocator for use in STL classes.
65 * The code was adapted from a code example from the book The C++
66 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
67 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
68 * Permission to copy, use, modify, sell and distribute this software
69 * is granted provided this copyright notice appears in all copies.
70 * This software is provided "as is" without express or implied
71 * warranty, and with no claim as to its suitability for any purpose.
79 typedef const T* const_pointer;
81 typedef const T& const_reference;
82 typedef size_t size_type;
83 typedef size_t difference_type;
85 // rebind allocator to type U
88 typedef ModelAlloc<U> other;
91 // return address of values
92 pointer address(reference value) const {
95 const_pointer address(const_reference value) const {
99 /* constructors and destructor
100 * - nothing to do because the allocator has no state
102 ModelAlloc() throw() {
104 ModelAlloc(const ModelAlloc&) throw() {
107 ModelAlloc(const ModelAlloc<U>&) throw() {
109 ~ModelAlloc() throw() {
112 // return maximum number of elements that can be allocated
113 size_type max_size() const throw() {
114 return std::numeric_limits<size_t>::max() / sizeof(T);
117 // allocate but don't initialize num elements of type T
118 pointer allocate(size_type num, const void * = 0) {
119 pointer p = (pointer)model_malloc(num * sizeof(T));
123 // initialize elements of allocated storage p with value value
124 void construct(pointer p, const T& value) {
125 // initialize memory with placement new
126 new((void*)p)T(value);
129 // destroy elements of initialized storage p
130 void destroy(pointer p) {
131 // destroy objects by calling their destructor
135 // deallocate storage p of deleted elements
136 void deallocate(pointer p, size_type num) {
137 model_free((void*)p);
141 /** Return that all specializations of this allocator are interchangeable. */
142 template <class T1, class T2>
143 bool operator ==(const ModelAlloc<T1>&,
144 const ModelAlloc<T2>&) throw() {
148 /** Return that all specializations of this allocator are interchangeable. */
149 template <class T1, class T2>
150 bool operator!= (const ModelAlloc<T1>&,
151 const ModelAlloc<T2>&) throw() {
155 /** @brief Provides a snapshotting allocator for use in STL classes.
157 * The code was adapted from a code example from the book The C++
158 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
159 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
160 * Permission to copy, use, modify, sell and distribute this software
161 * is granted provided this copyright notice appears in all copies.
162 * This software is provided "as is" without express or implied
163 * warranty, and with no claim as to its suitability for any purpose.
166 class SnapshotAlloc {
169 typedef T value_type;
171 typedef const T* const_pointer;
172 typedef T& reference;
173 typedef const T& const_reference;
174 typedef size_t size_type;
175 typedef size_t difference_type;
177 // rebind allocator to type U
180 typedef SnapshotAlloc<U> other;
183 // return address of values
184 pointer address(reference value) const {
187 const_pointer address(const_reference value) const {
191 /* constructors and destructor
192 * - nothing to do because the allocator has no state
194 SnapshotAlloc() throw() {
196 SnapshotAlloc(const SnapshotAlloc&) throw() {
199 SnapshotAlloc(const SnapshotAlloc<U>&) throw() {
201 ~SnapshotAlloc() throw() {
204 // return maximum number of elements that can be allocated
205 size_type max_size() const throw() {
206 return std::numeric_limits<size_t>::max() / sizeof(T);
209 // allocate but don't initialize num elements of type T
210 pointer allocate(size_type num, const void * = 0) {
211 pointer p = (pointer)snapshot_malloc(num * sizeof(T));
215 // initialize elements of allocated storage p with value value
216 void construct(pointer p, const T& value) {
217 // initialize memory with placement new
218 new((void*)p)T(value);
221 // destroy elements of initialized storage p
222 void destroy(pointer p) {
223 // destroy objects by calling their destructor
227 // deallocate storage p of deleted elements
228 void deallocate(pointer p, size_type num) {
229 snapshot_free((void*)p);
233 /** Return that all specializations of this allocator are interchangeable. */
234 template <class T1, class T2>
235 bool operator ==(const SnapshotAlloc<T1>&,
236 const SnapshotAlloc<T2>&) throw() {
240 /** Return that all specializations of this allocator are interchangeable. */
241 template <class T1, class T2>
242 bool operator!= (const SnapshotAlloc<T1>&,
243 const SnapshotAlloc<T2>&) throw() {
250 typedef void * mspace;
251 extern void * mspace_malloc(mspace msp, size_t bytes);
252 extern void mspace_free(mspace msp, void* mem);
253 extern void * mspace_realloc(mspace msp, void* mem, size_t newsize);
254 extern void * mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
255 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
256 extern mspace create_mspace(size_t capacity, int locked);
258 #if USE_MPROTECT_SNAPSHOT
259 extern mspace user_snapshot_space;
262 extern mspace model_snapshot_space;
265 }; /* end of extern "C" */
268 #endif /* _MY_MEMORY_H */