Initialize assignments of reals
[iverilog.git] / StringHeap.cc
blob9bc244d2c59af324596e1ac79bee485a21bec514
1 /*
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)
8 * any later version.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: StringHeap.cc,v 1.6 2004/02/18 17:11:54 steve Exp $"
21 #endif
23 # include "StringHeap.h"
24 #ifdef HAVE_MALLOC_H
25 # include <malloc.h>
26 #endif
27 # include <stdlib.h>
28 # include <string.h>
29 # include <assert.h>
31 StringHeap::StringHeap()
33 cell_base_ = 0;
34 cell_ptr_ = HEAPCELL;
35 cell_count_ = 0;
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_;
50 if (rem < (len+1)) {
51 cell_base_ = (char*)malloc(HEAPCELL);
52 cell_ptr_ = 0;
53 cell_count_ += 1;
54 assert(cell_base_ != 0);
57 char*res = cell_base_ + cell_ptr_;
58 memcpy(res, text, len);
59 cell_ptr_ += len;
60 cell_base_[cell_ptr_++] = 0;
62 assert(cell_ptr_ <= HEAPCELL);
64 return res;
67 perm_string StringHeap::make(const char*text)
69 return perm_string(add(text));
73 StringHeapLex::StringHeapLex()
75 hit_count_ = 0;
76 add_count_ = 0;
78 for (unsigned idx = 0 ; idx < HASH_SIZE ; idx += 1)
79 hash_table_[idx] = 0;
82 StringHeapLex::~StringHeapLex()
86 unsigned StringHeapLex::add_hit_count() const
88 return hit_count_;
91 unsigned StringHeapLex::add_count() const
93 return add_count_;
96 static unsigned hash_string(const char*text)
98 unsigned h = 0;
100 while (*text) {
101 h = (h << 4) ^ (h >> 28) ^ *text;
102 text += 1;
104 return h;
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
112 that and be done. */
113 if (hash_table_[hash_value]
114 && (strcmp(hash_table_[hash_value], text) == 0)) {
115 hit_count_ += 1;
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;
124 add_count_ += 1;
126 return 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)
141 if (a.str() == b)
142 return true;
144 if (! (a.str() && b))
145 return false;
147 if (strcmp(a.str(), b) == 0)
148 return true;
150 return false;
153 bool operator == (perm_string a, perm_string b)
155 return a == b.str();
158 bool operator != (perm_string a, const char*b)
160 return ! (a == b);
163 bool operator != (perm_string a, perm_string b)
165 return ! (a == b);
168 bool operator < (perm_string a, perm_string b)
170 if (b.str() && !a.str())
171 return true;
173 if (b.str() == a.str())
174 return false;
176 if (strcmp(a.str(), b.str()) < 0)
177 return true;
179 return false;
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
194 * Spelling fixes.
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.