common: print tracing info for ASSERT() failures
[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 MYMALLOC(size);\
15         }\
16         void operator delete(void *p, size_t size) { \
17                 MYFREE( p ); \
18         }\
19         void * operator new[](size_t size) { \
20                 return MYMALLOC(size);\
21         }\
22         void operator delete[](void *p, size_t size) {\
23                 MYFREE(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 *MYMALLOC(size_t size);
31 void *MYCALLOC(size_t count, size_t size);
32 void MYFREE(void *ptr);
33
34 void system_free( void * ptr );
35 void *system_malloc( size_t size );
36
37 /** @brief Provides a non-snapshotting allocator for use in STL classes.
38  *
39  * The code was adapted from a code example from the book The C++
40  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
41  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
42  * Permission to copy, use, modify, sell and distribute this software
43  * is granted provided this copyright notice appears in all copies.
44  * This software is provided "as is" without express or implied
45  * warranty, and with no claim as to its suitability for any purpose.
46  */
47 template <class T>
48    class MyAlloc {
49      public:
50        // type definitions
51        typedef T        value_type;
52        typedef T*       pointer;
53        typedef const T* const_pointer;
54        typedef T&       reference;
55        typedef const T& const_reference;
56        typedef size_t   size_type;
57        typedef size_t difference_type;
58
59        // rebind allocator to type U
60        template <class U>
61        struct rebind {
62            typedef MyAlloc<U> other;
63        };
64
65        // return address of values
66        pointer address (reference value) const {
67            return &value;
68        }
69        const_pointer address (const_reference value) const {
70            return &value;
71        }
72
73        /* constructors and destructor
74         * - nothing to do because the allocator has no state
75         */
76        MyAlloc() throw() {
77        }
78        MyAlloc(const MyAlloc&) throw() {
79        }
80        template <class U>
81          MyAlloc (const MyAlloc<U>&) throw() {
82        }
83        ~MyAlloc() throw() {
84        }
85
86        // return maximum number of elements that can be allocated
87        size_type max_size () const throw() {
88            return std::numeric_limits<size_t>::max() / sizeof(T);
89        }
90
91        // allocate but don't initialize num elements of type T
92        pointer allocate (size_type num, const void* = 0) {
93            pointer p = ( pointer )MYMALLOC( num * sizeof( T ) );
94            return p;
95        }
96
97        // initialize elements of allocated storage p with value value
98        void construct (pointer p, const T& value) {
99            // initialize memory with placement new
100            new((void*)p)T(value);
101        }
102
103        // destroy elements of initialized storage p
104        void destroy (pointer p) {
105            // destroy objects by calling their destructor
106            p->~T();
107        }
108
109        // deallocate storage p of deleted elements
110        void deallocate (pointer p, size_type num) {
111            MYFREE((void*)p);
112        }
113    };
114
115 /** Return that all specializations of this allocator are interchangeable. */
116  template <class T1, class T2>
117  bool operator== (const MyAlloc<T1>&,
118                   const MyAlloc<T2>&) throw() {
119      return true;
120  }
121
122 /** Return that all specializations of this allocator are interchangeable. */
123  template <class T1, class T2>
124  bool operator!= (const MyAlloc<T1>&,
125                   const MyAlloc<T2>&) throw() {
126      return false;
127  }
128
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 typedef void * mspace;
133 extern void* mspace_malloc(mspace msp, size_t bytes);
134 extern void mspace_free(mspace msp, void* mem);
135 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
136 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
137 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
138 extern mspace create_mspace(size_t capacity, int locked);
139 extern mspace mySpace;
140 extern void * basemySpace;
141 #ifdef __cplusplus
142 };  /* end of extern "C" */
143 #endif
144
145 #endif /* _MY_MEMORY_H */