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