mymemory: make snapshot_{malloc,calloc,free} externally linkable
[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 void * snapshot_malloc(size_t size);
35 void * snapshot_calloc(size_t count, size_t size);
36 void snapshot_free(void *ptr);
37
38 void system_free( void * ptr );
39 void *system_malloc( size_t size );
40
41 /** @brief Provides a non-snapshotting allocator for use in STL classes.
42  *
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.
50  */
51 template <class T>
52    class MyAlloc {
53      public:
54        // type definitions
55        typedef T        value_type;
56        typedef T*       pointer;
57        typedef const T* const_pointer;
58        typedef T&       reference;
59        typedef const T& const_reference;
60        typedef size_t   size_type;
61        typedef size_t difference_type;
62
63        // rebind allocator to type U
64        template <class U>
65        struct rebind {
66            typedef MyAlloc<U> other;
67        };
68
69        // return address of values
70        pointer address (reference value) const {
71            return &value;
72        }
73        const_pointer address (const_reference value) const {
74            return &value;
75        }
76
77        /* constructors and destructor
78         * - nothing to do because the allocator has no state
79         */
80        MyAlloc() throw() {
81        }
82        MyAlloc(const MyAlloc&) throw() {
83        }
84        template <class U>
85          MyAlloc (const MyAlloc<U>&) throw() {
86        }
87        ~MyAlloc() throw() {
88        }
89
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);
93        }
94
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 ) );
98            return p;
99        }
100
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);
105        }
106
107        // destroy elements of initialized storage p
108        void destroy (pointer p) {
109            // destroy objects by calling their destructor
110            p->~T();
111        }
112
113        // deallocate storage p of deleted elements
114        void deallocate (pointer p, size_type num) {
115            model_free((void*)p);
116        }
117    };
118
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() {
123      return true;
124  }
125
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() {
130      return false;
131  }
132
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
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;
145 #ifdef __cplusplus
146 };  /* end of extern "C" */
147 #endif
148
149 #endif /* _MY_MEMORY_H */