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)
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
22 #ident "$Id: StringHeap.h,v 1.6 2005/06/14 19:13:43 steve Exp $"
33 perm_string() : text_(0) { }
34 perm_string(const perm_string
&that
) : text_(that
.text_
) { }
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
); }
49 friend class StringHeap
;
50 friend class StringHeapLex
;
51 perm_string(const char*t
) : text_(t
) { };
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.
77 const char*add(const char*);
78 perm_string
make(const char*);
81 enum { HEAPCELL
= 0x10000 };
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
{
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;
112 enum { HASH_SIZE
= 4096 };
113 const char*hash_table_
[HASH_SIZE
];
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.