simple.cc - generated code example
[prop.git] / lib-src / memory / strpool.cc
blob7f45b3a37487c567a1be270a6370a4609f6537fb
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
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
9 // your programs.
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
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #include <string.h>
26 #include <AD/memory/strpool.h>
28 //////////////////////////////////////////////////////////////////////////////
29 // Constructor
30 //////////////////////////////////////////////////////////////////////////////
31 StringPool::StringPool(size_t page_size) : Mem("StringPool")
32 { next = limit = 0; pages = 0;
33 pageSize = page_size;
34 bytes_reserved = 0;
37 StringPool::StringPool(Mem& m, size_t page_size) : Mem(m,"StringPool")
38 { next = limit = 0; pages = 0;
39 pageSize = page_size;
40 bytes_reserved = 0;
43 //////////////////////////////////////////////////////////////////////////////
44 // Destructor
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;
55 pages = newPage;
56 next = pages->data;
57 limit = next + size;
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';
67 return newStr;
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];
75 strcpy(newStr,str);
76 return newStr;
79 //////////////////////////////////////////////////////////////////////////////
80 // Mem protocol methods
81 //////////////////////////////////////////////////////////////////////////////
82 void StringPool::free(void *) {}
84 size_t StringPool::bytes_used() const { return bytes_reserved; }
86 //////////////////////////////////////////////////////////////////////////////
87 // Clear the pool
88 //////////////////////////////////////////////////////////////////////////////
89 void StringPool::clear()
90 { Page * next_page;
91 for (Page * p = pages; p; p = next_page) {
92 next_page = p->next;
93 manager_mem->free(p);
95 next = limit = 0; pages = 0;