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