2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/gdiobj.c
5 * PURPOSE: General GDI object manipulation routines
6 * PROGRAMMERS: Timo Kreuzer
10 * If you want to understand this code, you need to start thinking in portals.
11 * - gpaulRefCount is a global pointer to an allocated array of ULONG values,
12 * one for each handle. Bits 0 - 22 contain a reference count for the handle.
13 * It gets increased for each handle lock / reference. Bit 23 contains a valid
14 * bit. If this bit is 0, the handle got deleted and will be pushed to the free
15 * list, once all references are gone. Bits 24 - 31 contain the reuse value of
16 * the handle, which allows to check if the entry was changed before atomically
17 * exchanging the reference count.
18 * - Objects can exist with or without a handle
19 * - Objects with a handle can be locked either exclusively or shared.
20 * Both locks increase the handle reference count in gpaulRefCount.
21 * Exclusive locks also increase the BASEOBJECT's cExclusiveLock field
22 * and the first lock (can be acquired recursively) acquires a pushlock
23 * that is also stored in the BASEOBJECT.
24 * - Objects without a handle cannot have exclusive locks. Their reference
25 * count is tracked in the BASEOBJECT's ulShareCount field.
26 * - An object that is inserted in the handle table automatically has an
27 * exclusive lock. For objects that are "shared objects" (BRUSH, PALETTE, ...)
28 * this is the only way it can ever be exclusively locked. It prevents the
29 * object from being locked by another thread. A shared lock will simply fail,
30 * while an exclusive lock will succeed after the object was unlocked.
34 /* INCLUDES ******************************************************************/
42 #define DBG_INCREASE_LOCK_COUNT(pti, hobj) \
43 if (pti) ((PTHREADINFO)pti)->acExclusiveLockCount[((ULONG_PTR)hobj >> 16) & 0x1f]++;
44 #define DBG_DECREASE_LOCK_COUNT(pti, hobj) \
45 if (pti) ((PTHREADINFO)pti)->acExclusiveLockCount[((ULONG_PTR)hobj >> 16) & 0x1f]--;
46 #define ASSERT_SHARED_OBJECT_TYPE(objt) \
47 ASSERT((objt) == GDIObjType_SURF_TYPE || \
48 (objt) == GDIObjType_PAL_TYPE || \
49 (objt) == GDIObjType_LFONT_TYPE || \
50 (objt) == GDIObjType_PATH_TYPE || \
51 (objt) == GDIObjType_BRUSH_TYPE)
52 #define ASSERT_EXCLUSIVE_OBJECT_TYPE(objt) \
53 ASSERT((objt) == GDIObjType_DC_TYPE || \
54 (objt) == GDIObjType_RGN_TYPE || \
55 (objt) == GDIObjType_LFONT_TYPE)
57 #define DBG_INCREASE_LOCK_COUNT(ppi, hobj)
58 #define DBG_DECREASE_LOCK_COUNT(x, y)
59 #define ASSERT_SHARED_OBJECT_TYPE(objt)
60 #define ASSERT_EXCLUSIVE_OBJECT_TYPE(objt)
63 #define MmMapViewInSessionSpace MmMapViewInSystemSpace
65 #if defined(_M_IX86) || defined(_M_AMD64)
66 #define InterlockedOr16 _InterlockedOr16
69 #define GDIOBJ_POOL_TAG(type) ('00hG' + ((objt & 0x1f) << 24))
73 REF_MASK_REUSE
= 0xff000000,
74 REF_INC_REUSE
= 0x01000000,
75 REF_MASK_VALID
= 0x00800000,
76 REF_MASK_COUNT
= 0x007fffff,
77 REF_MASK_INUSE
= 0x00ffffff,
80 /* GLOBALS *******************************************************************/
82 /* Per session handle table globals */
83 static PVOID gpvGdiHdlTblSection
= NULL
;
84 static PENTRY gpentHmgr
;
85 static PULONG gpaulRefCount
;
88 static PPAGED_LOOKASIDE_LIST gpaLookasideList
;
90 static BOOL NTAPI
GDIOBJ_Cleanup(PVOID ObjectBody
);
96 NULL
, /* 00 GDIObjType_DEF_TYPE */
97 DC_Cleanup
, /* 01 GDIObjType_DC_TYPE */
98 NULL
, /* 02 GDIObjType_UNUSED1_TYPE */
99 NULL
, /* 03 GDIObjType_UNUSED2_TYPE */
100 REGION_Cleanup
, /* 04 GDIObjType_RGN_TYPE */
101 SURFACE_Cleanup
, /* 05 GDIObjType_SURF_TYPE */
102 GDIOBJ_Cleanup
, /* 06 GDIObjType_CLIENTOBJ_TYPE */
103 GDIOBJ_Cleanup
, /* 07 GDIObjType_PATH_TYPE */
104 PALETTE_Cleanup
, /* 08 GDIObjType_PAL_TYPE */
105 GDIOBJ_Cleanup
, /* 09 GDIObjType_ICMLCS_TYPE */
106 GDIOBJ_Cleanup
, /* 0a GDIObjType_LFONT_TYPE */
107 NULL
, /* 0b GDIObjType_RFONT_TYPE, unused */
108 NULL
, /* 0c GDIObjType_PFE_TYPE, unused */
109 NULL
, /* 0d GDIObjType_PFT_TYPE, unused */
110 GDIOBJ_Cleanup
, /* 0e GDIObjType_ICMCXF_TYPE */
111 NULL
, /* 0f GDIObjType_SPRITE_TYPE, unused */
112 BRUSH_Cleanup
, /* 10 GDIObjType_BRUSH_TYPE, BRUSH, PEN, EXTPEN */
113 NULL
, /* 11 GDIObjType_UMPD_TYPE, unused */
114 NULL
, /* 12 GDIObjType_UNUSED4_TYPE */
115 NULL
, /* 13 GDIObjType_SPACE_TYPE, unused */
116 NULL
, /* 14 GDIObjType_UNUSED5_TYPE */
117 NULL
, /* 15 GDIObjType_META_TYPE, unused */
118 NULL
, /* 16 GDIObjType_EFSTATE_TYPE, unused */
119 NULL
, /* 17 GDIObjType_BMFD_TYPE, unused */
120 NULL
, /* 18 GDIObjType_VTFD_TYPE, unused */
121 NULL
, /* 19 GDIObjType_TTFD_TYPE, unused */
122 NULL
, /* 1a GDIObjType_RC_TYPE, unused */
123 NULL
, /* 1b GDIObjType_TEMP_TYPE, unused */
124 DRIVEROBJ_Cleanup
,/* 1c GDIObjType_DRVOBJ_TYPE */
125 NULL
, /* 1d GDIObjType_DCIOBJ_TYPE, unused */
126 NULL
, /* 1e GDIObjType_SPOOL_TYPE, unused */
127 NULL
, /* 1f reserved entry */
130 /* INTERNAL FUNCTIONS ********************************************************/
134 GDIOBJ_Cleanup(PVOID ObjectBody
)
141 InitLookasideList(UCHAR objt
, ULONG cjSize
)
143 ExInitializePagedLookasideList(&gpaLookasideList
[objt
],
148 GDITAG_HMGR_LOOKASIDE_START
+ (objt
<< 24),
155 InitGdiHandleTable(void)
158 LARGE_INTEGER liSize
;
160 SIZE_T cjViewSize
= 0;
162 /* Create a section for the shared handle table */
163 liSize
.QuadPart
= sizeof(GDI_HANDLE_TABLE
); // GDI_HANDLE_COUNT * sizeof(ENTRY);
164 status
= MmCreateSection(&gpvGdiHdlTblSection
,
172 if (!NT_SUCCESS(status
))
174 DPRINT1("INITGDI: Could not allocate a GDI handle table.\n");
178 /* Map the section in session space */
179 status
= MmMapViewInSessionSpace(gpvGdiHdlTblSection
,
182 if (!NT_SUCCESS(status
))
184 DPRINT1("INITGDI: Failed to map handle table section\n");
185 ObDereferenceObject(gpvGdiHdlTblSection
);
189 /* Allocate memory for the reference counter table */
190 gpaulRefCount
= EngAllocSectionMem(&pvSection
,
192 GDI_HANDLE_COUNT
* sizeof(ULONG
),
196 DPRINT1("INITGDI: Failed to allocate reference table.\n");
197 ObDereferenceObject(gpvGdiHdlTblSection
);
198 return STATUS_INSUFFICIENT_RESOURCES
;
202 gulFirstUnused
= RESERVE_ENTRIES_COUNT
;
204 GdiHandleTable
= (PVOID
)gpentHmgr
;
206 /* Initialize the lookaside lists */
207 gpaLookasideList
= ExAllocatePoolWithTag(NonPagedPool
,
208 GDIObjTypeTotal
* sizeof(PAGED_LOOKASIDE_LIST
),
210 if(!gpaLookasideList
)
211 return STATUS_NO_MEMORY
;
213 InitLookasideList(GDIObjType_DC_TYPE
, sizeof(DC
));
214 InitLookasideList(GDIObjType_RGN_TYPE
, sizeof(REGION
));
215 InitLookasideList(GDIObjType_SURF_TYPE
, sizeof(SURFACE
));
216 InitLookasideList(GDIObjType_CLIENTOBJ_TYPE
, sizeof(CLIENTOBJ
));
217 InitLookasideList(GDIObjType_PATH_TYPE
, sizeof(PATH
));
218 InitLookasideList(GDIObjType_PAL_TYPE
, sizeof(PALETTE
));
219 InitLookasideList(GDIObjType_ICMLCS_TYPE
, sizeof(COLORSPACE
));
220 InitLookasideList(GDIObjType_LFONT_TYPE
, sizeof(TEXTOBJ
));
221 InitLookasideList(GDIObjType_BRUSH_TYPE
, sizeof(BRUSH
));
223 return STATUS_SUCCESS
;
228 IncrementGdiHandleCount(void)
230 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
231 if (ppi
) InterlockedIncrement((LONG
*)&ppi
->GDIHandleCount
);
236 DecrementGdiHandleCount(void)
238 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
239 if (ppi
) InterlockedDecrement((LONG
*)&ppi
->GDIHandleCount
);
244 ENTRY_pentPopFreeEntry(VOID
)
246 ULONG iFirst
, iNext
, iPrev
;
249 DPRINT("Enter InterLockedPopFreeEntry\n");
253 /* Get the index and sequence number of the first free entry */
254 iFirst
= gulFirstFree
;
256 /* Check if we have a free entry */
257 if (!(iFirst
& GDI_HANDLE_INDEX_MASK
))
259 /* Increment FirstUnused and get the new index */
260 iFirst
= InterlockedIncrement((LONG
*)&gulFirstUnused
) - 1;
262 /* Check if we have unused entries left */
263 if (iFirst
>= GDI_HANDLE_COUNT
)
265 DPRINT1("No more GDI handles left!\n");
269 /* Return the old entry */
270 return &gpentHmgr
[iFirst
];
273 /* Get a pointer to the first free entry */
274 pentFree
= &gpentHmgr
[iFirst
& GDI_HANDLE_INDEX_MASK
];
276 /* Create a new value with an increased sequence number */
277 iNext
= (USHORT
)(ULONG_PTR
)pentFree
->einfo
.pobj
;
278 iNext
|= (iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000;
280 /* Try to exchange the FirstFree value */
281 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
285 while (iPrev
!= iFirst
);
287 /* Sanity check: is entry really free? */
288 ASSERT(((ULONG_PTR
)pentFree
->einfo
.pobj
& ~GDI_HANDLE_INDEX_MASK
) == 0);
293 /* Pushes an entry of the handle table to the free list,
294 The entry must not have any references left */
297 ENTRY_vPushFreeEntry(PENTRY pentFree
)
299 ULONG iToFree
, iFirst
, iPrev
, idxToFree
;
301 DPRINT("Enter ENTRY_vPushFreeEntry\n");
303 idxToFree
= pentFree
- gpentHmgr
;
304 ASSERT((gpaulRefCount
[idxToFree
] & REF_MASK_INUSE
) == 0);
306 /* Initialize entry */
307 pentFree
->Objt
= GDIObjType_DEF_TYPE
;
308 pentFree
->ObjectOwner
.ulObj
= 0;
309 pentFree
->pUser
= NULL
;
311 /* Increase reuse counter in entry and reference counter */
312 InterlockedExchangeAdd((LONG
*)&gpaulRefCount
[idxToFree
], REF_INC_REUSE
);
313 pentFree
->FullUnique
+= 0x0100;
317 /* Get the current first free index and sequence number */
318 iFirst
= gulFirstFree
;
320 /* Set the einfo.pobj member to the index of the first free entry */
321 pentFree
->einfo
.pobj
= UlongToPtr(iFirst
& GDI_HANDLE_INDEX_MASK
);
323 /* Combine new index and increased sequence number in iToFree */
324 iToFree
= idxToFree
| ((iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000);
326 /* Try to atomically update the first free entry */
327 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
331 while (iPrev
!= iFirst
);
336 ENTRY_ReferenceEntryByHandle(HGDIOBJ hobj
, FLONG fl
)
338 ULONG ulIndex
, cNewRefs
, cOldRefs
;
341 /* Get the handle index and check if its too big */
342 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
343 if (ulIndex
>= GDI_HANDLE_COUNT
) return NULL
;
345 /* Get pointer to the entry */
346 pentry
= &gpentHmgr
[ulIndex
];
348 /* Get the current reference count */
349 cOldRefs
= gpaulRefCount
[ulIndex
];
353 /* Check if the slot is deleted */
354 if ((cOldRefs
& REF_MASK_VALID
) == 0)
356 DPRINT("GDIOBJ: Slot is not valid: 0x%lx, hobh=%p\n", cOldRefs
, hobj
);
360 /* Check if the unique value matches */
361 if (pentry
->FullUnique
!= (USHORT
)((ULONG_PTR
)hobj
>> 16))
363 DPRINT("GDIOBJ: Wrong unique value. Handle: 0x%4x, entry: 0x%4x\n",
364 (USHORT
)((ULONG_PTR
)hobj
>> 16, pentry
->FullUnique
));
368 /* Check if the object owner is this process or public */
369 if (!(fl
& GDIOBJFLAG_IGNOREPID
) &&
370 pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
371 pentry
->ObjectOwner
.ulObj
!= PtrToUlong(PsGetCurrentProcessId()))
373 DPRINT("GDIOBJ: Cannot reference foreign handle %p, pentry=%p:%lx.\n",
374 hobj
, pentry
, pentry
->ObjectOwner
.ulObj
);
378 /* Try to atomically increment the reference count */
379 cNewRefs
= cOldRefs
+ 1;
380 cOldRefs
= InterlockedCompareExchange((PLONG
)&gpaulRefCount
[ulIndex
],
384 while (cNewRefs
!= cOldRefs
+ 1);
386 /* Integrity checks */
387 ASSERT((pentry
->FullUnique
& 0x1f) == pentry
->Objt
);
388 ASSERT(pentry
->einfo
.pobj
&& pentry
->einfo
.pobj
->hHmgr
== hobj
);
395 ENTRY_hInsertObject(PENTRY pentry
, POBJ pobj
, UCHAR objt
, ULONG ulOwner
)
399 /* Calculate the handle index */
400 ulIndex
= pentry
- gpentHmgr
;
402 /* Update the fields in the ENTRY */
403 pentry
->einfo
.pobj
= pobj
;
404 pentry
->Objt
= objt
& 0x1f;
405 pentry
->FullUnique
= (pentry
->FullUnique
& 0xff00) | objt
;
406 pentry
->ObjectOwner
.ulObj
= ulOwner
;
408 /* Make the handle valid with 1 reference */
409 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_INUSE
) == 0);
410 InterlockedOr((LONG
*)&gpaulRefCount
[ulIndex
], REF_MASK_VALID
| 1);
412 /* Return the handle */
413 return (HGDIOBJ
)(((ULONG_PTR
)pentry
->FullUnique
<< 16) | ulIndex
);
418 GDIOBJ_AllocateObject(UCHAR objt
, ULONG cjSize
, FLONG fl
)
422 if (fl
& BASEFLAG_LOOKASIDE
)
424 /* Allocate the object from a lookaside list */
425 pobj
= ExAllocateFromPagedLookasideList(&gpaLookasideList
[objt
& 0x1f]);
429 /* Allocate the object from paged pool */
430 pobj
= ExAllocatePoolWithTag(PagedPool
, cjSize
, GDIOBJ_POOL_TAG(objt
));
433 if (!pobj
) return NULL
;
435 /* Initialize the object */
436 RtlZeroMemory(pobj
, cjSize
);
437 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)objt
<< 16);
438 pobj
->cExclusiveLock
= 0;
439 pobj
->ulShareCount
= 1;
440 pobj
->BaseFlags
= fl
& 0xffff;
441 DBG_INITLOG(&pobj
->slhLog
);
442 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_ALLOCATE
, 0);
449 GDIOBJ_vFreeObject(POBJ pobj
)
453 DBG_CLEANUP_EVENT_LIST(&pobj
->slhLog
);
455 /* Get the object type */
456 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0x1f;
458 /* Call the cleanup procedure */
459 ASSERT(apfnCleanup
[objt
]);
460 apfnCleanup
[objt
](pobj
);
462 /* Check if the object is allocated from a lookaside list */
463 if (pobj
->BaseFlags
& BASEFLAG_LOOKASIDE
)
465 ExFreeToPagedLookasideList(&gpaLookasideList
[objt
], pobj
);
469 ExFreePoolWithTag(pobj
, GDIOBJ_POOL_TAG(objt
));
475 GDIOBJ_vDereferenceObject(POBJ pobj
)
477 ULONG cRefs
, ulIndex
;
479 /* Must not be exclusively locked */
480 ASSERT(pobj
->cExclusiveLock
== 0);
482 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DEREFERENCE
, cRefs
);
484 /* Check if the object has a handle */
485 if (GDI_HANDLE_GET_INDEX(pobj
->hHmgr
))
487 /* Calculate the index */
488 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
490 /* Decrement reference count */
491 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
492 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]) & REF_MASK_INUSE
;
494 /* Check if we reached 0 and handle bit is not set */
497 /* Make sure it's ok to delete the object */
498 ASSERT(pobj
->BaseFlags
& BASEFLAG_READY_TO_DIE
);
500 /* Check if the handle was process owned */
501 if (gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
502 gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_NONE
)
504 /* Decrement the process handle count */
505 ASSERT(gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
==
506 HandleToUlong(PsGetCurrentProcessId()));
507 DecrementGdiHandleCount();
510 /* Push entry to the free list */
511 ENTRY_vPushFreeEntry(&gpentHmgr
[ulIndex
]);
513 /* Free the object */
514 GDIOBJ_vFreeObject(pobj
);
519 /* Decrement the objects reference count */
520 ASSERT(pobj
->ulShareCount
> 0);
521 cRefs
= InterlockedDecrement((LONG
*)&pobj
->ulShareCount
);
523 /* Check if we reached 0 */
526 /* Free the object */
527 GDIOBJ_vFreeObject(pobj
);
534 GDIOBJ_ReferenceObjectByHandle(
541 /* Check if the handle type matches */
542 ASSERT_SHARED_OBJECT_TYPE(objt
);
543 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
545 DPRINT("GDIOBJ: Wrong type. handle=%p, type=%x\n", hobj
, objt
);
549 /* Reference the handle entry */
550 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
553 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
557 /* Get the pointer to the BASEOBJECT */
558 pobj
= pentry
->einfo
.pobj
;
560 /* Check if the object is exclusively locked */
561 if (pobj
->cExclusiveLock
!= 0)
563 DPRINT1("GDIOBJ: Cannot reference oject %p with exclusive lock.\n", hobj
);
564 GDIOBJ_vDereferenceObject(pobj
);
565 DBG_DUMP_EVENT_LIST(&pobj
->slhLog
);
569 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, gpaulRefCount
[pentry
- gpentHmgr
]);
571 /* All is well, return the object */
577 GDIOBJ_vReferenceObjectByPointer(POBJ pobj
)
581 /* Must not be exclusively locked */
582 ASSERT(pobj
->cExclusiveLock
== 0);
584 /* Check if the object has a handle */
585 if (GDI_HANDLE_GET_INDEX(pobj
->hHmgr
))
587 /* Increase the handle's reference count */
588 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
589 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
590 cRefs
= InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
594 /* Increase the object's reference count */
595 cRefs
= InterlockedIncrement((LONG
*)&pobj
->ulShareCount
);
598 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, cRefs
);
611 /* Check if the handle type matches */
612 ASSERT_EXCLUSIVE_OBJECT_TYPE(objt
);
613 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
615 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj
, objt
);
619 /* Reference the handle entry */
620 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
623 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
627 /* Get the pointer to the BASEOBJECT */
628 pobj
= pentry
->einfo
.pobj
;
630 /* Check if we already own the lock */
631 dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
632 if (pobj
->dwThreadId
!= dwThreadId
)
634 /* Disable APCs and acquire the push lock */
635 KeEnterCriticalRegion();
636 ExAcquirePushLockExclusive(&pobj
->pushlock
);
638 /* Set us as lock owner */
639 ASSERT(pobj
->dwThreadId
== 0);
640 pobj
->dwThreadId
= dwThreadId
;
643 /* Increase lock count */
644 pobj
->cExclusiveLock
++;
645 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj
);
646 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_LOCK
, 0);
648 /* Return the object */
654 GDIOBJ_vUnlockObject(POBJ pobj
)
656 ULONG cRefs
, ulIndex
;
657 ASSERT(pobj
->cExclusiveLock
> 0);
659 /* Decrease lock count */
660 pobj
->cExclusiveLock
--;
661 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
662 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_UNLOCK
, 0);
664 /* Check if this was the last lock */
665 if (pobj
->cExclusiveLock
== 0)
667 /* Reset lock owner */
668 pobj
->dwThreadId
= 0;
670 /* Release the pushlock and reenable APCs */
671 ExReleasePushLockExclusive(&pobj
->pushlock
);
672 KeLeaveCriticalRegion();
675 /* Calculate the index */
676 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
678 /* Decrement reference count */
679 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
680 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
681 ASSERT(cRefs
& REF_MASK_VALID
);
686 GDIOBJ_hInsertObject(
693 /* Must have no handle and only one reference */
694 ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
) == 0);
695 ASSERT(pobj
->cExclusiveLock
== 0);
696 ASSERT(pobj
->ulShareCount
== 1);
698 /* Get a free handle entry */
699 pentry
= ENTRY_pentPopFreeEntry();
702 DPRINT1("GDIOBJ: Could not get a free entry.\n");
706 /* Make the object exclusively locked */
707 ExInitializePushLock(&pobj
->pushlock
);
708 KeEnterCriticalRegion();
709 ExAcquirePushLockExclusive(&pobj
->pushlock
);
710 pobj
->cExclusiveLock
= 1;
711 pobj
->dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
712 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
714 /* Get object type from the hHmgr field */
715 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0xff;
716 ASSERT(objt
!= GDIObjType_DEF_TYPE
);
718 /* Check if current process is requested owner */
719 if (ulOwner
== GDI_OBJ_HMGR_POWNED
)
721 /* Increment the process handle count */
722 IncrementGdiHandleCount();
725 ulOwner
= HandleToUlong(PsGetCurrentProcessId());
728 /* Insert the object into the handle table */
729 pobj
->hHmgr
= ENTRY_hInsertObject(pentry
, pobj
, objt
, ulOwner
);
731 /* Return the handle */
732 DPRINT("GDIOBJ: Created handle: %p\n", pobj
->hHmgr
);
733 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_CREATE_HANDLE
, 0);
739 GDIOBJ_vSetObjectOwner(
745 /* This is a ugly HACK, needed to fix IntGdiSetDCOwnerEx */
746 if (GDI_HANDLE_IS_STOCKOBJ(pobj
->hHmgr
))
748 DPRINT("Trying to set ownership of stock object %p to %lx\n", pobj
->hHmgr
, ulOwner
);
752 /* Get the handle entry */
753 ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
));
754 pentry
= &gpentHmgr
[GDI_HANDLE_GET_INDEX(pobj
->hHmgr
)];
756 /* Is the current process requested? */
757 if (ulOwner
== GDI_OBJ_HMGR_POWNED
)
760 ulOwner
= HandleToUlong(PsGetCurrentProcessId());
761 if (pentry
->ObjectOwner
.ulObj
!= ulOwner
)
763 IncrementGdiHandleCount();
768 if (ulOwner
== GDI_OBJ_HMGR_NONE
)
769 ulOwner
= GDI_OBJ_HMGR_PUBLIC
;
771 if (ulOwner
== GDI_OBJ_HMGR_PUBLIC
||
772 ulOwner
== GDI_OBJ_HMGR_NONE
)
774 /* Make sure we don't leak user mode memory */
775 ASSERT(pentry
->pUser
== NULL
);
776 if (pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
777 pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_NONE
)
779 DecrementGdiHandleCount();
784 pentry
->ObjectOwner
.ulObj
= ulOwner
;
787 /* Locks 2 or 3 objects at a time */
790 GDIOBJ_bLockMultipleObjects(
796 UINT auiIndices
[3] = {0, 1, 2};
799 ASSERT(ulCount
<= 3);
801 /* Sort the handles */
802 for (i
= 0; i
< ulCount
- 1; i
++)
804 for (j
= i
+ 1; j
< ulCount
; j
++)
806 if ((ULONG_PTR
)ahObj
[auiIndices
[i
]] <
807 (ULONG_PTR
)ahObj
[auiIndices
[j
]])
810 auiIndices
[i
] = auiIndices
[j
];
816 /* Lock the objects in safe order */
817 for (i
= 0; i
< ulCount
; i
++)
819 /* Skip NULL handles */
820 if (ahObj
[auiIndices
[i
]] == NULL
)
822 apObj
[auiIndices
[i
]] = NULL
;
826 /* Lock the object */
827 apObj
[auiIndices
[i
]] = GDIOBJ_LockObject(ahObj
[auiIndices
[i
]], objt
);
829 /* Check for failure */
830 if (apObj
[auiIndices
[i
]] == NULL
)
835 if (apObj
[auiIndices
[i
]])
836 GDIOBJ_vUnlockObject(apObj
[auiIndices
[i
]]);
847 GDIOBJ_pvGetObjectAttr(POBJ pobj
)
849 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
850 return gpentHmgr
[ulIndex
].pUser
;
855 GDIOBJ_vSetObjectAttr(POBJ pobj
, PVOID pvObjAttr
)
861 /* Get the handle index */
862 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
864 /* Set pointer to the usermode attribute */
865 gpentHmgr
[ulIndex
].pUser
= pvObjAttr
;
870 GDIOBJ_vDeleteObject(POBJ pobj
)
874 /* Set the object's delete flag */
875 InterlockedOr16((SHORT
*)&pobj
->BaseFlags
, BASEFLAG_READY_TO_DIE
);
876 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DELETE
, 0);
878 /* Get the handle index */
879 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
882 /* Reset the handle valid bit */
883 InterlockedAnd((LONG
*)&gpaulRefCount
[ulIndex
], ~REF_MASK_VALID
);
885 /* Check if the object is exclusively locked */
886 if (pobj
->cExclusiveLock
!= 0)
888 /* Reset lock owner and lock count */
889 pobj
->dwThreadId
= 0;
890 pobj
->cExclusiveLock
= 0;
892 /* Release the pushlock and reenable APCs */
893 ExReleasePushLockExclusive(&pobj
->pushlock
);
894 KeLeaveCriticalRegion();
895 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
899 /* Dereference the object (will take care of deletion) */
900 GDIOBJ_vDereferenceObject(pobj
);
905 GreIsHandleValid(HGDIOBJ hobj
)
909 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
910 if (!pentry
) return FALSE
;
911 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
917 GreDeleteObject(HGDIOBJ hobj
)
921 /* Check for stock objects */
922 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
924 DPRINT1("GreDeleteObject: Cannot delete stock object %p.\n", hobj
);
928 /* Reference the handle entry */
929 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
932 DPRINT1("GreDeleteObject: Trying to delete invalid object %p\n", hobj
);
936 /* Check for public owner */
937 if (pentry
->ObjectOwner
.ulObj
== GDI_OBJ_HMGR_PUBLIC
)
939 DPRINT1("GreDeleteObject: Trying to delete global object %p\n", hobj
);
940 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
944 /* Delete the object */
945 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
951 GreGetObjectOwner(HGDIOBJ hobj
)
953 ULONG ulIndex
, ulOwner
;
955 /* Get the handle index */
956 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
958 /* Check if the handle is valid */
959 if (ulIndex
>= GDI_HANDLE_COUNT
||
960 gpentHmgr
[ulIndex
].Objt
== GDIObjType_DEF_TYPE
||
961 ((ULONG_PTR
)hobj
>> 16) != gpentHmgr
[ulIndex
].FullUnique
)
963 DPRINT1("GreGetObjectOwner: invalid handle 0x%p.\n", hobj
);
964 return GDI_OBJ_HMGR_RESTRICTED
;
967 /* Get the object owner */
968 ulOwner
= gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
;
970 if (ulOwner
== HandleToUlong(PsGetCurrentProcessId()))
971 return GDI_OBJ_HMGR_POWNED
;
973 if (ulOwner
== GDI_OBJ_HMGR_PUBLIC
)
974 return GDI_OBJ_HMGR_PUBLIC
;
976 return GDI_OBJ_HMGR_RESTRICTED
;
987 /* Check for stock objects */
988 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
990 DPRINT("GreSetObjectOwner: Got stock object %p\n", hobj
);
994 /* Reference the handle entry */
995 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
998 DPRINT("GreSetObjectOwner: Invalid handle 0x%p.\n", hobj
);
1002 /* Call internal function */
1003 GDIOBJ_vSetObjectOwner(pentry
->einfo
.pobj
, ulOwner
);
1005 /* Dereference the object */
1006 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
1022 /* Verify object type */
1023 objt
= ((ULONG_PTR
)hobj
>> 16) & 0x1f;
1024 if (objt
!= GDIObjType_BRUSH_TYPE
&&
1025 objt
!= GDIObjType_SURF_TYPE
&&
1026 objt
!= GDIObjType_LFONT_TYPE
&&
1027 objt
!= GDIObjType_PAL_TYPE
)
1029 DPRINT1("GreGetObject: Invalid object type\n");
1033 pvObj
= GDIOBJ_ReferenceObjectByHandle(hobj
, objt
);
1036 DPRINT("GreGetObject: Could not lock object\n");
1040 switch (GDI_HANDLE_GET_TYPE(hobj
))
1042 case GDILoObjType_LO_PEN_TYPE
:
1043 case GDILoObjType_LO_EXTPEN_TYPE
:
1044 iResult
= PEN_GetObject(pvObj
, cbCount
, pvBuffer
);
1047 case GDILoObjType_LO_BRUSH_TYPE
:
1048 iResult
= BRUSH_GetObject(pvObj
, cbCount
, pvBuffer
);
1051 case GDILoObjType_LO_BITMAP_TYPE
:
1052 iResult
= BITMAP_GetObject(pvObj
, cbCount
, pvBuffer
);
1055 case GDILoObjType_LO_FONT_TYPE
:
1056 iResult
= FontGetObject(pvObj
, cbCount
, pvBuffer
);
1059 case GDILoObjType_LO_PALETTE_TYPE
:
1060 iResult
= PALETTE_GetObject(pvObj
, cbCount
, pvBuffer
);
1064 DPRINT1("GDI object type of 0x%p not implemented\n", hobj
);
1068 GDIOBJ_vDereferenceObject(pvObj
);
1078 OUT LPVOID lpBuffer
)
1085 DIBSECTION dibsection
;
1089 EXTLOGFONTW extlogfontw
;
1090 ENUMLOGFONTEXDVW enumlogfontexdvw
;
1093 /* Normalize to the largest supported object size */
1094 cbCount
= min((UINT
)cbCount
, sizeof(object
));
1096 /* Now do the actual call */
1097 iRetCount
= GreGetObject(hobj
, cbCount
, lpBuffer
? &object
: NULL
);
1098 cbCopyCount
= min((UINT
)cbCount
, (UINT
)iRetCount
);
1100 /* Make sure we have a buffer and a copy size */
1101 if ((cbCopyCount
) && (lpBuffer
))
1103 /* Enter SEH for buffer transfer */
1106 /* Probe the buffer and copy it */
1107 ProbeForWrite(lpBuffer
, cbCopyCount
, sizeof(WORD
));
1108 RtlCopyMemory(lpBuffer
, &object
, cbCopyCount
);
1110 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1112 /* Clear the return value.
1113 * Do *NOT* set last error here! */
1119 /* Return the count */
1126 NtGdiCreateClientObj(
1132 /* Allocate a new object */
1133 pObject
= GDIOBJ_AllocateObject(GDIObjType_CLIENTOBJ_TYPE
,
1135 BASEFLAG_LOOKASIDE
);
1138 DPRINT1("NtGdiCreateClientObj: Could not allocate a clientobj.\n");
1142 /* Mask out everything that would change the type in a wrong manner */
1143 ulType
&= (GDI_HANDLE_TYPE_MASK
& ~GDI_HANDLE_BASETYPE_MASK
);
1145 /* Set the real object type */
1146 pObject
->hHmgr
= UlongToHandle(ulType
| GDILoObjType_LO_CLIENTOBJ_TYPE
);
1148 /* Create a handle */
1149 handle
= GDIOBJ_hInsertObject(pObject
, GDI_OBJ_HMGR_POWNED
);
1152 DPRINT1("NtGdiCreateClientObj: Could not create a handle.\n");
1153 GDIOBJ_vFreeObject(pObject
);
1158 GDIOBJ_vUnlockObject(pObject
);
1166 NtGdiDeleteClientObj(
1169 /* We first need to get the real type from the handle */
1170 ULONG ulType
= GDI_HANDLE_GET_TYPE(hobj
);
1172 /* Check if it's really a CLIENTOBJ */
1173 if ((ulType
& GDI_HANDLE_BASETYPE_MASK
) != GDILoObjType_LO_CLIENTOBJ_TYPE
)
1175 /* FIXME: SetLastError? */
1179 return GreDeleteObject(hobj
);
1184 PGDI_HANDLE_TABLE GdiHandleTable
= NULL
;
1187 GDIOBJ_ShareLockObj(HGDIOBJ hObj
, DWORD ExpectedType
)
1189 if (ExpectedType
== GDI_OBJECT_TYPE_DONTCARE
)
1190 ExpectedType
= GDI_HANDLE_GET_TYPE(hObj
);
1191 return GDIOBJ_ReferenceObjectByHandle(hObj
, (ExpectedType
>> 16) & 0x1f);
1194 // This function is not safe to use with concurrent deleting attempts
1195 // That shouldn't be a problem, since we don't have any processes yet,
1196 // that could delete the handle
1199 GDIOBJ_ConvertToStockObj(HGDIOBJ
*phObj
)
1204 /* Reference the handle entry */
1205 pentry
= ENTRY_ReferenceEntryByHandle(*phObj
, 0);
1208 DPRINT1("GDIOBJ: Requested handle 0x%p is not valid.\n", *phObj
);
1212 /* Update the entry */
1213 pentry
->FullUnique
|= GDI_ENTRY_STOCK_MASK
;
1214 pentry
->ObjectOwner
.ulObj
= 0;
1216 /* Get the pointer to the BASEOBJECT */
1217 pobj
= pentry
->einfo
.pobj
;
1219 /* Calculate the new handle */
1220 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)pobj
->hHmgr
| GDI_HANDLE_STOCK_MASK
);
1222 /* Return the new handle */
1223 *phObj
= pobj
->hHmgr
;
1225 /* Dereference the handle */
1226 GDIOBJ_vDereferenceObject(pobj
);
1232 GDIOBJ_AllocObjWithHandle(ULONG ObjectType
, ULONG cjSize
)
1236 UCHAR objt
= (ObjectType
>> 16) & 0xFF;
1238 if ((objt
== GDIObjType_DC_TYPE
&& cjSize
== sizeof(DC
)) ||
1239 (objt
== GDIObjType_PAL_TYPE
&& cjSize
== sizeof(PALETTE
)) ||
1240 (objt
== GDIObjType_RGN_TYPE
&& cjSize
== sizeof(REGION
)) ||
1241 (objt
== GDIObjType_SURF_TYPE
&& cjSize
== sizeof(SURFACE
)) ||
1242 (objt
== GDIObjType_PATH_TYPE
&& cjSize
== sizeof(PATH
)))
1244 fl
|= BASEFLAG_LOOKASIDE
;
1247 pobj
= GDIOBJ_AllocateObject(objt
, cjSize
, fl
);
1248 if (!GDIOBJ_hInsertObject(pobj
, GDI_OBJ_HMGR_POWNED
))
1250 GDIOBJ_vFreeObject(pobj
);
1257 GDI_MapHandleTable(PEPROCESS pProcess
)
1259 PVOID pvMappedView
= NULL
;
1261 LARGE_INTEGER liOffset
;
1262 ULONG cjViewSize
= sizeof(GDI_HANDLE_TABLE
);
1264 liOffset
.QuadPart
= 0;
1266 ASSERT(gpvGdiHdlTblSection
!= NULL
);
1267 ASSERT(pProcess
!= NULL
);
1269 Status
= MmMapViewOfSection(gpvGdiHdlTblSection
,
1280 if (!NT_SUCCESS(Status
))
1283 return pvMappedView
;
1287 GDI_CleanupForProcess(struct _EPROCESS
*Process
)
1294 DPRINT("CleanupForProcess prochandle %x Pid %d\n",
1295 Process
, Process
->UniqueProcessId
);
1297 ASSERT(Process
== PsGetCurrentProcess());
1299 /* Get the current process Id */
1300 dwProcessId
= PtrToUlong(PsGetCurrentProcessId());
1302 /* Loop all handles in the handle table */
1303 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1305 pentry
= &gpentHmgr
[ulIndex
];
1307 /* Check if the object is owned by the process */
1308 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1310 ASSERT(pentry
->einfo
.pobj
->cExclusiveLock
== 0);
1312 /* Reference the object and delete it */
1313 InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
1314 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
1320 DbgGdiHTIntegrityCheck();
1324 ppi
= PsGetCurrentProcessWin32Process();
1325 DPRINT("Completed cleanup for process %d\n", Process
->UniqueProcessId
);
1326 if (ppi
->GDIHandleCount
!= 0)
1328 DPRINT1("Leaking %d handles!\n", ppi
->GDIHandleCount
);
1332 /* Loop all handles in the handle table */
1333 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1335 pentry
= &gpentHmgr
[ulIndex
];
1337 /* Check if the object is owned by the process */
1338 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1340 DPRINT1("Leaking object. Index=%lx, type=0x%x, refcount=%lx\n",
1341 ulIndex
, pentry
->Objt
, gpaulRefCount
[ulIndex
]);
1342 DBG_DUMP_EVENT_LIST(&pentry
->einfo
.pobj
->slhLog
);
1343 //DBG_CLEANUP_EVENT_LIST(&pentry->einfo.pobj->slhLog);