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