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 boundary_tag_memory_manager_h
26 #define boundary_tag_memory_manager_h
28 //////////////////////////////////////////////////////////////////////////////
29 // Class |BoundaryTag| is a generic purpose memory manager based
30 // on the boundary tag algorithm. In this algorithm, a free list
31 // of free memory blocks are kept. The list is {\em not} sorted
32 // by address order, but rather, two {\bf tags} are kept in the
33 // the header of each allocated block, one denoting the size of
34 // the current block, and one denoting the size of the last block
35 // in address order. Notice that the size of each block is actually
36 // duplicated within this scheme: one in the current header, and one
37 // in the header of the last block in the address order. This way,
38 // it is possible to free a block and merge adjacent blocks in contant
39 // time. Allocation, however, is accomplished using a roving first fit
40 // strategy. Simulation results have shown that this algorithm
41 // performs efficiently if the pool of memory is not close to being
42 // exhausted\cite{LD-1991}
43 //////////////////////////////////////////////////////////////////////////////
45 #include <AD/memory/mem.h>
47 class BoundaryTag
: public Mem
{
49 BoundaryTag(const BoundaryTag
&); // no copy constructor
50 void operator = (const BoundaryTag
&); // no assignment
54 ///////////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////////////
69 LongWord last_size
; // Size of last block
70 LongWord size
; // Size of current block
71 Block
* next
, * last
; // Link to next and last block on the free list
78 Block
* freeList
; // Doubly linked circular free list
79 Block stub
; // A dummy free block, not allocatable.
82 Page
* next
; // next page in the linked list of pages
83 BoundaryTag::Block blocks
[1]; // actual storage
84 } * pages
; // A linked list of pages allocated
86 long pageSize
; // default size of a page
87 long allocated
; // bytes allocated
89 virtual void grow(long size
);
91 enum BoundaryTag_constant
{
92 MY_PAGE_SIZE
= 1024 // Default page size is 1K bytes
98 long bytes_allocated
; // bytes allocated from system
99 long bytes_available
; // bytes available in manager
100 long free_block_count
; // number of free blocks within free list
101 long page_count
; // number of pages allocated
106 ///////////////////////////////////////////////////////////////////////////
107 // Constructor and destructor
108 ///////////////////////////////////////////////////////////////////////////
109 BoundaryTag(long defaultPageSize
= MY_PAGE_SIZE
);
110 BoundaryTag(Mem
&, long defaultPageSize
= MY_PAGE_SIZE
);
113 ///////////////////////////////////////////////////////////////////////////
114 // Methods for allocation
115 ///////////////////////////////////////////////////////////////////////////
116 inline void * operator [] (long size
) { return m_alloc(size
); }
117 char * operator [] (const char *);
119 ///////////////////////////////////////////////////////////////////////////
121 ///////////////////////////////////////////////////////////////////////////
122 Statistics
statistics() const;
124 ///////////////////////////////////////////////////////////////////////////
125 // Virtual methods for the Mem protocol
126 ///////////////////////////////////////////////////////////////////////////
127 virtual void clear ();
128 virtual void * m_alloc (size_t);
129 virtual void free (void *);
130 virtual size_t size (const void *) const;