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 //////////////////////////////////////////////////////////////////////////////
26 #include <AD/memory/strpool.h>
28 //////////////////////////////////////////////////////////////////////////////
30 //////////////////////////////////////////////////////////////////////////////
31 StringPool::StringPool(size_t page_size
) : Mem("StringPool")
32 { next
= limit
= 0; pages
= 0;
37 StringPool::StringPool(Mem
& m
, size_t page_size
) : Mem(m
,"StringPool")
38 { next
= limit
= 0; pages
= 0;
43 //////////////////////////////////////////////////////////////////////////////
45 //////////////////////////////////////////////////////////////////////////////
46 StringPool::~StringPool() { clear(); }
48 //////////////////////////////////////////////////////////////////////////////
49 // Method to increase the size of the pool
50 //////////////////////////////////////////////////////////////////////////////
51 void StringPool::grow(size_t len
)
52 { size_t size
= len
> pageSize
? len
: pageSize
;
53 Page
* newPage
= (Page
*)manager_mem
->m_alloc(sizeof(Page
) + size
- 1);
54 newPage
->next
= pages
;
58 bytes_reserved
+= size
;
61 //////////////////////////////////////////////////////////////////////////////
62 // Acquire a string of length $len$ from the pool and initialize it
63 //////////////////////////////////////////////////////////////////////////////
64 char * StringPool::operator () (const char * str
, int len
)
65 { char * newStr
= (*this)[len
+ 1];
66 memcpy(newStr
,str
,len
); newStr
[len
] = '\0';
70 //////////////////////////////////////////////////////////////////////////////
71 // Acquire a string from the pool and initialize it
72 //////////////////////////////////////////////////////////////////////////////
73 char * StringPool::operator [] (const char * str
)
74 { char * newStr
= (*this)[strlen(str
)+1];
79 //////////////////////////////////////////////////////////////////////////////
80 // Mem protocol methods
81 //////////////////////////////////////////////////////////////////////////////
82 void StringPool::free(void *) {}
84 size_t StringPool::bytes_used() const { return bytes_reserved
; }
86 //////////////////////////////////////////////////////////////////////////////
88 //////////////////////////////////////////////////////////////////////////////
89 void StringPool::clear()
91 for (Page
* p
= pages
; p
; p
= next_page
) {
95 next
= limit
= 0; pages
= 0;