1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef memory_manager_base_class_h
26 #define memory_manager_base_class_h
30 #include <AD/generic/generic.h>
32 //////////////////////////////////////////////////////////////////////////////
33 // The class |Mem| represents the basic protocol of a memory manager
34 // with at least a method to acquire memory. Most memory managers in this
35 // library are subclasses.
37 // Some subclasses, however, define their own inlined methods to
38 // perform allocation/deallocation. The user can choose to fix the
39 // class to use those methods, or use the protocol defined in
40 // this class for generality. In general, this protocol should be
42 //////////////////////////////////////////////////////////////////////////////
46 Mem(const Mem
&); // no copy constructor
47 void operator = (const Mem
&); // no assignment
51 ///////////////////////////////////////////////////////////////////////////
52 // A simple type for memory alignment needs
53 ///////////////////////////////////////////////////////////////////////////
54 typedef union { long a
; double b
; void * c
; } Align
;
56 ///////////////////////////////////////////////////////////////////////////
57 // STL style type definitions
58 ///////////////////////////////////////////////////////////////////////////
59 typedef size_t size_type
;
60 typedef ptrdiff_t difference_type
;
64 ///////////////////////////////////////////////////////////////////////////
65 // Name of the memory manager
66 ///////////////////////////////////////////////////////////////////////////
67 const char * memory_manager_name
;
70 ///////////////////////////////////////////////////////////////////////////
71 // Get/set the manager of this class
72 ///////////////////////////////////////////////////////////////////////////
73 void set_manager(Mem
&);
78 ///////////////////////////////////////////////////////////////////////////
79 // Constructor and destructor
80 ///////////////////////////////////////////////////////////////////////////
82 Mem(const char manager_name
[]);
83 Mem(Mem
&, const char manager_name
[]);
86 ///////////////////////////////////////////////////////////////////////////
87 // Memory allocation/deallocation.
88 // The names malloc() and calloc() have been changed to
89 // m_alloc() and c_alloc() respectively to avoid name conflict with
90 // GNU <stdlib.h> macros definitions.
91 ///////////////////////////////////////////////////////////////////////////
92 virtual void clear (); // release all the memory within
93 virtual void * m_alloc (size_t); // acquire a block of memory of size n
94 virtual void * c_alloc (size_t); // acquire and initialize to 0's
95 virtual void free (void *); // release the memory
96 virtual size_t size (const void *) const; // returns the size of a block
97 virtual size_t bytes_used () const; // returns the amount of storage used
99 ///////////////////////////////////////////////////////////////////////////
100 // Additional STL style protocol
101 ///////////////////////////////////////////////////////////////////////////
102 // optimal buffer(page) size for an object of size n
103 virtual size_t init_page_size(size_t n
) const;
104 // maximum capacity for an object of size n
105 virtual size_t max_size(size_t n
) const;
107 ///////////////////////////////////////////////////////////////////////////
109 ///////////////////////////////////////////////////////////////////////////
110 virtual void error (const char message
[]) const; // report an error
112 static Mem
& system_mem();
115 //////////////////////////////////////////////////////////////////////////////
116 // Allocate memory from mem using the operator new.
117 //////////////////////////////////////////////////////////////////////////////
118 inline void * operator new (size_t n
, Mem
& mem
)
119 { return mem
.m_alloc(n
); }
120 inline void * operator new (size_t n
, Mem
& mem
, size_t m
)
121 { return mem
.m_alloc(n
* m
); }
122 inline void * operator new (size_t n
, Mem
* mem
)
123 { return mem
->m_alloc(n
); }
124 inline void * operator new (size_t n
, Mem
* mem
, size_t m
)
125 { return mem
->m_alloc(n
* m
); }