2 * Copyright (c) 2002-2004 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: StringHeap.cc,v 1.6 2004/02/18 17:11:54 steve Exp $"
23 # include "StringHeap.h"
31 StringHeap::StringHeap()
38 StringHeap::~StringHeap()
40 // This is a planned memory leak. The string heap is intended
41 // to hold permanently-allocated strings.
44 const char* StringHeap::add(const char*text
)
46 unsigned len
= strlen(text
);
47 assert((len
+1) <= HEAPCELL
);
49 unsigned rem
= HEAPCELL
- cell_ptr_
;
51 cell_base_
= (char*)malloc(HEAPCELL
);
54 assert(cell_base_
!= 0);
57 char*res
= cell_base_
+ cell_ptr_
;
58 memcpy(res
, text
, len
);
60 cell_base_
[cell_ptr_
++] = 0;
62 assert(cell_ptr_
<= HEAPCELL
);
67 perm_string
StringHeap::make(const char*text
)
69 return perm_string(add(text
));
73 StringHeapLex::StringHeapLex()
78 for (unsigned idx
= 0 ; idx
< HASH_SIZE
; idx
+= 1)
82 StringHeapLex::~StringHeapLex()
86 unsigned StringHeapLex::add_hit_count() const
91 unsigned StringHeapLex::add_count() const
96 static unsigned hash_string(const char*text
)
101 h
= (h
<< 4) ^ (h
>> 28) ^ *text
;
107 const char* StringHeapLex::add(const char*text
)
109 unsigned hash_value
= hash_string(text
) % HASH_SIZE
;
111 /* If we easily find the string in the hash table, then return
113 if (hash_table_
[hash_value
]
114 && (strcmp(hash_table_
[hash_value
], text
) == 0)) {
116 return hash_table_
[hash_value
];
119 /* The existing hash entry is not a match. Replace it with the
120 newly allocated value, and return the new pointer as the
121 result to the add. */
122 const char*res
= StringHeap::add(text
);
123 hash_table_
[hash_value
] = res
;
129 perm_string
StringHeapLex::make(const char*text
)
131 return perm_string(add(text
));
134 perm_string
StringHeapLex::make(const string
&text
)
136 return perm_string(add(text
.c_str()));
139 bool operator == (perm_string a
, const char*b
)
144 if (! (a
.str() && b
))
147 if (strcmp(a
.str(), b
) == 0)
153 bool operator == (perm_string a
, perm_string b
)
158 bool operator != (perm_string a
, const char*b
)
163 bool operator != (perm_string a
, perm_string b
)
168 bool operator < (perm_string a
, perm_string b
)
170 if (b
.str() && !a
.str())
173 if (b
.str() == a
.str())
176 if (strcmp(a
.str(), b
.str()) < 0)
183 * $Log: StringHeap.cc,v $
184 * Revision 1.6 2004/02/18 17:11:54 steve
185 * Use perm_strings for named langiage items.
187 * Revision 1.5 2003/03/01 06:25:30 steve
188 * Add the lex_strings string handler, and put
189 * scope names and system task/function names
190 * into this table. Also, permallocate event
191 * names from the beginning.
193 * Revision 1.4 2003/01/27 05:09:17 steve
196 * Revision 1.3 2003/01/16 21:44:46 steve
197 * Keep some debugging status.
199 * Revision 1.2 2002/08/12 01:34:58 steve
200 * conditional ident string using autoconfig.
202 * Revision 1.1 2002/08/04 19:13:16 steve
203 * dll uses StringHeap for named items.