2 * @brief Memory allocation functions.
10 /** MEMALLOC declares the allocators for a class to allocate
11 * memory in the non-snapshotting heap. */
14 void * operator new(size_t size) { \
15 return MYMALLOC(size);\
17 void operator delete(void *p, size_t size) { \
20 void * operator new[](size_t size) { \
21 return MYMALLOC(size);\
23 void operator delete[](void *p, size_t size) {\
27 /** SNAPSHOTALLOC declares the allocators for a class to allocate
28 * memory in the snapshotting heap. */
32 void *MYMALLOC(size_t size);
33 void MYFREE(void *ptr);
35 void system_free( void * ptr );
36 void *system_malloc( size_t size );
38 /** @brief Provides a non-snapshotting allocator for use in STL classes.
40 * The code was adapted from a code example from the book The C++
41 * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
42 * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
43 * Permission to copy, use, modify, sell and distribute this software
44 * is granted provided this copyright notice appears in all copies.
45 * This software is provided "as is" without express or implied
46 * warranty, and with no claim as to its suitability for any purpose.
55 typedef const T* const_pointer;
57 typedef const T& const_reference;
58 typedef size_t size_type;
59 typedef size_t difference_type;
61 // rebind allocator to type U
64 typedef MyAlloc<U> other;
67 // return address of values
68 pointer address (reference value) const {
71 const_pointer address (const_reference value) const {
75 /* constructors and destructor
76 * - nothing to do because the allocator has no state
80 MyAlloc(const MyAlloc&) throw() {
83 MyAlloc (const MyAlloc<U>&) throw() {
88 // return maximum number of elements that can be allocated
89 size_type max_size () const throw() {
90 return std::numeric_limits<size_t>::max() / sizeof(T);
93 // allocate but don't initialize num elements of type T
94 pointer allocate (size_type num, const void* = 0) {
95 pointer p = ( pointer )MYMALLOC( num * sizeof( T ) );
99 // initialize elements of allocated storage p with value value
100 void construct (pointer p, const T& value) {
101 // initialize memory with placement new
102 new((void*)p)T(value);
105 // destroy elements of initialized storage p
106 void destroy (pointer p) {
107 // destroy objects by calling their destructor
111 // deallocate storage p of deleted elements
112 void deallocate (pointer p, size_type num) {
117 /** Return that all specializations of this allocator are interchangeable. */
118 template <class T1, class T2>
119 bool operator== (const MyAlloc<T1>&,
120 const MyAlloc<T2>&) throw() {
124 /** Return that all specializations of this allocator are interchangeable. */
125 template <class T1, class T2>
126 bool operator!= (const MyAlloc<T1>&,
127 const MyAlloc<T2>&) throw() {
134 typedef void * mspace;
135 extern void* mspace_malloc(mspace msp, size_t bytes);
136 extern void mspace_free(mspace msp, void* mem);
137 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
138 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
139 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
140 extern mspace create_mspace(size_t capacity, int locked);
141 extern mspace mySpace;
142 extern void * basemySpace;
144 }; /* end of extern "C" */
147 #endif /* _MY_MEMORY_H */