2 * @brief Memory allocation functions.
12 /** MEMALLOC declares the allocators for a class to allocate
13 * memory in the non-snapshotting heap. */
15 void * operator new(size_t size) { \
16 return model_malloc(size); \
18 void operator delete(void *p, size_t size) { \
21 void * operator new[](size_t size) { \
22 return model_malloc(size); \
24 void operator delete[](void *p, size_t size) { \
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); \
34 void operator delete(void *p, size_t size) { \
37 void * operator new[](size_t size) { \
38 return snapshot_malloc(size); \
40 void operator delete[](void *p, size_t size) { \
44 void *model_malloc(size_t size);
45 void *model_calloc(size_t count, size_t size);
46 void model_free(void *ptr);
48 void * snapshot_malloc(size_t size);
49 void * snapshot_calloc(size_t count, size_t size);
50 void snapshot_free(void *ptr);
52 /** @brief Provides a non-snapshotting allocator for use in STL classes.
54 * The code was adapted from a code example from the book The C++
55 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
56 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
57 * Permission to copy, use, modify, sell and distribute this software
58 * is granted provided this copyright notice appears in all copies.
59 * This software is provided "as is" without express or implied
60 * warranty, and with no claim as to its suitability for any purpose.
68 typedef const T* const_pointer;
70 typedef const T& const_reference;
71 typedef size_t size_type;
72 typedef size_t difference_type;
74 // rebind allocator to type U
77 typedef ModelAlloc<U> other;
80 // return address of values
81 pointer address(reference value) const {
84 const_pointer address(const_reference value) const {
88 /* constructors and destructor
89 * - nothing to do because the allocator has no state
91 ModelAlloc() throw() {
93 ModelAlloc(const ModelAlloc&) throw() {
96 ModelAlloc(const ModelAlloc<U>&) throw() {
98 ~ModelAlloc() throw() {
101 // return maximum number of elements that can be allocated
102 size_type max_size() const throw() {
103 return std::numeric_limits<size_t>::max() / sizeof(T);
106 // allocate but don't initialize num elements of type T
107 pointer allocate(size_type num, const void * = 0) {
108 pointer p = (pointer)model_malloc(num * sizeof(T));
112 // initialize elements of allocated storage p with value value
113 void construct(pointer p, const T& value) {
114 // initialize memory with placement new
115 new((void*)p)T(value);
118 // destroy elements of initialized storage p
119 void destroy(pointer p) {
120 // destroy objects by calling their destructor
124 // deallocate storage p of deleted elements
125 void deallocate(pointer p, size_type num) {
126 model_free((void*)p);
130 /** Return that all specializations of this allocator are interchangeable. */
131 template <class T1, class T2>
132 bool operator ==(const ModelAlloc<T1>&,
133 const ModelAlloc<T2>&) throw() {
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() {
147 typedef void * mspace;
148 extern void* mspace_malloc(mspace msp, size_t bytes);
149 extern void mspace_free(mspace msp, void* mem);
150 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
151 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
152 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
153 extern mspace create_mspace(size_t capacity, int locked);
155 #if USE_MPROTECT_SNAPSHOT
156 extern mspace user_snapshot_space;
160 }; /* end of extern "C" */
163 #endif /* _MY_MEMORY_H */