memspy/Driver/Kernel/Source/MemSpyDriverObjectIx.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 49 7fdc9a71d314
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
    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 
       
    82 #if MCL_ROBJECTIX_DUPLICATION
    58 #if MCL_ROBJECTIX_DUPLICATION
    83 
    59 
    84 #define asserta(x)	do { if (!(x)) { __crash(); } } while(0)
    60 #define asserta(x)	do { if (!(x)) { __crash(); } } while(0)
    85 
    61 
    86 RMemSpyObjectIx::RMemSpyObjectIx()
    62 RMemSpyObjectIx::RMemSpyObjectIx()
    87   : iRWL(TSpinLock::EOrderGenericIrqLow3)
    63   : iRWL(TSpinLock::EOrderGenericIrqLow3)
    88     {
    64     {
    89     }
    65     }
    90 
    66 
    91 
    67 
    92 /*
       
    93 void RMemSpyObjectIx::Wait()
    68 void RMemSpyObjectIx::Wait()
    94 	{
    69 	{
    95 	Kern::MutexWait(*HandleMutex);
    70 //	Kern::MutexWait(*HandleMutex);
    96 	} // RObjectIx::Wait
    71 	} // RObjectIx::Wait
    97 
    72 
    98 
    73 
    99 void RMemSpyObjectIx::Signal()
    74 void RMemSpyObjectIx::Signal()
   100 	{
    75 	{
   101 	Kern::MutexSignal(*HandleMutex);
    76 //	Kern::MutexSignal(*HandleMutex);
   102 	} // RObjectIx::Signal
    77 	} // RObjectIx::Signal
   103 */
    78 
   104 
    79 
   105 DObject* RMemSpyObjectIx::operator[](TInt aIndex)
    80 DObject* RMemSpyObjectIx::operator[](TInt aIndex)
   106 	{
    81 	{
   107 	DObject* obj = 0;
    82 	DObject* obj = 0;
   108 	AcquireReadLock();
    83 	AcquireReadLock();
   112 	ReleaseReadLock();
    87 	ReleaseReadLock();
   113 	return obj;
    88 	return obj;
   114 	} // RObjectIx::operator[]
    89 	} // RObjectIx::operator[]
   115 
    90 
   116 
    91 
       
    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 
   117 #elif MCL_DOBJECTIX_DUPLICATION
   147 #elif MCL_DOBJECTIX_DUPLICATION
   118 
   148 
   119 /*
   149 void DMemSpyObjectIx::Wait( DMemSpyObjectIx* /*aObjectIndex*/ )
   120 void DMemSpyObjectIx::Wait( DMemSpyObjectIx* aObjectIndex )
       
   121 	{
   150 	{
   122 //	Kern::MutexWait(*aObjectIndex->HandleMutex);
   151 //	Kern::MutexWait(*aObjectIndex->HandleMutex);
   123 	}
   152 	}
   124 
   153 
   125 void DMemSpyObjectIx::Signal( DMemSpyObjectIx* aObjectIndex )
   154 void DMemSpyObjectIx::Signal( DMemSpyObjectIx* /*aObjectIndex*/ )
   126 	{
   155 	{
   127 //	Kern::MutexSignal(*aObjectIndex->HandleMutex);
   156 //	Kern::MutexSignal(*aObjectIndex->HandleMutex);
   128 	}
   157 	}
   129 */
   158 
   130 
   159 
   131 /** Counts the number of times an object appears in this index.
   160 /** Counts the number of times an object appears in this index.
   132 
   161 
   133 	@param	aObject	Object whose occurrences are to be counted.
   162 	@param	aObject	Object whose occurrences are to be counted.
   134 
   163 
   206 	if (pS->str.instance!=instance(aHandle))
   235 	if (pS->str.instance!=instance(aHandle))
   207 		return NULL;
   236 		return NULL;
   208 	return pS->obj;
   237 	return pS->obj;
   209 	}
   238 	}
   210 
   239 
       
   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 
   211 /** Finds the object at a specific position in the index array.
   276 /** Finds the object at a specific position in the index array.
   212 
   277 
   213 	@param	aIndex	Index into array.
   278 	@param	aIndex	Index into array.
   214 	
   279 	
   215 	@return	Pointer to the object at that position (could be NULL).
   280 	@return	Pointer to the object at that position (could be NULL).