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