Change initialize a bit
[c11tester.git] / mymemory.h
1 /** @file mymemory.h
2  *  @brief Memory allocation functions.
3  */
4
5 #ifndef _MY_MEMORY_H
6 #define _MY_MEMORY_H
7 #include <limits>
8 #include <stddef.h>
9
10 #include "config.h"
11
12 /** MEMALLOC declares the allocators for a class to allocate
13  *      memory in the non-snapshotting heap. */
14 #define MEMALLOC \
15         void * operator new(size_t size) { \
16                 return model_malloc(size); \
17         } \
18         void operator delete(void *p, size_t size) { \
19                 model_free(p); \
20         } \
21         void * operator new[](size_t size) { \
22                 return model_malloc(size); \
23         } \
24         void operator delete[](void *p, size_t size) { \
25                 model_free(p); \
26         } \
27         void * operator new(size_t size, void *p) {     /* placement new */ \
28                 return p; \
29         }
30
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); \
36         } \
37         void operator delete(void *p, size_t size) { \
38                 snapshot_free(p); \
39         } \
40         void * operator new[](size_t size) { \
41                 return snapshot_malloc(size); \
42         } \
43         void operator delete[](void *p, size_t size) { \
44                 snapshot_free(p); \
45         } \
46         void * operator new(size_t size, void *p) {     /* placement new */ \
47                 return p; \
48         }
49
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);
54
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);
59
60 typedef void * mspace;
61 extern mspace sStaticSpace;
62
63 void * Thread_malloc(size_t size);
64 void Thread_free(void *ptr);
65
66 /** @brief Provides a non-snapshotting allocator for use in STL classes.
67  *
68  * The code was adapted from a code example from the book The C++
69  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
70  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
71  * Permission to copy, use, modify, sell and distribute this software
72  * is granted provided this copyright notice appears in all copies.
73  * This software is provided "as is" without express or implied
74  * warranty, and with no claim as to its suitability for any purpose.
75  */
76 template <class T>
77 class ModelAlloc {
78 public:
79         // type definitions
80         typedef T value_type;
81         typedef T*       pointer;
82         typedef const T* const_pointer;
83         typedef T&       reference;
84         typedef const T& const_reference;
85         typedef size_t size_type;
86         typedef size_t difference_type;
87
88         // rebind allocator to type U
89         template <class U>
90         struct rebind {
91                 typedef ModelAlloc<U> other;
92         };
93
94         // return address of values
95         pointer address(reference value) const {
96                 return &value;
97         }
98         const_pointer address(const_reference value) const {
99                 return &value;
100         }
101
102         /* constructors and destructor
103          * - nothing to do because the allocator has no state
104          */
105         ModelAlloc() throw() {
106         }
107         ModelAlloc(const ModelAlloc&) throw() {
108         }
109         template <class U>
110         ModelAlloc(const ModelAlloc<U>&) throw() {
111         }
112         ~ModelAlloc() throw() {
113         }
114
115         // return maximum number of elements that can be allocated
116         size_type max_size() const throw() {
117                 return std::numeric_limits<size_t>::max() / sizeof(T);
118         }
119
120         // allocate but don't initialize num elements of type T
121         pointer allocate(size_type num, const void * = 0) {
122                 pointer p = (pointer)model_malloc(num * sizeof(T));
123                 return p;
124         }
125
126         // initialize elements of allocated storage p with value value
127         void construct(pointer p, const T& value) {
128                 // initialize memory with placement new
129                 new((void*)p)T(value);
130         }
131
132         // destroy elements of initialized storage p
133         void destroy(pointer p) {
134                 // destroy objects by calling their destructor
135                 p->~T();
136         }
137
138         // deallocate storage p of deleted elements
139         void deallocate(pointer p, size_type num) {
140                 model_free((void*)p);
141         }
142 };
143
144 /** Return that all specializations of this allocator are interchangeable. */
145 template <class T1, class T2>
146 bool operator ==(const ModelAlloc<T1>&,
147                                                                  const ModelAlloc<T2>&) throw() {
148         return true;
149 }
150
151 /** Return that all specializations of this allocator are interchangeable. */
152 template <class T1, class T2>
153 bool operator!= (const ModelAlloc<T1>&,
154                                                                  const ModelAlloc<T2>&) throw() {
155         return false;
156 }
157
158 /** @brief Provides a snapshotting allocator for use in STL classes.
159  *
160  * The code was adapted from a code example from the book The C++
161  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
162  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
163  * Permission to copy, use, modify, sell and distribute this software
164  * is granted provided this copyright notice appears in all copies.
165  * This software is provided "as is" without express or implied
166  * warranty, and with no claim as to its suitability for any purpose.
167  */
168 template <class T>
169 class SnapshotAlloc {
170 public:
171         // type definitions
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 size_t size_type;
178         typedef size_t difference_type;
179
180         // rebind allocator to type U
181         template <class U>
182         struct rebind {
183                 typedef SnapshotAlloc<U> other;
184         };
185
186         // return address of values
187         pointer address(reference value) const {
188                 return &value;
189         }
190         const_pointer address(const_reference value) const {
191                 return &value;
192         }
193
194         /* constructors and destructor
195          * - nothing to do because the allocator has no state
196          */
197         SnapshotAlloc() throw() {
198         }
199         SnapshotAlloc(const SnapshotAlloc&) throw() {
200         }
201         template <class U>
202         SnapshotAlloc(const SnapshotAlloc<U>&) throw() {
203         }
204         ~SnapshotAlloc() throw() {
205         }
206
207         // return maximum number of elements that can be allocated
208         size_type max_size() const throw() {
209                 return std::numeric_limits<size_t>::max() / sizeof(T);
210         }
211
212         // allocate but don't initialize num elements of type T
213         pointer allocate(size_type num, const void * = 0) {
214                 pointer p = (pointer)snapshot_malloc(num * sizeof(T));
215                 return p;
216         }
217
218         // initialize elements of allocated storage p with value value
219         void construct(pointer p, const T& value) {
220                 // initialize memory with placement new
221                 new((void*)p)T(value);
222         }
223
224         // destroy elements of initialized storage p
225         void destroy(pointer p) {
226                 // destroy objects by calling their destructor
227                 p->~T();
228         }
229
230         // deallocate storage p of deleted elements
231         void deallocate(pointer p, size_type num) {
232                 snapshot_free((void*)p);
233         }
234 };
235
236 /** Return that all specializations of this allocator are interchangeable. */
237 template <class T1, class T2>
238 bool operator ==(const SnapshotAlloc<T1>&,
239                                                                  const SnapshotAlloc<T2>&) throw() {
240         return true;
241 }
242
243 /** Return that all specializations of this allocator are interchangeable. */
244 template <class T1, class T2>
245 bool operator!= (const SnapshotAlloc<T1>&,
246                                                                  const SnapshotAlloc<T2>&) throw() {
247         return false;
248 }
249
250 #ifdef __cplusplus
251 extern "C" {
252 #endif
253 typedef void * mspace;
254 extern void * mspace_malloc(mspace msp, size_t bytes);
255 extern void mspace_free(mspace msp, void* mem);
256 extern void * mspace_realloc(mspace msp, void* mem, size_t newsize);
257 extern void * mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
258 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
259 extern mspace create_mspace(size_t capacity, int locked);
260
261 extern mspace model_snapshot_space;
262
263 #ifdef __cplusplus
264 };      /* end of extern "C" */
265 #endif
266
267 #endif  /* _MY_MEMORY_H */