Initializing variable which contains global snapshotting regions
[c11tester.git] / mymemory.h
1 #ifndef _MY_MEMORY_H
2 #define _MY_MEMORY_H
3 #include <stdlib.h>
4 #include <limits>
5 #define MEMALLOC void *operator new( size_t size ){ \
6                                                                                                                                         return MYMALLOC( size );\
7                                                                                                                                         }\
8                                                            void operator delete( void *p, size_t size ){ \
9                                                                                                                                         MYFREE( p ); \
10                                                                                                                                         }\
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
18
19 void *MYMALLOC(size_t size);
20 void MYFREE(void *ptr);
21                                                                                                 
22 /*
23 The following code example is taken from the book
24 The C++ Standard Library - A Tutorial and Reference
25 by Nicolai M. Josuttis, Addison-Wesley, 1999
26 © Copyright Nicolai M. Josuttis 1999
27 */
28 template <class T>
29    class MyAlloc {
30      public:
31        // type definitions
32        typedef T        value_type;
33        typedef T*       pointer;
34        typedef const T* const_pointer;
35        typedef T&       reference;
36        typedef const T& const_reference;
37        typedef size_t   size_type;
38        typedef size_t difference_type;
39
40        // rebind allocator to type U
41        template <class U>
42        struct rebind {
43            typedef MyAlloc<U> other;
44        };
45
46        // return address of values
47        pointer address (reference value) const {
48            return &value;
49        }
50        const_pointer address (const_reference value) const {
51            return &value;
52        }
53
54        /* constructors and destructor
55         * - nothing to do because the allocator has no state
56         */
57        MyAlloc() throw() {
58        }
59        MyAlloc(const MyAlloc&) throw() {
60        }
61        template <class U>
62          MyAlloc (const MyAlloc<U>&) throw() {
63        }
64        ~MyAlloc() throw() {
65        }
66
67        // return maximum number of elements that can be allocated
68        size_type max_size () const throw() {
69            return std::numeric_limits<size_t>::max() / sizeof(T);
70        }
71
72        // allocate but don't initialize num elements of type T
73        pointer allocate (size_type num, const void* = 0) {
74            pointer p = ( pointer )MYMALLOC( num * sizeof( T ) );
75            return p;
76        }
77
78        // initialize elements of allocated storage p with value value
79        void construct (pointer p, const T& value) {
80            // initialize memory with placement new
81            new((void*)p)T(value);
82        }
83
84        // destroy elements of initialized storage p
85        void destroy (pointer p) {
86            // destroy objects by calling their destructor
87            p->~T();
88        }
89
90        // deallocate storage p of deleted elements
91        void deallocate (pointer p, size_type num) {
92            MYFREE((void*)p);
93        }
94    };
95
96 // return that all specializations of this allocator are interchangeable
97  template <class T1, class T2>
98  bool operator== (const MyAlloc<T1>&,
99                   const MyAlloc<T2>&) throw() {
100      return true;
101  }
102  template <class T1, class T2>
103  bool operator!= (const MyAlloc<T1>&,
104                   const MyAlloc<T2>&) throw() {
105      return false;
106  }
107
108
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
112 typedef void * mspace;
113 extern void* mspace_malloc(mspace msp, size_t bytes);
114 extern void mspace_free(mspace msp, void* mem);
115 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
116 extern mspace create_mspace(size_t capacity, int locked);
117 #ifdef __cplusplus
118 };  /* end of extern "C" */
119 #endif
120 #endif