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