0
|
1 |
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\kernel\objectix.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
/**
|
|
19 |
@file
|
|
20 |
@internalTechnology
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include <kernel/kern_priv.h>
|
|
24 |
#include "dobject.h"
|
|
25 |
|
|
26 |
#ifdef __VC32__
|
|
27 |
#pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
|
|
28 |
#endif
|
|
29 |
|
|
30 |
#undef asserta
|
|
31 |
#undef assertd
|
|
32 |
#undef assertt
|
|
33 |
|
|
34 |
#define asserta(x) do { if (!(x)) { __crash(); } } while(0)
|
|
35 |
|
|
36 |
#ifdef DOBJECT_TEST_CODE
|
|
37 |
#define assertt(x) asserta(x)
|
|
38 |
#else
|
|
39 |
#define assertt(x)
|
|
40 |
#endif
|
|
41 |
|
|
42 |
#ifdef _DEBUG
|
|
43 |
#define __DEBUG_TRANSFER(s,d) ((RHeap::SDebugCell*)d)[-1].nestingLevel = ((RHeap::SDebugCell*)s)[-1].nestingLevel
|
|
44 |
#define assertd(x) asserta(x)
|
|
45 |
#else
|
|
46 |
#define __DEBUG_TRANSFER(s,d)
|
|
47 |
#define assertd(x)
|
|
48 |
#endif
|
|
49 |
|
|
50 |
volatile TUint32 RObjectIx::NextInstance = 0x00040000u;
|
|
51 |
|
|
52 |
|
|
53 |
void RObjectIx::Wait()
|
|
54 |
{
|
|
55 |
Kern::MutexWait(*HandleMutex);
|
|
56 |
} // RObjectIx::Wait
|
|
57 |
|
|
58 |
|
|
59 |
void RObjectIx::Signal()
|
|
60 |
{
|
|
61 |
Kern::MutexSignal(*HandleMutex);
|
|
62 |
} // RObjectIx::Signal
|
|
63 |
|
|
64 |
|
|
65 |
RObjectIx::RObjectIx()
|
|
66 |
: iRWL(TSpinLock::EOrderGenericIrqLow3)
|
|
67 |
{
|
|
68 |
iAllocated=0;
|
|
69 |
iCount=0;
|
|
70 |
iActiveCount=0;
|
|
71 |
iReservedFree=0;
|
|
72 |
iReservedTotal=0;
|
|
73 |
iReservedFreeHWM=0;
|
|
74 |
iSlots=NULL;
|
|
75 |
iAmortize=0;
|
|
76 |
iState=ENormal;
|
|
77 |
iModCount=255; // iModCount=255 means iModList not in use
|
|
78 |
iModListShift=0;
|
|
79 |
iSpare1=0;
|
|
80 |
|
|
81 |
TInt i;
|
|
82 |
for (i=-ENumFreeQ; i<0; ++i)
|
|
83 |
{
|
|
84 |
Empty(i);
|
|
85 |
}
|
|
86 |
} // RObjectIx::RObjectIx
|
|
87 |
|
|
88 |
|
|
89 |
/** Destroys a kernel object index.
|
|
90 |
|
|
91 |
Any objects in the index are closed.
|
|
92 |
|
|
93 |
@param aPtr Passed as the parameter to Close() for each object.
|
|
94 |
@pre Calling thread must be in a critical section.
|
|
95 |
@pre No fast mutex can be held.
|
|
96 |
@pre Call in a thread context.
|
|
97 |
*/
|
|
98 |
TInt RObjectIx::Close(TAny* aPtr)
|
|
99 |
{
|
|
100 |
Wait();
|
|
101 |
AcquireWriteLock();
|
|
102 |
SSlot* slots = iSlots;
|
|
103 |
TInt count = iCount;
|
|
104 |
iSlots = NULL;
|
|
105 |
iCount = 0; // stops any future handle lookups succeeding
|
|
106 |
iState = (TUint8)ETerminated; // stops any future add/remove/reserve etc.
|
|
107 |
iAllocated = 0;
|
|
108 |
iModCount = 255;
|
|
109 |
ReleaseWriteLock();
|
|
110 |
Signal();
|
|
111 |
TInt i=-1;
|
|
112 |
while(++i<count)
|
|
113 |
{
|
|
114 |
DObject* pO = Occupant(slots+i);
|
|
115 |
if (pO)
|
|
116 |
{
|
|
117 |
pO->Close(aPtr);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
Kern::Free(slots); // free memory
|
|
121 |
|
|
122 |
return KErrNone;
|
|
123 |
} // RObjectIx::Close
|
|
124 |
|
|
125 |
|
|
126 |
/* Look up a handle in the object index
|
|
127 |
@param aHandle looks like this:
|
|
128 |
Bits 0-14 index
|
|
129 |
Bit 15 no-close flag (ignored here)
|
|
130 |
Bits 16-29 instance value
|
|
131 |
Bit 30 thread local flag (ignored here)
|
|
132 |
Bit 31 special handle flag (should be 0 here)
|
|
133 |
@param aUniqueID type of object required (enum value + 1), 0 for any
|
|
134 |
@param aAttr receives attributes of handle
|
|
135 |
@return pointer to the object if handle valid, 0 if not
|
|
136 |
@pre Interrupts enabled
|
|
137 |
*/
|
|
138 |
// Note: The aAttr was changed to a pointer to avoid the amibiguous overload of
|
|
139 |
// the RObjectIx::At(TInt aHandle, TInt aUniqueID, TUint32 aRequiredAttr)
|
|
140 |
// version.
|
|
141 |
DObject* RObjectIx::At(TInt aHandle, TInt aUniqueID, TUint32* aAttr)
|
|
142 |
{
|
|
143 |
TInt ix = HandleIndex(aHandle);
|
|
144 |
TInt in = HandleInstance(aHandle);
|
|
145 |
|
|
146 |
if (aAttr != NULL)
|
|
147 |
{
|
|
148 |
*aAttr = 0;
|
|
149 |
}
|
|
150 |
|
|
151 |
DObject* pO = 0;
|
|
152 |
AcquireReadLock();
|
|
153 |
if (ix<iCount)
|
|
154 |
{
|
|
155 |
SSlot* slot = iSlots + ix;
|
|
156 |
TUint32 attr = slot->iUsed.iAttr;
|
|
157 |
TUint32 objr = slot->iUsed.iObjR;
|
|
158 |
if (objr>=EObjROccupied && ((attr&0x3FFF)==(TUint)in))
|
|
159 |
{
|
|
160 |
// slot occupied and instance value matches
|
|
161 |
if (!aUniqueID || ((attr>>14)&0x3F) == (TUint)aUniqueID)
|
|
162 |
{
|
|
163 |
// object of correct type (if type specified)
|
|
164 |
if (aAttr != NULL)
|
|
165 |
{
|
|
166 |
*aAttr = (objr<<31)|(attr>>20);
|
|
167 |
}
|
|
168 |
|
|
169 |
#ifdef __HANDLES_USE_RW_SPIN_LOCK__
|
|
170 |
// If handle lookup protected only by RW spin lock, must take a reference on the object
|
|
171 |
DObject* obj = (DObject*)(objr & EObjRObjMask);
|
|
172 |
if (obj->Open()==KErrNone)
|
|
173 |
{
|
|
174 |
pO = obj;
|
|
175 |
}
|
|
176 |
#else
|
|
177 |
pO = (DObject*)(objr & EObjRObjMask);
|
|
178 |
#endif
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
|
182 |
ReleaseReadLock();
|
|
183 |
|
|
184 |
return pO;
|
|
185 |
} // RObjectIx::At
|
|
186 |
|
|
187 |
|
|
188 |
/* Look up a handle in the object index
|
|
189 |
@param aHandle looks like this:
|
|
190 |
Bits 0-14 index
|
|
191 |
Bit 15 no-close flag (ignored here)
|
|
192 |
Bits 16-29 instance value
|
|
193 |
Bit 30 thread local flag (ignored here)
|
|
194 |
Bit 31 special handle flag (should be 0 here)
|
|
195 |
@param aUniqueID type of object required (enum value + 1), 0 for any
|
|
196 |
@param aRequiredAttr bitmask of attributes which handle should have
|
|
197 |
@return pointer to the object if handle valid, 0 if not
|
|
198 |
@pre Interrupts enabled
|
|
199 |
*/
|
|
200 |
DObject* RObjectIx::At(TInt aHandle, TInt aUniqueID, TUint32 aRequiredAttr)
|
|
201 |
{
|
|
202 |
TInt ix = HandleIndex(aHandle);
|
|
203 |
TInt in = HandleInstance(aHandle);
|
|
204 |
DObject* pO = 0;
|
|
205 |
AcquireReadLock();
|
|
206 |
if (ix<iCount)
|
|
207 |
{
|
|
208 |
SSlot* slot = iSlots + ix;
|
|
209 |
TUint32 attr = slot->iUsed.iAttr;
|
|
210 |
TUint32 objr = slot->iUsed.iObjR;
|
|
211 |
if (objr>=EObjROccupied && ((attr&0x3FFF)==(TUint)in))
|
|
212 |
{
|
|
213 |
// slot occupied and instance value matches
|
|
214 |
if (!aUniqueID || ((attr>>14)&0x3F) == (TUint)aUniqueID)
|
|
215 |
{
|
|
216 |
// object of correct type (if type specified)
|
|
217 |
TUint32 xattr = (objr<<31)|(attr>>20);
|
|
218 |
if (!(aRequiredAttr &~ xattr))
|
|
219 |
{
|
|
220 |
// all required attributes present
|
|
221 |
#ifdef __HANDLES_USE_RW_SPIN_LOCK__
|
|
222 |
// If handle lookup protected only by RW spin lock, must take a reference on the object
|
|
223 |
DObject* obj = (DObject*)(objr & EObjRObjMask);
|
|
224 |
if (obj->Open()==KErrNone)
|
|
225 |
{
|
|
226 |
pO = obj;
|
|
227 |
}
|
|
228 |
#else
|
|
229 |
pO = (DObject*)(objr & EObjRObjMask);
|
|
230 |
#endif
|
|
231 |
}
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
ReleaseReadLock();
|
|
236 |
|
|
237 |
return pO;
|
|
238 |
} // RObjectIx::At
|
|
239 |
|
|
240 |
|
|
241 |
/* Look up a handle in the object index
|
|
242 |
@param aHandle looks like this:
|
|
243 |
Bits 0-14 index
|
|
244 |
Bit 15 no-close flag (ignored here)
|
|
245 |
Bits 16-29 instance value
|
|
246 |
Bit 30 thread local flag (ignored here)
|
|
247 |
Bit 31 special handle flag (should be 0 here)
|
|
248 |
@param aUniqueID type of object required (enum value + 1), 0 for any
|
|
249 |
@return pointer to the object if handle valid, 0 if not
|
|
250 |
@pre Interrupts enabled
|
|
251 |
*/
|
|
252 |
DObject* RObjectIx::At(TInt aHandle, TInt aUniqueID)
|
|
253 |
{
|
|
254 |
return At(aHandle, aUniqueID, (TUint32)0);
|
|
255 |
} // RObjectIx::At
|
|
256 |
|
|
257 |
|
|
258 |
/* Look up a handle in the object index
|
|
259 |
@param aHandle looks like this:
|
|
260 |
Bits 0-14 index
|
|
261 |
Bit 15 no-close flag (ignored here)
|
|
262 |
Bits 16-29 instance value
|
|
263 |
Bit 30 thread local flag (ignored here)
|
|
264 |
Bit 31 special handle flag (should be 0 here)
|
|
265 |
@return pointer to the object if handle valid, 0 if not
|
|
266 |
@pre Interrupts enabled
|
|
267 |
*/
|
|
268 |
DObject* RObjectIx::At(TInt aHandle)
|
|
269 |
{
|
|
270 |
return At(aHandle, 0, (TUint32)0);
|
|
271 |
} // RObjectIx::At
|
|
272 |
|
|
273 |
|
|
274 |
TUint32 RObjectIx::GetNextInstanceValue()
|
|
275 |
{
|
|
276 |
TUint32 x = NextInstance;
|
|
277 |
TUint32 y;
|
|
278 |
do {
|
|
279 |
y = x >> 1;
|
|
280 |
y ^= (x>>11);
|
|
281 |
y ^= (x>>12);
|
|
282 |
y ^= (x>>13);
|
|
283 |
y &= 0x00040000u;
|
|
284 |
y |= (x<<1);
|
|
285 |
} while(!__e32_atomic_cas_rlx32(&NextInstance, &x, y));
|
|
286 |
return ((y>>18) * 10133u) & 0x3FFFu;
|
|
287 |
} // RObjectIx::GetNextInstanceValue
|
|
288 |
|
|
289 |
|
|
290 |
void RObjectIx::Empty(TInt aQueue)
|
|
291 |
{
|
|
292 |
assertd(TUint(aQueue+ENumFreeQ)<TUint(ENumFreeQ));
|
|
293 |
SSlotQLink* s = Link(aQueue);
|
|
294 |
s->iNext = (TInt16)aQueue;
|
|
295 |
s->iPrev = (TInt16)aQueue;
|
|
296 |
} // RObjectIx::Empty
|
|
297 |
|
|
298 |
|
|
299 |
// requires write lock
|
|
300 |
RObjectIx::SSlot* RObjectIx::Dequeue(TInt aSlotIndex)
|
|
301 |
{
|
|
302 |
assertd(TUint(aSlotIndex)<TUint(iAllocated));
|
|
303 |
SSlot* s = iSlots + aSlotIndex;
|
|
304 |
|
|
305 |
Link(s->iFree.iPrev)->iNext = TInt16(s->iFree.iNext);
|
|
306 |
Link(s->iFree.iNext)->iPrev = TInt16(s->iFree.iPrev);
|
|
307 |
if (iModCount<255)
|
|
308 |
{
|
|
309 |
MarkModified(aSlotIndex);
|
|
310 |
if (s->iFree.iPrev >= 0)
|
|
311 |
{
|
|
312 |
MarkModified(s->iFree.iPrev);
|
|
313 |
}
|
|
314 |
if (s->iFree.iNext >= 0)
|
|
315 |
{
|
|
316 |
MarkModified(s->iFree.iNext);
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
return s;
|
|
321 |
} // RObjectIx::Dequeue
|
|
322 |
|
|
323 |
|
|
324 |
// requires write lock
|
|
325 |
void RObjectIx::AddBefore(TInt aBase, TInt aSlotIndex)
|
|
326 |
{
|
|
327 |
assertd(TUint(aSlotIndex)<TUint(iAllocated));
|
|
328 |
SSlotQLink* base = Link(aBase);
|
|
329 |
SSlotQLink* slot = Link(aSlotIndex);
|
|
330 |
TInt prevIndex = base->iPrev;
|
|
331 |
SSlotQLink* prev = Link(prevIndex);
|
|
332 |
|
|
333 |
slot->iNext = TInt16(aBase);
|
|
334 |
slot->iPrev = TInt16(prevIndex);
|
|
335 |
prev->iNext = TInt16(aSlotIndex);
|
|
336 |
base->iPrev = TInt16(aSlotIndex);
|
|
337 |
|
|
338 |
if (iModCount<255)
|
|
339 |
{
|
|
340 |
if (aBase>=0)
|
|
341 |
{
|
|
342 |
MarkModified(aBase);
|
|
343 |
}
|
|
344 |
if (aSlotIndex>=0)
|
|
345 |
{
|
|
346 |
MarkModified(aSlotIndex);
|
|
347 |
}
|
|
348 |
if (prevIndex>=0)
|
|
349 |
{
|
|
350 |
MarkModified(prevIndex);
|
|
351 |
}
|
|
352 |
}
|
|
353 |
} // RObjectIx::AddBefore
|
|
354 |
|
|
355 |
|
|
356 |
// requires write lock
|
|
357 |
void RObjectIx::AddAfter(TInt aBase, TInt aSlotIndex)
|
|
358 |
{
|
|
359 |
assertd(TUint(aSlotIndex)<TUint(iAllocated));
|
|
360 |
|
|
361 |
SSlotQLink* base = Link(aBase);
|
|
362 |
SSlotQLink* slot = Link(aSlotIndex);
|
|
363 |
TInt nextIndex = base->iNext;
|
|
364 |
SSlotQLink* next = Link(nextIndex);
|
|
365 |
|
|
366 |
slot->iNext = TInt16(nextIndex);
|
|
367 |
slot->iPrev = TInt16(aBase);
|
|
368 |
next->iPrev = TInt16(aSlotIndex);
|
|
369 |
base->iNext = TInt16(aSlotIndex);
|
|
370 |
|
|
371 |
if (iModCount<255)
|
|
372 |
{
|
|
373 |
if (aBase>=0)
|
|
374 |
{
|
|
375 |
MarkModified(aBase);
|
|
376 |
}
|
|
377 |
if (aSlotIndex>=0)
|
|
378 |
{
|
|
379 |
MarkModified(aSlotIndex);
|
|
380 |
}
|
|
381 |
if (nextIndex>=0)
|
|
382 |
{
|
|
383 |
MarkModified(nextIndex);
|
|
384 |
}
|
|
385 |
}
|
|
386 |
} // RObjectIx::AddAfter
|
|
387 |
|
|
388 |
|
|
389 |
// requires write lock
|
|
390 |
inline void RObjectIx::AddHead(TInt aQueue, TInt aSlotIndex)
|
|
391 |
{
|
|
392 |
assertd(TUint(aSlotIndex)<TUint(iAllocated));
|
|
393 |
assertd(TUint(aQueue+ENumFreeQ)<TUint(ENumFreeQ));
|
|
394 |
|
|
395 |
AddAfter(aQueue, aSlotIndex);
|
|
396 |
} // RObjectIx::AddHead
|
|
397 |
|
|
398 |
|
|
399 |
// requires write lock
|
|
400 |
inline void RObjectIx::AddTail(TInt aQueue, TInt aSlotIndex)
|
|
401 |
{
|
|
402 |
assertd(TUint(aSlotIndex)<TUint(iAllocated));
|
|
403 |
assertd(TUint(aQueue+ENumFreeQ)<TUint(ENumFreeQ));
|
|
404 |
|
|
405 |
AddBefore(aQueue, aSlotIndex);
|
|
406 |
} // RObjectIx::AddTail
|
|
407 |
|
|
408 |
|
|
409 |
// Take all entries from aSrcQ and append them to aDestQ (i.e. add to end)
|
|
410 |
// Empty aSrcQ
|
|
411 |
// requires write lock
|
|
412 |
void RObjectIx::AppendList(TInt aSrcQ, TInt aDestQ)
|
|
413 |
{
|
|
414 |
assertd(TUint(aSrcQ+ENumFreeQ)<TUint(ENumFreeQ));
|
|
415 |
assertd(TUint(aDestQ+ENumFreeQ)<TUint(ENumFreeQ));
|
|
416 |
|
|
417 |
SSlotQLink* sq = Link(aSrcQ);
|
|
418 |
SSlotQLink* dq = Link(aDestQ);
|
|
419 |
|
|
420 |
if (sq->iNext >= 0) // if source queue already empty, nothing to do
|
|
421 |
{
|
|
422 |
SSlotQLink* oldLast = Link(dq->iPrev);
|
|
423 |
SSlotQLink* firstNew = Link(sq->iNext);
|
|
424 |
SSlotQLink* newLast = Link(sq->iPrev);
|
|
425 |
oldLast->iNext = (TInt16)sq->iNext;
|
|
426 |
firstNew->iPrev = (TInt16)dq->iPrev;
|
|
427 |
dq->iPrev = (TInt16)sq->iPrev;
|
|
428 |
newLast->iNext = (TInt16)aDestQ;
|
|
429 |
sq->iNext = (TInt16)aSrcQ;
|
|
430 |
sq->iPrev = (TInt16)aSrcQ;
|
|
431 |
}
|
|
432 |
} // RObjectIx::AppendList
|
|
433 |
|
|
434 |
|
|
435 |
// Take all entries from aSrcQ and prepend them to aDestQ (i.e. add to front)
|
|
436 |
// Empty aSrcQ
|
|
437 |
// requires write lock
|
|
438 |
void RObjectIx::PrependList(TInt aSrcQ, TInt aDestQ)
|
|
439 |
{
|
|
440 |
assertd(TUint(aSrcQ+ENumFreeQ)<TUint(ENumFreeQ));
|
|
441 |
assertd(TUint(aDestQ+ENumFreeQ)<TUint(ENumFreeQ));
|
|
442 |
|
|
443 |
SSlotQLink* sq = Link(aSrcQ);
|
|
444 |
SSlotQLink* dq = Link(aDestQ);
|
|
445 |
|
|
446 |
if (sq->iNext >= 0) // if source queue already empty, nothing to do
|
|
447 |
{
|
|
448 |
SSlotQLink* oldFirst = Link(dq->iNext);
|
|
449 |
SSlotQLink* lastNew = Link(sq->iPrev);
|
|
450 |
SSlotQLink* newFirst = Link(sq->iNext);
|
|
451 |
|
|
452 |
oldFirst->iPrev = (TInt16)sq->iPrev;
|
|
453 |
lastNew->iNext = (TInt16)dq->iNext;
|
|
454 |
dq->iNext = (TInt16)sq->iNext;
|
|
455 |
newFirst->iPrev = (TInt16)aDestQ;
|
|
456 |
sq->iNext = (TInt16)aSrcQ;
|
|
457 |
sq->iPrev = (TInt16)aSrcQ;
|
|
458 |
}
|
|
459 |
} // RObjectIx::PrependList
|
|
460 |
|
|
461 |
|
|
462 |
void RObjectIx::MarkModified(TInt aSlotIndex)
|
|
463 |
{
|
|
464 |
if (iModCount < EModCount)
|
|
465 |
{
|
|
466 |
iModList.iIndex[iModCount++] = (TInt16)aSlotIndex;
|
|
467 |
return;
|
|
468 |
}
|
|
469 |
|
|
470 |
if (iModCount==EModCount)
|
|
471 |
{
|
|
472 |
// switch to bitmap
|
|
473 |
SModList modList(iModList);
|
|
474 |
memclr(&iModList, sizeof(iModList));
|
|
475 |
TInt i;
|
|
476 |
|
|
477 |
for (i=0; i<EModCount; ++i)
|
|
478 |
{
|
|
479 |
TInt ix = modList.iIndex[i];
|
|
480 |
ix >>= iModListShift;
|
|
481 |
iModList.iBitMap[ix>>5] |= (1u<<(ix&31));
|
|
482 |
}
|
|
483 |
iModCount = TUint8(EModCount+1);
|
|
484 |
}
|
|
485 |
|
|
486 |
aSlotIndex >>= iModListShift;
|
|
487 |
iModList.iBitMap[aSlotIndex>>5] |= (1u<<(aSlotIndex & 31));
|
|
488 |
} // RObjectIx::MarkModified
|
|
489 |
|
|
490 |
|
|
491 |
/**
|
|
492 |
* Add a handle to the index
|
|
493 |
* Use a free or reserved slot, don't grow the array
|
|
494 |
*
|
|
495 |
* @param aObj Object to make a handle to
|
|
496 |
* @param aAttr Attributes for handle
|
|
497 |
* If RObjectIx::EReserved is set, use a reserved slot else use a free slot
|
|
498 |
* @param aSlot If non-null, receives data to be written to array
|
|
499 |
* @return The handle value (always positive) if operation successful
|
|
500 |
* KErrNoMemory if no free or reserved slot was available
|
|
501 |
* @pre Interrupts enabled
|
|
502 |
*/
|
|
503 |
TInt RObjectIx::DoAdd(DObject* aObj, TUint32 aAttr, SSlot* aSlot)
|
|
504 |
{
|
|
505 |
TBool rsvd = aAttr & EReserved;
|
|
506 |
TInt fqn = rsvd ? EQRsvd : EQFree; // free queue number
|
|
507 |
SSlot* slot = aSlot;
|
|
508 |
SSlotQLink* anchor = Link(fqn);
|
|
509 |
TUint32 in = GetNextInstanceValue();
|
|
510 |
TUint32 ot = aObj->UniqueID() & 0x3F;
|
|
511 |
TInt r = KErrNoMemory;
|
|
512 |
|
|
513 |
AcquireWriteLock();
|
|
514 |
if (iState==ETerminated)
|
|
515 |
{
|
|
516 |
ReleaseWriteLock();
|
|
517 |
return KErrDied;
|
|
518 |
}
|
|
519 |
|
|
520 |
TInt firstfree = anchor->iNext;
|
|
521 |
if (firstfree < 0)
|
|
522 |
{
|
|
523 |
++anchor, ++fqn, firstfree = anchor->iNext; // if main queue empty try alternate queue
|
|
524 |
}
|
|
525 |
|
|
526 |
if (firstfree >= 0)
|
|
527 |
{
|
|
528 |
// a slot is available
|
|
529 |
// dequeue it from the free/reserved list
|
|
530 |
slot = Dequeue(firstfree);
|
|
531 |
|
|
532 |
// update the HWM if necessary
|
|
533 |
if (firstfree >= iCount)
|
|
534 |
{
|
|
535 |
iCount = firstfree + 1;
|
|
536 |
}
|
|
537 |
if ((iState==ETidying || iState==EFindingLast) && firstfree>=iModList.iMonitor.iBoundary)
|
|
538 |
{
|
|
539 |
iModList.iMonitor.iBoundary = firstfree;
|
|
540 |
}
|
|
541 |
else if (iState==ECounting && aObj==iModList.iMonitor.iObj && firstfree<iModList.iMonitor.iBoundary)
|
|
542 |
{
|
|
543 |
++iModList.iMonitor.iResult;
|
|
544 |
}
|
|
545 |
else if (iState==ESearching && aObj==iModList.iMonitor.iObj && firstfree<iModList.iMonitor.iBoundary)
|
|
546 |
{
|
|
547 |
iModList.iMonitor.iBoundary = firstfree;
|
|
548 |
}
|
|
549 |
|
|
550 |
// update the active count
|
|
551 |
++iActiveCount;
|
|
552 |
|
|
553 |
// update the free reserved count if necessary
|
|
554 |
if (rsvd)
|
|
555 |
{
|
|
556 |
--iReservedFree;
|
|
557 |
}
|
|
558 |
|
|
559 |
// synthesise the handle
|
|
560 |
r = MakeHandle(firstfree, in);
|
|
561 |
}
|
|
562 |
|
|
563 |
if (slot)
|
|
564 |
{
|
|
565 |
// populate the slot or the temporary storage
|
|
566 |
slot->iUsed.iAttr = in | (ot<<14) | (aAttr<<20);
|
|
567 |
slot->iUsed.iObjR = TUint32(aObj) | (aAttr>>31);
|
|
568 |
}
|
|
569 |
ReleaseWriteLock();
|
|
570 |
|
|
571 |
return r;
|
|
572 |
} // RObjectIx::DoAdd
|
|
573 |
|
|
574 |
|
|
575 |
/**
|
|
576 |
* Remove a handle from the index
|
|
577 |
* Mark the slot free or reserved but don't shrink the array
|
|
578 |
*
|
|
579 |
* @param aHandle The handle to remove
|
|
580 |
* @param aObj Receives a pointer to the DObject referred to by the handle
|
|
581 |
* @param aAttr Receives the attributes of the removed handle
|
|
582 |
* If RObjectIx::EReserved is set, it was a reserved handle
|
|
583 |
* @return 0 if the handle was removed successfully
|
|
584 |
* 1 if the handle was removed successfully and the amortize count changed from 1 to 0
|
|
585 |
* KErrBadHandle if the handle did not exist
|
|
586 |
* @pre Interrupts enabled
|
|
587 |
*/
|
|
588 |
TInt RObjectIx::DoRemove(TInt aHandle, DObject*& aObj, TUint32& aAttr)
|
|
589 |
{
|
|
590 |
TInt ix = HandleIndex(aHandle);
|
|
591 |
TInt in = HandleInstance(aHandle);
|
|
592 |
SSlot* slot = 0;
|
|
593 |
TUint32 attr = 0;
|
|
594 |
TUint32 objr = 0;
|
|
595 |
|
|
596 |
AcquireWriteLock();
|
|
597 |
if (iState==ETerminated)
|
|
598 |
{
|
|
599 |
ReleaseWriteLock();
|
|
600 |
return KErrDied;
|
|
601 |
}
|
|
602 |
|
|
603 |
if (ix < iCount)
|
|
604 |
{
|
|
605 |
slot = iSlots + ix;
|
|
606 |
attr = slot->iUsed.iAttr;
|
|
607 |
objr = slot->iUsed.iObjR;
|
|
608 |
}
|
|
609 |
|
|
610 |
if (objr<EObjROccupied || ((attr&0x3FFF)!=(TUint)in)) // slot empty or instance value doesn't match
|
|
611 |
{
|
|
612 |
ReleaseWriteLock();
|
|
613 |
aObj = 0;
|
|
614 |
aAttr = 0;
|
|
615 |
return KErrBadHandle;
|
|
616 |
}
|
|
617 |
|
|
618 |
aObj = (DObject*)(objr & EObjRObjMask);
|
|
619 |
TUint32 xattr = (objr<<31)|(attr>>20);
|
|
620 |
aAttr = xattr;
|
|
621 |
|
|
622 |
slot->iFree.iRsvd &= EObjRRsvd; // mark slot as free
|
|
623 |
|
|
624 |
--iActiveCount; // one less valid handle
|
|
625 |
|
|
626 |
// help someone searching the whole index if necessary
|
|
627 |
if (iState==ECounting && aObj==iModList.iMonitor.iObj && ix<iModList.iMonitor.iBoundary)
|
|
628 |
{
|
|
629 |
--iModList.iMonitor.iResult;
|
|
630 |
}
|
|
631 |
|
|
632 |
// update the free reserved count if necessary
|
|
633 |
if (xattr & EReserved)
|
|
634 |
{
|
|
635 |
++iReservedFree;
|
|
636 |
// if TidyAndCompact() is running concurrently, we should only update
|
|
637 |
// iReservedFreeHWM if the freed slot has already been scanned
|
|
638 |
if (iState != ETidying || ix < iModList.iMonitor.iResult)
|
|
639 |
{
|
|
640 |
if (ix >= iReservedFreeHWM)
|
|
641 |
{
|
|
642 |
iReservedFreeHWM = ix + 1;
|
|
643 |
assertd(iReservedFreeHWM <= iCount);
|
|
644 |
}
|
|
645 |
}
|
|
646 |
// put the slot at the front of the corresponding free queue
|
|
647 |
AddHead(EQRsvd, ix);
|
|
648 |
ReleaseWriteLock();
|
|
649 |
return KErrNone;
|
|
650 |
}
|
|
651 |
|
|
652 |
// put the slot at the front of the corresponding free queue
|
|
653 |
AddHead(EQFree, ix);
|
|
654 |
|
|
655 |
// Reduce amortize count if necessary.
|
|
656 |
// Signal compaction if amortize count reaches zero or index is entirely
|
|
657 |
// empty (i.e. no occupied or reserved entries), but not if we just freed
|
|
658 |
// a reserved entry.
|
|
659 |
TInt r = 0;
|
|
660 |
if (iAmortize>0)
|
|
661 |
{
|
|
662 |
if (iAmortize>1) // don't want to tidy/compact on freeing a reserved handle
|
|
663 |
{
|
|
664 |
--iAmortize;
|
|
665 |
}
|
|
666 |
|
|
667 |
if (iAmortize==0 || (iActiveCount==0 && iReservedTotal==0))
|
|
668 |
{
|
|
669 |
r = 1;
|
|
670 |
}
|
|
671 |
}
|
|
672 |
ReleaseWriteLock();
|
|
673 |
|
|
674 |
return r;
|
|
675 |
} // RObjectIx::DoRemove
|
|
676 |
|
|
677 |
|
|
678 |
/**
|
|
679 |
* Adds a kernel object to an index and return a handle.
|
|
680 |
*
|
|
681 |
* @param aObj Pointer to the object to add
|
|
682 |
* @param aAttr Attributes for handle
|
|
683 |
* If RObjectIx::EReserved is set, a reserved slot will be used
|
|
684 |
*
|
|
685 |
* @return Positive handle value if operation successful;
|
|
686 |
* KErrNoMemory, if there was insufficient memory to expand the array
|
|
687 |
* or if there was no reserved slot available.
|
|
688 |
* KErrDied if the index has been closed
|
|
689 |
*
|
|
690 |
* @pre Calling thread must be in a critical section.
|
|
691 |
* @pre No fast mutex can be held.
|
|
692 |
* @pre Call in a thread context.
|
|
693 |
*/
|
|
694 |
TInt RObjectIx::Add(DObject* aObj, TUint32 aAttr)
|
|
695 |
{
|
|
696 |
TBool rsvd = aAttr & EReserved;
|
|
697 |
TInt r = DoAdd(aObj, aAttr, 0);
|
|
698 |
if (r>=0 || r==KErrDied || rsvd)
|
|
699 |
return r;
|
|
700 |
|
|
701 |
Wait(); // acquire heavyweight mutex
|
|
702 |
SSlot slotData;
|
|
703 |
r = DoAdd(aObj, aAttr, &slotData); // try to add again
|
|
704 |
if (r==KErrNoMemory)
|
|
705 |
{
|
|
706 |
r = Grow(0, &slotData); // grow array and insert entry
|
|
707 |
}
|
|
708 |
Signal();
|
|
709 |
return r;
|
|
710 |
} // RObjectIx::Add
|
|
711 |
|
|
712 |
|
|
713 |
/**
|
|
714 |
* Increase the number of reserved slots
|
|
715 |
*
|
|
716 |
* @param aCount Number of additional reserved slots to allocate
|
|
717 |
* @return KErrNone if operation successful
|
|
718 |
* KErrNoMemory if additional slots could not be allocated
|
|
719 |
* @pre HandleMutex held
|
|
720 |
*/
|
|
721 |
TInt RObjectIx::ReserveSlots(TInt aCount)
|
|
722 |
{
|
|
723 |
__ASSERT_MUTEX(HandleMutex);
|
|
724 |
|
|
725 |
TInt r = KErrNone;
|
|
726 |
TInt remain = aCount;
|
|
727 |
while (remain > 0)
|
|
728 |
{
|
|
729 |
TInt got = 0;
|
|
730 |
AcquireWriteLock();
|
|
731 |
TInt fqn = EQFree; // free queue number
|
|
732 |
SSlot* slot = 0;
|
|
733 |
SSlotQLink* anchor = Link(fqn);
|
|
734 |
TInt firstfree = anchor->iNext;
|
|
735 |
if (firstfree < 0)
|
|
736 |
{
|
|
737 |
++anchor, ++fqn, firstfree = anchor->iNext; // if main queue empty try alternate queue
|
|
738 |
}
|
|
739 |
|
|
740 |
if (firstfree >= 0)
|
|
741 |
{
|
|
742 |
// a slot is available
|
|
743 |
// move it from the free to the reserved list
|
|
744 |
slot = Dequeue(firstfree);
|
|
745 |
slot->iFree.iRsvd = EObjRRsvd;
|
|
746 |
AddHead(EQRsvd, firstfree);
|
|
747 |
|
|
748 |
// update HWMs/counts if necessary
|
|
749 |
// can't happen concurrently with TidyAndCompact()
|
|
750 |
if (firstfree >= iCount)
|
|
751 |
{
|
|
752 |
iCount = firstfree + 1;
|
|
753 |
}
|
|
754 |
if (firstfree >= iReservedFreeHWM)
|
|
755 |
{
|
|
756 |
iReservedFreeHWM = firstfree + 1;
|
|
757 |
assertd (iReservedFreeHWM <= iCount);
|
|
758 |
}
|
|
759 |
++iReservedFree;
|
|
760 |
++iReservedTotal;
|
|
761 |
got = 1;
|
|
762 |
}
|
|
763 |
ReleaseWriteLock();
|
|
764 |
if (!got)
|
|
765 |
{
|
|
766 |
break; // no more free slots to pinch
|
|
767 |
}
|
|
768 |
remain -= got;
|
|
769 |
}
|
|
770 |
if (remain == 0)
|
|
771 |
{
|
|
772 |
return r; // got them all from the free list
|
|
773 |
}
|
|
774 |
|
|
775 |
// else try to expand the array to get the others
|
|
776 |
r = Grow(remain, 0);
|
|
777 |
if (r != KErrNone)
|
|
778 |
{
|
|
779 |
// out of memory - unreserve any slots we reserved beforehand
|
|
780 |
if (aCount > remain)
|
|
781 |
{
|
|
782 |
UnReserveSlots(aCount-remain, EFalse);
|
|
783 |
}
|
|
784 |
}
|
|
785 |
return r;
|
|
786 |
} // RObjectIx::ReserveSlots
|
|
787 |
|
|
788 |
|
|
789 |
/**
|
|
790 |
* Unreserve a number of reserved slots
|
|
791 |
*
|
|
792 |
* @param aCount Number of reserved slots to unreserve
|
|
793 |
* @return 1 if the array should be compacted afterwards
|
|
794 |
* 0 if not
|
|
795 |
* @pre HandleMutex held
|
|
796 |
*/
|
|
797 |
TInt RObjectIx::UnReserveSlots(TInt aCount, TBool aAmortize)
|
|
798 |
{
|
|
799 |
__ASSERT_MUTEX(HandleMutex);
|
|
800 |
|
|
801 |
TInt r = 0;
|
|
802 |
TInt remain = aCount;
|
|
803 |
while (remain>0)
|
|
804 |
{
|
|
805 |
AcquireWriteLock();
|
|
806 |
|
|
807 |
TInt pos = iReservedFreeHWM;
|
|
808 |
|
|
809 |
// pos=0 if reserved slots have gone missing
|
|
810 |
// pos<0 or pos>iCount -> corrupt
|
|
811 |
asserta(pos>0 && pos<=iCount);
|
|
812 |
|
|
813 |
TInt limit = pos<EMaxLockedIter ? 0 : pos-EMaxLockedIter; // last pos to test before releasing spinlock
|
|
814 |
--pos;
|
|
815 |
|
|
816 |
SSlot* slot = iSlots + pos;
|
|
817 |
for (; pos>=limit && !IsFreeReserved(slot); --pos, --slot)
|
|
818 |
{
|
|
819 |
}
|
|
820 |
|
|
821 |
if (pos>=limit)
|
|
822 |
{
|
|
823 |
// slot is reserved and unoccupied so unreserve it
|
|
824 |
// can't happen concurrently with TidyAndCompact()
|
|
825 |
slot->iFree.iRsvd = 0;
|
|
826 |
--iReservedFree;
|
|
827 |
--iReservedTotal;
|
|
828 |
Dequeue(pos);
|
|
829 |
AddHead(EQAltFree, pos); // put on front of alternate free queue
|
|
830 |
iReservedFreeHWM = pos;
|
|
831 |
assertd(iReservedFreeHWM <= iCount);
|
|
832 |
--remain;
|
|
833 |
}
|
|
834 |
else
|
|
835 |
{
|
|
836 |
iReservedFreeHWM = pos + 1; // haven't examined pos yet
|
|
837 |
assertd(iReservedFreeHWM <= iCount);
|
|
838 |
}
|
|
839 |
ReleaseWriteLock();
|
|
840 |
}
|
|
841 |
AcquireWriteLock();
|
|
842 |
if (aAmortize && iAmortize>0)
|
|
843 |
{
|
|
844 |
if (iAmortize <= aCount)
|
|
845 |
{
|
|
846 |
iAmortize = 0, r = 1;
|
|
847 |
}
|
|
848 |
else
|
|
849 |
{
|
|
850 |
iAmortize -= aCount;
|
|
851 |
}
|
|
852 |
}
|
|
853 |
|
|
854 |
// move alternate free queue to front of normal free queue
|
|
855 |
PrependList(EQAltFree, EQFree);
|
|
856 |
ReleaseWriteLock();
|
|
857 |
|
|
858 |
return r;
|
|
859 |
} // RObjectIx::UnReserveSlots
|
|
860 |
|
|
861 |
|
|
862 |
/**
|
|
863 |
* Change the number of reserved slots
|
|
864 |
*
|
|
865 |
* @param aCount If positive, number of additional reserved slots to allocate
|
|
866 |
* If negative, -number of reserved slots to unreserve
|
|
867 |
* @return KErrNone if operation successful
|
|
868 |
* KErrNoMemory if additional slots could not be allocated
|
|
869 |
*/
|
|
870 |
TInt RObjectIx::Reserve(TInt aCount)
|
|
871 |
{
|
|
872 |
if (aCount==0)
|
|
873 |
{
|
|
874 |
return KErrNone;
|
|
875 |
}
|
|
876 |
|
|
877 |
Wait();
|
|
878 |
TInt r = KErrNone;
|
|
879 |
|
|
880 |
if (iState==ETerminated)
|
|
881 |
{
|
|
882 |
r = KErrDied;
|
|
883 |
}
|
|
884 |
else if (aCount>0)
|
|
885 |
{
|
|
886 |
r = ReserveSlots(aCount);
|
|
887 |
}
|
|
888 |
else if (UnReserveSlots(-aCount, ETrue))
|
|
889 |
{
|
|
890 |
TidyAndCompact();
|
|
891 |
}
|
|
892 |
|
|
893 |
Signal();
|
|
894 |
|
|
895 |
return r;
|
|
896 |
} // RObjectIx::Reserve
|
|
897 |
|
|
898 |
|
|
899 |
TInt RObjectIx::Remove(TInt aHandle, DObject*& aObj, TUint32& aAttr)
|
|
900 |
{
|
|
901 |
TInt r = DoRemove(aHandle, aObj, aAttr);
|
|
902 |
if (r<=0)
|
|
903 |
{
|
|
904 |
return r;
|
|
905 |
}
|
|
906 |
|
|
907 |
Wait();
|
|
908 |
if (iState==ETerminated)
|
|
909 |
{
|
|
910 |
r = KErrDied;
|
|
911 |
}
|
|
912 |
else
|
|
913 |
{
|
|
914 |
TidyAndCompact();
|
|
915 |
r = KErrNone;
|
|
916 |
}
|
|
917 |
Signal();
|
|
918 |
|
|
919 |
return r;
|
|
920 |
} // RObjectIx::Remove
|
|
921 |
|
|
922 |
|
|
923 |
/**
|
|
924 |
* Grow the array either to add a new handle or to add some more
|
|
925 |
* reserved entries.
|
|
926 |
*
|
|
927 |
* @param aReserve Number of reserved slots to add
|
|
928 |
* @param aSlotData Pointer to data to be placed in first new slot
|
|
929 |
* @return KErrNone if aSlotData==NULL and operation successful
|
|
930 |
* A positive handle value if aSlotData!=NULL and operation successful
|
|
931 |
* KErrNoMemory if insufficient memory available to expand the array or
|
|
932 |
* if the array is already at the maximum possible size.
|
|
933 |
* @pre HandleMutex held
|
|
934 |
*/
|
|
935 |
TInt RObjectIx::Grow(TInt aReserve, SSlot* aSlotData)
|
|
936 |
{
|
|
937 |
__ASSERT_MUTEX(HandleMutex);
|
|
938 |
|
|
939 |
if (iAllocated == EMaxSlots)
|
|
940 |
{
|
|
941 |
return KErrNoMemory;
|
|
942 |
}
|
|
943 |
|
|
944 |
TInt normal = aSlotData ? 1 : 0;
|
|
945 |
TInt newAlloc = iAllocated + aReserve + normal;
|
|
946 |
if (newAlloc > EMaxSlots)
|
|
947 |
{
|
|
948 |
return KErrNoMemory;
|
|
949 |
}
|
|
950 |
if (newAlloc < EMinSlots)
|
|
951 |
{
|
|
952 |
newAlloc = EMinSlots;
|
|
953 |
}
|
|
954 |
--newAlloc;
|
|
955 |
newAlloc |= (newAlloc>>1);
|
|
956 |
newAlloc |= (newAlloc>>2);
|
|
957 |
newAlloc |= (newAlloc>>4);
|
|
958 |
newAlloc |= (newAlloc>>8);
|
|
959 |
++newAlloc; // round up newAlloc to next power of 2
|
|
960 |
SSlot* newSlots = (SSlot*)Kern::Alloc(newAlloc * sizeof(SSlot)); // zero initialized memory
|
|
961 |
if (!newSlots)
|
|
962 |
{
|
|
963 |
return KErrNoMemory;
|
|
964 |
}
|
|
965 |
TInt r = KErrNone;
|
|
966 |
|
|
967 |
Empty(EQTempFree); // new normal free slots will go here
|
|
968 |
Empty(EQTempRsvd); // new reserved slots will go here
|
|
969 |
|
|
970 |
// initialize all the new slots
|
|
971 |
SSlotQLink* tf = Link(EQTempFree);
|
|
972 |
SSlotQLink* tr = Link(EQTempRsvd);
|
|
973 |
TInt first = iAllocated;
|
|
974 |
TInt last = first + aReserve - 1;
|
|
975 |
TInt ix = first;
|
|
976 |
if (aReserve>0)
|
|
977 |
{
|
|
978 |
for (; ix<=last; ++ix)
|
|
979 |
{
|
|
980 |
SSlot* slot = newSlots + ix;
|
|
981 |
slot->iFree.iRsvd = EObjRRsvd;
|
|
982 |
slot->iFree.iPrev = (TInt16)(ix-1);
|
|
983 |
slot->iFree.iNext = (TInt16)(ix+1);
|
|
984 |
}
|
|
985 |
newSlots[first].iFree.iPrev = (TInt16)EQTempRsvd;
|
|
986 |
newSlots[last].iFree.iNext = (TInt16)EQTempRsvd;
|
|
987 |
tr->iNext = (TInt16)first;
|
|
988 |
tr->iPrev = (TInt16)last;
|
|
989 |
}
|
|
990 |
if (normal)
|
|
991 |
{
|
|
992 |
SSlot* slot = newSlots + ix;
|
|
993 |
*slot = *aSlotData;
|
|
994 |
r = MakeHandle(ix, slot->iUsed.iAttr);
|
|
995 |
++ix;
|
|
996 |
}
|
|
997 |
first = ix;
|
|
998 |
last = newAlloc - 1;
|
|
999 |
if (first<=last)
|
|
1000 |
{
|
|
1001 |
for (; ix<=last; ++ix)
|
|
1002 |
{
|
|
1003 |
SSlot* slot = newSlots + ix;
|
|
1004 |
slot->iFree.iRsvd = 0;
|
|
1005 |
slot->iFree.iPrev = (TInt16)(ix-1);
|
|
1006 |
slot->iFree.iNext = (TInt16)(ix+1);
|
|
1007 |
}
|
|
1008 |
newSlots[first].iFree.iPrev = (TInt16)EQTempFree;
|
|
1009 |
newSlots[last].iFree.iNext = (TInt16)EQTempFree;
|
|
1010 |
tf->iNext = (TInt16)first;
|
|
1011 |
tf->iPrev = (TInt16)last;
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
// copy the existing slot contents
|
|
1015 |
AcquireWriteLock();
|
|
1016 |
if (iAllocated==0)
|
|
1017 |
{
|
|
1018 |
goto first_alloc; // no existing slots, so skip this section
|
|
1019 |
}
|
|
1020 |
__DEBUG_TRANSFER(iSlots, newSlots); // so kernel heap checking doesn't barf on a realloc'd cell
|
|
1021 |
memclr(&iModList, sizeof(iModList));
|
|
1022 |
iState = (TUint8)ENormal;
|
|
1023 |
iModCount = 0; // any concurrent modifications will now be recorded
|
|
1024 |
ReleaseWriteLock();
|
|
1025 |
wordmove(newSlots, iSlots, iAllocated*sizeof(SSlot));
|
|
1026 |
FOREVER
|
|
1027 |
{
|
|
1028 |
AcquireWriteLock();
|
|
1029 |
if (iModCount==0)
|
|
1030 |
{
|
|
1031 |
break; // there were no concurrent modifications, so finish up
|
|
1032 |
}
|
|
1033 |
// get list of modifications and reset it
|
|
1034 |
TInt modCount = iModCount;
|
|
1035 |
SModList modList = iModList;
|
|
1036 |
memclr(&iModList, sizeof(iModList));
|
|
1037 |
iModCount = 0; // any concurrent modifications will now be recorded
|
|
1038 |
ReleaseWriteLock();
|
|
1039 |
if (modCount <= EModCount)
|
|
1040 |
{
|
|
1041 |
// list of particular modified entries
|
|
1042 |
TInt i;
|
|
1043 |
for (i=0; i<modCount; ++i)
|
|
1044 |
{
|
|
1045 |
TInt ix = modList.iIndex[i];
|
|
1046 |
newSlots[ix] = iSlots[ix];
|
|
1047 |
}
|
|
1048 |
continue;
|
|
1049 |
}
|
|
1050 |
// bitmap of groups of modified entries
|
|
1051 |
TInt i;
|
|
1052 |
TInt nbits = 0;
|
|
1053 |
for (i=0; i<EBitMapSize/32; ++i)
|
|
1054 |
{
|
|
1055 |
nbits += __e32_bit_count_32(modList.iBitMap[i]);
|
|
1056 |
}
|
|
1057 |
while (nbits--)
|
|
1058 |
{
|
|
1059 |
for (i=0; modList.iBitMap[i]==0; ++i)
|
|
1060 |
{
|
|
1061 |
}
|
|
1062 |
TInt ix = __e32_find_ls1_32(modList.iBitMap[i]);
|
|
1063 |
modList.iBitMap[i] &= ~(1u<<ix);
|
|
1064 |
ix += (i<<5);
|
|
1065 |
// copy affected block of slots
|
|
1066 |
wordmove(newSlots + (ix<<iModListShift), iSlots + (ix<<iModListShift), sizeof(SSlot)<<iModListShift);
|
|
1067 |
}
|
|
1068 |
}
|
|
1069 |
// exit loop with write lock held, no outstanding modifications to copy
|
|
1070 |
first_alloc:
|
|
1071 |
SSlot* oldSlots = iSlots; // remember old array so it can be freed
|
|
1072 |
iSlots = newSlots; // swing pointer to new larger array
|
|
1073 |
iCount = iAllocated + aReserve + normal; // HWM = one above new reserved and/or occupied slots
|
|
1074 |
iActiveCount += normal; // increment if we added a new occupied slot
|
|
1075 |
iReservedFree += aReserve; // add new reserved slots
|
|
1076 |
iReservedTotal += aReserve;
|
|
1077 |
if (aReserve>0)
|
|
1078 |
{
|
|
1079 |
// can't happen concurrently with TidyAndCompact()
|
|
1080 |
iReservedFreeHWM = iAllocated + aReserve;
|
|
1081 |
assertd(iReservedFreeHWM <= iCount);
|
|
1082 |
}
|
|
1083 |
iAllocated = newAlloc; // new allocated array size
|
|
1084 |
if (newAlloc > EBitMapSize) // recalculate modification bitmap granularity
|
|
1085 |
{
|
|
1086 |
iModListShift = (TUint8)__e32_find_ms1_32(newAlloc / EBitMapSize);
|
|
1087 |
}
|
|
1088 |
else
|
|
1089 |
{
|
|
1090 |
iModListShift = 0;
|
|
1091 |
}
|
|
1092 |
iState = (TUint8)ENormal;
|
|
1093 |
iModCount = 255; // stop tracking modifications
|
|
1094 |
AppendList(EQTempRsvd, EQRsvd); // append new reserved slots to reserved list
|
|
1095 |
AppendList(EQTempFree, EQFree); // append new free slots to free list
|
|
1096 |
if (iAmortize == 0)
|
|
1097 |
{
|
|
1098 |
iAmortize = iAllocated / 2; // number of Remove() before we tidy/compact
|
|
1099 |
}
|
|
1100 |
ReleaseWriteLock();
|
|
1101 |
Kern::Free(oldSlots); // free old array
|
|
1102 |
|
|
1103 |
return r; // return new handle or KErrNone
|
|
1104 |
} // RObjectIx::Grow
|
|
1105 |
|
|
1106 |
|
|
1107 |
/**
|
|
1108 |
* Tidy up the index and shrink the array if possible
|
|
1109 |
*
|
|
1110 |
* Reorder the free list and reserved list so that the reserved list consists of
|
|
1111 |
* the lowest index free slots and the free list is in ascending order of slot
|
|
1112 |
* index (so the lowest index slots are used first). Move the HWM down as much
|
|
1113 |
* as possible and, if it would reduce the size of the array by at least half,
|
|
1114 |
* shrink the array.
|
|
1115 |
*
|
|
1116 |
* @pre HandleMutex held
|
|
1117 |
*/
|
|
1118 |
void RObjectIx::TidyAndCompact()
|
|
1119 |
{
|
|
1120 |
__ASSERT_MUTEX(HandleMutex);
|
|
1121 |
|
|
1122 |
if (!iAllocated || iState==ETerminated)
|
|
1123 |
{
|
|
1124 |
return; // nothing to tidy or compact, or index dead
|
|
1125 |
}
|
|
1126 |
|
|
1127 |
SSlotQLink* fq = Link(EQFree);
|
|
1128 |
SSlotQLink* afq = Link(EQAltFree);
|
|
1129 |
SSlotQLink* arq = Link(EQAltRsvd);
|
|
1130 |
|
|
1131 |
// This function is the only one which adds items to the AltFree or AltRsvd
|
|
1132 |
// queues, so they should be empty at this point.
|
|
1133 |
asserta(afq->iNext == EQAltFree);
|
|
1134 |
asserta(afq->iPrev == EQAltFree);
|
|
1135 |
asserta(arq->iNext == EQAltRsvd);
|
|
1136 |
asserta(arq->iPrev == EQAltRsvd);
|
|
1137 |
|
|
1138 |
AcquireWriteLock();
|
|
1139 |
if (iActiveCount==0 && iReservedTotal==0)
|
|
1140 |
{
|
|
1141 |
// no occupied or reserved slots, so just reset the index to the initial state
|
|
1142 |
SSlot* oldSlots = iSlots;
|
|
1143 |
iAllocated = 0;
|
|
1144 |
iCount = 0;
|
|
1145 |
iReservedFree = 0;
|
|
1146 |
iReservedFreeHWM = 0;
|
|
1147 |
iSlots = 0;
|
|
1148 |
iAmortize = 0;
|
|
1149 |
iState = (TUint8)ENormal;
|
|
1150 |
iModCount = 255;
|
|
1151 |
iModListShift = 0;
|
|
1152 |
iSpare1 = 0;
|
|
1153 |
memclr(&iModList, sizeof(iModList));
|
|
1154 |
TInt i;
|
|
1155 |
for (i=-ENumFreeQ; i<0; ++i)
|
|
1156 |
{
|
|
1157 |
Empty(i); // Empty lists
|
|
1158 |
}
|
|
1159 |
ReleaseWriteLock();
|
|
1160 |
Kern::Free(oldSlots);
|
|
1161 |
return;
|
|
1162 |
}
|
|
1163 |
iAmortize = -1; // don't need to check for compaction
|
|
1164 |
iModList.iMonitor.iBoundary = -1; // will change if any slots become occupied
|
|
1165 |
iModList.iMonitor.iResult = 0; // tracks next position to be scanned (to update iReservedFreeHWM)
|
|
1166 |
iModCount = 255;
|
|
1167 |
iState = (TUint8)ETidying;
|
|
1168 |
|
|
1169 |
iReservedFreeHWM = 0; // will be updated if any more reserved slots are freed while we are tidying
|
|
1170 |
// only relied on by UnReserveSlots(), which can't happen since we have the HandleMutex locked
|
|
1171 |
ReleaseWriteLock();
|
|
1172 |
TInt pos = 0;
|
|
1173 |
TInt hwm = -1; // tracks last occupied and/or reserved slot
|
|
1174 |
TInt rfhwm = 0; // tracks last free reserved slot
|
|
1175 |
TInt rsvd_remain = iReservedTotal; // this can't change since we have the HandleMutex locked
|
|
1176 |
FOREVER
|
|
1177 |
{
|
|
1178 |
TInt limit = pos + EMaxLockedIter;
|
|
1179 |
AcquireWriteLock();
|
|
1180 |
if (limit > iCount)
|
|
1181 |
{
|
|
1182 |
limit = iCount;
|
|
1183 |
}
|
|
1184 |
|
|
1185 |
SSlot* slot = iSlots + pos;
|
|
1186 |
// skip occupied slots
|
|
1187 |
for (; pos<limit && Occupant(slot); ++pos, ++slot)
|
|
1188 |
{
|
|
1189 |
assertd(hwm < pos);
|
|
1190 |
hwm = pos; // hwm tracks last occupied and/or reserved slot
|
|
1191 |
if (IsReserved(slot))
|
|
1192 |
{
|
|
1193 |
--rsvd_remain; // count off reserved slots as we see them
|
|
1194 |
}
|
|
1195 |
}
|
|
1196 |
if (pos==iCount)
|
|
1197 |
{
|
|
1198 |
break; // reached end of occupied and/or reserved entries
|
|
1199 |
}
|
|
1200 |
|
|
1201 |
if (pos<limit)
|
|
1202 |
{
|
|
1203 |
// found unoccupied slot
|
|
1204 |
if (IsReserved(slot))
|
|
1205 |
{
|
|
1206 |
// Reserved, unoccupied slot.
|
|
1207 |
// First take it off the free reserved queue.
|
|
1208 |
Dequeue(pos);
|
|
1209 |
|
|
1210 |
// Now see if there is anything on the AltFree queue.
|
|
1211 |
// If there is, it must be a free slot we have previously encountered and moved.
|
|
1212 |
// We interchange this slot with the first one on the AltFree queue
|
|
1213 |
// so that the reserved slots get moved as close as possible to the beginning
|
|
1214 |
// of the index - this makes it more likely that we can compact the index,
|
|
1215 |
// since we can't free the space used by reserved slots.
|
|
1216 |
TInt rsvpos = pos;
|
|
1217 |
TInt substitute = afq->iNext;
|
|
1218 |
if (substitute>=0)
|
|
1219 |
{
|
|
1220 |
assertd(substitute < pos);
|
|
1221 |
rsvpos = substitute;
|
|
1222 |
Dequeue(substitute); // take substitute slot off AltFree queue
|
|
1223 |
SSlot* substSlot = iSlots + substitute;
|
|
1224 |
substSlot->iFree.iRsvd = EObjRRsvd; // mark slot as reserved
|
|
1225 |
AddTail(EQAltRsvd, substitute); // move substitute slot to alternate reserved queue
|
|
1226 |
slot->iFree.iRsvd = 0; // mark original reserved slot as normal free
|
|
1227 |
AddTail(EQAltFree, pos); // move this slot to the the alternate free queue
|
|
1228 |
}
|
|
1229 |
else
|
|
1230 |
{
|
|
1231 |
// nothing to substitute with so leave this slot as reserved
|
|
1232 |
AddTail(EQAltRsvd, pos); // move it to the alternate reserved queue
|
|
1233 |
}
|
|
1234 |
if (rsvpos >= rfhwm)
|
|
1235 |
{
|
|
1236 |
rfhwm = rsvpos + 1; // remember last free reserved slot
|
|
1237 |
assertd(rfhwm <= iCount);
|
|
1238 |
}
|
|
1239 |
if (rsvpos > hwm)
|
|
1240 |
{
|
|
1241 |
hwm = rsvpos; // hwm tracks last occupied and/or reserved slot
|
|
1242 |
}
|
|
1243 |
|
|
1244 |
--rsvd_remain; // count off reserved slots as we see them
|
|
1245 |
++pos;
|
|
1246 |
}
|
|
1247 |
else
|
|
1248 |
{
|
|
1249 |
// unoccupied, non-reserved slot
|
|
1250 |
Dequeue(pos);
|
|
1251 |
AddTail(EQAltFree, pos); // move it to the alternate free queue
|
|
1252 |
++pos;
|
|
1253 |
}
|
|
1254 |
}
|
|
1255 |
iModList.iMonitor.iResult = pos; // tracks next position to be scanned (to update iReservedFreeHWM)
|
|
1256 |
ReleaseWriteLock();
|
|
1257 |
}
|
|
1258 |
|
|
1259 |
//
|
|
1260 |
// At this point we have been through all the occupied and/or reserved entries
|
|
1261 |
// The AltRsvd queue should contain an ordered list of free reserved slots
|
|
1262 |
// The AltFree queue should contain an ordered list of free normal slots
|
|
1263 |
// If there has been no concurrent activity, iModList.iMonitor.iBoundary == -1,
|
|
1264 |
// the Rsvd queue is empty and the Free queue holds only the entries between
|
|
1265 |
// iCount and iAllocated which have never been used. Otherwise,
|
|
1266 |
// iModList.iMonitor.iBoundary holds the highest index allocated during the scan
|
|
1267 |
// and the Free and/or Rsvd queues hold any slots freed after they were scanned.
|
|
1268 |
// In either case we can set iCount to Max(hwm, iModList.iMonitor.iBoundary)+1.
|
|
1269 |
//
|
|
1270 |
asserta(rsvd_remain == 0); // should have seen all reserved slots
|
|
1271 |
|
|
1272 |
TInt oldCount = iCount;
|
|
1273 |
iCount = Max(hwm, iModList.iMonitor.iBoundary) + 1;
|
|
1274 |
asserta(iCount <= oldCount);
|
|
1275 |
|
|
1276 |
// iReservedFreeHWM is set to the last free reserved slot we saw during the
|
|
1277 |
// scan or to the highest index one freed after we had scanned it
|
|
1278 |
if (rfhwm > iReservedFreeHWM)
|
|
1279 |
iReservedFreeHWM = rfhwm;
|
|
1280 |
asserta(iReservedFreeHWM <= iCount);
|
|
1281 |
|
|
1282 |
//
|
|
1283 |
// Move all unoccupied entries back to their usual queues...
|
|
1284 |
//
|
|
1285 |
PrependList(EQAltRsvd, EQRsvd);
|
|
1286 |
|
|
1287 |
// ok we want the random/newly freed entries at the head of the free queue,
|
|
1288 |
// followed by the sorted used entries
|
|
1289 |
// followed by the never used entries
|
|
1290 |
|
|
1291 |
TInt split = Max(oldCount, (iModList.iMonitor.iBoundary + 1));
|
|
1292 |
|
|
1293 |
if (split < iAllocated)
|
|
1294 |
{
|
|
1295 |
SSlot* firstUnused = iSlots + split;
|
|
1296 |
if (firstUnused->iFree.iPrev != (TInt16)EQFree)
|
|
1297 |
{
|
|
1298 |
// split, random entries will be at the head and unused entries at the end
|
|
1299 |
SSlotQLink* lastUsed = Link(firstUnused->iFree.iPrev);
|
|
1300 |
TInt lastUnused = fq->iPrev;
|
|
1301 |
|
|
1302 |
fq->iPrev = firstUnused->iFree.iPrev;
|
|
1303 |
lastUsed->iNext = (TInt16)EQFree;
|
|
1304 |
|
|
1305 |
// now added sorted used entries
|
|
1306 |
AppendList(EQAltFree, EQFree);
|
|
1307 |
|
|
1308 |
// now added never used entries
|
|
1309 |
SSlotQLink* last = Link(fq->iPrev);
|
|
1310 |
|
|
1311 |
last->iNext = (TInt16)split;
|
|
1312 |
firstUnused->iFree.iPrev = fq->iPrev;
|
|
1313 |
// note last unused should point to the QFree
|
|
1314 |
fq->iPrev = (TInt16)lastUnused;
|
|
1315 |
}
|
|
1316 |
else
|
|
1317 |
{
|
|
1318 |
// No entries freed before tidy, so put sorted 'used' entries at the head
|
|
1319 |
PrependList(EQAltFree, EQFree);
|
|
1320 |
}
|
|
1321 |
}
|
|
1322 |
else
|
|
1323 |
{
|
|
1324 |
// Free list only contains random entries
|
|
1325 |
// now added sorted used entries
|
|
1326 |
AppendList(EQAltFree, EQFree);
|
|
1327 |
}
|
|
1328 |
|
|
1329 |
// Work out if we can shrink the array
|
|
1330 |
TInt newAlloc = Max(iCount, EMinSlots) - 1;
|
|
1331 |
newAlloc |= (newAlloc>>1);
|
|
1332 |
newAlloc |= (newAlloc>>2);
|
|
1333 |
newAlloc |= (newAlloc>>4);
|
|
1334 |
newAlloc |= (newAlloc>>8);
|
|
1335 |
++newAlloc; // round up newAlloc to next power of 2
|
|
1336 |
|
|
1337 |
if (newAlloc < iAllocated)
|
|
1338 |
{
|
|
1339 |
// We are going to shrink the array.
|
|
1340 |
// Cut out any slots which will disappear
|
|
1341 |
SSlot* last = iSlots + newAlloc; // first slot to go
|
|
1342 |
|
|
1343 |
if (last->iFree.iPrev != (TInt16)EQFree)
|
|
1344 |
{
|
|
1345 |
// there are other free slots before the last block, so free queue
|
|
1346 |
// will not be empty after shrink
|
|
1347 |
fq->iPrev = last->iFree.iPrev;
|
|
1348 |
last = iSlots + last->iFree.iPrev; // last free slot after shrink
|
|
1349 |
last->iFree.iNext = (TInt16)EQFree;
|
|
1350 |
}
|
|
1351 |
else
|
|
1352 |
{
|
|
1353 |
// free queue is empty after shrinking
|
|
1354 |
fq->iNext = (TInt16)EQFree;
|
|
1355 |
fq->iPrev = (TInt16)EQFree;
|
|
1356 |
}
|
|
1357 |
|
|
1358 |
iAllocated = newAlloc;
|
|
1359 |
if (newAlloc > EBitMapSize) // recalculate modification bitmap granularity
|
|
1360 |
{
|
|
1361 |
iModListShift = (TUint8)__e32_find_ms1_32(newAlloc / EBitMapSize);
|
|
1362 |
}
|
|
1363 |
else
|
|
1364 |
{
|
|
1365 |
iModListShift = 0;
|
|
1366 |
}
|
|
1367 |
}
|
|
1368 |
else
|
|
1369 |
{
|
|
1370 |
newAlloc = -1;
|
|
1371 |
}
|
|
1372 |
|
|
1373 |
// set the amortize value before next tidy/compaction
|
|
1374 |
iAmortize = iAllocated / 2;
|
|
1375 |
iState = (TUint8)ENormal;
|
|
1376 |
ReleaseWriteLock();
|
|
1377 |
|
|
1378 |
// Now actually shrink the array. This relies on the current heap behaviour
|
|
1379 |
// when shrinking - the memory must not be moved.
|
|
1380 |
if (newAlloc>0)
|
|
1381 |
{
|
|
1382 |
Kern::ReAlloc(iSlots, iAllocated*sizeof(SSlot), RAllocator::ENeverMove);
|
|
1383 |
}
|
|
1384 |
|
|
1385 |
} // RObjectIx::TidyAndCompact
|
|
1386 |
|
|
1387 |
|
|
1388 |
/**
|
|
1389 |
* Counts the number of times an object appears in this index.
|
|
1390 |
*
|
|
1391 |
* @param aObject Object whose occurrences are to be counted.
|
|
1392 |
*
|
|
1393 |
* @return Number of times aObject appears in the index.
|
|
1394 |
|
|
1395 |
* @pre Calling thread must be in a critical section.
|
|
1396 |
* @pre No fast mutex can be held.
|
|
1397 |
* @pre Call in a thread context.
|
|
1398 |
* @pre DObject::HandleMutex held
|
|
1399 |
*/
|
|
1400 |
TInt RObjectIx::Count(DObject* aObject)
|
|
1401 |
{
|
|
1402 |
//Check preconditions(debug build only)
|
|
1403 |
__ASSERT_CRITICAL;
|
|
1404 |
__ASSERT_NO_FAST_MUTEX;
|
|
1405 |
__ASSERT_MUTEX(HandleMutex);
|
|
1406 |
|
|
1407 |
if (iState==ETerminated)
|
|
1408 |
{
|
|
1409 |
return 0;
|
|
1410 |
}
|
|
1411 |
|
|
1412 |
AcquireWriteLock();
|
|
1413 |
iModList.iMonitor.iBoundary = 0; // count changes before this point
|
|
1414 |
iModList.iMonitor.iObj = aObject; // count additions/removals of this object
|
|
1415 |
iModList.iMonitor.iResult = 0; // initial count
|
|
1416 |
iState = (TUint8)ECounting; // enable monitoring of additions/removals
|
|
1417 |
TInt pos = 0;
|
|
1418 |
while (pos<iCount && iActiveCount) // stop if index empty
|
|
1419 |
{
|
|
1420 |
TInt limit = pos + EMaxLockedIter;
|
|
1421 |
if (limit>iCount)
|
|
1422 |
{
|
|
1423 |
limit = iCount;
|
|
1424 |
}
|
|
1425 |
|
|
1426 |
while (pos<limit)
|
|
1427 |
{
|
|
1428 |
SSlot* slot = iSlots + pos;
|
|
1429 |
if (Occupant(slot) == aObject)
|
|
1430 |
{
|
|
1431 |
++iModList.iMonitor.iResult;
|
|
1432 |
}
|
|
1433 |
pos++;
|
|
1434 |
}
|
|
1435 |
iModList.iMonitor.iBoundary = pos; // count additions/removals of aObject before this point
|
|
1436 |
ReleaseWriteLock(); // let other threads in
|
|
1437 |
AcquireWriteLock();
|
|
1438 |
}
|
|
1439 |
TInt result = iActiveCount ? iModList.iMonitor.iResult : 0;
|
|
1440 |
iState = (TUint8)ENormal;
|
|
1441 |
ReleaseWriteLock();
|
|
1442 |
|
|
1443 |
return result;
|
|
1444 |
} // RObjectIx::Count
|
|
1445 |
|
|
1446 |
|
|
1447 |
/**
|
|
1448 |
* Looks up an object in the index by object pointer.
|
|
1449 |
*
|
|
1450 |
* Returns a handle to the object.
|
|
1451 |
*
|
|
1452 |
* @param aObj Pointer to the object to look up.
|
|
1453 |
*
|
|
1454 |
* @return Handle to object (always >0);
|
|
1455 |
* KErrNotFound, if object not present in index.
|
|
1456 |
*
|
|
1457 |
* @pre Calling thread must be in a critical section.
|
|
1458 |
* @pre No fast mutex can be held.
|
|
1459 |
* @pre Call in a thread context.
|
|
1460 |
* @pre DObject::HandleMutex held.
|
|
1461 |
*/
|
|
1462 |
TInt RObjectIx::At(DObject* aObj)
|
|
1463 |
{
|
|
1464 |
//Check preconditions(debug build only)
|
|
1465 |
__ASSERT_CRITICAL;
|
|
1466 |
__ASSERT_NO_FAST_MUTEX;
|
|
1467 |
__ASSERT_MUTEX(HandleMutex);
|
|
1468 |
|
|
1469 |
if (iState==ETerminated)
|
|
1470 |
{
|
|
1471 |
return KErrNotFound;
|
|
1472 |
}
|
|
1473 |
|
|
1474 |
TInt h = KErrNotFound;
|
|
1475 |
AcquireWriteLock();
|
|
1476 |
iState = (TUint8)ESearching; // enable monitoring of new handles
|
|
1477 |
iModList.iMonitor.iObj = aObj; // object to check for
|
|
1478 |
iModList.iMonitor.iBoundary = 0; // will change if aObj is added to a slot before this point
|
|
1479 |
TInt pos = 0;
|
|
1480 |
while (pos<iCount && iActiveCount) // stop if index empty
|
|
1481 |
{
|
|
1482 |
TInt limit = pos + EMaxLockedIter;
|
|
1483 |
if (limit>iCount)
|
|
1484 |
{
|
|
1485 |
limit = iCount;
|
|
1486 |
}
|
|
1487 |
while (pos<limit)
|
|
1488 |
{
|
|
1489 |
SSlot* slot = iSlots + pos;
|
|
1490 |
if (Occupant(slot) == aObj)
|
|
1491 |
{
|
|
1492 |
// found it, finish
|
|
1493 |
h = MakeHandle(pos, slot->iUsed.iAttr);
|
|
1494 |
break;
|
|
1495 |
}
|
|
1496 |
pos++;
|
|
1497 |
}
|
|
1498 |
if (h>0)
|
|
1499 |
{
|
|
1500 |
break; // found it, finish
|
|
1501 |
}
|
|
1502 |
iModList.iMonitor.iBoundary = pos; // will change if aObj is added to a slot already checked
|
|
1503 |
ReleaseWriteLock(); // let other threads in
|
|
1504 |
AcquireWriteLock();
|
|
1505 |
pos = iModList.iMonitor.iBoundary; // next position to check
|
|
1506 |
}
|
|
1507 |
iState = (TUint8)ENormal;
|
|
1508 |
ReleaseWriteLock();
|
|
1509 |
return h;
|
|
1510 |
} // RObjectIx::At
|
|
1511 |
|
|
1512 |
|
|
1513 |
/**
|
|
1514 |
* Finds the object at a specific position in the index array.
|
|
1515 |
*
|
|
1516 |
* @param aIndex Index into array.
|
|
1517 |
*
|
|
1518 |
* @return Pointer to the object at that position (could be NULL).
|
|
1519 |
*
|
|
1520 |
* @pre Call in a thread context.
|
|
1521 |
* @pre System lock must be held.
|
|
1522 |
*/
|
|
1523 |
DObject* RObjectIx::operator[](TInt aIndex)
|
|
1524 |
{
|
|
1525 |
DObject* obj = 0;
|
|
1526 |
AcquireReadLock();
|
|
1527 |
asserta(TUint(aIndex)<TUint(iCount));
|
|
1528 |
SSlot* slot = iSlots + aIndex;
|
|
1529 |
obj = Occupant(slot);
|
|
1530 |
ReleaseReadLock();
|
|
1531 |
return obj;
|
|
1532 |
} // RObjectIx::operator[]
|
|
1533 |
|
|
1534 |
|
|
1535 |
TInt RObjectIx::LastHandle()
|
|
1536 |
//
|
|
1537 |
// Return the last valid handle
|
|
1538 |
// Must wait on HandleMutex before calling this.
|
|
1539 |
//
|
|
1540 |
{
|
|
1541 |
//Check preconditions(debug build only)
|
|
1542 |
__ASSERT_CRITICAL;
|
|
1543 |
__ASSERT_NO_FAST_MUTEX;
|
|
1544 |
__ASSERT_MUTEX(HandleMutex);
|
|
1545 |
|
|
1546 |
if (iState==ETerminated)
|
|
1547 |
{
|
|
1548 |
return 0;
|
|
1549 |
}
|
|
1550 |
|
|
1551 |
TInt h = 0;
|
|
1552 |
AcquireWriteLock();
|
|
1553 |
iState = (TUint8)EFindingLast; // enable monitoring of new handles
|
|
1554 |
iModList.iMonitor.iBoundary = iCount-1; // will change if any slots past this point become occupied
|
|
1555 |
TInt pos = iCount;
|
|
1556 |
while (pos>=0 && iActiveCount) // stop if index empty
|
|
1557 |
{
|
|
1558 |
TInt limit = pos - EMaxLockedIter;
|
|
1559 |
|
|
1560 |
if (limit<0)
|
|
1561 |
{
|
|
1562 |
limit = 0;
|
|
1563 |
}
|
|
1564 |
|
|
1565 |
while (--pos>=limit)
|
|
1566 |
{
|
|
1567 |
SSlot* slot = iSlots + pos;
|
|
1568 |
if (Occupant(slot))
|
|
1569 |
{
|
|
1570 |
// found it, finish
|
|
1571 |
h = MakeHandle(pos, slot->iUsed.iAttr);
|
|
1572 |
break;
|
|
1573 |
}
|
|
1574 |
}
|
|
1575 |
if (h)
|
|
1576 |
{
|
|
1577 |
break; // found it, finish
|
|
1578 |
}
|
|
1579 |
iModList.iMonitor.iBoundary = pos - 1; // will change if any slots already checked or after become occupied
|
|
1580 |
ReleaseWriteLock(); // let other threads in
|
|
1581 |
AcquireWriteLock();
|
|
1582 |
pos = iModList.iMonitor.iBoundary + 1; // one after last used position or last checked position
|
|
1583 |
}
|
|
1584 |
iState = (TUint8)ENormal;
|
|
1585 |
ReleaseWriteLock();
|
|
1586 |
|
|
1587 |
return h;
|
|
1588 |
} // RObjectIx::LastHandle
|
|
1589 |
|
|
1590 |
|
|
1591 |
#ifdef DOBJECT_TEST_CODE
|
|
1592 |
void RObjectIx::Check(RObjectIx::TValidateEntry aV)
|
|
1593 |
{
|
|
1594 |
Wait();
|
|
1595 |
AcquireWriteLock();
|
|
1596 |
|
|
1597 |
TInt i;
|
|
1598 |
|
|
1599 |
if (iAllocated==0)
|
|
1600 |
{
|
|
1601 |
assertt(iCount==0);
|
|
1602 |
assertt(iActiveCount==0);
|
|
1603 |
assertt(iReservedFree==0);
|
|
1604 |
assertt(iReservedTotal==0);
|
|
1605 |
assertt(iReservedFreeHWM==0);
|
|
1606 |
assertt(iSlots==0);
|
|
1607 |
assertt(iAmortize==0);
|
|
1608 |
assertt(iState==ENormal);
|
|
1609 |
assertt(iModCount==255);
|
|
1610 |
assertt(iModListShift==0);
|
|
1611 |
assertt(iSpare1==0);
|
|
1612 |
for (i=-ENumFreeQ; i<0; ++i)
|
|
1613 |
{
|
|
1614 |
SSlotQLink* l = Link(i);
|
|
1615 |
assertt(l->iNext == (TInt16)i);
|
|
1616 |
assertt(l->iPrev == (TInt16)i);
|
|
1617 |
}
|
|
1618 |
|
|
1619 |
ReleaseWriteLock();
|
|
1620 |
Signal();
|
|
1621 |
|
|
1622 |
return;
|
|
1623 |
}
|
|
1624 |
assertt((iAllocated & (iAllocated-1))==0);
|
|
1625 |
assertt(iSlots!=0);
|
|
1626 |
|
|
1627 |
TInt hwm = 0;
|
|
1628 |
TInt active = 0;
|
|
1629 |
TInt rf = 0;
|
|
1630 |
TInt rt = 0;
|
|
1631 |
TInt rfhwm = 0;
|
|
1632 |
|
|
1633 |
for (i=0; i<iAllocated; ++i)
|
|
1634 |
{
|
|
1635 |
SSlot* slot = iSlots + i;
|
|
1636 |
DObject* pO = Occupant(slot);
|
|
1637 |
TBool rsvd = IsReserved(slot);
|
|
1638 |
if (pO)
|
|
1639 |
{
|
|
1640 |
TUint attrW = slot->iUsed.iAttr;
|
|
1641 |
TUint in = attrW & 0x3fffu;
|
|
1642 |
assertt(in!=0);
|
|
1643 |
TUint objtype = (attrW>>14)&0x3fu;
|
|
1644 |
TUint attr = (attrW>>20);
|
|
1645 |
if (rsvd)
|
|
1646 |
{
|
|
1647 |
attr |= 0x80000000u;
|
|
1648 |
}
|
|
1649 |
assertt(objtype == pO->iContainerID);
|
|
1650 |
TInt h = MakeHandle(i, in);
|
|
1651 |
if (aV)
|
|
1652 |
{
|
|
1653 |
aV(i, h, in, objtype, attr, pO);
|
|
1654 |
}
|
|
1655 |
hwm = i+1;
|
|
1656 |
++active;
|
|
1657 |
if (rsvd)
|
|
1658 |
{
|
|
1659 |
++rt;
|
|
1660 |
}
|
|
1661 |
}
|
|
1662 |
else if (rsvd)
|
|
1663 |
{
|
|
1664 |
hwm = i+1;
|
|
1665 |
rfhwm = i+1;
|
|
1666 |
++rf;
|
|
1667 |
++rt;
|
|
1668 |
}
|
|
1669 |
}
|
|
1670 |
assertt(iCount >= 0);
|
|
1671 |
assertt(iCount <= iAllocated);
|
|
1672 |
assertt(iCount >= hwm);
|
|
1673 |
assertt(iActiveCount == active);
|
|
1674 |
assertt(iReservedFree == rf);
|
|
1675 |
assertt(iReservedTotal == rt);
|
|
1676 |
assertt(iReservedFreeHWM >= 0);
|
|
1677 |
assertt(iReservedFreeHWM <= iAllocated);
|
|
1678 |
assertt(iReservedFreeHWM >= rfhwm);
|
|
1679 |
assertt(iAmortize>=-1 && iAmortize<=iAllocated/2);
|
|
1680 |
assertt(iState==ENormal);
|
|
1681 |
assertt(iModCount==255);
|
|
1682 |
if (iAllocated <= EBitMapSize)
|
|
1683 |
{
|
|
1684 |
assertt(iModListShift==0);
|
|
1685 |
}
|
|
1686 |
else
|
|
1687 |
{
|
|
1688 |
assertt((EBitMapSize<<iModListShift)==iAllocated);
|
|
1689 |
}
|
|
1690 |
assertt(iSpare1==0);
|
|
1691 |
if (rf>0)
|
|
1692 |
{
|
|
1693 |
TInt prev = EQRsvd;
|
|
1694 |
SSlotQLink* p = Link(prev);
|
|
1695 |
for (i=0; i<=rf; ++i)
|
|
1696 |
{
|
|
1697 |
TInt n = p->iNext;
|
|
1698 |
p = Link(n);
|
|
1699 |
assertt(p->iPrev == prev);
|
|
1700 |
prev = n;
|
|
1701 |
}
|
|
1702 |
assertt(prev == EQRsvd);
|
|
1703 |
}
|
|
1704 |
if ((iAllocated - active) > rf)
|
|
1705 |
{
|
|
1706 |
TInt nf = iAllocated - active - rf;
|
|
1707 |
TInt never_used = iAllocated - iCount;
|
|
1708 |
TInt used = nf - never_used;
|
|
1709 |
TInt prev = EQFree;
|
|
1710 |
SSlotQLink* p = Link(prev);
|
|
1711 |
for (i=0; i<=nf; ++i)
|
|
1712 |
{
|
|
1713 |
TInt n = p->iNext;
|
|
1714 |
if (i>=used && i<nf)
|
|
1715 |
{
|
|
1716 |
assertt(n==iCount+i-used);
|
|
1717 |
}
|
|
1718 |
p = Link(n);
|
|
1719 |
assertt(p->iPrev == prev);
|
|
1720 |
prev = n;
|
|
1721 |
}
|
|
1722 |
assertt(prev == EQFree);
|
|
1723 |
}
|
|
1724 |
for (i=-ENumFreeQ; i<0; ++i)
|
|
1725 |
{
|
|
1726 |
if (i==EQRsvd && rf>0)
|
|
1727 |
{
|
|
1728 |
continue;
|
|
1729 |
}
|
|
1730 |
if (i==EQFree && (iAllocated-active)>rf)
|
|
1731 |
{
|
|
1732 |
continue;
|
|
1733 |
}
|
|
1734 |
SSlotQLink* l = Link(i);
|
|
1735 |
assertt(l->iNext == (TInt16)i);
|
|
1736 |
assertt(l->iPrev == (TInt16)i);
|
|
1737 |
}
|
|
1738 |
|
|
1739 |
ReleaseWriteLock();
|
|
1740 |
Signal();
|
|
1741 |
}
|
|
1742 |
#endif
|
|
1743 |
|
|
1744 |
|
|
1745 |
|
|
1746 |
|
|
1747 |
|