- Add Magic number back in

- Move all HANDLE_ENTRY fields into GDIOBJHDR (saves memory)

svn path=/trunk/; revision=5727
This commit is contained in:
Gé van Geldorp 2003-08-21 20:16:55 +00:00
parent 113ee4971a
commit 60e08b722e
2 changed files with 154 additions and 143 deletions

View file

@ -35,33 +35,23 @@
#define GDI_OBJECT_TYPE_DONTCARE 0x007f0000 #define GDI_OBJECT_TYPE_DONTCARE 0x007f0000
/*@}*/ /*@}*/
typedef PVOID PGDIOBJ;
typedef BOOL (FASTCALL *GDICLEANUPPROC)(PGDIOBJ Obj);
/*! /*!
* GDI object header. This is a part of any GDI object * GDI object header. This is a part of any GDI object
*/ */
typedef struct _GDIOBJHDR typedef struct _GDIOBJHDR
{ {
WORD wTableIndex;
DWORD dwCount; /* reference count for the object */ DWORD dwCount; /* reference count for the object */
} GDIOBJHDR, *PGDIOBJHDR;
typedef PVOID PGDIOBJ;
typedef BOOL (FASTCALL *GDICLEANUPPROC)(PGDIOBJ Obj);
typedef struct _GDI_HANDLE_ENTRY
{
GDICLEANUPPROC CleanupProc;
HANDLE hProcessId; HANDLE hProcessId;
PGDIOBJ pObject; GDICLEANUPPROC CleanupProc;
WORD wTableIndex;
WORD Magic;
const char* lockfile; const char* lockfile;
int lockline; int lockline;
} GDI_HANDLE_ENTRY, *PGDI_HANDLE_ENTRY; } GDIOBJHDR, *PGDIOBJHDR;
typedef struct _GDI_HANDLE_TABLE
{
WORD wTableSize;
GDI_HANDLE_ENTRY Handles [1];
} GDI_HANDLE_TABLE, *PGDI_HANDLE_TABLE;
typedef struct _GDIMULTILOCK typedef struct _GDIMULTILOCK
{ {

View file

@ -19,7 +19,7 @@
/* /*
* GDIOBJ.C - GDI object manipulation routines * GDIOBJ.C - GDI object manipulation routines
* *
* $Id: gdiobj.c,v 1.38 2003/08/20 07:45:02 gvg Exp $ * $Id: gdiobj.c,v 1.39 2003/08/21 20:16:55 gvg Exp $
* *
*/ */
@ -41,6 +41,8 @@
#define NDEBUG #define NDEBUG
#include <win32k/debug1.h> #include <win32k/debug1.h>
#define GDI_GLOBAL_PROCESS ((HANDLE) 0xffffffff)
#define GDI_HANDLE_INDEX_MASK 0x00000fff #define GDI_HANDLE_INDEX_MASK 0x00000fff
#define GDI_HANDLE_TYPE_MASK 0x007f0000 #define GDI_HANDLE_TYPE_MASK 0x007f0000
#define GDI_HANDLE_STOCK_MASK 0x00800000 #define GDI_HANDLE_STOCK_MASK 0x00800000
@ -52,6 +54,23 @@
#define GDI_HANDLE_IS_STOCKOBJ(h) (0 != (((DWORD)(h)) & GDI_HANDLE_STOCK_MASK)) #define GDI_HANDLE_IS_STOCKOBJ(h) (0 != (((DWORD)(h)) & GDI_HANDLE_STOCK_MASK))
#define GDI_HANDLE_SET_STOCKOBJ(h) ((h) = (HANDLE)(((DWORD)(h)) | GDI_HANDLE_STOCK_MASK)) #define GDI_HANDLE_SET_STOCKOBJ(h) ((h) = (HANDLE)(((DWORD)(h)) | GDI_HANDLE_STOCK_MASK))
#define GDI_TYPE_TO_MAGIC(t) ((WORD) ((t) >> 16))
#define GDI_MAGIC_TO_TYPE(m) ((DWORD)(m) << 16)
#define GDI_VALID_OBJECT(h, obj, t, f) \
(NULL != (obj) \
&& (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
&& (GDI_HANDLE_GET_TYPE((h)) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
&& (((obj)->hProcessId == PsGetCurrentProcessId()) \
|| (GDI_GLOBAL_PROCESS == (obj)->hProcessId) \
|| ((f) & GDIOBJFLAG_IGNOREPID)))
typedef struct _GDI_HANDLE_TABLE
{
WORD wTableSize;
PGDIOBJHDR Handles[1];
} GDI_HANDLE_TABLE, *PGDI_HANDLE_TABLE;
/* GDI stock objects */ /* GDI stock objects */
static LOGBRUSH WhiteBrush = static LOGBRUSH WhiteBrush =
@ -139,11 +158,11 @@ GDIOBJ_iAllocHandleTable (WORD Size)
ExAcquireFastMutexUnsafe (&HandleTableMutex); ExAcquireFastMutexUnsafe (&HandleTableMutex);
handleTable = ExAllocatePool(PagedPool, handleTable = ExAllocatePool(PagedPool,
sizeof(GDI_HANDLE_TABLE) + sizeof(GDI_HANDLE_TABLE) +
sizeof (GDI_HANDLE_ENTRY) * Size); sizeof(PGDIOBJ) * Size);
ASSERT( handleTable ); ASSERT( handleTable );
memset (handleTable, memset (handleTable,
0, 0,
sizeof (GDI_HANDLE_TABLE) + sizeof (GDI_HANDLE_ENTRY) * Size); sizeof(GDI_HANDLE_TABLE) + sizeof(PGDIOBJ) * Size);
handleTable->wTableSize = Size; handleTable->wTableSize = Size;
ExReleaseFastMutexUnsafe (&HandleTableMutex); ExReleaseFastMutexUnsafe (&HandleTableMutex);
@ -153,13 +172,16 @@ GDIOBJ_iAllocHandleTable (WORD Size)
/*! /*!
* Returns the entry into the handle table by index. * Returns the entry into the handle table by index.
*/ */
static PGDI_HANDLE_ENTRY FASTCALL static PGDIOBJHDR FASTCALL
GDIOBJ_iGetHandleEntryForIndex (WORD TableIndex) GDIOBJ_iGetObjectForIndex(WORD TableIndex)
{ {
/*DPRINT("GDIOBJ_iGetHandleEntryForIndex: TableIndex: %d,\n handle: %x, ptr: %x\n", TableIndex, HandleTable->Handles [TableIndex], &(HandleTable->Handles [TableIndex]) );*/ if (0 == TableIndex || HandleTable->wTableSize < TableIndex)
/*DPRINT("GIG: HandleTable: %x, Handles: %x, \n TableIndex: %x, pt: %x\n", HandleTable, HandleTable->Handles, TableIndex, ((PGDI_HANDLE_ENTRY)HandleTable->Handles+TableIndex));*/ {
/*DPRINT("GIG: Hndl: %x\n", ((PGDI_HANDLE_ENTRY)HandleTable->Handles+TableIndex));*/ DPRINT1("Invalid TableIndex %u\n", (unsigned) TableIndex);
return ((PGDI_HANDLE_ENTRY)HandleTable->Handles+TableIndex); return NULL;
}
return HandleTable->Handles[TableIndex];
} }
/*! /*!
@ -174,9 +196,9 @@ GDIOBJ_iGetNextOpenHandleIndex (void)
ExAcquireFastMutexUnsafe (&HandleTableMutex); ExAcquireFastMutexUnsafe (&HandleTableMutex);
for (tableIndex = 1; tableIndex < HandleTable->wTableSize; tableIndex++) for (tableIndex = 1; tableIndex < HandleTable->wTableSize; tableIndex++)
{ {
if (NULL == HandleTable->Handles[tableIndex].pObject) if (NULL == HandleTable->Handles[tableIndex])
{ {
HandleTable->Handles[tableIndex].pObject = (PGDIOBJ) -1; HandleTable->Handles[tableIndex] = (PGDIOBJHDR) -1;
break; break;
} }
} }
@ -201,26 +223,33 @@ HGDIOBJ FASTCALL
GDIOBJ_AllocObj(WORD Size, DWORD ObjectType, GDICLEANUPPROC CleanupProc) GDIOBJ_AllocObj(WORD Size, DWORD ObjectType, GDICLEANUPPROC CleanupProc)
{ {
PGDIOBJHDR newObject; PGDIOBJHDR newObject;
PGDI_HANDLE_ENTRY handleEntry;
DPRINT("GDIOBJ_AllocObj: size: %d, type: 0x%08x\n", Size, ObjectType); DPRINT("GDIOBJ_AllocObj: size: %d, type: 0x%08x\n", Size, ObjectType);
newObject = ExAllocatePool(PagedPool, Size + sizeof (GDIOBJHDR)); newObject = ExAllocatePool(PagedPool, Size + sizeof (GDIOBJHDR));
if (newObject == NULL) if (newObject == NULL)
{ {
DPRINT("GDIOBJ_AllocObj: failed\n"); DPRINT1("GDIOBJ_AllocObj: failed\n");
return NULL; return NULL;
} }
RtlZeroMemory (newObject, Size + sizeof(GDIOBJHDR)); RtlZeroMemory (newObject, Size + sizeof(GDIOBJHDR));
newObject->wTableIndex = GDIOBJ_iGetNextOpenHandleIndex (); newObject->wTableIndex = GDIOBJ_iGetNextOpenHandleIndex ();
newObject->dwCount = 0;
handleEntry = GDIOBJ_iGetHandleEntryForIndex (newObject->wTableIndex);
handleEntry->CleanupProc = CleanupProc;
handleEntry->hProcessId = PsGetCurrentProcessId ();
handleEntry->pObject = newObject;
handleEntry->lockfile = NULL;
handleEntry->lockline = 0;
DPRINT("GDIOBJ_AllocObj: object handle %d\n", newObject->wTableIndex ); DPRINT("GDIOBJ_AllocObj: object handle %d\n", newObject->wTableIndex );
if (0 == newObject->wTableIndex)
{
DPRINT1("Out of GDI handles\n");
ExFreePool(newObject);
return NULL;
}
newObject->dwCount = 0;
newObject->hProcessId = PsGetCurrentProcessId ();
newObject->CleanupProc = CleanupProc;
newObject->Magic = GDI_TYPE_TO_MAGIC(ObjectType);
newObject->lockfile = NULL;
newObject->lockline = 0;
HandleTable->Handles[newObject->wTableIndex] = newObject;
return GDI_HANDLE_CREATE(newObject->wTableIndex, ObjectType); return GDI_HANDLE_CREATE(newObject->wTableIndex, ObjectType);
} }
@ -243,23 +272,20 @@ BOOL STDCALL
GDIOBJ_FreeObj(HGDIOBJ hObj, DWORD ObjectType, DWORD Flag) GDIOBJ_FreeObj(HGDIOBJ hObj, DWORD ObjectType, DWORD Flag)
{ {
PGDIOBJHDR objectHeader; PGDIOBJHDR objectHeader;
PGDI_HANDLE_ENTRY handleEntry;
PGDIOBJ Obj; PGDIOBJ Obj;
BOOL bRet = TRUE; BOOL bRet = TRUE;
handleEntry = GDIOBJ_iGetHandleEntryForIndex(GDI_HANDLE_GET_INDEX(hObj)); objectHeader = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x, handleEntry: %x\n", hObj, handleEntry ); DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x, object: %x\n", hObj, objectHeader);
if (! GDI_VALID_OBJECT(hObj, objectHeader, ObjectType, Flag)
|| GDI_GLOBAL_PROCESS == objectHeader->hProcessId)
if (NULL == handleEntry
|| (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
|| ((handleEntry->hProcessId != PsGetCurrentProcessId()) && !(Flag & GDIOBJFLAG_IGNOREPID)))
{ {
DPRINT("Can't Delete hObj: 0x%08x, type: 0x%08x, pid:%d\n currpid:%d, flag:%d, hmm:%d\n", hObj, ObjectType, handleEntry->hProcessId, PsGetCurrentProcessId(), (Flag&GDIOBJFLAG_IGNOREPID), ((handleEntry->hProcessId != PsGetCurrentProcessId()) && !(Flag&GDIOBJFLAG_IGNOREPID)) ); DPRINT1("Can't delete hObj:0x%08x, type:0x%08x, flag:%d\n", hObj, ObjectType, Flag);
return FALSE; return FALSE;
} }
objectHeader = handleEntry->pObject;
ASSERT(objectHeader);
DPRINT("FreeObj: locks: %x\n", objectHeader->dwCount ); DPRINT("FreeObj: locks: %x\n", objectHeader->dwCount );
if (!(Flag & GDIOBJFLAG_IGNORELOCK)) if (!(Flag & GDIOBJFLAG_IGNORELOCK))
{ {
@ -277,13 +303,14 @@ GDIOBJ_FreeObj(HGDIOBJ hObj, DWORD ObjectType, DWORD Flag)
} }
/* allow object to delete internal data */ /* allow object to delete internal data */
if (NULL != handleEntry->CleanupProc) if (NULL != objectHeader->CleanupProc)
{ {
Obj = (PGDIOBJ)((PCHAR)handleEntry->pObject + sizeof(GDIOBJHDR)); Obj = (PGDIOBJ)((PCHAR)objectHeader + sizeof(GDIOBJHDR));
bRet = (*(handleEntry->CleanupProc))(Obj); bRet = (*(objectHeader->CleanupProc))(Obj);
} }
ExFreePool (handleEntry->pObject);
memset(handleEntry, 0, sizeof(GDI_HANDLE_ENTRY)); ExFreePool(objectHeader);
HandleTable->Handles[GDI_HANDLE_GET_INDEX(hObj)] = NULL;
return bRet; return bRet;
} }
@ -368,7 +395,7 @@ GDIOBJ_UnlockMultipleObj(PGDIMULTILOCK pList, INT nObj)
} }
/*! /*!
* Marks the object as global. (Creator process ID is set to 0xFFFFFFFF). Global objects may be * Marks the object as global. (Creator process ID is set to GDI_GLOBAL_PROCESS). Global objects may be
* accessed by any process. * accessed by any process.
* \param ObjectHandle - handle of the object to make global. * \param ObjectHandle - handle of the object to make global.
* *
@ -377,20 +404,16 @@ GDIOBJ_UnlockMultipleObj(PGDIMULTILOCK pList, INT nObj)
VOID FASTCALL VOID FASTCALL
GDIOBJ_MarkObjectGlobal(HGDIOBJ ObjectHandle) GDIOBJ_MarkObjectGlobal(HGDIOBJ ObjectHandle)
{ {
PGDI_HANDLE_ENTRY handleEntry; PGDIOBJHDR ObjHdr;
if (NULL == ObjectHandle) DPRINT("GDIOBJ_MarkObjectGlobal handle 0x%08x\n", ObjectHandle);
ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle));
if (NULL == ObjHdr)
{ {
return; return;
} }
handleEntry = GDIOBJ_iGetHandleEntryForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle)); ObjHdr->hProcessId = GDI_GLOBAL_PROCESS;
if (NULL == handleEntry)
{
return;
}
handleEntry->hProcessId = (HANDLE)0xFFFFFFFF;
} }
/*! /*!
@ -401,7 +424,18 @@ GDIOBJ_MarkObjectGlobal(HGDIOBJ ObjectHandle)
DWORD FASTCALL DWORD FASTCALL
GDIOBJ_GetObjectType(HGDIOBJ ObjectHandle) GDIOBJ_GetObjectType(HGDIOBJ ObjectHandle)
{ {
return GDI_HANDLE_GET_TYPE(ObjectHandle); PGDIOBJHDR ObjHdr;
ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle));
if (NULL == ObjHdr)
{
DPRINT1("Invalid ObjectHandle 0x%08x\n", ObjectHandle);
return 0;
}
DPRINT("GDIOBJ_GetObjectType for handle 0x%08x returns 0x%08x\n", ObjectHandle,
GDI_MAGIC_TO_TYPE(ObjHdr->Magic));
return GDI_MAGIC_TO_TYPE(ObjHdr->Magic);
} }
/*! /*!
@ -428,6 +462,8 @@ CreateStockObjects(void)
{ {
unsigned Object; unsigned Object;
DPRINT("Beginning creation of stock objects\n");
/* Create GDI Stock Objects from the logical structures we've defined */ /* Create GDI Stock Objects from the logical structures we've defined */
StockObjects[WHITE_BRUSH] = NtGdiCreateBrushIndirect(&WhiteBrush); StockObjects[WHITE_BRUSH] = NtGdiCreateBrushIndirect(&WhiteBrush);
@ -451,12 +487,17 @@ CreateStockObjects(void)
StockObjects[DEFAULT_PALETTE] = (HGDIOBJ*)PALETTE_Init(); StockObjects[DEFAULT_PALETTE] = (HGDIOBJ*)PALETTE_Init();
for (Object = 0; Object < NB_STOCK_OBJECTS; Object++) for (Object = 0; Object < NB_STOCK_OBJECTS; Object++)
{
if (NULL != StockObjects[Object])
{ {
GDIOBJ_MarkObjectGlobal(StockObjects[Object]); GDIOBJ_MarkObjectGlobal(StockObjects[Object]);
GDI_HANDLE_SET_STOCKOBJ(StockObjects[Object]); GDI_HANDLE_SET_STOCKOBJ(StockObjects[Object]);
} }
} }
DPRINT("Completed creation of stock objects\n");
}
/*! /*!
* Return stock object. * Return stock object.
* \param Object - stock object id. * \param Object - stock object id.
@ -465,17 +506,21 @@ CreateStockObjects(void)
HGDIOBJ STDCALL HGDIOBJ STDCALL
NtGdiGetStockObject(INT Object) NtGdiGetStockObject(INT Object)
{ {
DPRINT("NtGdiGetStockObject index %d\n", Object);
return ((Object < 0) || (NB_STOCK_OBJECTS <= Object)) ? NULL : StockObjects[Object]; return ((Object < 0) || (NB_STOCK_OBJECTS <= Object)) ? NULL : StockObjects[Object];
} }
/*! /*!
* Delete GDI object * Delete GDI object
* \param hObject object handle * \param hObject object handle
* \return if the function fails the returned value is NULL. * \return if the function fails the returned value is FALSE.
*/ */
BOOL STDCALL BOOL STDCALL
NtGdiDeleteObject(HGDIOBJ hObject) NtGdiDeleteObject(HGDIOBJ hObject)
{ {
DPRINT("NtGdiDeleteObject handle 0x%08x\n", hObject);
return GDIOBJ_FreeObj(hObject, GDI_OBJECT_TYPE_DONTCARE, GDIOBJFLAG_DEFAULT); return GDIOBJ_FreeObj(hObject, GDI_OBJECT_TYPE_DONTCARE, GDIOBJFLAG_DEFAULT);
} }
@ -487,24 +532,23 @@ BOOL FASTCALL
CleanupForProcess (struct _EPROCESS *Process, INT Pid) CleanupForProcess (struct _EPROCESS *Process, INT Pid)
{ {
DWORD i; DWORD i;
PGDI_HANDLE_ENTRY handleEntry;
PGDIOBJHDR objectHeader; PGDIOBJHDR objectHeader;
PEPROCESS CurrentProcess; PEPROCESS CurrentProcess;
DPRINT("Starting CleanupForProcess prochandle %x Pid %d\n", Process, Pid);
CurrentProcess = PsGetCurrentProcess(); CurrentProcess = PsGetCurrentProcess();
if (CurrentProcess != Process) if (CurrentProcess != Process)
{ {
KeAttachProcess(Process); KeAttachProcess(Process);
} }
for(i = 1; i < GDI_HANDLE_NUMBER; i++) for(i = 1; i < HandleTable->wTableSize; i++)
{ {
handleEntry = GDIOBJ_iGetHandleEntryForIndex(i); objectHeader = GDIOBJ_iGetObjectForIndex(i);
if (NULL != handleEntry && NULL != handleEntry->pObject && if (NULL != objectHeader &&
(INT)handleEntry->hProcessId == Pid) (INT) objectHeader->hProcessId == Pid)
{ {
objectHeader = (PGDIOBJHDR) handleEntry->pObject; DPRINT("CleanupForProcess: %d, process: %d, locks: %d, magic: 0x%x", i, objectHeader->hProcessId, objectHeader->dwCount, objectHeader->Magic);
DPRINT("\nNtGdiCleanup: %d, process: %d, locks: %d", i, handleEntry->hProcessId, objectHeader->dwCount);
GDIOBJ_FreeObj(GDI_HANDLE_CREATE(i, GDI_OBJECT_TYPE_DONTCARE), GDIOBJ_FreeObj(GDI_HANDLE_CREATE(i, GDI_OBJECT_TYPE_DONTCARE),
GDI_OBJECT_TYPE_DONTCARE, GDI_OBJECT_TYPE_DONTCARE,
GDIOBJFLAG_IGNOREPID | GDIOBJFLAG_IGNORELOCK); GDIOBJFLAG_IGNOREPID | GDIOBJFLAG_IGNORELOCK);
@ -516,6 +560,8 @@ CleanupForProcess (struct _EPROCESS *Process, INT Pid)
KeDetachProcess(); KeDetachProcess();
} }
DPRINT("Completed cleanup for process %d\n", Pid);
return TRUE; return TRUE;
} }
@ -527,47 +573,45 @@ PGDIOBJ FASTCALL
GDIOBJ_LockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType) GDIOBJ_LockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
{ {
PGDIOBJ rc; PGDIOBJ rc;
PGDI_HANDLE_ENTRY handleEntry PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
= GDIOBJ_iGetHandleEntryForIndex(GDI_HANDLE_GET_INDEX(hObj));
if (NULL == handleEntry if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
|| (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
|| (handleEntry->hProcessId != (HANDLE)0xFFFFFFFF
&& handleEntry->hProcessId != PsGetCurrentProcessId ()
)
)
{ {
int reason = 0; int reason = 0;
if (NULL == handleEntry) if (NULL == ObjHdr)
{ {
reason = 1; reason = 1;
} }
else if (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE) else if (GDI_MAGIC_TO_TYPE(ObjHdr->Magic) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
{ {
reason = 2; reason = 2;
} }
else if (handleEntry->hProcessId != (HANDLE)0xFFFFFFFF else if (ObjHdr->hProcessId != GDI_GLOBAL_PROCESS
&& handleEntry->hProcessId != PsGetCurrentProcessId()) && ObjHdr->hProcessId != PsGetCurrentProcessId())
{ {
reason = 3; reason = 3;
} }
else if (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
{
reason = 4;
}
DPRINT1("GDIOBJ_LockObj failed for 0x%08x, reqtype 0x%08x reason %d\n", DPRINT1("GDIOBJ_LockObj failed for 0x%08x, reqtype 0x%08x reason %d\n",
hObj, ObjectType, reason ); hObj, ObjectType, reason );
DPRINT1("\tcalled from: %s:%i\n", file, line ); DPRINT1("\tcalled from: %s:%i\n", file, line );
return NULL; return NULL;
} }
if (NULL != handleEntry->lockfile) if (NULL != ObjHdr->lockfile)
{ {
DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj ); DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj );
DPRINT1("\tcalled from: %s:%i\n", file, line ); DPRINT1("\tcalled from: %s:%i\n", file, line );
DPRINT1("\tpreviously locked from: %s:%i\n", handleEntry->lockfile, handleEntry->lockline ); DPRINT1("\tpreviously locked from: %s:%i\n", ObjHdr->lockfile, ObjHdr->lockline );
} }
DPRINT("(%s:%i) GDIOBJ_LockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType); DPRINT("(%s:%i) GDIOBJ_LockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType);
rc = GDIOBJ_LockObj(hObj, ObjectType); rc = GDIOBJ_LockObj(hObj, ObjectType);
if (rc && NULL == handleEntry->lockfile) if (rc && NULL == ObjHdr->lockfile)
{ {
handleEntry->lockfile = file; ObjHdr->lockfile = file;
handleEntry->lockline = line; ObjHdr->lockline = line;
} }
return rc; return rc;
@ -579,14 +623,9 @@ GDIOBJ_LockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
BOOL FASTCALL BOOL FASTCALL
GDIOBJ_UnlockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType) GDIOBJ_UnlockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
{ {
PGDI_HANDLE_ENTRY handleEntry PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
= GDIOBJ_iGetHandleEntryForIndex (GDI_HANDLE_GET_INDEX(hObj));
if (NULL == handleEntry if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
|| (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
|| (handleEntry->hProcessId != (HANDLE)0xFFFFFFFF
&& handleEntry->hProcessId != PsGetCurrentProcessId ()
)
)
{ {
DPRINT1("GDIBOJ_UnlockObj failed for 0x%08x, reqtype 0x%08x\n", DPRINT1("GDIBOJ_UnlockObj failed for 0x%08x, reqtype 0x%08x\n",
hObj, ObjectType); hObj, ObjectType);
@ -594,8 +633,9 @@ GDIOBJ_UnlockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
return FALSE; return FALSE;
} }
DPRINT("(%s:%i) GDIOBJ_UnlockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType); DPRINT("(%s:%i) GDIOBJ_UnlockObj(0x%08x,0x%08x)\n", file, line, hObj, ObjectType);
handleEntry->lockfile = NULL; ObjHdr->lockfile = NULL;
handleEntry->lockline = 0; ObjHdr->lockline = 0;
return GDIOBJ_UnlockObj(hObj, ObjectType); return GDIOBJ_UnlockObj(hObj, ObjectType);
} }
#endif//GDIOBJ_LockObj #endif//GDIOBJ_LockObj
@ -614,35 +654,26 @@ GDIOBJ_UnlockObjDbg (const char* file, int line, HGDIOBJ hObj, DWORD ObjectType)
PGDIOBJ FASTCALL PGDIOBJ FASTCALL
GDIOBJ_LockObj(HGDIOBJ hObj, DWORD ObjectType) GDIOBJ_LockObj(HGDIOBJ hObj, DWORD ObjectType)
{ {
PGDI_HANDLE_ENTRY handleEntry PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
= GDIOBJ_iGetHandleEntryForIndex(GDI_HANDLE_GET_INDEX(hObj));
PGDIOBJHDR objectHeader;
DPRINT("GDIOBJ_LockObj: hObj: 0x%08x, type: 0x%08x, handleEntry: %x\n", hObj, ObjectType, handleEntry); DPRINT("GDIOBJ_LockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj, ObjectType, ObjHdr);
if (NULL == handleEntry if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
|| (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
|| (handleEntry->hProcessId != (HANDLE)0xFFFFFFFF
&& handleEntry->hProcessId != PsGetCurrentProcessId()
)
)
{ {
DPRINT1("GDIBOJ_LockObj failed for 0x%08x, type 0x%08x\n", DPRINT1("GDIBOJ_LockObj failed for 0x%08x, type 0x%08x\n",
hObj, ObjectType); hObj, ObjectType);
return NULL; return NULL;
} }
objectHeader = (PGDIOBJHDR) handleEntry->pObject; if(0 < ObjHdr->dwCount)
ASSERT(objectHeader);
if(0 < objectHeader->dwCount)
{ {
DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj); DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj);
DPRINT1("\t called from: %x\n", __builtin_return_address(0)); DPRINT1("\t called from: %x\n", __builtin_return_address(0));
} }
ExAcquireFastMutex(&RefCountHandling); ExAcquireFastMutex(&RefCountHandling);
objectHeader->dwCount++; ObjHdr->dwCount++;
ExReleaseFastMutex(&RefCountHandling); ExReleaseFastMutex(&RefCountHandling);
return (PGDIOBJ)((PCHAR)objectHeader + sizeof(GDIOBJHDR)); return (PGDIOBJ)((PCHAR)ObjHdr + sizeof(GDIOBJHDR));
} }
/*! /*!
@ -661,39 +692,29 @@ GDIOBJ_LockObj(HGDIOBJ hObj, DWORD ObjectType)
BOOL FASTCALL BOOL FASTCALL
GDIOBJ_UnlockObj(HGDIOBJ hObj, DWORD ObjectType) GDIOBJ_UnlockObj(HGDIOBJ hObj, DWORD ObjectType)
{ {
PGDI_HANDLE_ENTRY handleEntry PGDIOBJHDR ObjHdr = GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj));
= GDIOBJ_iGetHandleEntryForIndex(GDI_HANDLE_GET_INDEX(hObj));
PGDIOBJHDR objectHeader;
DPRINT("GDIOBJ_UnlockObj: hObj: 0x%08x, type: 0x%08x, handleEntry: %x\n", hObj, ObjectType, handleEntry); DPRINT("GDIOBJ_UnlockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj, ObjectType, ObjHdr);
if (NULL == handleEntry if (! GDI_VALID_OBJECT(hObj, ObjHdr, ObjectType, GDIOBJFLAG_DEFAULT))
|| (GDI_HANDLE_GET_TYPE(hObj) != ObjectType && ObjectType != GDI_OBJECT_TYPE_DONTCARE)
|| (handleEntry->hProcessId != (HANDLE)0xFFFFFFFF
&& handleEntry->hProcessId != PsGetCurrentProcessId ()
)
)
{ {
DPRINT1( "GDIOBJ_UnLockObj: failed\n"); DPRINT1( "GDIOBJ_UnLockObj: failed\n");
return FALSE; return FALSE;
} }
objectHeader = (PGDIOBJHDR) handleEntry->pObject;
ASSERT(objectHeader);
ExAcquireFastMutex(&RefCountHandling); ExAcquireFastMutex(&RefCountHandling);
if (0 == (objectHeader->dwCount & ~0x80000000)) if (0 == (ObjHdr->dwCount & ~0x80000000))
{ {
ExReleaseFastMutex(&RefCountHandling); ExReleaseFastMutex(&RefCountHandling);
DPRINT1( "GDIOBJ_UnLockObj: unlock object (0x%x) that is not locked\n", hObj ); DPRINT1( "GDIOBJ_UnLockObj: unlock object (0x%x) that is not locked\n", hObj );
return FALSE; return FALSE;
} }
objectHeader->dwCount--; ObjHdr->dwCount--;
if( objectHeader->dwCount == 0x80000000 ) if (ObjHdr->dwCount == 0x80000000)
{ {
//delayed object release //delayed object release
objectHeader->dwCount = 0; ObjHdr->dwCount = 0;
ExReleaseFastMutex(&RefCountHandling); ExReleaseFastMutex(&RefCountHandling);
DPRINT("GDIOBJ_UnlockObj: delayed delete\n"); DPRINT("GDIOBJ_UnlockObj: delayed delete\n");
return GDIOBJ_FreeObj(hObj, ObjectType, GDIOBJFLAG_DEFAULT); return GDIOBJ_FreeObj(hObj, ObjectType, GDIOBJFLAG_DEFAULT);