Fix for pr1833776.
[iverilog.git] / StringHeap.h
blob0948c12a51667f66d2dd1148149bf008062c8e4c
1 #ifndef __StringHeap_H
2 #define __StringHeap_H
3 /*
4 * Copyright (c) 2002-2004 Stephen Williams (steve@icarus.com)
6 * This source code is free software; you can redistribute it
7 * and/or modify it in source code form under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #ifdef HAVE_CVS_IDENT
22 #ident "$Id: StringHeap.h,v 1.6 2005/06/14 19:13:43 steve Exp $"
23 #endif
25 # include "config.h"
26 # include <string>
28 using namespace std;
30 class perm_string {
32 public:
33 perm_string() : text_(0) { }
34 perm_string(const perm_string&that) : text_(that.text_) { }
35 ~perm_string() { }
37 perm_string& operator = (const perm_string&that)
38 { text_ = that.text_; return *this; }
40 const char*str() const { return text_; }
41 operator const char* () const { return str(); }
43 // This is an escape for making perm_string objects out of
44 // literals. For example, per_string::literal("Label"); Please
45 // do *not* cheat and pass arbitrary const char* items here.
46 static perm_string literal(const char*t) { return perm_string(t); }
48 private:
49 friend class StringHeap;
50 friend class StringHeapLex;
51 perm_string(const char*t) : text_(t) { };
53 private:
54 const char*text_;
57 extern bool operator == (perm_string a, perm_string b);
58 extern bool operator == (perm_string a, const char* b);
59 extern bool operator != (perm_string a, perm_string b);
60 extern bool operator != (perm_string a, const char* b);
61 extern bool operator > (perm_string a, perm_string b);
62 extern bool operator < (perm_string a, perm_string b);
63 extern bool operator >= (perm_string a, perm_string b);
64 extern bool operator <= (perm_string a, perm_string b);
67 * The string heap is a way to permanently allocate strings
68 * efficiently. They only take up the space of the string characters
69 * and the terminating nul, there is no malloc overhead.
71 class StringHeap {
73 public:
74 StringHeap();
75 ~StringHeap();
77 const char*add(const char*);
78 perm_string make(const char*);
80 private:
81 enum { HEAPCELL = 0x10000 };
83 char*cell_base_;
84 unsigned cell_ptr_;
85 unsigned cell_count_;
87 private: // not implemented
88 StringHeap(const StringHeap&);
89 StringHeap& operator= (const StringHeap&);
93 * A lexical string heap is a string heap that makes an effort to
94 * return the same pointer for identical strings. This saves further
95 * space by not allocating duplicate strings, so in a system with lots
96 * of identifiers, this can theoretically save more space.
98 class StringHeapLex : private StringHeap {
100 public:
101 StringHeapLex();
102 ~StringHeapLex();
104 const char*add(const char*);
105 perm_string make(const char*);
106 perm_string make(const string&);
108 unsigned add_count() const;
109 unsigned add_hit_count() const;
111 private:
112 enum { HASH_SIZE = 4096 };
113 const char*hash_table_[HASH_SIZE];
115 unsigned add_count_;
116 unsigned hit_count_;
118 private: // not implemented
119 StringHeapLex(const StringHeapLex&);
120 StringHeapLex& operator= (const StringHeapLex&);
124 * $Log: StringHeap.h,v $
125 * Revision 1.6 2005/06/14 19:13:43 steve
126 * gcc3/4 compile errors.
128 * Revision 1.5 2004/02/18 17:11:54 steve
129 * Use perm_strings for named langiage items.
131 * Revision 1.4 2003/03/01 06:25:30 steve
132 * Add the lex_strings string handler, and put
133 * scope names and system task/function names
134 * into this table. Also, permallocate event
135 * names from the beginning.
137 * Revision 1.3 2003/01/16 21:44:46 steve
138 * Keep some debugging status.
140 * Revision 1.2 2002/08/12 01:34:58 steve
141 * conditional ident string using autoconfig.
143 * Revision 1.1 2002/08/04 19:13:16 steve
144 * dll uses StringHeap for named items.
147 #endif