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