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