rename MYFREE -> model_free
[c11tester.git] / mymemory.h
1 /** @file mymemory.h
2  *  @brief Memory allocation functions.
3  */
4
5 #ifndef _MY_MEMORY_H
6 #define _MY_MEMORY_H
7 #include <stdlib.h>
8 #include <limits>
9
10 /** MEMALLOC declares the allocators for a class to allocate
11  *      memory in the non-snapshotting heap. */
12 #define MEMALLOC \
13         void * operator new(size_t size) { \
14                 return model_malloc(size);\
15         }\
16         void operator delete(void *p, size_t size) { \
17                 model_free( p ); \
18         }\
19         void * operator new[](size_t size) { \
20                 return model_malloc(size);\
21         }\
22         void operator delete[](void *p, size_t size) {\
23                 model_free(p);\
24         }
25
26 /** SNAPSHOTALLOC declares the allocators for a class to allocate
27  *      memory in the snapshotting heap. */
28 #define SNAPSHOTALLOC
29
30 void *model_malloc(size_t size);
31 void *model_calloc(size_t count, size_t size);
32 void model_free(void *ptr);
33
34 static inline void * snapshot_malloc(size_t size) {
35         return malloc(size);
36 }
37 static inline void * snapshot_calloc(size_t count, size_t size) {
38         return calloc(count, size);
39 }
40 static inline void snapshot_free(void *ptr) {
41         free(ptr);
42 }
43
44 void system_free( void * ptr );
45 void *system_malloc( size_t size );
46
47 /** @brief Provides a non-snapshotting allocator for use in STL classes.
48  *
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.
56  */
57 template <class T>
58    class MyAlloc {
59      public:
60        // type definitions
61        typedef T        value_type;
62        typedef T*       pointer;
63        typedef const T* const_pointer;
64        typedef T&       reference;
65        typedef const T& const_reference;
66        typedef size_t   size_type;
67        typedef size_t difference_type;
68
69        // rebind allocator to type U
70        template <class U>
71        struct rebind {
72            typedef MyAlloc<U> other;
73        };
74
75        // return address of values
76        pointer address (reference value) const {
77            return &value;
78        }
79        const_pointer address (const_reference value) const {
80            return &value;
81        }
82
83        /* constructors and destructor
84         * - nothing to do because the allocator has no state
85         */
86        MyAlloc() throw() {
87        }
88        MyAlloc(const MyAlloc&) throw() {
89        }
90        template <class U>
91          MyAlloc (const MyAlloc<U>&) throw() {
92        }
93        ~MyAlloc() throw() {
94        }
95
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);
99        }
100
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 ) );
104            return p;
105        }
106
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);
111        }
112
113        // destroy elements of initialized storage p
114        void destroy (pointer p) {
115            // destroy objects by calling their destructor
116            p->~T();
117        }
118
119        // deallocate storage p of deleted elements
120        void deallocate (pointer p, size_type num) {
121            model_free((void*)p);
122        }
123    };
124
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() {
129      return true;
130  }
131
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() {
136      return false;
137  }
138
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
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;
151 #ifdef __cplusplus
152 };  /* end of extern "C" */
153 #endif
154
155 #endif /* _MY_MEMORY_H */