Update readme
[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 void init_memory_ops();
67
68 /** @brief Provides a non-snapshotting allocator for use in STL classes.
69  *
70  * The code was adapted from a code example from the book The C++
71  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
72  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
73  * Permission to copy, use, modify, sell and distribute this software
74  * is granted provided this copyright notice appears in all copies.
75  * This software is provided "as is" without express or implied
76  * warranty, and with no claim as to its suitability for any purpose.
77  */
78 template <class T>
79 class ModelAlloc {
80 public:
81         // type definitions
82         typedef T value_type;
83         typedef T*       pointer;
84         typedef const T* const_pointer;
85         typedef T&       reference;
86         typedef const T& const_reference;
87         typedef size_t size_type;
88         typedef size_t difference_type;
89
90         // rebind allocator to type U
91         template <class U>
92         struct rebind {
93                 typedef ModelAlloc<U> other;
94         };
95
96         // return address of values
97         pointer address(reference value) const {
98                 return &value;
99         }
100         const_pointer address(const_reference value) const {
101                 return &value;
102         }
103
104         /* constructors and destructor
105          * - nothing to do because the allocator has no state
106          */
107         ModelAlloc() throw() {
108         }
109         ModelAlloc(const ModelAlloc&) throw() {
110         }
111         template <class U>
112         ModelAlloc(const ModelAlloc<U>&) throw() {
113         }
114         ~ModelAlloc() throw() {
115         }
116
117         // return maximum number of elements that can be allocated
118         size_type max_size() const throw() {
119                 return std::numeric_limits<size_t>::max() / sizeof(T);
120         }
121
122         // allocate but don't initialize num elements of type T
123         pointer allocate(size_type num, const void * = 0) {
124                 pointer p = (pointer)model_malloc(num * sizeof(T));
125                 return p;
126         }
127
128         // initialize elements of allocated storage p with value value
129         void construct(pointer p, const T& value) {
130                 // initialize memory with placement new
131                 new((void*)p)T(value);
132         }
133
134         // destroy elements of initialized storage p
135         void destroy(pointer p) {
136                 // destroy objects by calling their destructor
137                 p->~T();
138         }
139
140         // deallocate storage p of deleted elements
141         void deallocate(pointer p, size_type num) {
142                 model_free((void*)p);
143         }
144 };
145
146 /** Return that all specializations of this allocator are interchangeable. */
147 template <class T1, class T2>
148 bool operator ==(const ModelAlloc<T1>&,
149                                                                  const ModelAlloc<T2>&) throw() {
150         return true;
151 }
152
153 /** Return that all specializations of this allocator are interchangeable. */
154 template <class T1, class T2>
155 bool operator!= (const ModelAlloc<T1>&,
156                                                                  const ModelAlloc<T2>&) throw() {
157         return false;
158 }
159
160 /** @brief Provides a snapshotting allocator for use in STL classes.
161  *
162  * The code was adapted from a code example from the book The C++
163  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
164  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
165  * Permission to copy, use, modify, sell and distribute this software
166  * is granted provided this copyright notice appears in all copies.
167  * This software is provided "as is" without express or implied
168  * warranty, and with no claim as to its suitability for any purpose.
169  */
170 template <class T>
171 class SnapshotAlloc {
172 public:
173         // type definitions
174         typedef T value_type;
175         typedef T*       pointer;
176         typedef const T* const_pointer;
177         typedef T&       reference;
178         typedef const T& const_reference;
179         typedef size_t size_type;
180         typedef size_t difference_type;
181
182         // rebind allocator to type U
183         template <class U>
184         struct rebind {
185                 typedef SnapshotAlloc<U> other;
186         };
187
188         // return address of values
189         pointer address(reference value) const {
190                 return &value;
191         }
192         const_pointer address(const_reference value) const {
193                 return &value;
194         }
195
196         /* constructors and destructor
197          * - nothing to do because the allocator has no state
198          */
199         SnapshotAlloc() throw() {
200         }
201         SnapshotAlloc(const SnapshotAlloc&) throw() {
202         }
203         template <class U>
204         SnapshotAlloc(const SnapshotAlloc<U>&) throw() {
205         }
206         ~SnapshotAlloc() throw() {
207         }
208
209         // return maximum number of elements that can be allocated
210         size_type max_size() const throw() {
211                 return std::numeric_limits<size_t>::max() / sizeof(T);
212         }
213
214         // allocate but don't initialize num elements of type T
215         pointer allocate(size_type num, const void * = 0) {
216                 pointer p = (pointer)snapshot_malloc(num * sizeof(T));
217                 return p;
218         }
219
220         // initialize elements of allocated storage p with value value
221         void construct(pointer p, const T& value) {
222                 // initialize memory with placement new
223                 new((void*)p)T(value);
224         }
225
226         // destroy elements of initialized storage p
227         void destroy(pointer p) {
228                 // destroy objects by calling their destructor
229                 p->~T();
230         }
231
232         // deallocate storage p of deleted elements
233         void deallocate(pointer p, size_type num) {
234                 snapshot_free((void*)p);
235         }
236 };
237
238 /** Return that all specializations of this allocator are interchangeable. */
239 template <class T1, class T2>
240 bool operator ==(const SnapshotAlloc<T1>&,
241                                                                  const SnapshotAlloc<T2>&) throw() {
242         return true;
243 }
244
245 /** Return that all specializations of this allocator are interchangeable. */
246 template <class T1, class T2>
247 bool operator!= (const SnapshotAlloc<T1>&,
248                                                                  const SnapshotAlloc<T2>&) throw() {
249         return false;
250 }
251
252 #ifdef __cplusplus
253 extern "C" {
254 #endif
255 typedef void * mspace;
256 extern void * mspace_malloc(mspace msp, size_t bytes);
257 extern void mspace_free(mspace msp, void* mem);
258 extern void * mspace_realloc(mspace msp, void* mem, size_t newsize);
259 extern void * mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
260 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
261 extern mspace create_mspace(size_t capacity, int locked);
262
263 extern mspace model_snapshot_space;
264
265 extern void * (*volatile real_memcpy)(void * dst, const void *src, size_t n);
266 extern void * (*volatile real_memmove)(void * dst, const void *src, size_t len);
267 extern void (*volatile real_bzero)(void * dst, size_t len);
268 extern void * (*volatile real_memset)(void * dst, int c, size_t len);
269
270 #ifdef __cplusplus
271 };      /* end of extern "C" */
272 #endif
273
274 #endif  /* _MY_MEMORY_H */