sl@0
|
1 |
// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include <e32base.h>
|
sl@0
|
17 |
#include "StringPoolImplementation.h"
|
sl@0
|
18 |
|
sl@0
|
19 |
const TInt KMapGranularity=20;
|
sl@0
|
20 |
|
sl@0
|
21 |
CStringPoolNode::~CStringPoolNode()
|
sl@0
|
22 |
{
|
sl@0
|
23 |
delete iDes;
|
sl@0
|
24 |
}
|
sl@0
|
25 |
|
sl@0
|
26 |
CStringPoolImplementation::CStringPoolImplementation() : iStringMapList(KMapGranularity, _FOFF(TStringIdMap, iSourceTableVal)), iStringMapListReverse(KMapGranularity, _FOFF(TStringIdMap, iTargetTableVal))
|
sl@0
|
27 |
{
|
sl@0
|
28 |
}
|
sl@0
|
29 |
|
sl@0
|
30 |
CStringPoolImplementation::~CStringPoolImplementation()
|
sl@0
|
31 |
{
|
sl@0
|
32 |
// Look for non-expirable strings
|
sl@0
|
33 |
TInt i;
|
sl@0
|
34 |
for (i = 0; i < KHashModulo; i++ )
|
sl@0
|
35 |
{
|
sl@0
|
36 |
DeleteUndeletableStrings(iCIHashTable, i);
|
sl@0
|
37 |
DeleteUndeletableStrings(iCSHashTable, i);
|
sl@0
|
38 |
}
|
sl@0
|
39 |
#ifdef _DEBUG
|
sl@0
|
40 |
|
sl@0
|
41 |
__LOG(_L8(":Closing String Pool.\n"))
|
sl@0
|
42 |
TBool leaksFound = EFalse;
|
sl@0
|
43 |
// Check that the string pool is empty, or more accurately that
|
sl@0
|
44 |
// everything in it is a pre-loaded string
|
sl@0
|
45 |
for (i = 0; i < KHashModulo; i++ )
|
sl@0
|
46 |
{
|
sl@0
|
47 |
if (iCIHashTable[i])
|
sl@0
|
48 |
{
|
sl@0
|
49 |
for (TInt j = 0; j < iCIHashTable[i]->Count(); j++)
|
sl@0
|
50 |
{
|
sl@0
|
51 |
if (!StringUtils::IsTableEntry(iCIHashTable[i]->At(j).iVal))
|
sl@0
|
52 |
{
|
sl@0
|
53 |
if (!leaksFound)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
__LOG(_L8("The following strings were leaked through not being Closed:\n"))
|
sl@0
|
56 |
leaksFound = ETrue;
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
// Get the problem string
|
sl@0
|
60 |
CStringPoolNode* theProblem =
|
sl@0
|
61 |
reinterpret_cast<CStringPoolNode*>(
|
sl@0
|
62 |
iCIHashTable[i]->At(j).iVal & KTokenToNode);
|
sl@0
|
63 |
if(theProblem->iDes)
|
sl@0
|
64 |
__LOG(theProblem->iDes->Des());
|
sl@0
|
65 |
}
|
sl@0
|
66 |
}
|
sl@0
|
67 |
}
|
sl@0
|
68 |
if (iCSHashTable[i])
|
sl@0
|
69 |
{
|
sl@0
|
70 |
for (TInt j = 0; j < iCSHashTable[i]->Count(); j++)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
if (!StringUtils::IsTableEntry(iCSHashTable[i]->At(j).iVal))
|
sl@0
|
73 |
{
|
sl@0
|
74 |
if (!leaksFound)
|
sl@0
|
75 |
{
|
sl@0
|
76 |
__LOG(_L8("The following strings were leaked through not being Closed:\n"))
|
sl@0
|
77 |
leaksFound = ETrue;
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
// Get the problem string
|
sl@0
|
81 |
CStringPoolNode* theProblem =
|
sl@0
|
82 |
reinterpret_cast<CStringPoolNode*>(
|
sl@0
|
83 |
iCSHashTable[i]->At(j).iVal & KTokenToNode);
|
sl@0
|
84 |
if(theProblem->iDes)
|
sl@0
|
85 |
__LOG(theProblem->iDes->Des());
|
sl@0
|
86 |
}
|
sl@0
|
87 |
}
|
sl@0
|
88 |
}
|
sl@0
|
89 |
if (leaksFound)
|
sl@0
|
90 |
__DEBUGGER();
|
sl@0
|
91 |
}
|
sl@0
|
92 |
|
sl@0
|
93 |
if (!leaksFound)
|
sl@0
|
94 |
__LOG(_L8("No leakages were detected\n"));
|
sl@0
|
95 |
|
sl@0
|
96 |
#endif //_DEBUG
|
sl@0
|
97 |
for (TInt ii = 0; ii < KHashModulo; ii++)
|
sl@0
|
98 |
{
|
sl@0
|
99 |
delete iCIHashTable[ii];
|
sl@0
|
100 |
delete iCSHashTable[ii];
|
sl@0
|
101 |
}
|
sl@0
|
102 |
|
sl@0
|
103 |
iTablePtrs.Close();
|
sl@0
|
104 |
iStringMapList.Close();
|
sl@0
|
105 |
iStringMapListReverse.Close();
|
sl@0
|
106 |
iRollbackMapList.Close();
|
sl@0
|
107 |
iRollbackHashListCS.Close();
|
sl@0
|
108 |
iRollbackHashListCI.Close();
|
sl@0
|
109 |
// Notify the external users of the StringPool that the object is getting closed
|
sl@0
|
110 |
TInt cBCounter = iCallBacks.Count();
|
sl@0
|
111 |
if(cBCounter>0)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
while (--cBCounter>=0)
|
sl@0
|
114 |
{
|
sl@0
|
115 |
iCallBacks[cBCounter]->StringPoolClosing();
|
sl@0
|
116 |
}
|
sl@0
|
117 |
}
|
sl@0
|
118 |
iCallBacks.Close();
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
// Check for any undeletable string and delete them now
|
sl@0
|
122 |
void CStringPoolImplementation::DeleteUndeletableStrings(CArrayFixSeg<RStringTokenEither>* aArray[KHashModulo], TInt i)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
if (aArray[i])
|
sl@0
|
125 |
{
|
sl@0
|
126 |
for (TInt j = 0; j < aArray[i]->Count(); ++j)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
if (!StringUtils::IsTableEntry(aArray[i]->At(j).iVal))
|
sl@0
|
129 |
{
|
sl@0
|
130 |
CStringPoolNode* node= reinterpret_cast<CStringPoolNode*>(aArray[i]->At(j).iVal & KTokenToNode);
|
sl@0
|
131 |
if (KMarkedForNoDeleted==node->iRefcount)
|
sl@0
|
132 |
{
|
sl@0
|
133 |
delete node;
|
sl@0
|
134 |
aArray[i]->Delete(j);
|
sl@0
|
135 |
j--;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
}
|
sl@0
|
138 |
}
|
sl@0
|
139 |
}
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
CStringPoolImplementation* CStringPoolImplementation::NewL()
|
sl@0
|
143 |
{
|
sl@0
|
144 |
CStringPoolImplementation* table = new (ELeave) CStringPoolImplementation();
|
sl@0
|
145 |
return table;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
void CStringPoolImplementation::CleanupHashCS(TAny* aImplementation)
|
sl@0
|
149 |
{
|
sl@0
|
150 |
CStringPoolImplementation* imp=reinterpret_cast<CStringPoolImplementation*>(aImplementation);
|
sl@0
|
151 |
CleanUpHash(&imp->iRollbackHashListCS, imp->iCSHashTable);
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
void CStringPoolImplementation::CleanupHashCI(TAny* aImplementation)
|
sl@0
|
155 |
{
|
sl@0
|
156 |
CStringPoolImplementation* imp=reinterpret_cast<CStringPoolImplementation*>(aImplementation);
|
sl@0
|
157 |
CleanUpHash(&imp->iRollbackHashListCI, imp->iCIHashTable);
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
void CStringPoolImplementation::CleanUpHash(RPointerArray <RStringTokenEither>* aHashCleanup, CArrayFixSeg<RStringTokenEither>* aHash[KHashModulo])
|
sl@0
|
161 |
{
|
sl@0
|
162 |
if (aHashCleanup->Count()>0)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
RStringTokenEither* token=(*aHashCleanup)[0]; // Get first entry
|
sl@0
|
165 |
for (TInt i = 0; i < KHashModulo; i++ )
|
sl@0
|
166 |
{
|
sl@0
|
167 |
if (aHash[i])
|
sl@0
|
168 |
{
|
sl@0
|
169 |
for (TInt j = 0; j < aHash[i]->Count(); j++)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
if (!StringUtils::IsTableEntry(aHash[i]->At(j).iVal))
|
sl@0
|
172 |
{
|
sl@0
|
173 |
if (aHash[i]->At(j).iVal==token->iVal)
|
sl@0
|
174 |
{
|
sl@0
|
175 |
CStringPoolNode* node= reinterpret_cast<CStringPoolNode*>(aHash[i]->At(j).iVal & KTokenToNode);
|
sl@0
|
176 |
delete node;
|
sl@0
|
177 |
aHash[i]->Delete(j);
|
sl@0
|
178 |
aHashCleanup->Remove(0);
|
sl@0
|
179 |
break;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
}
|
sl@0
|
182 |
}
|
sl@0
|
183 |
}
|
sl@0
|
184 |
|
sl@0
|
185 |
}
|
sl@0
|
186 |
}
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
void CStringPoolImplementation::CleanupIdMap(TAny* aImplementation)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
CStringPoolImplementation* imp=reinterpret_cast<CStringPoolImplementation*>(aImplementation);
|
sl@0
|
192 |
if (imp->iRollbackMapList.Count()>0)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
TStringIdMap* map=imp->iRollbackMapList[0];
|
sl@0
|
195 |
TInt index=imp->iStringMapList.FindInUnsignedKeyOrder(*map);
|
sl@0
|
196 |
imp->iRollbackMapList.Remove(0);
|
sl@0
|
197 |
if (index!=KErrNotFound)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
imp->iStringMapList.Remove(index);
|
sl@0
|
200 |
}
|
sl@0
|
201 |
index=imp->iStringMapListReverse.FindInUnsignedKeyOrder(*map);
|
sl@0
|
202 |
if (index!=KErrNotFound)
|
sl@0
|
203 |
{
|
sl@0
|
204 |
for (TInt count=index;count<imp->iStringMapListReverse.Count();++count)
|
sl@0
|
205 |
{
|
sl@0
|
206 |
if (imp->iStringMapListReverse[count].iTargetTableVal==map->iTargetTableVal && imp->iStringMapListReverse[count].iSourceTableVal==map->iSourceTableVal)
|
sl@0
|
207 |
{
|
sl@0
|
208 |
imp->iStringMapListReverse.Remove(index);
|
sl@0
|
209 |
}
|
sl@0
|
210 |
}
|
sl@0
|
211 |
}
|
sl@0
|
212 |
}
|
sl@0
|
213 |
}
|
sl@0
|
214 |
|
sl@0
|
215 |
void CStringPoolImplementation::AddTableL(const TStringTable& aTable)
|
sl@0
|
216 |
{
|
sl@0
|
217 |
for (TInt count=0;count<iTablePtrs.Count();++count) // check for adding the same table twice
|
sl@0
|
218 |
{
|
sl@0
|
219 |
if (iTablePtrs[count]==&aTable)
|
sl@0
|
220 |
{
|
sl@0
|
221 |
return;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
}
|
sl@0
|
224 |
User::LeaveIfError(iTablePtrs.Append(&aTable)); // Add the pointer to this table so we can keep track of the Table IDs. The table ID is the index in this array
|
sl@0
|
225 |
//Is the table Case Sensitive or not?
|
sl@0
|
226 |
TBool (*genericValFromIndex)(TInt, TUint16);
|
sl@0
|
227 |
if (aTable.iCaseSensitive==1)
|
sl@0
|
228 |
genericValFromIndex = StringUtils::ValFromIndex;
|
sl@0
|
229 |
else
|
sl@0
|
230 |
genericValFromIndex = StringUtils::ValFromIndexF;
|
sl@0
|
231 |
|
sl@0
|
232 |
CArrayFixSeg<RStringTokenEither>** hashTableToUse =
|
sl@0
|
233 |
aTable.iCaseSensitive ? iCSHashTable :iCIHashTable ;
|
sl@0
|
234 |
TInt cleanupCounter=0;
|
sl@0
|
235 |
for (TUint i = 0; i < aTable.iCount; ++i)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
const TStLitC8<1>* string=reinterpret_cast<const TStLitC8<1>* >(aTable.iTable[i]);
|
sl@0
|
238 |
|
sl@0
|
239 |
// Try to find the string in memory, maybe as a dynamic string or as a member of an another table
|
sl@0
|
240 |
RStringTokenEither token=FindDes(*string, !aTable.iCaseSensitive);
|
sl@0
|
241 |
if (!token.IsNull())
|
sl@0
|
242 |
{
|
sl@0
|
243 |
TStringIdMap map;
|
sl@0
|
244 |
map.iSourceTableVal=StringUtils::ValFromIndex(i, (TInt16)(iTablePtrs.Count()-1),aTable.iCaseSensitive);
|
sl@0
|
245 |
map.iTargetTableVal=token.iVal;
|
sl@0
|
246 |
|
sl@0
|
247 |
// Put on cleanup stack
|
sl@0
|
248 |
User::LeaveIfError(iRollbackMapList.Append(&map));
|
sl@0
|
249 |
TCleanupItem cleanup(CleanupIdMap, this);
|
sl@0
|
250 |
CleanupStack::PushL(cleanup);
|
sl@0
|
251 |
++cleanupCounter;
|
sl@0
|
252 |
|
sl@0
|
253 |
User::LeaveIfError(iStringMapList.InsertInUnsignedKeyOrder(map));
|
sl@0
|
254 |
|
sl@0
|
255 |
// Check if this is a link to a dynamic string
|
sl@0
|
256 |
if (!StringUtils::IsTableEntry(token.iVal))
|
sl@0
|
257 |
{
|
sl@0
|
258 |
CStringPoolNode* node = StringUtils::NodePtr(token.iVal);
|
sl@0
|
259 |
node->iRefcount=KMarkedForNoDeleted; // Make sure this string never gets deleted
|
sl@0
|
260 |
}
|
sl@0
|
261 |
|
sl@0
|
262 |
// Now store the reverse array
|
sl@0
|
263 |
User::LeaveIfError(iStringMapListReverse.InsertInUnsignedKeyOrderAllowRepeats(map));
|
sl@0
|
264 |
}
|
sl@0
|
265 |
else
|
sl@0
|
266 |
{
|
sl@0
|
267 |
TUint8 hash = static_cast<TUint8>(Hash(*string));
|
sl@0
|
268 |
CArrayFixSeg<RStringTokenEither>* collisionList = hashTableToUse[hash];
|
sl@0
|
269 |
if ( !collisionList )
|
sl@0
|
270 |
//HashTableToUse now is used as list of all entry with the same hash
|
sl@0
|
271 |
collisionList = hashTableToUse[hash] =
|
sl@0
|
272 |
new (ELeave) CArrayFixSeg<RStringTokenEither>( 2 );
|
sl@0
|
273 |
RStringTokenEither s;
|
sl@0
|
274 |
s.iVal = genericValFromIndex(i, (TInt16)(iTablePtrs.Count()-1));
|
sl@0
|
275 |
|
sl@0
|
276 |
__LOG2(_L8("Table entry being added with hash %d, val %d"), hash, s.iVal);
|
sl@0
|
277 |
__LOG(*reinterpret_cast<const TStLitC8<1>* >(aTable.iTable[i]));
|
sl@0
|
278 |
// Put on cleanup stack
|
sl@0
|
279 |
if (aTable.iCaseSensitive==1)
|
sl@0
|
280 |
{
|
sl@0
|
281 |
User::LeaveIfError(iRollbackHashListCS.Append(&s));
|
sl@0
|
282 |
TCleanupItem cleanup(CleanupHashCS, this);
|
sl@0
|
283 |
CleanupStack::PushL(cleanup);
|
sl@0
|
284 |
}
|
sl@0
|
285 |
else
|
sl@0
|
286 |
{
|
sl@0
|
287 |
User::LeaveIfError(iRollbackHashListCI.Append(&s));
|
sl@0
|
288 |
TCleanupItem cleanup(CleanupHashCI, this);
|
sl@0
|
289 |
CleanupStack::PushL(cleanup);
|
sl@0
|
290 |
}
|
sl@0
|
291 |
|
sl@0
|
292 |
++cleanupCounter;
|
sl@0
|
293 |
collisionList->AppendL(s);
|
sl@0
|
294 |
}
|
sl@0
|
295 |
}
|
sl@0
|
296 |
CleanupStack::Pop(cleanupCounter);
|
sl@0
|
297 |
iRollbackMapList.Reset();
|
sl@0
|
298 |
iRollbackHashListCS.Reset();
|
sl@0
|
299 |
iRollbackHashListCI.Reset();
|
sl@0
|
300 |
}
|
sl@0
|
301 |
|
sl@0
|
302 |
// Find FirstVal given duplicate val
|
sl@0
|
303 |
TInt32 CStringPoolImplementation::FindFirstValFromDuplicate(TInt32 aDuplicateVal) const
|
sl@0
|
304 |
{
|
sl@0
|
305 |
TStringIdMap map;
|
sl@0
|
306 |
map.iSourceTableVal=aDuplicateVal;
|
sl@0
|
307 |
TInt index=iStringMapList.FindInUnsignedKeyOrder(map);
|
sl@0
|
308 |
if (index!=KErrNotFound)
|
sl@0
|
309 |
return iStringMapList[index].iTargetTableVal;
|
sl@0
|
310 |
else
|
sl@0
|
311 |
return KErrNotFound;
|
sl@0
|
312 |
}
|
sl@0
|
313 |
|
sl@0
|
314 |
|
sl@0
|
315 |
|
sl@0
|
316 |
// Find table index Val given first val & table UID
|
sl@0
|
317 |
TInt CStringPoolImplementation::FindTableIndexFromFirstVal(TInt32 aFirstVal, TInt aTableUid) const
|
sl@0
|
318 |
{
|
sl@0
|
319 |
TStringIdMap map;
|
sl@0
|
320 |
map.iTargetTableVal=aFirstVal;
|
sl@0
|
321 |
TInt index=iStringMapListReverse.FindInUnsignedKeyOrder(map);
|
sl@0
|
322 |
if (KErrNotFound==index)
|
sl@0
|
323 |
return KErrNotFound;
|
sl@0
|
324 |
|
sl@0
|
325 |
for (TInt count=index;count<iStringMapListReverse.Count();++count)
|
sl@0
|
326 |
{
|
sl@0
|
327 |
if (iStringMapListReverse[count].iTargetTableVal==aFirstVal && StringUtils::TableUid(iStringMapListReverse[count].iSourceTableVal)==aTableUid)
|
sl@0
|
328 |
{
|
sl@0
|
329 |
return StringUtils::TableIndex(iStringMapListReverse[count].iSourceTableVal);
|
sl@0
|
330 |
}
|
sl@0
|
331 |
}
|
sl@0
|
332 |
return KErrNotFound;
|
sl@0
|
333 |
}
|
sl@0
|
334 |
|
sl@0
|
335 |
|
sl@0
|
336 |
// Find the UId for a given table
|
sl@0
|
337 |
TInt16 CStringPoolImplementation::TableUid(const TStringTable& aTable) const
|
sl@0
|
338 |
{
|
sl@0
|
339 |
for (TInt count=0; count<iTablePtrs.Count(); ++count)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
if (iTablePtrs[count]==&aTable)
|
sl@0
|
342 |
return (TInt16)count;
|
sl@0
|
343 |
}
|
sl@0
|
344 |
return KErrNotFound;
|
sl@0
|
345 |
}
|
sl@0
|
346 |
|
sl@0
|
347 |
// Find a reference to the table that first added the string represented by aVal to the pool
|
sl@0
|
348 |
const TStringTable& CStringPoolImplementation::TableRef(TInt32 aVal) const
|
sl@0
|
349 |
{
|
sl@0
|
350 |
__ASSERT_DEBUG(aVal!=0, StringPoolPanic::Panic(StringPoolPanic::EIllegalUseOfNullString));
|
sl@0
|
351 |
TInt16 tableUid=(TInt16)(aVal>>20);
|
sl@0
|
352 |
const TStringTable* theTableRef=(iTablePtrs[tableUid]);
|
sl@0
|
353 |
return *theTableRef;
|
sl@0
|
354 |
}
|
sl@0
|
355 |
|
sl@0
|
356 |
// Find the descriptor for a given table and index
|
sl@0
|
357 |
const TDesC8& CStringPoolImplementation::TableLookup(TInt aIndex, TInt aTableUid) const
|
sl@0
|
358 |
{
|
sl@0
|
359 |
return *reinterpret_cast<const TStLitC8<1>*>(iTablePtrs[aTableUid]->iTable[aIndex]);
|
sl@0
|
360 |
}
|
sl@0
|
361 |
|
sl@0
|
362 |
|
sl@0
|
363 |
// Lookup with allocating
|
sl@0
|
364 |
//
|
sl@0
|
365 |
RStringTokenEither
|
sl@0
|
366 |
CStringPoolImplementation::OpenL( const TDesC8& aAttributeName,
|
sl@0
|
367 |
TBool aCaseInsensitive)
|
sl@0
|
368 |
{
|
sl@0
|
369 |
// lookup the attribute
|
sl@0
|
370 |
RStringTokenEither s(FindDes( aAttributeName , aCaseInsensitive));
|
sl@0
|
371 |
if (!s.IsNull())
|
sl@0
|
372 |
{
|
sl@0
|
373 |
if (!StringUtils::IsTableEntry(s.iVal))
|
sl@0
|
374 |
{
|
sl@0
|
375 |
|
sl@0
|
376 |
CStringPoolNode* node = StringUtils::NodePtr(s.iVal);
|
sl@0
|
377 |
if (KMarkedForNoDeleted!=node->iRefcount)
|
sl@0
|
378 |
node->iRefcount++;
|
sl@0
|
379 |
__LOG1(_L8("String copied (during open). Count is now %d"), node->iRefcount);
|
sl@0
|
380 |
__LOG(*node->iDes);
|
sl@0
|
381 |
}
|
sl@0
|
382 |
return s;
|
sl@0
|
383 |
}
|
sl@0
|
384 |
|
sl@0
|
385 |
// create a new node at the end of the appropriate array
|
sl@0
|
386 |
CStringPoolNode* newnode = new (ELeave) CStringPoolNode();
|
sl@0
|
387 |
CleanupStack::PushL( newnode );
|
sl@0
|
388 |
newnode->iDes = aAttributeName.AllocL();
|
sl@0
|
389 |
newnode->iRefcount = 1;
|
sl@0
|
390 |
|
sl@0
|
391 |
TInt hash = Hash( aAttributeName );
|
sl@0
|
392 |
CArrayFixSeg<RStringTokenEither>** hashTableToUse =
|
sl@0
|
393 |
aCaseInsensitive ? iCIHashTable : iCSHashTable;
|
sl@0
|
394 |
__LOG2(_L8("Newly added with hash value %d, node val 0x%x\n"), hash, newnode)
|
sl@0
|
395 |
__LOG(aAttributeName);
|
sl@0
|
396 |
|
sl@0
|
397 |
newnode->iHash = static_cast<TUint8>(hash);
|
sl@0
|
398 |
CArrayFixSeg<RStringTokenEither>* collisionList = hashTableToUse[hash];
|
sl@0
|
399 |
if ( !collisionList )
|
sl@0
|
400 |
collisionList = hashTableToUse[hash] = new (ELeave) CArrayFixSeg<RStringTokenEither>( 2 );
|
sl@0
|
401 |
|
sl@0
|
402 |
s.iVal = reinterpret_cast<TUint32>(newnode);
|
sl@0
|
403 |
if (aCaseInsensitive)
|
sl@0
|
404 |
s.iVal += 2;
|
sl@0
|
405 |
collisionList->AppendL(s);
|
sl@0
|
406 |
|
sl@0
|
407 |
CleanupStack::Pop(); // newnode
|
sl@0
|
408 |
|
sl@0
|
409 |
return s;
|
sl@0
|
410 |
}
|
sl@0
|
411 |
|
sl@0
|
412 |
void CStringPoolImplementation::Close(RStringTokenEither aString)
|
sl@0
|
413 |
{
|
sl@0
|
414 |
if (StringUtils::IsTableEntry(aString.iVal))
|
sl@0
|
415 |
return;
|
sl@0
|
416 |
|
sl@0
|
417 |
CStringPoolNode* node = StringUtils::NodePtr(aString.iVal);
|
sl@0
|
418 |
if (KMarkedForNoDeleted == node->iRefcount) // -1 means a non-expirable string
|
sl@0
|
419 |
return;
|
sl@0
|
420 |
if (--node->iRefcount == 0)
|
sl@0
|
421 |
{
|
sl@0
|
422 |
//this is the last reference of this string
|
sl@0
|
423 |
CArrayFixSeg<RStringTokenEither>** hashTableToUse =
|
sl@0
|
424 |
aString.iVal & 2 ? iCIHashTable : iCSHashTable;
|
sl@0
|
425 |
|
sl@0
|
426 |
// Delete the node and delete the entry in the relevant collision list
|
sl@0
|
427 |
CArrayFixSeg<RStringTokenEither>* collisionList = hashTableToUse[node->iHash];
|
sl@0
|
428 |
TInt count = collisionList->Count();
|
sl@0
|
429 |
for (TInt i = 0; i < count; i++)
|
sl@0
|
430 |
{
|
sl@0
|
431 |
if (collisionList->At(i) == aString)
|
sl@0
|
432 |
{
|
sl@0
|
433 |
// Log the fact that a string reference is about to die...
|
sl@0
|
434 |
__LOG1(_L8("Removing string with hash value %d\n"), node->iHash)
|
sl@0
|
435 |
__LOG(node->iDes->Des());
|
sl@0
|
436 |
collisionList->Delete(i);
|
sl@0
|
437 |
break;
|
sl@0
|
438 |
}
|
sl@0
|
439 |
}
|
sl@0
|
440 |
delete node;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
else
|
sl@0
|
443 |
{
|
sl@0
|
444 |
__LOG1(_L8("String closed. Count is now %d"),
|
sl@0
|
445 |
node->iRefcount);
|
sl@0
|
446 |
__LOG(node->iDes->Des());
|
sl@0
|
447 |
}
|
sl@0
|
448 |
}
|
sl@0
|
449 |
|
sl@0
|
450 |
void CStringPoolImplementation::IncrementCount(RStringTokenEither aString)
|
sl@0
|
451 |
{
|
sl@0
|
452 |
if (StringUtils::IsTableEntry(aString.iVal))
|
sl@0
|
453 |
return;
|
sl@0
|
454 |
CStringPoolNode* node = StringUtils::NodePtr(aString.iVal);
|
sl@0
|
455 |
if (KMarkedForNoDeleted!=node->iRefcount)
|
sl@0
|
456 |
node->iRefcount++;
|
sl@0
|
457 |
__LOG1(_L8("String copied. Count is now %d"), node->iRefcount);
|
sl@0
|
458 |
__LOG(*node->iDes);
|
sl@0
|
459 |
}
|
sl@0
|
460 |
|
sl@0
|
461 |
// Very simple case-sensitive comparison. We can assume that the
|
sl@0
|
462 |
// strings are the same length, and we only care if the strings are
|
sl@0
|
463 |
// the same. (Unlike normal comparison functions that also tell you
|
sl@0
|
464 |
// which one is 'smaller')
|
sl@0
|
465 |
TBool CStringPoolImplementation::CompareCS(const TDesC8& s1, const TDesC8& s2)
|
sl@0
|
466 |
{
|
sl@0
|
467 |
const TUint8* ptr1 = s1.Ptr();
|
sl@0
|
468 |
const TUint8* ptr2 = s2.Ptr();
|
sl@0
|
469 |
const TUint8* stop = &ptr1[s1.Length()];
|
sl@0
|
470 |
for (; ptr1 < stop; ptr1++,ptr2++)
|
sl@0
|
471 |
{
|
sl@0
|
472 |
if (*ptr1 != *ptr2)
|
sl@0
|
473 |
return EFalse;
|
sl@0
|
474 |
}
|
sl@0
|
475 |
return ETrue;
|
sl@0
|
476 |
}
|
sl@0
|
477 |
|
sl@0
|
478 |
// Note that the hash function must generate the same hash values for
|
sl@0
|
479 |
// strings that differ by case. If changing the algorithm here make
|
sl@0
|
480 |
// sure this is still true.
|
sl@0
|
481 |
TBool CStringPoolImplementation::CompareCI(const TDesC8& s1, const TDesC8& s2)
|
sl@0
|
482 |
{
|
sl@0
|
483 |
const TUint8* ptr1 = s1.Ptr();
|
sl@0
|
484 |
const TUint8* ptr2 = s2.Ptr();
|
sl@0
|
485 |
const TUint8* stop = &ptr1[s1.Length()];
|
sl@0
|
486 |
for (; ptr1 < stop; ptr1++,ptr2++)
|
sl@0
|
487 |
{
|
sl@0
|
488 |
if (*ptr1 != *ptr2)
|
sl@0
|
489 |
{
|
sl@0
|
490 |
// They're not exactly the same; see if they differ only
|
sl@0
|
491 |
// by case. If one character is a letter, we can do a
|
sl@0
|
492 |
// comparison ignoring bit 5 in both cases. If that
|
sl@0
|
493 |
// matches, they are the same.
|
sl@0
|
494 |
if (!((*ptr1 & KCaseInsensitive) == (*ptr2 & KCaseInsensitive) &&
|
sl@0
|
495 |
(*ptr1 >= 'A' && *ptr1 <= 'Z' ||
|
sl@0
|
496 |
*ptr1 >= 'a' && *ptr1 <= 'z')))
|
sl@0
|
497 |
return EFalse;
|
sl@0
|
498 |
}
|
sl@0
|
499 |
}
|
sl@0
|
500 |
return ETrue;
|
sl@0
|
501 |
}
|
sl@0
|
502 |
|
sl@0
|
503 |
// Find the given descriptor in the hash table
|
sl@0
|
504 |
//
|
sl@0
|
505 |
RStringTokenEither
|
sl@0
|
506 |
CStringPoolImplementation::FindDes( const TDesC8& aAttributeName, TBool aCaseInsensitive)
|
sl@0
|
507 |
{
|
sl@0
|
508 |
CArrayFixSeg<RStringTokenEither>** hashTableToUse =
|
sl@0
|
509 |
aCaseInsensitive ? iCIHashTable : iCSHashTable;
|
sl@0
|
510 |
CArrayFixSeg<RStringTokenEither>* collisionList =hashTableToUse[Hash(aAttributeName)];
|
sl@0
|
511 |
RStringPool pool;
|
sl@0
|
512 |
TBool (*compareFunction)(const TDesC8&, const TDesC8&);
|
sl@0
|
513 |
if (aCaseInsensitive)
|
sl@0
|
514 |
compareFunction = CompareCI;
|
sl@0
|
515 |
else
|
sl@0
|
516 |
compareFunction = CompareCS;
|
sl@0
|
517 |
pool.iImplementation = this;
|
sl@0
|
518 |
if ( collisionList )
|
sl@0
|
519 |
{
|
sl@0
|
520 |
TInt length=aAttributeName.Length();
|
sl@0
|
521 |
TInt count = collisionList->Count();
|
sl@0
|
522 |
for ( TInt i = 0; i < count; i++ )
|
sl@0
|
523 |
{
|
sl@0
|
524 |
RStringTokenEither token = collisionList->At(i);
|
sl@0
|
525 |
RStringEither s(this, token);
|
sl@0
|
526 |
const TDesC8& string = s.DesC();
|
sl@0
|
527 |
if ( string.Length()==length &&
|
sl@0
|
528 |
(*compareFunction)(aAttributeName, string))
|
sl@0
|
529 |
return token;
|
sl@0
|
530 |
}
|
sl@0
|
531 |
}
|
sl@0
|
532 |
return RStringTokenEither();
|
sl@0
|
533 |
}
|
sl@0
|
534 |
|
sl@0
|
535 |
|
sl@0
|
536 |
// Generate a hash value
|
sl@0
|
537 |
//
|
sl@0
|
538 |
TUint CStringPoolImplementation::Hash( const TDesC8& aDes ) const
|
sl@0
|
539 |
{
|
sl@0
|
540 |
// We ignore bit 5, which is a crude way of making the hash case
|
sl@0
|
541 |
// insensitive. This means that things that might differ only by
|
sl@0
|
542 |
// case end up in the same bucket, and we can then worry about
|
sl@0
|
543 |
// whether they're really the same later.
|
sl@0
|
544 |
TInt len=aDes.Length();
|
sl@0
|
545 |
TUint hash = 0;
|
sl@0
|
546 |
const TUint8* ptr=aDes.Ptr();
|
sl@0
|
547 |
for ( TInt i = 0; i < len; i++ )
|
sl@0
|
548 |
hash = 131*hash + (*ptr++ & KCaseInsensitive);
|
sl@0
|
549 |
return hash % KHashModulo;
|
sl@0
|
550 |
}
|
sl@0
|
551 |
|
sl@0
|
552 |
TInt StringUtils::ValFromIndex(TInt aIndex, TUint16 aTableId)
|
sl@0
|
553 |
{
|
sl@0
|
554 |
return (aTableId << 20) + (aIndex << 2) + 1;
|
sl@0
|
555 |
}
|
sl@0
|
556 |
|
sl@0
|
557 |
TInt StringUtils::ValFromIndexF(TInt aIndex, TUint16 aTableId)
|
sl@0
|
558 |
{
|
sl@0
|
559 |
return (aTableId << 20) + (aIndex << 2) + 3;
|
sl@0
|
560 |
}
|
sl@0
|
561 |
|
sl@0
|
562 |
TInt StringUtils::ValFromIndex(TInt aIndex, TUint16 aTableId, TBool aCaseSensitive)
|
sl@0
|
563 |
{
|
sl@0
|
564 |
if (aCaseSensitive)
|
sl@0
|
565 |
return ValFromIndex(aIndex, aTableId);
|
sl@0
|
566 |
else
|
sl@0
|
567 |
return ValFromIndexF(aIndex, aTableId);
|
sl@0
|
568 |
}
|
sl@0
|
569 |
void CStringPoolImplementation::AddCallBackL( MStringPoolCloseCallBack& aCallBack)
|
sl@0
|
570 |
{
|
sl@0
|
571 |
User::LeaveIfError(iCallBacks.Append(&aCallBack));
|
sl@0
|
572 |
}
|