7 void * operator new(size_t size) { \
8 return MYMALLOC(size);\
10 void operator delete(void *p, size_t size) { \
13 void * operator new[](size_t size) { \
14 return MYMALLOC(size);\
16 void operator delete[](void *p, size_t size) {\
20 /* Empty define; represents opposite of MEMALLOC */
23 void *MYMALLOC(size_t size);
24 void MYFREE(void *ptr);
26 void system_free( void * ptr );
27 void *system_malloc( size_t size );
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.
44 typedef const T* const_pointer;
46 typedef const T& const_reference;
47 typedef size_t size_type;
48 typedef size_t difference_type;
50 // rebind allocator to type U
53 typedef MyAlloc<U> other;
56 // return address of values
57 pointer address (reference value) const {
60 const_pointer address (const_reference value) const {
64 /* constructors and destructor
65 * - nothing to do because the allocator has no state
69 MyAlloc(const MyAlloc&) throw() {
72 MyAlloc (const MyAlloc<U>&) throw() {
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);
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 ) );
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);
94 // destroy elements of initialized storage p
95 void destroy (pointer p) {
96 // destroy objects by calling their destructor
100 // deallocate storage p of deleted elements
101 void deallocate (pointer p, size_type num) {
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() {
112 template <class T1, class T2>
113 bool operator!= (const MyAlloc<T1>&,
114 const MyAlloc<T2>&) throw() {
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;
131 }; /* end of extern "C" */
134 #endif /* _MY_MEMORY_H */