memspy/Driver/Kernel/Source/MemSpyDriverObjectIx.cpp
changeset 48 516af714ebb4
parent 45 185201be11b0
child 55 f2950aff7424
equal deleted inserted replaced
45:185201be11b0 48:516af714ebb4
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "MemSpyDriverObjectIx.h"
       
    19 
       
    20 // System includes
       
    21 #include <kern_priv.h>
       
    22 //#include <dobject.h>
       
    23 
       
    24 //const TInt KObjectIxGranularity=8;
       
    25 const TInt KObjectIndexMask=0x7fff;
       
    26 //const TInt KObjectMaxIndex=0x7fff;
       
    27 const TInt KObjectInstanceShift=16;
       
    28 const TInt KObjectInstanceMask=0x3fff;
       
    29 //const TInt KObjectIxMaxHandles=0x8000;
       
    30 
       
    31 inline TInt index(TInt aHandle)
       
    32 	{return(aHandle&KObjectIndexMask);}
       
    33 inline TInt instance(TInt aHandle)
       
    34 	{return((aHandle>>KObjectInstanceShift)&KObjectInstanceMask);}
       
    35 inline TInt instanceLimit(TInt& aCount)
       
    36 	{return ((aCount&KObjectInstanceMask)==0) ? ((++aCount)&KObjectInstanceMask) : aCount&KObjectInstanceMask;}
       
    37 inline TInt makeHandle(TInt aIndex, TInt aInstance)
       
    38 	{return((TInt)((aInstance<<KObjectInstanceShift)|aIndex));}
       
    39 
       
    40 enum TDObjectPanic
       
    41 	{
       
    42 	EObjObjectStillReferenced,
       
    43 	EObjNegativeAccessCount,
       
    44 	EObjRemoveObjectNotFound,
       
    45 	EObjRemoveContainerNotFound,
       
    46 	EObjRemoveBadHandle,
       
    47 	EObjFindBadHandle,
       
    48 	EObjFindIndexOutOfRange,
       
    49 	EDObjectConDestroyed,
       
    50 	EArrayIndexOutOfRange,
       
    51 	EObjInconsistent,
       
    52 	};
       
    53 
       
    54 inline void Panic(TDObjectPanic aPanic)
       
    55 	{ Kern::Fault("DOBJECT",aPanic); }
       
    56 	
       
    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
       
    83 
       
    84 #define asserta(x)	do { if (!(x)) { __crash(); } } while(0)
       
    85 
       
    86 RMemSpyObjectIx::RMemSpyObjectIx()
       
    87   : iRWL(TSpinLock::EOrderGenericIrqLow3)
       
    88     {
       
    89     }
       
    90 
       
    91 
       
    92 /*
       
    93 void RMemSpyObjectIx::Wait()
       
    94 	{
       
    95 	Kern::MutexWait(*HandleMutex);
       
    96 	} // RObjectIx::Wait
       
    97 
       
    98 
       
    99 void RMemSpyObjectIx::Signal()
       
   100 	{
       
   101 	Kern::MutexSignal(*HandleMutex);
       
   102 	} // RObjectIx::Signal
       
   103 */
       
   104 
       
   105 DObject* RMemSpyObjectIx::operator[](TInt aIndex)
       
   106 	{
       
   107 	DObject* obj = 0;
       
   108 	AcquireReadLock();
       
   109 	asserta(TUint(aIndex)<TUint(iCount));
       
   110 	SSlot* slot = iSlots + aIndex;
       
   111 	obj = Occupant(slot);
       
   112 	ReleaseReadLock();
       
   113 	return obj;
       
   114 	} // RObjectIx::operator[]
       
   115 
       
   116 
       
   117 #elif MCL_DOBJECTIX_DUPLICATION
       
   118 
       
   119 /*
       
   120 void DMemSpyObjectIx::Wait( DMemSpyObjectIx* aObjectIndex )
       
   121 	{
       
   122 //	Kern::MutexWait(*aObjectIndex->HandleMutex);
       
   123 	}
       
   124 
       
   125 void DMemSpyObjectIx::Signal( DMemSpyObjectIx* aObjectIndex )
       
   126 	{
       
   127 //	Kern::MutexSignal(*aObjectIndex->HandleMutex);
       
   128 	}
       
   129 */
       
   130 
       
   131 /** Counts the number of times an object appears in this index.
       
   132 
       
   133 	@param	aObject	Object whose occurrences are to be counted.
       
   134 
       
   135 	@return	Number of times aObject appears in the index.
       
   136 
       
   137     @pre    Calling thread must be in a critical section.
       
   138     @pre    No fast mutex can be held.
       
   139 	@pre	Call in a thread context.
       
   140 	@pre	DObject::HandleMutex held
       
   141  */
       
   142 TInt DMemSpyObjectIx::Count(DObject* aObject)
       
   143 	{
       
   144 	//Check preconditions(debug build only)
       
   145 	__ASSERT_CRITICAL;
       
   146 	__ASSERT_NO_FAST_MUTEX;
       
   147 
       
   148 	TInt c=0;
       
   149 	if (iCount)
       
   150 		{
       
   151 		SDObjectIxRec* pS=iObjects;
       
   152 		SDObjectIxRec* pE=pS+iCount;
       
   153 		do
       
   154 			{
       
   155 			if (pS->obj==aObject)
       
   156 				c++;
       
   157 			} while (++pS<pE);
       
   158 		}
       
   159 	return c;
       
   160 	}
       
   161 
       
   162 
       
   163 /**	Looks up an object in the index by handle.
       
   164 	
       
   165 	The object must be of a specified type (specified by container ID)
       
   166 
       
   167 	@param	aHandle		Handle to look up.
       
   168 	@param	aUniqueID	Unique ID (container ID) that object should have.
       
   169 	
       
   170 	@return	Pointer to object or NULL if handle invalid.
       
   171 
       
   172 	@pre	Call in a thread context.
       
   173 	@pre    System lock must be held.
       
   174  */
       
   175 DObject* DMemSpyObjectIx::At(TInt aHandle, TInt aUniqueID)
       
   176 	{
       
   177 	__ASSERT_SYSTEM_LOCK; //Check preconditions (debug build only)
       
   178 	TInt i=index(aHandle);
       
   179 	if (i>=iCount)
       
   180 		return(NULL);
       
   181 	SDObjectIxRec *pS=iObjects+i;
       
   182 	if (pS->str.instance!=instance(aHandle) || pS->str.uniqueID!=aUniqueID)
       
   183 		return(NULL);
       
   184 	return(pS->obj);
       
   185 	}
       
   186 
       
   187 
       
   188 /**	Looks up an object in the index by handle.
       
   189 
       
   190 	The object may be of any type.
       
   191 
       
   192 	@param	aHandle		Handle to look up.
       
   193 	
       
   194 	@return	Pointer to object or NULL if handle invalid.
       
   195 
       
   196 	@pre	Call in a thread context.
       
   197 	@pre    System lock must be held.
       
   198  */
       
   199 DObject* DMemSpyObjectIx::At(TInt aHandle)
       
   200 	{
       
   201 	__ASSERT_SYSTEM_LOCK; //Check preconditions (debug build only)
       
   202 	TInt i=index(aHandle);
       
   203 	if (i>=iCount)
       
   204 		return NULL;
       
   205 	SDObjectIxRec *pS=iObjects+i;
       
   206 	if (pS->str.instance!=instance(aHandle))
       
   207 		return NULL;
       
   208 	return pS->obj;
       
   209 	}
       
   210 
       
   211 /** Finds the object at a specific position in the index array.
       
   212 
       
   213 	@param	aIndex	Index into array.
       
   214 	
       
   215 	@return	Pointer to the object at that position (could be NULL).
       
   216 
       
   217 	@pre	Call in a thread context. 
       
   218     @pre    System lock must be held.
       
   219  */
       
   220 DObject* DMemSpyObjectIx::operator[](TInt aIndex)
       
   221 	{
       
   222 	__ASSERT_SYSTEM_LOCK; //Check preconditions (debug build only)
       
   223 	__ASSERT_ALWAYS(aIndex>=0 && aIndex<iCount,Panic(EArrayIndexOutOfRange));
       
   224 	return iObjects[aIndex].obj;
       
   225 	}
       
   226 
       
   227 #endif
       
   228