1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
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.
11 * @brief Memory allocation functions.
21 /** MEMALLOC declares the allocators for a class to allocate
22 * memory in the non-snapshotting heap. */
24 void * operator new(size_t size) { \
25 return model_malloc(size); \
27 void operator delete(void *p, size_t size) { \
30 void * operator new[](size_t size) { \
31 return model_malloc(size); \
33 void operator delete[](void *p, size_t size) { \
36 void * operator new(size_t size, void *p) { /* placement new */ \
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); \
46 void operator delete(void *p, size_t size) { \
49 void * operator new[](size_t size) { \
50 return snapshot_malloc(size); \
52 void operator delete[](void *p, size_t size) { \
55 void * operator new(size_t size, void *p) { /* placement new */ \
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);
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);
71 void * Thread_malloc(size_t size);
72 void Thread_free(void *ptr);
75 /** @brief Provides a non-snapshotting allocator for use in STL classes.
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.
91 typedef const T* const_pointer;
93 typedef const T& const_reference;
94 typedef size_t size_type;
95 typedef size_t difference_type;
97 // rebind allocator to type U
100 typedef ModelAlloc<U> other;
103 // return address of values
104 pointer address(reference value) const {
107 const_pointer address(const_reference value) const {
111 /* constructors and destructor
112 * - nothing to do because the allocator has no state
114 ModelAlloc() throw() {
116 ModelAlloc(const ModelAlloc&) throw() {
119 ModelAlloc(const ModelAlloc<U>&) throw() {
121 ~ModelAlloc() throw() {
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);
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));
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);
141 // destroy elements of initialized storage p
142 void destroy(pointer p) {
143 // destroy objects by calling their destructor
147 // deallocate storage p of deleted elements
148 void deallocate(pointer p, size_type num) {
149 model_free((void*)p);
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() {
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() {
167 /** @brief Provides a snapshotting allocator for use in STL classes.
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.
178 class SnapshotAlloc {
181 typedef T value_type;
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;
189 // rebind allocator to type U
192 typedef SnapshotAlloc<U> other;
195 // return address of values
196 pointer address(reference value) const {
199 const_pointer address(const_reference value) const {
203 /* constructors and destructor
204 * - nothing to do because the allocator has no state
206 SnapshotAlloc() throw() {
208 SnapshotAlloc(const SnapshotAlloc&) throw() {
211 SnapshotAlloc(const SnapshotAlloc<U>&) throw() {
213 ~SnapshotAlloc() throw() {
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);
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));
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);
233 // destroy elements of initialized storage p
234 void destroy(pointer p) {
235 // destroy objects by calling their destructor
239 // deallocate storage p of deleted elements
240 void deallocate(pointer p, size_type num) {
241 snapshot_free((void*)p);
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() {
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() {
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);
270 struct snapshot_heap_data {
271 void *allocation_ptr;
275 extern struct snapshot_heap_data * snapshot_struct;
277 #if USE_MPROTECT_SNAPSHOT
278 extern void * user_snapshot_space;
279 extern mspace thread_snapshot_space;
282 extern mspace model_snapshot_space;
285 }; /* end of extern "C" */
288 #endif/* _MY_MEMORY_H */