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\object.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
|
|
19 |
#include <kernel/kern_priv.h>
|
|
20 |
#include "dobject.h"
|
|
21 |
|
|
22 |
#ifdef _DEBUG
|
|
23 |
#define __DEBUG_SAVE(p) TInt dbgNestLevel = ((RHeap::SDebugCell*)p)[-1].nestingLevel
|
|
24 |
#define __DEBUG_RESTORE(p) ((RHeap::SDebugCell*)p)[-1].nestingLevel = dbgNestLevel
|
|
25 |
#else
|
|
26 |
#define __DEBUG_SAVE(p)
|
|
27 |
#define __DEBUG_RESTORE(p)
|
|
28 |
#endif
|
|
29 |
|
|
30 |
_LIT(KLitLocal,"Local-");
|
|
31 |
_LIT8(KLitLocal8,"Local-");
|
|
32 |
_LIT(KColonColon,"::");
|
|
33 |
_LIT8(KColonColon8,"::");
|
|
34 |
_LIT(KLitDObjectCon,"DObjectCon");
|
|
35 |
_LIT(KLitHandleMutex,"HandleMutex");
|
|
36 |
|
|
37 |
#ifdef __SMP__
|
|
38 |
// SMP_FIXME: Investigate what the purpose of this is
|
|
39 |
TSpinLock TraceSpinLock(TSpinLock::EOrderNone);
|
|
40 |
#endif
|
|
41 |
|
|
42 |
inline TUint64 DObject::ObjectId() const
|
|
43 |
{
|
|
44 |
return iObjectId >> 18;
|
|
45 |
}
|
|
46 |
|
|
47 |
/** Default constructor for DObject
|
|
48 |
Sets access count to 1, no name, no container, no owner.
|
|
49 |
|
|
50 |
@internalComponent
|
|
51 |
*/
|
|
52 |
EXPORT_C DObject::DObject()
|
|
53 |
{
|
|
54 |
// iContainer=NULL;
|
|
55 |
// iOwner=NULL;
|
|
56 |
// iName=NULL;
|
|
57 |
iAccessCount=1;
|
|
58 |
iObjectId = __e32_atomic_add_ord64(&NextObjectId, TUint64(1<<18));
|
|
59 |
}
|
|
60 |
|
|
61 |
|
|
62 |
/** Base class destructor for DObject
|
|
63 |
Removes the name, removes object from its container if any, and removes owner.
|
|
64 |
|
|
65 |
@pre Calling thread must be in a critical section.
|
|
66 |
@pre No fast mutex can be held.
|
|
67 |
@pre Call in a thread context.
|
|
68 |
@pre Kernel must be unlocked
|
|
69 |
@pre interrupts enabled
|
|
70 |
|
|
71 |
@internalComponent
|
|
72 |
*/
|
|
73 |
EXPORT_C DObject::~DObject()
|
|
74 |
{
|
|
75 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObject::~DObject");
|
|
76 |
__ASSERT_ALWAYS(AccessCount()==0,Panic(EObjObjectStillReferenced));
|
|
77 |
__KTRACE_OPT(KOBJECT,Kern::Printf("DObject::~DObject %O owner %O",this,iOwner));
|
|
78 |
NKern::FMWait(&Lock);
|
|
79 |
HBuf* pN=iName;
|
|
80 |
iName=NULL;
|
|
81 |
DObjectCon* pC;
|
|
82 |
if(iContainerID)
|
|
83 |
{
|
|
84 |
pC=K::Containers[iContainerID-1];
|
|
85 |
iContainerID=0;
|
|
86 |
}
|
|
87 |
else
|
|
88 |
pC=NULL;
|
|
89 |
DObject* pO=iOwner;
|
|
90 |
iOwner=NULL;
|
|
91 |
NKern::FMSignal(&Lock);
|
|
92 |
if (pC)
|
|
93 |
pC->Remove(this);
|
|
94 |
if (HasNotifierQ()) // this can't go from zero to nonzero now since no-one has a reference on this object
|
|
95 |
{
|
|
96 |
SDblQue dq;
|
|
97 |
TMiscNotifierMgr& m = K::TheMiscNotifierMgr;
|
|
98 |
m.Lock();
|
|
99 |
SMiscNotifierQ* q = NotifierQ();
|
|
100 |
SetNotifierQ(0);
|
|
101 |
dq.MoveFrom(q);
|
|
102 |
m.Unlock();
|
|
103 |
if (q)
|
|
104 |
{
|
|
105 |
Kern::Free(q);
|
|
106 |
m.CompleteNotifications(dq);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
if (pN)
|
|
110 |
Kern::Free(pN);
|
|
111 |
if (pO)
|
|
112 |
pO->Close(NULL);
|
|
113 |
}
|
|
114 |
|
|
115 |
void K::ObjDelete(DObject* aObj)
|
|
116 |
{
|
|
117 |
SDblQue dq;
|
|
118 |
SMiscNotifierQ* q = 0;
|
|
119 |
TMiscNotifierMgr& m = K::TheMiscNotifierMgr;
|
|
120 |
if (aObj->HasNotifierQ()) // this can't go from zero to nonzero now since no-one has a reference on this object
|
|
121 |
{
|
|
122 |
m.Lock();
|
|
123 |
q = aObj->NotifierQ();
|
|
124 |
aObj->SetNotifierQ(0);
|
|
125 |
dq.MoveFrom(q);
|
|
126 |
m.Unlock();
|
|
127 |
}
|
|
128 |
TBool deferCodesegCleanup = aObj->iObjectFlags&DObject::EObjectDeferKernelCodesegCleanup;
|
|
129 |
if(deferCodesegCleanup)
|
|
130 |
DCodeSeg::DeferKernelCleanup();
|
|
131 |
DBase::Delete(aObj);
|
|
132 |
if (q)
|
|
133 |
{
|
|
134 |
Kern::Free(q);
|
|
135 |
m.CompleteNotifications(dq);
|
|
136 |
}
|
|
137 |
if(deferCodesegCleanup)
|
|
138 |
DCodeSeg::EndDeferKernelCleanup();
|
|
139 |
}
|
|
140 |
|
|
141 |
|
|
142 |
/** Closes a reference on a DObject-derived class.
|
|
143 |
|
|
144 |
This is the default implementation of the virtual function.
|
|
145 |
|
|
146 |
Decrements reference count and, if it becomes zero, deletes the object.
|
|
147 |
Uses an atomic safe decrement so that a zero access count is unchanged.
|
|
148 |
|
|
149 |
@param aPtr Points to relevant DProcess if a user handle is being closed
|
|
150 |
NULL if any other reference is being closed
|
|
151 |
|
|
152 |
@return Bit mask of values from enum DObject::TCloseReturn
|
|
153 |
EObjectDeleted Set if object has been deleted
|
|
154 |
EObjectUnmapped Set if the last reference from a user process has been removed
|
|
155 |
Only used for DLibrary
|
|
156 |
|
|
157 |
@pre Calling thread must be in a critical section.
|
|
158 |
@pre No fast mutex can be held.
|
|
159 |
@pre Call in a thread context.
|
|
160 |
@pre Kernel must be unlocked
|
|
161 |
@pre interrupts enabled
|
|
162 |
*/
|
|
163 |
EXPORT_C TInt DObject::Close(TAny* /*aPtr*/)
|
|
164 |
{
|
|
165 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObject::Close");
|
|
166 |
__KTRACE_OPT(KOBJECT,Kern::Printf("DObject::Close %d %O",AccessCount(),this));
|
|
167 |
if (Dec()==1)
|
|
168 |
{
|
|
169 |
NKern::LockSystem(); // just in case someone is still using this object
|
|
170 |
NKern::UnlockSystem();
|
|
171 |
K::ObjDelete(this);
|
|
172 |
return EObjectDeleted;
|
|
173 |
}
|
|
174 |
return 0;
|
|
175 |
}
|
|
176 |
|
|
177 |
|
|
178 |
/** Opens a reference on a DObject-derived class, checking that its reference
|
|
179 |
count was not initially zero.
|
|
180 |
|
|
181 |
@panic Current thread KERN-EXEC 0 if initial reference count was 0
|
|
182 |
|
|
183 |
@pre Call in a thread context.
|
|
184 |
@pre System lock must be held.
|
|
185 |
|
|
186 |
@internalComponent
|
|
187 |
*/
|
|
188 |
EXPORT_C void DObject::CheckedOpen()
|
|
189 |
{
|
|
190 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_NOT_IDFC|MASK_SYSTEM_LOCKED,"DObject::CheckedOpen");
|
|
191 |
if (Open())
|
|
192 |
K::PanicCurrentThread(EBadHandle);
|
|
193 |
}
|
|
194 |
|
|
195 |
|
|
196 |
/** Appends the object's short name to a descriptor.
|
|
197 |
|
|
198 |
This is the default implementation of the virtual function.
|
|
199 |
|
|
200 |
This is overridden by some
|
|
201 |
DObject-derived classes to add extra information to the name.
|
|
202 |
|
|
203 |
@param aName Writeable descriptor to which name should be appended.
|
|
204 |
|
|
205 |
@pre Call in a thread context.
|
|
206 |
@pre DObject::Lock fast mutex held.
|
|
207 |
*/
|
|
208 |
EXPORT_C void DObject::DoAppendName(TDes& aName)
|
|
209 |
{
|
|
210 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_NOT_IDFC,"DObject::DoAppendName");
|
|
211 |
__ASSERT_WITH_MESSAGE_DEBUG(NKern::HeldFastMutex()==&DObject::Lock,"DObject::Lock fast mutex must be held","DObject::DoAppendName");
|
|
212 |
if (iName)
|
|
213 |
aName.Append(*iName);
|
|
214 |
else
|
|
215 |
{
|
|
216 |
aName.Append(KLitLocal);
|
|
217 |
aName.AppendNumFixedWidth((TInt)this,EHex,8);
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
|
|
222 |
/** Appends the object's full name to a descriptor.
|
|
223 |
|
|
224 |
@param aFullName Writeable descriptor to which name should be appended.
|
|
225 |
|
|
226 |
@pre Call in a thread context.
|
|
227 |
@pre DObject::Lock fast mutex held.
|
|
228 |
*/
|
|
229 |
EXPORT_C void DObject::DoAppendFullName(TDes& aFullName)
|
|
230 |
{
|
|
231 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_NOT_IDFC,"DObject::DoAppendFullName");
|
|
232 |
__ASSERT_WITH_MESSAGE_DEBUG(NKern::HeldFastMutex()==&DObject::Lock,"DObject::Lock fast mutex must be held","DObject::DoAppendFullName");
|
|
233 |
if (iOwner)
|
|
234 |
{
|
|
235 |
iOwner->DoAppendFullName(aFullName);
|
|
236 |
aFullName.Append(KColonColon);
|
|
237 |
}
|
|
238 |
DoAppendName(aFullName);
|
|
239 |
}
|
|
240 |
|
|
241 |
|
|
242 |
/** Gets the object's short name into a descriptor.
|
|
243 |
|
|
244 |
@param aName Writeable descriptor to which name should be copied.
|
|
245 |
|
|
246 |
@pre No fast mutex can be held.
|
|
247 |
@pre Call in a thread context.
|
|
248 |
@pre Kernel must be unlocked
|
|
249 |
@pre interrupts enabled
|
|
250 |
*/
|
|
251 |
EXPORT_C void DObject::Name(TDes& aName)
|
|
252 |
{
|
|
253 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DObject::Name");
|
|
254 |
aName.Zero();
|
|
255 |
AppendName(aName);
|
|
256 |
}
|
|
257 |
|
|
258 |
|
|
259 |
/** Appends the object's short name to a descriptor.
|
|
260 |
|
|
261 |
@param aName Writeable descriptor to which name should be appended.
|
|
262 |
|
|
263 |
@pre No fast mutex can be held.
|
|
264 |
@pre Call in a thread context.
|
|
265 |
@pre Kernel must be unlocked
|
|
266 |
@pre interrupts enabled
|
|
267 |
*/
|
|
268 |
EXPORT_C void DObject::AppendName(TDes& aName)
|
|
269 |
{
|
|
270 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DObject::AppendName");
|
|
271 |
NKern::FMWait(&Lock);
|
|
272 |
DoAppendName(aName);
|
|
273 |
NKern::FMSignal(&Lock);
|
|
274 |
}
|
|
275 |
|
|
276 |
|
|
277 |
/** Gets the object's full name into a descriptor.
|
|
278 |
|
|
279 |
@param aFullName Writeable descriptor to which name should be copied.
|
|
280 |
|
|
281 |
@pre No fast mutex can be held.
|
|
282 |
@pre Call in a thread context.
|
|
283 |
@pre Kernel must be unlocked
|
|
284 |
@pre interrupts enabled
|
|
285 |
*/
|
|
286 |
EXPORT_C void DObject::FullName(TDes& aFullName)
|
|
287 |
{
|
|
288 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DObject::FullName");
|
|
289 |
aFullName.Zero();
|
|
290 |
AppendFullName(aFullName);
|
|
291 |
}
|
|
292 |
|
|
293 |
|
|
294 |
/** Appends the object's full name to a descriptor.
|
|
295 |
|
|
296 |
@param aFullName Writeable descriptor to which name should be appended.
|
|
297 |
|
|
298 |
@pre No fast mutex can be held.
|
|
299 |
@pre Call in a thread context.
|
|
300 |
@pre Kernel must be unlocked
|
|
301 |
@pre interrupts enabled
|
|
302 |
*/
|
|
303 |
EXPORT_C void DObject::AppendFullName(TDes& aFullName)
|
|
304 |
{
|
|
305 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DObject::AppendFullName");
|
|
306 |
NKern::FMWait(&Lock);
|
|
307 |
DoAppendFullName(aFullName);
|
|
308 |
NKern::FMSignal(&Lock);
|
|
309 |
}
|
|
310 |
|
|
311 |
|
|
312 |
/** Appends the object's short name to a descriptor without waiting on any fast
|
|
313 |
mutexes.
|
|
314 |
|
|
315 |
Used in kernel tracing.
|
|
316 |
|
|
317 |
@param aName Writeable descriptor to which name should be appended.
|
|
318 |
@param aLock TRUE if kernel should be locked while copying out name
|
|
319 |
|
|
320 |
@pre Call either in a thread or an IDFC context, if aLock=TRUE.
|
|
321 |
@pre Call in any context, if aLock=FALSE.
|
|
322 |
*/
|
|
323 |
EXPORT_C void DObject::TraceAppendName(TDes8& aName, TBool aLock)
|
|
324 |
{
|
|
325 |
TInt c = NKern::CurrentContext();
|
|
326 |
#ifdef __SMP__
|
|
327 |
TInt irq = 0;
|
|
328 |
if (aLock)
|
|
329 |
{
|
|
330 |
if (c==NKern::EInterrupt)
|
|
331 |
irq = TraceSpinLock.LockIrqSave();
|
|
332 |
else
|
|
333 |
{
|
|
334 |
NKern::Lock();
|
|
335 |
TraceSpinLock.LockOnly();
|
|
336 |
}
|
|
337 |
}
|
|
338 |
#else
|
|
339 |
if (aLock && c!=NKern::EInterrupt)
|
|
340 |
NKern::Lock();
|
|
341 |
#endif
|
|
342 |
if (iName)
|
|
343 |
aName.Append(*iName);
|
|
344 |
else
|
|
345 |
{
|
|
346 |
aName.Append(KLitLocal8);
|
|
347 |
aName.AppendNumFixedWidth((TUint)this,EHex,8);
|
|
348 |
}
|
|
349 |
#ifdef __SMP__
|
|
350 |
if (aLock)
|
|
351 |
{
|
|
352 |
if (c==NKern::EInterrupt)
|
|
353 |
TraceSpinLock.UnlockIrqRestore(irq);
|
|
354 |
else
|
|
355 |
{
|
|
356 |
TraceSpinLock.UnlockOnly();
|
|
357 |
NKern::Unlock();
|
|
358 |
}
|
|
359 |
}
|
|
360 |
#else
|
|
361 |
if (aLock && c!=NKern::EInterrupt)
|
|
362 |
NKern::Unlock();
|
|
363 |
#endif
|
|
364 |
}
|
|
365 |
|
|
366 |
|
|
367 |
/** Appends the object's full name to a descriptor without waiting on any fast
|
|
368 |
mutexes.
|
|
369 |
|
|
370 |
Used in kernel tracing.
|
|
371 |
|
|
372 |
@param aFullName Writeable descriptor to which name should be appended.
|
|
373 |
@param aLock TRUE if kernel should be locked while copying out name.
|
|
374 |
|
|
375 |
@pre Call either in a thread or an IDFC context, if aLock=TRUE.
|
|
376 |
@pre Call in any context, if aLock=FALSE.
|
|
377 |
*/
|
|
378 |
EXPORT_C void DObject::TraceAppendFullName(TDes8& aFullName, TBool aLock)
|
|
379 |
{
|
|
380 |
TInt c = NKern::CurrentContext();
|
|
381 |
#ifdef __SMP__
|
|
382 |
TInt irq = 0;
|
|
383 |
if (aLock)
|
|
384 |
{
|
|
385 |
if (c==NKern::EInterrupt)
|
|
386 |
irq = TraceSpinLock.LockIrqSave();
|
|
387 |
else
|
|
388 |
{
|
|
389 |
NKern::Lock();
|
|
390 |
TraceSpinLock.LockOnly();
|
|
391 |
}
|
|
392 |
}
|
|
393 |
#else
|
|
394 |
if (aLock && c!=NKern::EInterrupt)
|
|
395 |
NKern::Lock();
|
|
396 |
#endif
|
|
397 |
if (iOwner)
|
|
398 |
{
|
|
399 |
iOwner->TraceAppendFullName(aFullName,EFalse);
|
|
400 |
aFullName.Append(KColonColon8);
|
|
401 |
}
|
|
402 |
TraceAppendName(aFullName,EFalse);
|
|
403 |
#ifdef __SMP__
|
|
404 |
if (aLock)
|
|
405 |
{
|
|
406 |
if (c==NKern::EInterrupt)
|
|
407 |
TraceSpinLock.UnlockIrqRestore(irq);
|
|
408 |
else
|
|
409 |
{
|
|
410 |
TraceSpinLock.UnlockOnly();
|
|
411 |
NKern::Unlock();
|
|
412 |
}
|
|
413 |
}
|
|
414 |
#else
|
|
415 |
if (aLock && c!=NKern::EInterrupt)
|
|
416 |
NKern::Unlock();
|
|
417 |
#endif
|
|
418 |
}
|
|
419 |
|
|
420 |
|
|
421 |
/** Sets the object's name, clearing any previous name.
|
|
422 |
|
|
423 |
If the object's name had already been set the old name is replaced. If aName
|
|
424 |
is NULL the object ends up with no name.
|
|
425 |
|
|
426 |
@param aName Pointer to descriptor containing new name;
|
|
427 |
NULL if there is no new name.
|
|
428 |
|
|
429 |
@return KErrNone, if name set successfully;
|
|
430 |
KErrNoMemory, if a heap buffer could not be allocated for the new name;
|
|
431 |
KErrBadName, if new name contained invalid characters.
|
|
432 |
|
|
433 |
@pre Calling thread must be in a critical section.
|
|
434 |
@pre No fast mutex can be held.
|
|
435 |
@pre Call in a thread context.
|
|
436 |
@pre Kernel must be unlocked
|
|
437 |
@pre interrupts enabled
|
|
438 |
|
|
439 |
@internalComponent
|
|
440 |
*/
|
|
441 |
EXPORT_C TInt DObject::SetName(const TDesC* aName)
|
|
442 |
{
|
|
443 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObject::SetName");
|
|
444 |
__KTRACE_OPT(KOBJECT,Kern::Printf("SetName %O (%lS)",this,aName));
|
|
445 |
TAny* pN=NULL;
|
|
446 |
if (aName)
|
|
447 |
{
|
|
448 |
TInt r = Kern::ValidateName(*aName);
|
|
449 |
if(r!=KErrNone)
|
|
450 |
return r;
|
|
451 |
pN=HBuf::New(*aName);
|
|
452 |
if (!pN)
|
|
453 |
return(KErrNoMemory);
|
|
454 |
}
|
|
455 |
NKern::FMWait(&Lock);
|
|
456 |
pN = __e32_atomic_swp_ord_ptr(&iName, pN);
|
|
457 |
if (iName)
|
|
458 |
{
|
|
459 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Name is now %lS",iName));
|
|
460 |
}
|
|
461 |
else
|
|
462 |
{
|
|
463 |
__KTRACE_OPT(KOBJECT,Kern::Printf("No name"));
|
|
464 |
}
|
|
465 |
#ifdef _DEBUG
|
|
466 |
if (pN && iName)
|
|
467 |
{
|
|
468 |
__DEBUG_SAVE(pN);
|
|
469 |
__DEBUG_RESTORE(iName);
|
|
470 |
}
|
|
471 |
#endif
|
|
472 |
NKern::FMSignal(&Lock);
|
|
473 |
if (pN)
|
|
474 |
Kern::Free(pN);
|
|
475 |
return KErrNone;
|
|
476 |
}
|
|
477 |
|
|
478 |
|
|
479 |
/** Sets the object's owner, clearing any previous owner.
|
|
480 |
|
|
481 |
If the object's owner had already been set the old owner is replaced. If aOwner
|
|
482 |
is NULL the object ends up with no owner.
|
|
483 |
An extra reference is opened on the new owner. The old owner, if any, has a
|
|
484 |
reference closed to compensate.
|
|
485 |
|
|
486 |
@param aOwner Pointer to DObject-derived class which is the new owner
|
|
487 |
NULL if there is no new owner
|
|
488 |
|
|
489 |
@return KErrNone, if name set successfully;
|
|
490 |
KErrGeneral, if new owner had zero access count.
|
|
491 |
|
|
492 |
@pre Calling thread must be in a critical section.
|
|
493 |
@pre No fast mutex can be held.
|
|
494 |
@pre Call in a thread context.
|
|
495 |
@pre Kernel must be unlocked
|
|
496 |
@pre interrupts enabled
|
|
497 |
|
|
498 |
@internalComponent
|
|
499 |
*/
|
|
500 |
EXPORT_C TInt DObject::SetOwner(DObject* aOwner)
|
|
501 |
{
|
|
502 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObject::SetOwner");
|
|
503 |
__KTRACE_OPT(KOBJECT,Kern::Printf("SetOwner %O (%O)",this,aOwner));
|
|
504 |
NKern::FMWait(&Lock);
|
|
505 |
DObject* pO=iOwner;
|
|
506 |
iOwner=NULL;
|
|
507 |
TInt r=KErrNone;
|
|
508 |
if (aOwner)
|
|
509 |
{
|
|
510 |
r=aOwner->Open();
|
|
511 |
if (r==KErrNone)
|
|
512 |
{
|
|
513 |
iOwner=aOwner;
|
|
514 |
}
|
|
515 |
}
|
|
516 |
NKern::FMSignal(&Lock);
|
|
517 |
if (pO)
|
|
518 |
pO->Close(NULL);
|
|
519 |
return r;
|
|
520 |
}
|
|
521 |
|
|
522 |
|
|
523 |
/** Notifies an object that a handle to it has been requested by a user thread.
|
|
524 |
|
|
525 |
This is the default implementation of the virtual function.
|
|
526 |
|
|
527 |
This function allows a DObject-derived class to control access to itself
|
|
528 |
from user threads. For example it might wish to restrict its use to a single
|
|
529 |
thread or to a single user process.
|
|
530 |
|
|
531 |
@param aThread Pointer to thread which requests the handle
|
|
532 |
@param aType Whether requested handle is thread or process relative
|
|
533 |
|
|
534 |
@return KErrNone, if it is acceptable for the handle to be created;
|
|
535 |
KErrAccessDenied, if it is not acceptable.
|
|
536 |
|
|
537 |
@pre Calling thread must be in a critical section.
|
|
538 |
@pre No fast mutex can be held.
|
|
539 |
@pre Call in a thread context.
|
|
540 |
@pre Kernel must be unlocked
|
|
541 |
@pre interrupts enabled
|
|
542 |
*/
|
|
543 |
EXPORT_C TInt DObject::RequestUserHandle(DThread* aThread, TOwnerType aType)
|
|
544 |
{
|
|
545 |
(void)aThread;
|
|
546 |
(void)aType;
|
|
547 |
return KErrNone;
|
|
548 |
}
|
|
549 |
|
|
550 |
/** Notifies an object that a handle to it has been requested by a user thread.
|
|
551 |
|
|
552 |
This is the default implementation of the virtual function.
|
|
553 |
|
|
554 |
This function allows a DObject-derived class to control access to itself
|
|
555 |
from user threads. For example it might wish to restrict its use to a single
|
|
556 |
thread or to a single user process.
|
|
557 |
|
|
558 |
@param aThread Pointer to thread which requests the handle
|
|
559 |
@param aType Whether requested handle is thread or process relative
|
|
560 |
@param aAttr Handle attributes
|
|
561 |
|
|
562 |
@return KErrNone, if it is acceptable for the handle to be created;
|
|
563 |
KErrAccessDenied, if it is not acceptable.
|
|
564 |
|
|
565 |
@pre Calling thread must be in a critical section.
|
|
566 |
@pre No fast mutex can be held.
|
|
567 |
@pre Call in a thread context.
|
|
568 |
@pre Kernel must be unlocked
|
|
569 |
@pre interrupts enabled
|
|
570 |
*/
|
|
571 |
|
|
572 |
EXPORT_C TInt DObject::RequestUserHandle(DThread* aThread, TOwnerType aType, TUint /* aAttr*/)
|
|
573 |
{
|
|
574 |
return RequestUserHandle(aThread, aType);
|
|
575 |
}
|
|
576 |
|
|
577 |
/** Notifies an object that a handle to it has been opened by a user thread.
|
|
578 |
|
|
579 |
This is the default implementation of the virtual function.
|
|
580 |
|
|
581 |
This function allows a DObject-derived class to track which user processes
|
|
582 |
are using it. For example a library knows when it must be mapped into a
|
|
583 |
user process address space and when it can be unmapped.
|
|
584 |
|
|
585 |
@param aProc Pointer to process which has opened the handle.
|
|
586 |
|
|
587 |
@return KErrNone, if no error;
|
|
588 |
negative (<0) value, if error.
|
|
589 |
|
|
590 |
@pre Calling thread must be in a critical section.
|
|
591 |
@pre No fast mutex can be held.
|
|
592 |
@pre Call in a thread context.
|
|
593 |
|
|
594 |
@internalComponent
|
|
595 |
*/
|
|
596 |
EXPORT_C TInt DObject::AddToProcess(DProcess* /*aProc*/)
|
|
597 |
{
|
|
598 |
CHECK_PRECONDITIONS(MASK_CRITICAL|MASK_NO_FAST_MUTEX|MASK_NOT_ISR|MASK_NOT_IDFC,"DObject::AddToProcess");
|
|
599 |
return KErrNone;
|
|
600 |
}
|
|
601 |
|
|
602 |
/** Notifies an object that a handle to it has been opened by a user thread.
|
|
603 |
|
|
604 |
This is the default implementation of the virtual function.
|
|
605 |
|
|
606 |
This function allows a DObject-derived class to track which user processes
|
|
607 |
are using it. For example a library knows when it must be mapped into a
|
|
608 |
user process address space and when it can be unmapped.
|
|
609 |
|
|
610 |
@param aProc Pointer to process which has opened the handle.
|
|
611 |
@param aAttr Handle attributes.
|
|
612 |
|
|
613 |
@return KErrNone, if no error;
|
|
614 |
negative (<0) value, if error.
|
|
615 |
|
|
616 |
@pre Calling thread must be in a critical section.
|
|
617 |
@pre No fast mutex can be held.
|
|
618 |
@pre Call in a thread context.
|
|
619 |
|
|
620 |
@internalComponent
|
|
621 |
*/
|
|
622 |
EXPORT_C TInt DObject::AddToProcess(DProcess* aProc, TUint /* aAttr */)
|
|
623 |
{
|
|
624 |
CHECK_PRECONDITIONS(MASK_CRITICAL|MASK_NO_FAST_MUTEX|MASK_NOT_ISR|MASK_NOT_IDFC,"DObject::AddToProcess");
|
|
625 |
return AddToProcess(aProc);
|
|
626 |
}
|
|
627 |
|
|
628 |
/** Gets the object's 'base' name into a descriptor. This is its name without
|
|
629 |
any adornments given to it (like process generation numbers or UID values).
|
|
630 |
|
|
631 |
@param aName Writeable descriptor to which name should be copied.
|
|
632 |
|
|
633 |
@pre No fast mutex can be held.
|
|
634 |
@pre Call in a thread context.
|
|
635 |
@pre Kernel must be unlocked
|
|
636 |
@pre interrupts enabled
|
|
637 |
*/
|
|
638 |
void DObject::BaseName(TDes& aName)
|
|
639 |
{
|
|
640 |
aName.Zero();
|
|
641 |
NKern::FMWait(&Lock);
|
|
642 |
DObject::DoAppendName(aName);
|
|
643 |
NKern::FMSignal(&Lock);
|
|
644 |
}
|
|
645 |
|
|
646 |
DObjectCon::DObjectCon(TInt aUniqueID)
|
|
647 |
//
|
|
648 |
// Constructor
|
|
649 |
//
|
|
650 |
: iUniqueID(aUniqueID)
|
|
651 |
{
|
|
652 |
}
|
|
653 |
|
|
654 |
DObjectCon::~DObjectCon()
|
|
655 |
//
|
|
656 |
// Destructor
|
|
657 |
//
|
|
658 |
{
|
|
659 |
// this should never occur
|
|
660 |
Panic(EDObjectConDestroyed);
|
|
661 |
}
|
|
662 |
|
|
663 |
|
|
664 |
/** Adds an object to the container.
|
|
665 |
|
|
666 |
The object's full name must be unique among objects in this container.
|
|
667 |
|
|
668 |
@param aObj Pointer to the object to be added.
|
|
669 |
|
|
670 |
@return KErrNone, if successful;
|
|
671 |
KErrBadHandle, if object's name contains invalid characters;
|
|
672 |
KErrAlreadyExists, if another object's full name matches the one to be added;
|
|
673 |
KErrNoMemory, if there is insufficient memory to expand the array.
|
|
674 |
|
|
675 |
@pre Calling thread must be in a critical section.
|
|
676 |
@pre No fast mutex can be held.
|
|
677 |
@pre Call in a thread context.
|
|
678 |
@pre Kernel must be unlocked
|
|
679 |
@pre interrupts enabled
|
|
680 |
*/
|
|
681 |
EXPORT_C TInt DObjectCon::Add(DObject* aObj)
|
|
682 |
{
|
|
683 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::Add");
|
|
684 |
Wait();
|
|
685 |
TInt r = KErrNone;
|
|
686 |
|
|
687 |
// If object is named, check the name doesn't clash with any object already in the container
|
|
688 |
if (aObj->iName)
|
|
689 |
{
|
|
690 |
r = CheckUniqueFullName(aObj);
|
|
691 |
if (r!=KErrNone)
|
|
692 |
{
|
|
693 |
Signal();
|
|
694 |
return r;
|
|
695 |
}
|
|
696 |
}
|
|
697 |
|
|
698 |
if (iCount==iAllocated)
|
|
699 |
{
|
|
700 |
#ifdef _DEBUG
|
|
701 |
TInt newAlloc=iAllocated + 1;
|
|
702 |
#else
|
|
703 |
TInt newAlloc;
|
|
704 |
if (iAllocated==0)
|
|
705 |
newAlloc = KObjectConMinSize;
|
|
706 |
else
|
|
707 |
{
|
|
708 |
// increase in sequence 8, 12, 16, 24, ... , 2^n, 3*2^n-1, ...
|
|
709 |
// can't get sign problems since iAllocated can't exceed 0x20000000
|
|
710 |
newAlloc = iAllocated + (iAllocated>>1) - ((iAllocated&(iAllocated-1))>>2);
|
|
711 |
}
|
|
712 |
#endif
|
|
713 |
r=Kern::SafeReAlloc((TAny*&)iObjects,iCount*sizeof(DObject*),newAlloc*sizeof(DObject*));
|
|
714 |
if (r!=KErrNone)
|
|
715 |
{
|
|
716 |
Signal();
|
|
717 |
return r;
|
|
718 |
}
|
|
719 |
iAllocated=newAlloc;
|
|
720 |
}
|
|
721 |
iObjects[iCount++]=aObj;
|
|
722 |
aObj->iContainerID=(TUint8)this->UniqueID();
|
|
723 |
Signal();
|
|
724 |
return KErrNone;
|
|
725 |
}
|
|
726 |
|
|
727 |
|
|
728 |
/** Removes an object from the container.
|
|
729 |
|
|
730 |
@param aObj Pointer to object to be removed. The object must be present.
|
|
731 |
|
|
732 |
@pre Calling thread must be in a critical section.
|
|
733 |
@pre No fast mutex can be held.
|
|
734 |
@pre Call in a thread context.
|
|
735 |
@pre Kernel must be unlocked
|
|
736 |
@pre interrupts enabled
|
|
737 |
*/
|
|
738 |
EXPORT_C void DObjectCon::Remove(DObject* aObj)
|
|
739 |
{
|
|
740 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::Remove");
|
|
741 |
Wait();
|
|
742 |
|
|
743 |
// The object to be removed is often the last one, so search backwards
|
|
744 |
DObject** pS=iObjects;
|
|
745 |
DObject** pO=pS+iCount;
|
|
746 |
DObject** pE=pO-1;
|
|
747 |
while(--pO>=pS && *pO!=aObj) {}
|
|
748 |
if (pO<pS)
|
|
749 |
Panic(EObjRemoveObjectNotFound);
|
|
750 |
|
|
751 |
if (pO<pE)
|
|
752 |
wordmove((TAny*)pO,(TAny*)(pO+1),TInt(pE)-TInt(pO));
|
|
753 |
--iCount;
|
|
754 |
#ifdef _DEBUG
|
|
755 |
TInt newAlloc=iCount;
|
|
756 |
#else
|
|
757 |
TInt newAlloc = 0;
|
|
758 |
if (iCount) // if now empty, free all memory
|
|
759 |
{
|
|
760 |
newAlloc = iAllocated;
|
|
761 |
if (iAllocated > KObjectConMinSize) // don't shrink below minimum size
|
|
762 |
{
|
|
763 |
// calculate next size down
|
|
764 |
TInt na2 = iAllocated - (iAllocated>>2) - ((iAllocated&(iAllocated-1))>>3);
|
|
765 |
|
|
766 |
// shrink if half full or 64 less than next size down, whichever comes first
|
|
767 |
if (iCount <= Max(iAllocated>>1, na2-64))
|
|
768 |
newAlloc = na2;
|
|
769 |
}
|
|
770 |
}
|
|
771 |
#endif
|
|
772 |
if (newAlloc!=iAllocated)
|
|
773 |
{
|
|
774 |
Kern::SafeReAlloc((TAny*&)iObjects,iAllocated*sizeof(DObject*),newAlloc*sizeof(DObject*));
|
|
775 |
iAllocated=newAlloc;
|
|
776 |
}
|
|
777 |
Signal();
|
|
778 |
}
|
|
779 |
|
|
780 |
|
|
781 |
#ifndef __DOBJECT_MACHINE_CODED__
|
|
782 |
/** Looks up the object at a specified array index within the container.
|
|
783 |
|
|
784 |
@param aIndex Array index to look up.
|
|
785 |
|
|
786 |
@return Pointer to object at that position.
|
|
787 |
|
|
788 |
@pre Call in a thread context.
|
|
789 |
@pre Container mutex must be held.
|
|
790 |
*/
|
|
791 |
EXPORT_C DObject* DObjectCon::operator[](TInt aIndex)
|
|
792 |
{
|
|
793 |
CHECK_PRECONDITIONS(MASK_NOT_ISR|MASK_NOT_IDFC,"DObjectCon::operator[]");
|
|
794 |
|
|
795 |
__ASSERT_WITH_MESSAGE_MUTEX(iMutex,"Container mutex must be held","DObjectCon::operator[])");
|
|
796 |
__ASSERT_ALWAYS(aIndex>=0 && aIndex<iCount, Panic(EArrayIndexOutOfRange));
|
|
797 |
return iObjects[aIndex];
|
|
798 |
}
|
|
799 |
|
|
800 |
|
|
801 |
/** Looks up the object in the container by find handle.
|
|
802 |
|
|
803 |
@param aFindHandle Find handle to look up.
|
|
804 |
|
|
805 |
@return Pointer to object, NULL if find handle invalid.
|
|
806 |
|
|
807 |
@pre Calling thread must be in a critical section.
|
|
808 |
@pre No fast mutex can be held.
|
|
809 |
@pre Call in a thread context.
|
|
810 |
@pre Container mutex must be held.
|
|
811 |
@pre Kernel must be unlocked
|
|
812 |
@pre interrupts enabled
|
|
813 |
*/
|
|
814 |
EXPORT_C DObject *DObjectCon::At(const TFindHandle& aFindHandle)
|
|
815 |
{
|
|
816 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::At(TInt aFindHandle)");
|
|
817 |
__ASSERT_WITH_MESSAGE_MUTEX(iMutex,"Container mutex must be held","DObjectCon::At");
|
|
818 |
if (aFindHandle.UniqueID()!=iUniqueID || iCount==0)
|
|
819 |
return NULL;
|
|
820 |
TUint64 objectId=aFindHandle.ObjectID();
|
|
821 |
DObject** pS=iObjects;
|
|
822 |
DObject** pO=pS+Min(aFindHandle.Index(), iCount-1);
|
|
823 |
while (pO>pS && (*pO)->ObjectId()>objectId)
|
|
824 |
--pO;
|
|
825 |
return (*pO)->ObjectId()==objectId ? *pO : NULL;
|
|
826 |
}
|
|
827 |
|
|
828 |
|
|
829 |
#else
|
|
830 |
GLDEF_C void PanicDObjectConIndexOutOfRange(void)
|
|
831 |
{
|
|
832 |
Panic(EArrayIndexOutOfRange);
|
|
833 |
}
|
|
834 |
#endif
|
|
835 |
|
|
836 |
|
|
837 |
/** Checks whether an object would have a valid unique full name if it were renamed.
|
|
838 |
|
|
839 |
Checks the object's full name assuming that the object owner is aOwner and the short
|
|
840 |
name is aName.
|
|
841 |
|
|
842 |
@param aOwner Pointer to owner of the object.
|
|
843 |
@param aName Short name to check.
|
|
844 |
|
|
845 |
@return KErrNone, if OK;
|
|
846 |
KErrBadName, if the supplied short name is invalid;
|
|
847 |
KErrAlreadyExists, if the full name would not be unique.
|
|
848 |
|
|
849 |
@pre Calling thread must be in a critical section.
|
|
850 |
@pre No fast mutex can be held.
|
|
851 |
@pre Call in a thread context.
|
|
852 |
@pre Container mutex must be held.
|
|
853 |
@pre Kernel must be unlocked
|
|
854 |
@pre interrupts enabled
|
|
855 |
*/
|
|
856 |
EXPORT_C TInt DObjectCon::CheckUniqueFullName(DObject* aOwner, const TDesC& aName)
|
|
857 |
{
|
|
858 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::CheckUniqueFullName(DObject* aOwner, const TDesC& aName)");
|
|
859 |
__ASSERT_WITH_MESSAGE_MUTEX(iMutex,"Container mutex must be held","DObjectCon::CheckUniqueFullName(DObject* aOwner, const TDesC& aName)");
|
|
860 |
TInt r=Kern::ValidateName(aName);
|
|
861 |
if (r!=KErrNone)
|
|
862 |
return r;
|
|
863 |
if (!iCount)
|
|
864 |
return KErrNone;
|
|
865 |
|
|
866 |
DObject** pS=iObjects;
|
|
867 |
DObject** pE=pS+iCount;
|
|
868 |
do
|
|
869 |
{
|
|
870 |
DObject* pO=*pS;
|
|
871 |
if (pO->iAccessCount>0 && NamesMatch(aOwner,aName,pO) && pO->iAccessCount>0)
|
|
872 |
return KErrAlreadyExists;
|
|
873 |
} while(++pS<pE);
|
|
874 |
return KErrNone;
|
|
875 |
}
|
|
876 |
|
|
877 |
|
|
878 |
TBool DObjectCon::NamesMatch(DObject* aOwner, const TDesC& aObjectName, DObject* aCurrentObject)
|
|
879 |
//
|
|
880 |
// Return TRUE if aCurrentObject has the same full name as aOwner::aObjectName
|
|
881 |
// Enter and leave with no fast mutexes held
|
|
882 |
//
|
|
883 |
{
|
|
884 |
TBool result=EFalse;
|
|
885 |
NKern::FMWait(&DObject::Lock);
|
|
886 |
|
|
887 |
// if current object has no name or owners are different, not the same
|
|
888 |
if (aCurrentObject->iName && aCurrentObject->iOwner==aOwner)
|
|
889 |
{
|
|
890 |
// same owners, so compare short names
|
|
891 |
TKName n;
|
|
892 |
aCurrentObject->DoAppendName(n);
|
|
893 |
if (aObjectName.MatchF(n)==0)
|
|
894 |
result=ETrue;
|
|
895 |
}
|
|
896 |
NKern::FMSignal(&DObject::Lock);
|
|
897 |
return result;
|
|
898 |
}
|
|
899 |
|
|
900 |
|
|
901 |
/** Checks whether the full name of an object would be valid in this container.
|
|
902 |
|
|
903 |
@param aObject Pointer to object to check.
|
|
904 |
|
|
905 |
@return KErrNone, if OK;
|
|
906 |
KErrBadName, if the object's short name is invalid;
|
|
907 |
KErrAlreadyExists, if the full name would not be unique.
|
|
908 |
|
|
909 |
@pre Calling thread must be in a critical section.
|
|
910 |
@pre No fast mutex can be held.
|
|
911 |
@pre Call in a thread context.
|
|
912 |
@pre Container mutex must be held.
|
|
913 |
@pre Kernel must be unlocked
|
|
914 |
@pre interrupts enabled
|
|
915 |
*/
|
|
916 |
EXPORT_C TInt DObjectCon::CheckUniqueFullName(DObject* aObject)
|
|
917 |
{
|
|
918 |
|
|
919 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::CheckUniqueFullName(DObject* aObject)");
|
|
920 |
__ASSERT_WITH_MESSAGE_MUTEX(iMutex,"Container mutex must be held","DObjectCon::CheckUniqueFullName(DObject* aObject)");
|
|
921 |
|
|
922 |
__KTRACE_OPT(KOBJECT,Kern::Printf("DObjectCon::CheckUniqueFullName(%O)",aObject));
|
|
923 |
__KTRACE_OPT(KOBJECT,Kern::Printf("DObjectCon::iCount=%d",iCount));
|
|
924 |
|
|
925 |
TKName name;
|
|
926 |
aObject->Name(name);
|
|
927 |
TInt r=Kern::ValidateName(name);
|
|
928 |
__KTRACE_OPT(KOBJECT,Kern::Printf("ValidateName returns %d",r));
|
|
929 |
if (r!=KErrNone)
|
|
930 |
return r;
|
|
931 |
|
|
932 |
if (!iCount)
|
|
933 |
return KErrNone;
|
|
934 |
|
|
935 |
DObject** pS=iObjects;
|
|
936 |
DObject** pE=pS+iCount;
|
|
937 |
|
|
938 |
if (aObject->iName)
|
|
939 |
{
|
|
940 |
do
|
|
941 |
{
|
|
942 |
DObject* pO=*pS;
|
|
943 |
if (pO->iAccessCount>0 && NamesMatch(aObject->iOwner,name,pO) && pO->iAccessCount>0)
|
|
944 |
return KErrAlreadyExists;
|
|
945 |
} while(++pS<pE);
|
|
946 |
}
|
|
947 |
else
|
|
948 |
{
|
|
949 |
// object has no name, just compare pointers
|
|
950 |
do
|
|
951 |
{
|
|
952 |
DObject* pO=*pS;
|
|
953 |
if (pO==aObject)
|
|
954 |
return KErrAlreadyExists;
|
|
955 |
} while(++pS<pE);
|
|
956 |
}
|
|
957 |
|
|
958 |
return KErrNone;
|
|
959 |
}
|
|
960 |
|
|
961 |
|
|
962 |
/** Finds an object in the container by short name.
|
|
963 |
|
|
964 |
Starts from a specified find handle and returns a find handle to the object.
|
|
965 |
Unnamed objects will never be returned. Objects with zero access count will
|
|
966 |
also never be returned.
|
|
967 |
|
|
968 |
@param aFindHandle Find handle to start from and place to put returned find handle.
|
|
969 |
@param aMatch Match string to compare object names against.
|
|
970 |
@param aName Returns short name of object found.
|
|
971 |
|
|
972 |
@return KErrNone, if an object was found;
|
|
973 |
KErrBadHandle, if aFindHandle was invalid;
|
|
974 |
KErrNotFound, if no object with a matching name was found.
|
|
975 |
|
|
976 |
@pre Calling thread must be in a critical section.
|
|
977 |
@pre No fast mutex can be held.
|
|
978 |
@pre Call in a thread context.
|
|
979 |
@pre Kernel must be unlocked
|
|
980 |
@pre interrupts enabled
|
|
981 |
*/
|
|
982 |
EXPORT_C TInt DObjectCon::FindByName(TFindHandle& aFindHandle, const TDesC& aMatch, TKName& aName)
|
|
983 |
{
|
|
984 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::FindByName");
|
|
985 |
|
|
986 |
if (aFindHandle.Handle()!=0 && aFindHandle.UniqueID()!=iUniqueID)
|
|
987 |
return KErrBadHandle;
|
|
988 |
Wait();
|
|
989 |
DObject** pS=iObjects;
|
|
990 |
DObject** pE=pS+iCount;
|
|
991 |
DObject** pP=pS;
|
|
992 |
if (aFindHandle.Handle()!=0)
|
|
993 |
{
|
|
994 |
// find start position - either after the last object found, or the
|
|
995 |
// oldest object younger than it, if it no longer exists
|
|
996 |
pP+=Min(aFindHandle.Index(), iCount-1);
|
|
997 |
while (pP>=pS && (*pP)->ObjectId()>aFindHandle.ObjectID())
|
|
998 |
--pP;
|
|
999 |
++pP;
|
|
1000 |
}
|
|
1001 |
while(pP<pE)
|
|
1002 |
{
|
|
1003 |
DObject* pO=*pP;
|
|
1004 |
if (pO->iAccessCount>0 && pO->NameBuf())
|
|
1005 |
{
|
|
1006 |
pO->Name(aName);
|
|
1007 |
if (aName.MatchF(aMatch)!=KErrNotFound)
|
|
1008 |
{
|
|
1009 |
if (pO->iAccessCount>0)
|
|
1010 |
{
|
|
1011 |
aFindHandle.Set(pP-pS,iUniqueID,pO->ObjectId());
|
|
1012 |
Signal();
|
|
1013 |
return(KErrNone);
|
|
1014 |
}
|
|
1015 |
}
|
|
1016 |
}
|
|
1017 |
++pP;
|
|
1018 |
}
|
|
1019 |
Signal();
|
|
1020 |
aName.Zero();
|
|
1021 |
aFindHandle.Set(KObjectMaxIndex,iUniqueID,KMaxTInt64);
|
|
1022 |
return(KErrNotFound);
|
|
1023 |
}
|
|
1024 |
|
|
1025 |
|
|
1026 |
/** Finds an object in the container by full name.
|
|
1027 |
|
|
1028 |
Starts from a specified find handle and returns a find handle to the object.
|
|
1029 |
Objects with zero access count will never be returned.
|
|
1030 |
Unnamed objects may be returned unless the calling thread has secure APIs
|
|
1031 |
enabled.
|
|
1032 |
|
|
1033 |
@param aFindHandle Find handle to start from and place to put returned find handle.
|
|
1034 |
@param aMatch Match string to compare object full names against.
|
|
1035 |
@param aFullName Returns full name of object found.
|
|
1036 |
|
|
1037 |
@return KErrNone, if an object was found;
|
|
1038 |
KErrBadHandle, if aFindHandle was invalid;
|
|
1039 |
KErrNotFound, if no object with a matching name was found.
|
|
1040 |
|
|
1041 |
@pre Calling thread must be in a critical section.
|
|
1042 |
@pre No fast mutex can be held.
|
|
1043 |
@pre Call in a thread context.
|
|
1044 |
@pre Kernel must be unlocked
|
|
1045 |
@pre interrupts enabled
|
|
1046 |
*/
|
|
1047 |
EXPORT_C TInt DObjectCon::FindByFullName(TFindHandle& aFindHandle, const TDesC& aMatch, TFullName& aFullName)
|
|
1048 |
{
|
|
1049 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::FindByFullName");
|
|
1050 |
TInt processIsolation = TheSuperPage().KernelConfigFlags() & EKernelConfigPlatSecProcessIsolation;
|
|
1051 |
if (aFindHandle.Handle()!=0 && aFindHandle.UniqueID()!=iUniqueID)
|
|
1052 |
return KErrBadHandle;
|
|
1053 |
Wait();
|
|
1054 |
DObject** pS=iObjects;
|
|
1055 |
DObject** pE=pS+iCount;
|
|
1056 |
DObject** pP=pS;
|
|
1057 |
if (aFindHandle.Handle()!=0)
|
|
1058 |
{
|
|
1059 |
// find start position - either after the last object found, or the
|
|
1060 |
// oldest object younger than it, if it no longer exists
|
|
1061 |
pP+=Min(aFindHandle.Index(), iCount-1);
|
|
1062 |
while (pP>=pS && (*pP)->ObjectId()>aFindHandle.ObjectID())
|
|
1063 |
--pP;
|
|
1064 |
++pP;
|
|
1065 |
}
|
|
1066 |
while(pP<pE)
|
|
1067 |
{
|
|
1068 |
DObject* pO=*pP;
|
|
1069 |
if (pO->iAccessCount>0 && (pO->iName || !processIsolation))
|
|
1070 |
{
|
|
1071 |
pO->FullName(aFullName);
|
|
1072 |
if (aFullName.MatchF(aMatch)!=KErrNotFound)
|
|
1073 |
{
|
|
1074 |
if (pO->iAccessCount>0)
|
|
1075 |
{
|
|
1076 |
aFindHandle.Set(pP-pS,iUniqueID,pO->ObjectId());
|
|
1077 |
Signal();
|
|
1078 |
return(KErrNone);
|
|
1079 |
}
|
|
1080 |
}
|
|
1081 |
}
|
|
1082 |
++pP;
|
|
1083 |
}
|
|
1084 |
Signal();
|
|
1085 |
aFullName.Zero();
|
|
1086 |
aFindHandle.Set(KObjectMaxIndex,iUniqueID,KMaxTInt64);
|
|
1087 |
return(KErrNotFound);
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
|
|
1091 |
/** Finds and opens an object in the container by full name.
|
|
1092 |
|
|
1093 |
Starts from a specified find handle and returns a find handle to the object.
|
|
1094 |
The reference count of the object found is incremented.
|
|
1095 |
Objects with zero access count will never be returned.
|
|
1096 |
In addition if the calling thread has secure APIs enabled, unnamed objects
|
|
1097 |
and objects which are not global will never be returned.
|
|
1098 |
|
|
1099 |
@param aFindHandle Find handle to start from and place to put returned find handle.
|
|
1100 |
@param aMatch Match string to compare object full names against.
|
|
1101 |
@param aFullName Returns full name of object found.
|
|
1102 |
|
|
1103 |
@return KErrNone, if an object was found;
|
|
1104 |
KErrBadHandle, if aFindHandle was invalid;
|
|
1105 |
KErrNotFound, if no object with a matching name was found.
|
|
1106 |
|
|
1107 |
@pre Calling thread must be in a critical section.
|
|
1108 |
@pre No fast mutex can be held.
|
|
1109 |
@pre Call in a thread context.
|
|
1110 |
@pre Kernel must be unlocked
|
|
1111 |
@pre interrupts enabled
|
|
1112 |
*/
|
|
1113 |
EXPORT_C TInt DObjectCon::OpenByFullName(DObject*& aObject, const TDesC& aName)
|
|
1114 |
{
|
|
1115 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DObjectCon::OpenByFullName");
|
|
1116 |
TInt processIsolation = TheSuperPage().KernelConfigFlags() & EKernelConfigPlatSecProcessIsolation;
|
|
1117 |
|
|
1118 |
aObject=NULL;
|
|
1119 |
Wait();
|
|
1120 |
TFullName fn;
|
|
1121 |
DObject** pS=iObjects;
|
|
1122 |
DObject** pE=pS+iCount;
|
|
1123 |
while(pS<pE)
|
|
1124 |
{
|
|
1125 |
DObject* pO=*pS++;
|
|
1126 |
if (pO->iAccessCount>0)
|
|
1127 |
{
|
|
1128 |
if(!pO->iName && processIsolation)
|
|
1129 |
{ continue; }
|
|
1130 |
|
|
1131 |
pO->FullName(fn);
|
|
1132 |
if (fn.MatchF(aName)!=KErrNotFound)
|
|
1133 |
{
|
|
1134 |
if(pO->Protection()!=DObject::EGlobal && processIsolation)
|
|
1135 |
{
|
|
1136 |
Signal();
|
|
1137 |
return KErrPermissionDenied;
|
|
1138 |
}
|
|
1139 |
|
|
1140 |
TInt r=pO->Open();
|
|
1141 |
if (r==KErrNone)
|
|
1142 |
{
|
|
1143 |
Signal();
|
|
1144 |
aObject=pO;
|
|
1145 |
return KErrNone;
|
|
1146 |
}
|
|
1147 |
}
|
|
1148 |
}
|
|
1149 |
}
|
|
1150 |
Signal();
|
|
1151 |
return KErrNotFound;
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
DObjectCon* DObjectCon::New(TInt aUniqueID)
|
|
1155 |
{
|
|
1156 |
DObjectCon* pC=new DObjectCon(aUniqueID);
|
|
1157 |
if (pC)
|
|
1158 |
{
|
|
1159 |
TBuf<16> n(KLitDObjectCon());
|
|
1160 |
n.AppendNum(pC->UniqueID());
|
|
1161 |
TInt r=K::MutexCreate(pC->iMutex, n, NULL, EFalse, KMutexOrdObjectCon);
|
|
1162 |
if (r==KErrNone)
|
|
1163 |
{
|
|
1164 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Container %d created OK",pC->UniqueID()));
|
|
1165 |
return pC;
|
|
1166 |
}
|
|
1167 |
__KTRACE_OPT(KOBJECT,Kern::Printf("Error %d creating mutex %lS",r,&n));
|
|
1168 |
}
|
|
1169 |
return NULL;
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
/** Returns the kernel object containers array.
|
|
1173 |
|
|
1174 |
@return Pointer to containers array. The array contains ENumObjectTypes
|
|
1175 |
containers and is indexed by TObjectType enum values.
|
|
1176 |
*/
|
|
1177 |
EXPORT_C DObjectCon* const *Kern::Containers()
|
|
1178 |
{
|
|
1179 |
return K::Containers;
|
|
1180 |
}
|
|
1181 |
|
|
1182 |
DObjectCon* K::ContainerFromFindHandle(const TFindHandle& aFindHandle)
|
|
1183 |
{
|
|
1184 |
TInt u=aFindHandle.UniqueID()-1;
|
|
1185 |
if ((TUint)u<(TUint)ENumObjectTypes)
|
|
1186 |
return K::Containers[u];
|
|
1187 |
return NULL;
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
TInt K::CreateObjectContainers()
|
|
1191 |
{
|
|
1192 |
DObjectCon** pC=K::Containers;
|
|
1193 |
TInt u;
|
|
1194 |
TInt r;
|
|
1195 |
for (u=1; u<=ENumObjectTypes; ++u)
|
|
1196 |
{
|
|
1197 |
*pC=DObjectCon::New(u);
|
|
1198 |
if (!*pC)
|
|
1199 |
return KErrNoMemory;
|
|
1200 |
++pC;
|
|
1201 |
}
|
|
1202 |
K::Containers[EServer]->Lock()->iOrder = KMutexOrdObjectCon2;
|
|
1203 |
r=K::MutexCreate(RObjectIx::HandleMutex, KLitHandleMutex, NULL, EFalse, KMutexOrdHandle);
|
|
1204 |
return r;
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
void K::LockContainer(TInt aObjType)
|
|
1208 |
{
|
|
1209 |
K::Containers[aObjType]->Wait();
|
|
1210 |
}
|
|
1211 |
|
|
1212 |
void K::UnlockContainer(TInt aObjType)
|
|
1213 |
{
|
|
1214 |
K::Containers[aObjType]->Signal();
|
|
1215 |
}
|
|
1216 |
|
|
1217 |
TInt K::AddObject(DObject* aObj, TInt aType)
|
|
1218 |
{
|
|
1219 |
return K::Containers[aType]->Add(aObj);
|
|
1220 |
}
|
|
1221 |
|
|
1222 |
#ifndef __DES8_MACHINE_CODED__
|
|
1223 |
|
|
1224 |
#ifndef __KERNEL_MODE__
|
|
1225 |
#error "TDesC is not 8-bit as __KERNEL_MODE__ is not defined (see e32cmn.h)"
|
|
1226 |
#endif //__KERNEL_MODE
|
|
1227 |
/** Checks whether the specified name is a valid DObject full name.
|
|
1228 |
|
|
1229 |
A name is deemed to be invalid, if it contains an asterisk or a question
|
|
1230 |
mark.
|
|
1231 |
|
|
1232 |
@param aName Name to check.
|
|
1233 |
|
|
1234 |
@return KErrNone, if valid;
|
|
1235 |
KErrBadName, if invalid.
|
|
1236 |
*/
|
|
1237 |
EXPORT_C TInt Kern::ValidateFullName(const TDesC& aName)
|
|
1238 |
{
|
|
1239 |
TUint8* pName = const_cast<TUint8*>(aName.Ptr());
|
|
1240 |
TInt pNameLen = aName.Length();
|
|
1241 |
for(;pNameLen;pName++,pNameLen--)
|
|
1242 |
if(*pName>0x7e || *pName<0x20 || *pName=='*' || *pName=='?')
|
|
1243 |
return(KErrBadName);
|
|
1244 |
return(KErrNone);
|
|
1245 |
}
|
|
1246 |
#endif //#if !defined(__DES8_MACHINE_CODED__)
|
|
1247 |
|
|
1248 |
|
|
1249 |
|