mymemory: define SNAPSHOTALLOC appropriately
[cdsspec-compiler.git] / mymemory.h
1 /** @file mymemory.h
2  *  @brief Memory allocation functions.
3  */
4
5 #ifndef _MY_MEMORY_H
6 #define _MY_MEMORY_H
7 #include <stdlib.h>
8 #include <limits>
9
10 /** MEMALLOC declares the allocators for a class to allocate
11  *      memory in the non-snapshotting heap. */
12 #define MEMALLOC \
13         void * operator new(size_t size) { \
14                 return model_malloc(size); \
15         } \
16         void operator delete(void *p, size_t size) { \
17                 model_free(p); \
18         } \
19         void * operator new[](size_t size) { \
20                 return model_malloc(size); \
21         } \
22         void operator delete[](void *p, size_t size) { \
23                 model_free(p); \
24         }
25
26 /** SNAPSHOTALLOC declares the allocators for a class to allocate
27  *      memory in the snapshotting heap. */
28 #define SNAPSHOTALLOC \
29         void * operator new(size_t size) { \
30                 return snapshot_malloc(size); \
31         } \
32         void operator delete(void *p, size_t size) { \
33                 snapshot_free(p); \
34         } \
35         void * operator new[](size_t size) { \
36                 return snapshot_malloc(size); \
37         } \
38         void operator delete[](void *p, size_t size) { \
39                 snapshot_free(p); \
40         }
41
42 void *model_malloc(size_t size);
43 void *model_calloc(size_t count, size_t size);
44 void model_free(void *ptr);
45
46 void * snapshot_malloc(size_t size);
47 void * snapshot_calloc(size_t count, size_t size);
48 void snapshot_free(void *ptr);
49
50 void system_free( void * ptr );
51 void *system_malloc( size_t size );
52
53 /** @brief Provides a non-snapshotting allocator for use in STL classes.
54  *
55  * The code was adapted from a code example from the book The C++
56  * Standard Library - A Tutorial and Reference by Nicolai M. Josuttis,
57  * Addison-Wesley, 1999 © Copyright Nicolai M. Josuttis 1999
58  * Permission to copy, use, modify, sell and distribute this software
59  * is granted provided this copyright notice appears in all copies.
60  * This software is provided "as is" without express or implied
61  * warranty, and with no claim as to its suitability for any purpose.
62  */
63 template <class T>
64    class MyAlloc {
65      public:
66        // type definitions
67        typedef T        value_type;
68        typedef T*       pointer;
69        typedef const T* const_pointer;
70        typedef T&       reference;
71        typedef const T& const_reference;
72        typedef size_t   size_type;
73        typedef size_t difference_type;
74
75        // rebind allocator to type U
76        template <class U>
77        struct rebind {
78            typedef MyAlloc<U> other;
79        };
80
81        // return address of values
82        pointer address (reference value) const {
83            return &value;
84        }
85        const_pointer address (const_reference value) const {
86            return &value;
87        }
88
89        /* constructors and destructor
90         * - nothing to do because the allocator has no state
91         */
92        MyAlloc() throw() {
93        }
94        MyAlloc(const MyAlloc&) throw() {
95        }
96        template <class U>
97          MyAlloc (const MyAlloc<U>&) throw() {
98        }
99        ~MyAlloc() throw() {
100        }
101
102        // return maximum number of elements that can be allocated
103        size_type max_size () const throw() {
104            return std::numeric_limits<size_t>::max() / sizeof(T);
105        }
106
107        // allocate but don't initialize num elements of type T
108        pointer allocate (size_type num, const void* = 0) {
109            pointer p = ( pointer )model_malloc( num * sizeof( T ) );
110            return p;
111        }
112
113        // initialize elements of allocated storage p with value value
114        void construct (pointer p, const T& value) {
115            // initialize memory with placement new
116            new((void*)p)T(value);
117        }
118
119        // destroy elements of initialized storage p
120        void destroy (pointer p) {
121            // destroy objects by calling their destructor
122            p->~T();
123        }
124
125        // deallocate storage p of deleted elements
126        void deallocate (pointer p, size_type num) {
127            model_free((void*)p);
128        }
129    };
130
131 /** Return that all specializations of this allocator are interchangeable. */
132  template <class T1, class T2>
133  bool operator== (const MyAlloc<T1>&,
134                   const MyAlloc<T2>&) throw() {
135      return true;
136  }
137
138 /** Return that all specializations of this allocator are interchangeable. */
139  template <class T1, class T2>
140  bool operator!= (const MyAlloc<T1>&,
141                   const MyAlloc<T2>&) throw() {
142      return false;
143  }
144
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148 typedef void * mspace;
149 extern void* mspace_malloc(mspace msp, size_t bytes);
150 extern void mspace_free(mspace msp, void* mem);
151 extern void* mspace_realloc(mspace msp, void* mem, size_t newsize);
152 extern void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
153 extern mspace create_mspace_with_base(void* base, size_t capacity, int locked);
154 extern mspace create_mspace(size_t capacity, int locked);
155 extern mspace mySpace;
156 extern void * basemySpace;
157 #ifdef __cplusplus
158 };  /* end of extern "C" */
159 #endif
160
161 #endif /* _MY_MEMORY_H */