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 static inline void * snapshot_malloc(size_t size) {
37 static inline void * snapshot_calloc(size_t count, size_t size) {
38 return calloc(count, size);
40 static inline void snapshot_free(void *ptr) {
44 void system_free( void * ptr );
45 void *system_malloc( size_t size );
47 /** @brief Provides a non-snapshotting allocator for use in STL classes.
49 * The code was adapted from a code example from the book The C++
50 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
51 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
52 * Permission to copy, use, modify, sell and distribute this software
53 * is granted provided this copyright notice appears in all copies.
54 * This software is provided "as is" without express or implied
55 * warranty, and with no claim as to its suitability for any purpose.
63 typedef const T* const_pointer;
65 typedef const T& const_reference;
66 typedef size_t size_type;
67 typedef size_t difference_type;
69 // rebind allocator to type U
72 typedef MyAlloc<U> other;
75 // return address of values
76 pointer address (reference value) const {
79 const_pointer address (const_reference value) const {
83 /* constructors and destructor
84 * - nothing to do because the allocator has no state
88 MyAlloc(const MyAlloc&) throw() {
91 MyAlloc (const MyAlloc<U>&) throw() {
96 // return maximum number of elements that can be allocated
97 size_type max_size () const throw() {
98 return std::numeric_limits<size_t>::max() / sizeof(T);
101 // allocate but don't initialize num elements of type T
102 pointer allocate (size_type num, const void* = 0) {
103 pointer p = ( pointer )model_malloc( num * sizeof( T ) );
107 // initialize elements of allocated storage p with value value
108 void construct (pointer p, const T& value) {
109 // initialize memory with placement new
110 new((void*)p)T(value);
113 // destroy elements of initialized storage p
114 void destroy (pointer p) {
115 // destroy objects by calling their destructor
119 // deallocate storage p of deleted elements
120 void deallocate (pointer p, size_type num) {
121 model_free((void*)p);
125 /** Return that all specializations of this allocator are interchangeable. */
126 template <class T1, class T2>
127 bool operator== (const MyAlloc<T1>&,
128 const MyAlloc<T2>&) throw() {
132 /** Return that all specializations of this allocator are interchangeable. */
133 template <class T1, class T2>
134 bool operator!= (const MyAlloc<T1>&,
135 const MyAlloc<T2>&) throw() {
142 typedef void * mspace;
143 extern void* mspace_malloc(mspace msp, size_t bytes);
144 extern void mspace_free(mspace msp, void* mem);
145 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
146 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
147 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
148 extern mspace create_mspace(size_t capacity, int locked);
149 extern mspace mySpace;
150 extern void * basemySpace;
152 }; /* end of extern "C" */
155 #endif /* _MY_MEMORY_H */