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