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