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_arena_h
26 #define memory_arena_h
28 #include <AD/memory/mem.h>
30 //////////////////////////////////////////////////////////////////////////////
31 // An arena keeps track of elements of a fixed size determined at
32 // creation time. The elements are kept in a single free list.
33 // All allocated storage is freed when an arena is destroyed.
34 //////////////////////////////////////////////////////////////////////////////
36 class Arena
: public Mem
{
38 Arena (const Arena
&); // No copy constructor
39 void operator = (const Arena
&); // No assignment
43 ///////////////////////////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////
53 struct FreeList
{ FreeList
* next
; };
55 FreeList
* free_list
; // linked list of free elements
56 FreeList
* pages
; // linked list of allocated pages
58 int element_size
; // rounded size of each element in sizeof(FreeList).
59 int elements_per_page
; // elements per page
61 void * alloc(); // allocation method
65 ///////////////////////////////////////////////////////////////////////////
66 // Constructor and destructor
67 ///////////////////////////////////////////////////////////////////////////
68 Arena(int byte_size
, int elements_per_page
= 256);
69 Arena(Mem
&, int byte_size
, int elements_per_page
= 256);
72 ///////////////////////////////////////////////////////////////////////////
73 // Fast allocation/deallocation methods
74 ///////////////////////////////////////////////////////////////////////////
75 inline void * operator () ()
76 { if (free_list
) { register FreeList
* cell
= free_list
;
77 free_list
= free_list
->next
;
82 inline void basic_free (void * cell
)
83 { FreeList
* chunk
= (FreeList
*)cell
;
84 chunk
->next
= free_list
; free_list
= chunk
;
87 ///////////////////////////////////////////////////////////////////////////
88 // Virtual methods for the Mem protocol
89 ///////////////////////////////////////////////////////////////////////////
90 virtual void clear ();
91 inline virtual void * m_alloc (size_t) { return (*this)(); }
92 inline virtual void free(void * cell
) { basic_free(cell
); }
93 inline virtual size_t size (const void *) const
94 { return element_size
* sizeof(FreeList
); }