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. */
30 void *model_malloc(size_t size);
31 void *model_calloc(size_t count, size_t size);
32 void model_free(void *ptr);
34 void * snapshot_malloc(size_t size);
35 void * snapshot_calloc(size_t count, size_t size);
36 void snapshot_free(void *ptr);
38 void system_free( void * ptr );
39 void *system_malloc( size_t size );
41 /** @brief Provides a non-snapshotting allocator for use in STL classes.
43 * The code was adapted from a code example from the book The C++
44 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
45 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
46 * Permission to copy, use, modify, sell and distribute this software
47 * is granted provided this copyright notice appears in all copies.
48 * This software is provided "as is" without express or implied
49 * warranty, and with no claim as to its suitability for any purpose.
57 typedef const T* const_pointer;
59 typedef const T& const_reference;
60 typedef size_t size_type;
61 typedef size_t difference_type;
63 // rebind allocator to type U
66 typedef MyAlloc<U> other;
69 // return address of values
70 pointer address (reference value) const {
73 const_pointer address (const_reference value) const {
77 /* constructors and destructor
78 * - nothing to do because the allocator has no state
82 MyAlloc(const MyAlloc&) throw() {
85 MyAlloc (const MyAlloc<U>&) throw() {
90 // return maximum number of elements that can be allocated
91 size_type max_size () const throw() {
92 return std::numeric_limits<size_t>::max() / sizeof(T);
95 // allocate but don't initialize num elements of type T
96 pointer allocate (size_type num, const void* = 0) {
97 pointer p = ( pointer )model_malloc( num * sizeof( T ) );
101 // initialize elements of allocated storage p with value value
102 void construct (pointer p, const T& value) {
103 // initialize memory with placement new
104 new((void*)p)T(value);
107 // destroy elements of initialized storage p
108 void destroy (pointer p) {
109 // destroy objects by calling their destructor
113 // deallocate storage p of deleted elements
114 void deallocate (pointer p, size_type num) {
115 model_free((void*)p);
119 /** Return that all specializations of this allocator are interchangeable. */
120 template <class T1, class T2>
121 bool operator== (const MyAlloc<T1>&,
122 const MyAlloc<T2>&) throw() {
126 /** Return that all specializations of this allocator are interchangeable. */
127 template <class T1, class T2>
128 bool operator!= (const MyAlloc<T1>&,
129 const MyAlloc<T2>&) throw() {
136 typedef void * mspace;
137 extern void* mspace_malloc(mspace msp, size_t bytes);
138 extern void mspace_free(mspace msp, void* mem);
139 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
140 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
141 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
142 extern mspace create_mspace(size_t capacity, int locked);
143 extern mspace mySpace;
144 extern void * basemySpace;
146 }; /* end of extern "C" */
149 #endif /* _MY_MEMORY_H */