try to infer nullity predicate inside update_predicate_tree. Maybe rethink about...
[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 void * Thread_malloc(size_t size);
61 void Thread_free(void *ptr);
62
63 /** @brief Provides a non-snapshotting allocator for use in STL classes.
64  *
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.
72  */
73 template <class T>
74 class ModelAlloc {
75 public:
76         // type definitions
77         typedef T value_type;
78         typedef T*       pointer;
79         typedef const T* const_pointer;
80         typedef T&       reference;
81         typedef const T& const_reference;
82         typedef size_t size_type;
83         typedef size_t difference_type;
84
85         // rebind allocator to type U
86         template <class U>
87         struct rebind {
88                 typedef ModelAlloc<U> other;
89         };
90
91         // return address of values
92         pointer address(reference value) const {
93                 return &value;
94         }
95         const_pointer address(const_reference value) const {
96                 return &value;
97         }
98
99         /* constructors and destructor
100          * - nothing to do because the allocator has no state
101          */
102         ModelAlloc() throw() {
103         }
104         ModelAlloc(const ModelAlloc&) throw() {
105         }
106         template <class U>
107         ModelAlloc(const ModelAlloc<U>&) throw() {
108         }
109         ~ModelAlloc() throw() {
110         }
111
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);
115         }
116
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));
120                 return p;
121         }
122
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);
127         }
128
129         // destroy elements of initialized storage p
130         void destroy(pointer p) {
131                 // destroy objects by calling their destructor
132                 p->~T();
133         }
134
135         // deallocate storage p of deleted elements
136         void deallocate(pointer p, size_type num) {
137                 model_free((void*)p);
138         }
139 };
140
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() {
145         return true;
146 }
147
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() {
152         return false;
153 }
154
155 /** @brief Provides a snapshotting allocator for use in STL classes.
156  *
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.
164  */
165 template <class T>
166 class SnapshotAlloc {
167 public:
168         // type definitions
169         typedef T value_type;
170         typedef T*       pointer;
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;
176
177         // rebind allocator to type U
178         template <class U>
179         struct rebind {
180                 typedef SnapshotAlloc<U> other;
181         };
182
183         // return address of values
184         pointer address(reference value) const {
185                 return &value;
186         }
187         const_pointer address(const_reference value) const {
188                 return &value;
189         }
190
191         /* constructors and destructor
192          * - nothing to do because the allocator has no state
193          */
194         SnapshotAlloc() throw() {
195         }
196         SnapshotAlloc(const SnapshotAlloc&) throw() {
197         }
198         template <class U>
199         SnapshotAlloc(const SnapshotAlloc<U>&) throw() {
200         }
201         ~SnapshotAlloc() throw() {
202         }
203
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);
207         }
208
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));
212                 return p;
213         }
214
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);
219         }
220
221         // destroy elements of initialized storage p
222         void destroy(pointer p) {
223                 // destroy objects by calling their destructor
224                 p->~T();
225         }
226
227         // deallocate storage p of deleted elements
228         void deallocate(pointer p, size_type num) {
229                 snapshot_free((void*)p);
230         }
231 };
232
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() {
237         return true;
238 }
239
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() {
244         return false;
245 }
246
247 #ifdef __cplusplus
248 extern "C" {
249 #endif
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);
257
258 #if USE_MPROTECT_SNAPSHOT
259 extern mspace user_snapshot_space;
260 #endif
261
262 extern mspace model_snapshot_space;
263
264 #ifdef __cplusplus
265 };      /* end of extern "C" */
266 #endif
267
268 #endif  /* _MY_MEMORY_H */