2 * @brief Memory allocation functions.
10 /** MEMALLOC declares the allocators for a class to allocate
11 * memory in the non-snapshotting heap. */
13 void * operator new(size_t size) { \
14 return model_malloc(size); \
16 void operator delete(void *p, size_t size) { \
19 void * operator new[](size_t size) { \
20 return model_malloc(size); \
22 void operator delete[](void *p, size_t size) { \
26 /** SNAPSHOTALLOC declares the allocators for a class to allocate
27 * memory in the snapshotting heap. */
28 #define SNAPSHOTALLOC \
29 void * operator new(size_t size) { \
30 return snapshot_malloc(size); \
32 void operator delete(void *p, size_t size) { \
35 void * operator new[](size_t size) { \
36 return snapshot_malloc(size); \
38 void operator delete[](void *p, size_t size) { \
42 void *model_malloc(size_t size);
43 void *model_calloc(size_t count, size_t size);
44 void model_free(void *ptr);
46 void * snapshot_malloc(size_t size);
47 void * snapshot_calloc(size_t count, size_t size);
48 void snapshot_free(void *ptr);
50 void system_free( void * ptr );
51 void *system_malloc( size_t size );
53 /** @brief Provides a non-snapshotting allocator for use in STL classes.
55 * The code was adapted from a code example from the book The C++
56 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
57 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
58 * Permission to copy, use, modify, sell and distribute this software
59 * is granted provided this copyright notice appears in all copies.
60 * This software is provided "as is" without express or implied
61 * warranty, and with no claim as to its suitability for any purpose.
69 typedef const T* const_pointer;
71 typedef const T& const_reference;
72 typedef size_t size_type;
73 typedef size_t difference_type;
75 // rebind allocator to type U
78 typedef ModelAlloc<U> other;
81 // return address of values
82 pointer address(reference value) const {
85 const_pointer address(const_reference value) const {
89 /* constructors and destructor
90 * - nothing to do because the allocator has no state
92 ModelAlloc() throw() {
94 ModelAlloc(const ModelAlloc&) throw() {
97 ModelAlloc(const ModelAlloc<U>&) throw() {
99 ~ModelAlloc() throw() {
102 // return maximum number of elements that can be allocated
103 size_type max_size() const throw() {
104 return std::numeric_limits<size_t>::max() / sizeof(T);
107 // allocate but don't initialize num elements of type T
108 pointer allocate(size_type num, const void * = 0) {
109 pointer p = (pointer)model_malloc(num * sizeof(T));
113 // initialize elements of allocated storage p with value value
114 void construct(pointer p, const T& value) {
115 // initialize memory with placement new
116 new((void*)p)T(value);
119 // destroy elements of initialized storage p
120 void destroy(pointer p) {
121 // destroy objects by calling their destructor
125 // deallocate storage p of deleted elements
126 void deallocate(pointer p, size_type num) {
127 model_free((void*)p);
131 /** Return that all specializations of this allocator are interchangeable. */
132 template <class T1, class T2>
133 bool operator== (const ModelAlloc<T1>&,
134 const ModelAlloc<T2>&) throw() {
138 /** Return that all specializations of this allocator are interchangeable. */
139 template <class T1, class T2>
140 bool operator!= (const ModelAlloc<T1>&,
141 const ModelAlloc<T2>&) throw() {
148 typedef void * mspace;
149 extern void* mspace_malloc(mspace msp, size_t bytes);
150 extern void mspace_free(mspace msp, void* mem);
151 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
152 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
153 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
154 extern mspace create_mspace(size_t capacity, int locked);
155 extern mspace mySpace;
156 extern void * basemySpace;
158 }; /* end of extern "C" */
161 #endif /* _MY_MEMORY_H */