memspy/Driver/Kernel/Source/MemSpyDriverObjectIx.cpp
branchRCL_3
changeset 20 ca8a1b6995f6
parent 0 a03f92240627
child 21 52e343bb8f80
equal deleted inserted replaced
19:07b41fa8d1dd 20:ca8a1b6995f6
    53 
    53 
    54 inline void Panic(TDObjectPanic aPanic)
    54 inline void Panic(TDObjectPanic aPanic)
    55 	{ Kern::Fault("DOBJECT",aPanic); }
    55 	{ Kern::Fault("DOBJECT",aPanic); }
    56 	
    56 	
    57 
    57 
       
    58 TBool MemSpyObjectIx::Find(DObject* aObj)
       
    59 	{
       
    60 	//Check preconditions(debug build only)
       
    61 	__ASSERT_CRITICAL;
       
    62 	__ASSERT_NO_FAST_MUTEX;
       
    63 
       
    64 	// I don't like the implementation of At() that was here before, it wasn't safe at all without HandleMutex. So I'm replacing it with a simpler
       
    65 	// version based on operator[] that only does what we need and does it safely.
       
    66 
       
    67 	TBool found = EFalse;
       
    68 	MemSpyObjectIx_HandleLookupLock();
       
    69 	const TInt count = Count();
       
    70 	for (TInt i = 0; i < count; i++)
       
    71 		{
       
    72 		if ((*this)[i] == aObj)
       
    73 			{
       
    74 			found = ETrue;
       
    75 			break;
       
    76 			}
       
    77 		}
       
    78 	MemSpyObjectIx_HandleLookupUnlock();
       
    79 	return found;
       
    80 	}
       
    81 
    58 #if MCL_ROBJECTIX_DUPLICATION
    82 #if MCL_ROBJECTIX_DUPLICATION
    59 
    83 
    60 #define asserta(x)	do { if (!(x)) { __crash(); } } while(0)
    84 #define asserta(x)	do { if (!(x)) { __crash(); } } while(0)
    61 
    85 
    62 RMemSpyObjectIx::RMemSpyObjectIx()
    86 RMemSpyObjectIx::RMemSpyObjectIx()
    63   : iRWL(TSpinLock::EOrderGenericIrqLow3)
    87   : iRWL(TSpinLock::EOrderGenericIrqLow3)
    64     {
    88     {
    65     }
    89     }
    66 
    90 
    67 
    91 
       
    92 /*
    68 void RMemSpyObjectIx::Wait()
    93 void RMemSpyObjectIx::Wait()
    69 	{
    94 	{
    70 //	Kern::MutexWait(*HandleMutex);
    95 	Kern::MutexWait(*HandleMutex);
    71 	} // RObjectIx::Wait
    96 	} // RObjectIx::Wait
    72 
    97 
    73 
    98 
    74 void RMemSpyObjectIx::Signal()
    99 void RMemSpyObjectIx::Signal()
    75 	{
   100 	{
    76 //	Kern::MutexSignal(*HandleMutex);
   101 	Kern::MutexSignal(*HandleMutex);
    77 	} // RObjectIx::Signal
   102 	} // RObjectIx::Signal
    78 
   103 */
    79 
   104 
    80 DObject* RMemSpyObjectIx::operator[](TInt aIndex)
   105 DObject* RMemSpyObjectIx::operator[](TInt aIndex)
    81 	{
   106 	{
    82 	DObject* obj = 0;
   107 	DObject* obj = 0;
    83 	AcquireReadLock();
   108 	AcquireReadLock();
    87 	ReleaseReadLock();
   112 	ReleaseReadLock();
    88 	return obj;
   113 	return obj;
    89 	} // RObjectIx::operator[]
   114 	} // RObjectIx::operator[]
    90 
   115 
    91 
   116 
    92 TInt RMemSpyObjectIx::At(DObject* aObj)
       
    93 	{
       
    94 	//Check preconditions(debug build only)
       
    95 	__ASSERT_CRITICAL;
       
    96 	__ASSERT_NO_FAST_MUTEX;
       
    97 	//__ASSERT_MUTEX(HandleMutex);
       
    98 
       
    99 	if (iState==ETerminated)
       
   100 		{
       
   101 		return KErrNotFound;
       
   102 		}
       
   103 
       
   104 	TInt h = KErrNotFound;
       
   105 	AcquireWriteLock();
       
   106 	iState = (TUint8)ESearching;		// enable monitoring of new handles
       
   107 	iModList.iMonitor.iObj = aObj;		// object to check for
       
   108 	iModList.iMonitor.iBoundary = 0;	// will change if aObj is added to a slot before this point
       
   109 	TInt pos = 0;
       
   110 	while (pos<iCount && iActiveCount)	// stop if index empty
       
   111 		{
       
   112 		TInt limit = pos + EMaxLockedIter;
       
   113 		if (limit>iCount)
       
   114 			{
       
   115 			limit = iCount;
       
   116 			}
       
   117 		while (pos<limit)
       
   118 			{
       
   119 			SSlot* slot = iSlots + pos;
       
   120 			if (Occupant(slot) == aObj)
       
   121 				{
       
   122 				// found it, finish
       
   123 				h = MakeHandle(pos, slot->iUsed.iAttr);
       
   124 				break;
       
   125 				}
       
   126 			pos++;
       
   127 			}
       
   128 		if (h>0)
       
   129 			{
       
   130 			break;	// found it, finish
       
   131 			}
       
   132 		iModList.iMonitor.iBoundary = pos;	// will change if aObj is added to a slot already checked
       
   133 		ReleaseWriteLock();	// let other threads in
       
   134 		AcquireWriteLock();
       
   135 		pos = iModList.iMonitor.iBoundary;	// next position to check
       
   136 		}
       
   137 	iState = (TUint8)ENormal;
       
   138 	ReleaseWriteLock();
       
   139 	return h;
       
   140 	} // RObjectIx::At
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 #elif MCL_DOBJECTIX_DUPLICATION
   117 #elif MCL_DOBJECTIX_DUPLICATION
   148 
   118 
   149 void DMemSpyObjectIx::Wait( DMemSpyObjectIx* /*aObjectIndex*/ )
   119 /*
       
   120 void DMemSpyObjectIx::Wait( DMemSpyObjectIx* aObjectIndex )
   150 	{
   121 	{
   151 //	Kern::MutexWait(*aObjectIndex->HandleMutex);
   122 //	Kern::MutexWait(*aObjectIndex->HandleMutex);
   152 	}
   123 	}
   153 
   124 
   154 void DMemSpyObjectIx::Signal( DMemSpyObjectIx* /*aObjectIndex*/ )
   125 void DMemSpyObjectIx::Signal( DMemSpyObjectIx* aObjectIndex )
   155 	{
   126 	{
   156 //	Kern::MutexSignal(*aObjectIndex->HandleMutex);
   127 //	Kern::MutexSignal(*aObjectIndex->HandleMutex);
   157 	}
   128 	}
   158 
   129 */
   159 
   130 
   160 /** Counts the number of times an object appears in this index.
   131 /** Counts the number of times an object appears in this index.
   161 
   132 
   162 	@param	aObject	Object whose occurrences are to be counted.
   133 	@param	aObject	Object whose occurrences are to be counted.
   163 
   134 
   235 	if (pS->str.instance!=instance(aHandle))
   206 	if (pS->str.instance!=instance(aHandle))
   236 		return NULL;
   207 		return NULL;
   237 	return pS->obj;
   208 	return pS->obj;
   238 	}
   209 	}
   239 
   210 
   240 
       
   241 
       
   242 /**	Looks up an object in the index by object pointer.
       
   243 
       
   244 	Returns a handle to the object.
       
   245 
       
   246 	@param	aObj	Pointer to the object to look up.
       
   247 	
       
   248 	@return	Handle to object (always >0);
       
   249 	        KErrNotFound, if object not present in index.
       
   250 
       
   251     @pre    Calling thread must be in a critical section.
       
   252     @pre    No fast mutex can be held.
       
   253 	@pre	Call in a thread context.
       
   254 	@pre	DObject::HandleMutex held.
       
   255  */
       
   256 TInt DMemSpyObjectIx::At(DObject* aObj)
       
   257 	{
       
   258 	//Check preconditions(debug build only)
       
   259 	__ASSERT_CRITICAL;
       
   260 	__ASSERT_NO_FAST_MUTEX;
       
   261 
       
   262 	if (iCount)
       
   263 		{
       
   264 		SDObjectIxRec* pS=iObjects;
       
   265 		SDObjectIxRec* pE=pS+iCount;
       
   266 		TInt i=0;
       
   267 		while(pS<pE && pS->obj!=aObj)
       
   268 			pS++, i++;
       
   269 		if (pS<pE)
       
   270 			return(makeHandle(i,pS->str.instance));
       
   271 		}
       
   272 	return KErrNotFound;
       
   273 	}
       
   274 
       
   275 
       
   276 /** Finds the object at a specific position in the index array.
   211 /** Finds the object at a specific position in the index array.
   277 
   212 
   278 	@param	aIndex	Index into array.
   213 	@param	aIndex	Index into array.
   279 	
   214 	
   280 	@return	Pointer to the object at that position (could be NULL).
   215 	@return	Pointer to the object at that position (could be NULL).