1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __STRPOOL_H__
30 #define __STRPOOL_H__
31
32 #include "idlib/containers/List.h"
33 #include "idlib/containers/HashIndex.h"
34
35 /*
36 ===============================================================================
37
38 idStrPool
39
40 ===============================================================================
41 */
42
43 class idStrPool;
44
45 class idPoolStr : public idStr {
46 friend class idStrPool;
47
48 public:
idPoolStr()49 idPoolStr() { numUsers = 0; }
~idPoolStr()50 ~idPoolStr() { assert( numUsers == 0 ); }
51
52 // returns total size of allocated memory
Allocated(void)53 size_t Allocated( void ) const { return idStr::Allocated(); }
54 // returns total size of allocated memory including size of string pool type
Size(void)55 size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
56 // returns a pointer to the pool this string was allocated from
GetPool(void)57 const idStrPool * GetPool( void ) const { return pool; }
58
59 private:
60 idStrPool * pool;
61 mutable int numUsers;
62 };
63
64 class idStrPool {
65 public:
idStrPool()66 idStrPool() { caseSensitive = true; }
67
68 void SetCaseSensitive( bool caseSensitive );
69
Num(void)70 int Num( void ) const { return pool.Num(); }
71 size_t Allocated( void ) const;
72 size_t Size( void ) const;
73
74 const idPoolStr * operator[]( int index ) const { return pool[index]; }
75
76 const idPoolStr * AllocString( const char *string );
77 void FreeString( const idPoolStr *poolStr );
78 const idPoolStr * CopyString( const idPoolStr *poolStr );
79 void Clear( void );
80
81 private:
82 bool caseSensitive;
83 idList<idPoolStr *> pool;
84 idHashIndex poolHash;
85 };
86
87 /*
88 ================
89 idStrPool::SetCaseSensitive
90 ================
91 */
SetCaseSensitive(bool caseSensitive)92 ID_INLINE void idStrPool::SetCaseSensitive( bool caseSensitive ) {
93 this->caseSensitive = caseSensitive;
94 }
95
96 /*
97 ================
98 idStrPool::AllocString
99 ================
100 */
AllocString(const char * string)101 ID_INLINE const idPoolStr *idStrPool::AllocString( const char *string ) {
102 int i, hash;
103 idPoolStr *poolStr;
104
105 hash = poolHash.GenerateKey( string, caseSensitive );
106 if ( caseSensitive ) {
107 for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
108 if ( pool[i]->Cmp( string ) == 0 ) {
109 pool[i]->numUsers++;
110 return pool[i];
111 }
112 }
113 } else {
114 for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
115 if ( pool[i]->Icmp( string ) == 0 ) {
116 pool[i]->numUsers++;
117 return pool[i];
118 }
119 }
120 }
121
122 poolStr = new idPoolStr;
123 *static_cast<idStr *>(poolStr) = string;
124 poolStr->pool = this;
125 poolStr->numUsers = 1;
126 poolHash.Add( hash, pool.Append( poolStr ) );
127 return poolStr;
128 }
129
130 /*
131 ================
132 idStrPool::FreeString
133 ================
134 */
FreeString(const idPoolStr * poolStr)135 ID_INLINE void idStrPool::FreeString( const idPoolStr *poolStr ) {
136 int i, hash;
137
138 assert( poolStr->numUsers >= 1 );
139 assert( poolStr->pool == this );
140
141 poolStr->numUsers--;
142 if ( poolStr->numUsers <= 0 ) {
143 hash = poolHash.GenerateKey( poolStr->c_str(), caseSensitive );
144 if ( caseSensitive ) {
145 for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
146 if ( pool[i]->Cmp( poolStr->c_str() ) == 0 ) {
147 break;
148 }
149 }
150 } else {
151 for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
152 if ( pool[i]->Icmp( poolStr->c_str() ) == 0 ) {
153 break;
154 }
155 }
156 }
157 assert( i != -1 );
158 assert( pool[i] == poolStr );
159 delete pool[i];
160 pool.RemoveIndex( i );
161 poolHash.RemoveIndex( hash, i );
162 }
163 }
164
165 /*
166 ================
167 idStrPool::CopyString
168 ================
169 */
CopyString(const idPoolStr * poolStr)170 ID_INLINE const idPoolStr *idStrPool::CopyString( const idPoolStr *poolStr ) {
171
172 assert( poolStr->numUsers >= 1 );
173
174 if ( poolStr->pool == this ) {
175 // the string is from this pool so just increase the user count
176 poolStr->numUsers++;
177 return poolStr;
178 } else {
179 // the string is from another pool so it needs to be re-allocated from this pool.
180 return AllocString( poolStr->c_str() );
181 }
182 }
183
184 /*
185 ================
186 idStrPool::Clear
187 ================
188 */
Clear(void)189 ID_INLINE void idStrPool::Clear( void ) {
190 int i;
191
192 for ( i = 0; i < pool.Num(); i++ ) {
193 pool[i]->numUsers = 0;
194 }
195 pool.DeleteContents( true );
196 poolHash.Free();
197 }
198
199 /*
200 ================
201 idStrPool::Allocated
202 ================
203 */
Allocated(void)204 ID_INLINE size_t idStrPool::Allocated( void ) const {
205 int i;
206 size_t size;
207
208 size = pool.Allocated() + poolHash.Allocated();
209 for ( i = 0; i < pool.Num(); i++ ) {
210 size += pool[i]->Allocated();
211 }
212 return size;
213 }
214
215 /*
216 ================
217 idStrPool::Size
218 ================
219 */
Size(void)220 ID_INLINE size_t idStrPool::Size( void ) const {
221 int i;
222 size_t size;
223
224 size = pool.Size() + poolHash.Size();
225 for ( i = 0; i < pool.Num(); i++ ) {
226 size += pool[i]->Size();
227 }
228 return size;
229 }
230
231 #endif /* !__STRPOOL_H__ */
232