snapshot: use snapshot_space only in mprotect-based
[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 <stdlib.h>
8 #include <limits>
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
28 /** SNAPSHOTALLOC declares the allocators for a class to allocate
29  *      memory in the snapshotting heap. */
30 #define SNAPSHOTALLOC \
31         void * operator new(size_t size) { \
32                 return snapshot_malloc(size); \
33         } \
34         void operator delete(void *p, size_t size) { \
35                 snapshot_free(p); \
36         } \
37         void * operator new[](size_t size) { \
38                 return snapshot_malloc(size); \
39         } \
40         void operator delete[](void *p, size_t size) { \
41                 snapshot_free(p); \
42         }
43
44 void *model_malloc(size_t size);
45 void *model_calloc(size_t count, size_t size);
46 void model_free(void *ptr);
47
48 void * snapshot_malloc(size_t size);
49 void * snapshot_calloc(size_t count, size_t size);
50 void snapshot_free(void *ptr);
51
52 void *system_malloc(size_t size );
53
54 /** @brief Provides a non-snapshotting allocator for use in STL classes.
55  *
56  * The code was adapted from a code example from the book The C++
57  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
58  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
59  * Permission to copy, use, modify, sell and distribute this software
60  * is granted provided this copyright notice appears in all copies.
61  * This software is provided "as is" without express or implied
62  * warranty, and with no claim as to its suitability for any purpose.
63  */
64 template <class T>
65 class ModelAlloc {
66  public:
67         // type definitions
68         typedef T        value_type;
69         typedef T*       pointer;
70         typedef const T* const_pointer;
71         typedef T&       reference;
72         typedef const T& const_reference;
73         typedef size_t   size_type;
74         typedef size_t   difference_type;
75
76         // rebind allocator to type U
77         template <class U>
78         struct rebind {
79                 typedef ModelAlloc<U> other;
80         };
81
82         // return address of values
83         pointer address(reference value) const {
84                 return &value;
85         }
86         const_pointer address(const_reference value) const {
87                 return &value;
88         }
89
90         /* constructors and destructor
91          * - nothing to do because the allocator has no state
92          */
93         ModelAlloc() throw() {
94         }
95         ModelAlloc(const ModelAlloc&) throw() {
96         }
97         template <class U>
98         ModelAlloc(const ModelAlloc<U>&) throw() {
99         }
100         ~ModelAlloc() throw() {
101         }
102
103         // return maximum number of elements that can be allocated
104         size_type max_size() const throw() {
105                 return std::numeric_limits<size_t>::max() / sizeof(T);
106         }
107
108         // allocate but don't initialize num elements of type T
109         pointer allocate(size_type num, const void * = 0) {
110                 pointer p = (pointer)model_malloc(num * sizeof(T));
111                 return p;
112         }
113
114         // initialize elements of allocated storage p with value value
115         void construct(pointer p, const T& value) {
116                 // initialize memory with placement new
117                 new((void*)p)T(value);
118         }
119
120         // destroy elements of initialized storage p
121         void destroy(pointer p) {
122                 // destroy objects by calling their destructor
123                 p->~T();
124         }
125
126         // deallocate storage p of deleted elements
127         void deallocate(pointer p, size_type num) {
128                 model_free((void*)p);
129         }
130 };
131
132 /** Return that all specializations of this allocator are interchangeable. */
133 template <class T1, class T2>
134 bool operator ==(const ModelAlloc<T1>&,
135                 const ModelAlloc<T2>&) throw() {
136         return true;
137 }
138
139 /** Return that all specializations of this allocator are interchangeable. */
140 template <class T1, class T2>
141 bool operator!= (const ModelAlloc<T1>&,
142                 const ModelAlloc<T2>&) throw() {
143         return false;
144 }
145
146 #ifdef __cplusplus
147 extern "C" {
148 #endif
149 typedef void * mspace;
150 extern void* mspace_malloc(mspace msp, size_t bytes);
151 extern void mspace_free(mspace msp, void* mem);
152 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
153 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
154 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
155 extern mspace create_mspace(size_t capacity, int locked);
156
157 #if USE_MPROTECT_SNAPSHOT
158 /** @brief mspace for the snapshotting heap */
159 extern mspace snapshot_space;
160 #endif
161
162 #ifdef __cplusplus
163 };  /* end of extern "C" */
164 #endif
165
166 #endif /* _MY_MEMORY_H */