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