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