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)
56 #define DBG_INCREASE_LOCK_COUNT(ppi, hobj)
57 #define DBG_DECREASE_LOCK_COUNT(x, y)
58 #define ASSERT_SHARED_OBJECT_TYPE(objt)
59 #define ASSERT_EXCLUSIVE_OBJECT_TYPE(objt)
62 #if defined(_M_IX86) || defined(_M_AMD64)
63 #define InterlockedOr16 _InterlockedOr16
66 #define GDIOBJ_POOL_TAG(type) ('00hG' + (((type) & 0x1f) << 24))
70 REF_MASK_REUSE
= 0xff000000,
71 REF_INC_REUSE
= 0x01000000,
72 REF_MASK_VALID
= 0x00800000,
73 REF_MASK_COUNT
= 0x007fffff,
74 REF_MASK_INUSE
= 0x00ffffff,
77 /* GLOBALS *******************************************************************/
79 /* Per session handle table globals */
80 static PVOID gpvGdiHdlTblSection
= NULL
;
85 static PPAGED_LOOKASIDE_LIST gpaLookasideList
;
87 static BOOL NTAPI
GDIOBJ_Cleanup(PVOID ObjectBody
);
93 NULL
, /* 00 GDIObjType_DEF_TYPE */
94 DC_Cleanup
, /* 01 GDIObjType_DC_TYPE */
95 NULL
, /* 02 GDIObjType_UNUSED1_TYPE */
96 NULL
, /* 03 GDIObjType_UNUSED2_TYPE */
97 REGION_Cleanup
, /* 04 GDIObjType_RGN_TYPE */
98 SURFACE_Cleanup
, /* 05 GDIObjType_SURF_TYPE */
99 GDIOBJ_Cleanup
, /* 06 GDIObjType_CLIENTOBJ_TYPE */
100 GDIOBJ_Cleanup
, /* 07 GDIObjType_PATH_TYPE */
101 PALETTE_Cleanup
, /* 08 GDIObjType_PAL_TYPE */
102 GDIOBJ_Cleanup
, /* 09 GDIObjType_ICMLCS_TYPE */
103 GDIOBJ_Cleanup
, /* 0a GDIObjType_LFONT_TYPE */
104 NULL
, /* 0b GDIObjType_RFONT_TYPE, unused */
105 NULL
, /* 0c GDIObjType_PFE_TYPE, unused */
106 NULL
, /* 0d GDIObjType_PFT_TYPE, unused */
107 GDIOBJ_Cleanup
, /* 0e GDIObjType_ICMCXF_TYPE */
108 NULL
, /* 0f GDIObjType_SPRITE_TYPE, unused */
109 BRUSH_Cleanup
, /* 10 GDIObjType_BRUSH_TYPE, BRUSH, PEN, EXTPEN */
110 NULL
, /* 11 GDIObjType_UMPD_TYPE, unused */
111 NULL
, /* 12 GDIObjType_UNUSED4_TYPE */
112 NULL
, /* 13 GDIObjType_SPACE_TYPE, unused */
113 NULL
, /* 14 GDIObjType_UNUSED5_TYPE */
114 NULL
, /* 15 GDIObjType_META_TYPE, unused */
115 NULL
, /* 16 GDIObjType_EFSTATE_TYPE, unused */
116 NULL
, /* 17 GDIObjType_BMFD_TYPE, unused */
117 NULL
, /* 18 GDIObjType_VTFD_TYPE, unused */
118 NULL
, /* 19 GDIObjType_TTFD_TYPE, unused */
119 NULL
, /* 1a GDIObjType_RC_TYPE, unused */
120 NULL
, /* 1b GDIObjType_TEMP_TYPE, unused */
121 DRIVEROBJ_Cleanup
,/* 1c GDIObjType_DRVOBJ_TYPE */
122 NULL
, /* 1d GDIObjType_DCIOBJ_TYPE, unused */
123 NULL
, /* 1e GDIObjType_SPOOL_TYPE, unused */
124 NULL
, /* 1f reserved entry */
127 /* INTERNAL FUNCTIONS ********************************************************/
131 GDIOBJ_Cleanup(PVOID ObjectBody
)
138 InitLookasideList(UCHAR objt
, ULONG cjSize
)
140 ExInitializePagedLookasideList(&gpaLookasideList
[objt
],
145 GDITAG_HMGR_LOOKASIDE_START
+ (objt
<< 24),
152 InitGdiHandleTable(void)
155 LARGE_INTEGER liSize
;
157 SIZE_T cjViewSize
= 0;
159 /* Create a section for the shared handle table */
160 liSize
.QuadPart
= sizeof(GDI_HANDLE_TABLE
); // GDI_HANDLE_COUNT * sizeof(ENTRY);
161 status
= MmCreateSection(&gpvGdiHdlTblSection
,
169 if (!NT_SUCCESS(status
))
171 DPRINT1("INITGDI: Could not allocate a GDI handle table.\n");
175 /* Map the section in session space */
176 status
= MmMapViewInSessionSpace(gpvGdiHdlTblSection
,
179 if (!NT_SUCCESS(status
))
181 DPRINT1("INITGDI: Failed to map handle table section\n");
182 ObDereferenceObject(gpvGdiHdlTblSection
);
186 /* Allocate memory for the reference counter table */
187 gpaulRefCount
= EngAllocSectionMem(&pvSection
,
189 GDI_HANDLE_COUNT
* sizeof(ULONG
),
193 DPRINT1("INITGDI: Failed to allocate reference table.\n");
194 ObDereferenceObject(gpvGdiHdlTblSection
);
195 return STATUS_INSUFFICIENT_RESOURCES
;
199 gulFirstUnused
= RESERVE_ENTRIES_COUNT
;
201 GdiHandleTable
= (PVOID
)gpentHmgr
;
203 /* Initialize the lookaside lists */
204 gpaLookasideList
= ExAllocatePoolWithTag(NonPagedPool
,
205 GDIObjTypeTotal
* sizeof(PAGED_LOOKASIDE_LIST
),
207 if(!gpaLookasideList
)
208 return STATUS_NO_MEMORY
;
210 InitLookasideList(GDIObjType_DC_TYPE
, sizeof(DC
));
211 InitLookasideList(GDIObjType_RGN_TYPE
, sizeof(REGION
));
212 InitLookasideList(GDIObjType_SURF_TYPE
, sizeof(SURFACE
));
213 InitLookasideList(GDIObjType_CLIENTOBJ_TYPE
, sizeof(CLIENTOBJ
));
214 InitLookasideList(GDIObjType_PATH_TYPE
, sizeof(PATH
));
215 InitLookasideList(GDIObjType_PAL_TYPE
, sizeof(PALETTE
));
216 InitLookasideList(GDIObjType_ICMLCS_TYPE
, sizeof(COLORSPACE
));
217 InitLookasideList(GDIObjType_LFONT_TYPE
, sizeof(TEXTOBJ
));
218 InitLookasideList(GDIObjType_BRUSH_TYPE
, sizeof(BRUSH
));
220 return STATUS_SUCCESS
;
225 IncrementGdiHandleCount(void)
227 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
228 if (ppi
) InterlockedIncrement((LONG
*)&ppi
->GDIHandleCount
);
233 DecrementGdiHandleCount(void)
235 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
236 if (ppi
) InterlockedDecrement((LONG
*)&ppi
->GDIHandleCount
);
241 ENTRY_pentPopFreeEntry(VOID
)
243 ULONG iFirst
, iNext
, iPrev
;
246 DPRINT("Enter InterLockedPopFreeEntry\n");
250 /* Get the index and sequence number of the first free entry */
251 iFirst
= gulFirstFree
;
253 /* Check if we have a free entry */
254 if (!(iFirst
& GDI_HANDLE_INDEX_MASK
))
256 /* Increment FirstUnused and get the new index */
257 iFirst
= InterlockedIncrement((LONG
*)&gulFirstUnused
) - 1;
259 /* Check if we have unused entries left */
260 if (iFirst
>= GDI_HANDLE_COUNT
)
262 DPRINT1("No more GDI handles left!\n");
266 /* Return the old entry */
267 return &gpentHmgr
[iFirst
];
270 /* Get a pointer to the first free entry */
271 pentFree
= &gpentHmgr
[iFirst
& GDI_HANDLE_INDEX_MASK
];
273 /* Create a new value with an increased sequence number */
274 iNext
= (USHORT
)(ULONG_PTR
)pentFree
->einfo
.pobj
;
275 iNext
|= (iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000;
277 /* Try to exchange the FirstFree value */
278 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
282 while (iPrev
!= iFirst
);
284 /* Sanity check: is entry really free? */
285 ASSERT(((ULONG_PTR
)pentFree
->einfo
.pobj
& ~GDI_HANDLE_INDEX_MASK
) == 0);
290 /* Pushes an entry of the handle table to the free list,
291 The entry must not have any references left */
294 ENTRY_vPushFreeEntry(PENTRY pentFree
)
296 ULONG iToFree
, iFirst
, iPrev
, idxToFree
;
298 DPRINT("Enter ENTRY_vPushFreeEntry\n");
300 idxToFree
= pentFree
- gpentHmgr
;
301 ASSERT((gpaulRefCount
[idxToFree
] & REF_MASK_INUSE
) == 0);
303 /* Initialize entry */
304 pentFree
->Objt
= GDIObjType_DEF_TYPE
;
305 pentFree
->ObjectOwner
.ulObj
= 0;
306 pentFree
->pUser
= NULL
;
308 /* Increase reuse counter in entry and reference counter */
309 InterlockedExchangeAdd((LONG
*)&gpaulRefCount
[idxToFree
], REF_INC_REUSE
);
310 pentFree
->FullUnique
+= 0x0100;
314 /* Get the current first free index and sequence number */
315 iFirst
= gulFirstFree
;
317 /* Set the einfo.pobj member to the index of the first free entry */
318 pentFree
->einfo
.pobj
= UlongToPtr(iFirst
& GDI_HANDLE_INDEX_MASK
);
320 /* Combine new index and increased sequence number in iToFree */
321 iToFree
= idxToFree
| ((iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000);
323 /* Try to atomically update the first free entry */
324 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
328 while (iPrev
!= iFirst
);
333 ENTRY_ReferenceEntryByHandle(HGDIOBJ hobj
, FLONG fl
)
335 ULONG ulIndex
, cNewRefs
, cOldRefs
;
338 /* Get the handle index and check if its too big */
339 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
340 if (ulIndex
>= GDI_HANDLE_COUNT
) return NULL
;
342 /* Get pointer to the entry */
343 pentry
= &gpentHmgr
[ulIndex
];
345 /* Get the current reference count */
346 cOldRefs
= gpaulRefCount
[ulIndex
];
350 /* Check if the slot is deleted */
351 if ((cOldRefs
& REF_MASK_VALID
) == 0)
353 DPRINT("GDIOBJ: Slot is not valid: 0x%lx, hobh=%p\n", cOldRefs
, hobj
);
357 /* Check if the unique value matches */
358 if (pentry
->FullUnique
!= (USHORT
)((ULONG_PTR
)hobj
>> 16))
360 DPRINT("GDIOBJ: Wrong unique value. Handle: 0x%4x, entry: 0x%4x\n",
361 (USHORT
)((ULONG_PTR
)hobj
>> 16), pentry
->FullUnique
);
365 /* Check if the object owner is this process or public */
366 if (!(fl
& GDIOBJFLAG_IGNOREPID
) &&
367 pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
368 pentry
->ObjectOwner
.ulObj
!= PtrToUlong(PsGetCurrentProcessId()))
370 DPRINT("GDIOBJ: Cannot reference foreign handle %p, pentry=%p:%lx.\n",
371 hobj
, pentry
, pentry
->ObjectOwner
.ulObj
);
375 /* Try to atomically increment the reference count */
376 cNewRefs
= cOldRefs
+ 1;
377 cOldRefs
= InterlockedCompareExchange((PLONG
)&gpaulRefCount
[ulIndex
],
381 while (cNewRefs
!= cOldRefs
+ 1);
383 /* Integrity checks */
384 ASSERT((pentry
->FullUnique
& 0x1f) == pentry
->Objt
);
385 ASSERT(pentry
->einfo
.pobj
&& pentry
->einfo
.pobj
->hHmgr
== hobj
);
392 ENTRY_hInsertObject(PENTRY pentry
, POBJ pobj
, UCHAR objt
, ULONG ulOwner
)
396 /* Calculate the handle index */
397 ulIndex
= pentry
- gpentHmgr
;
399 /* Update the fields in the ENTRY */
400 pentry
->einfo
.pobj
= pobj
;
401 pentry
->Objt
= objt
& 0x1f;
402 pentry
->FullUnique
= (pentry
->FullUnique
& 0xff00) | objt
;
403 pentry
->ObjectOwner
.ulObj
= ulOwner
;
405 /* Make the handle valid with 1 reference */
406 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_INUSE
) == 0);
407 InterlockedOr((LONG
*)&gpaulRefCount
[ulIndex
], REF_MASK_VALID
| 1);
409 /* Return the handle */
410 return (HGDIOBJ
)(((ULONG_PTR
)pentry
->FullUnique
<< 16) | ulIndex
);
415 GDIOBJ_AllocateObject(UCHAR objt
, ULONG cjSize
, FLONG fl
)
419 if (fl
& BASEFLAG_LOOKASIDE
)
421 /* Allocate the object from a lookaside list */
422 pobj
= ExAllocateFromPagedLookasideList(&gpaLookasideList
[objt
& 0x1f]);
426 /* Allocate the object from paged pool */
427 pobj
= ExAllocatePoolWithTag(PagedPool
, cjSize
, GDIOBJ_POOL_TAG(objt
));
430 if (!pobj
) return NULL
;
432 /* Initialize the object */
433 RtlZeroMemory(pobj
, cjSize
);
434 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)objt
<< 16);
435 pobj
->cExclusiveLock
= 0;
436 pobj
->ulShareCount
= 1;
437 pobj
->BaseFlags
= fl
& 0xffff;
438 DBG_INITLOG(&pobj
->slhLog
);
439 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_ALLOCATE
, 0);
446 GDIOBJ_vFreeObject(POBJ pobj
)
450 DBG_CLEANUP_EVENT_LIST(&pobj
->slhLog
);
452 /* Get the object type */
453 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0x1f;
455 /* Call the cleanup procedure */
456 ASSERT(apfnCleanup
[objt
]);
457 apfnCleanup
[objt
](pobj
);
459 /* Check if the object is allocated from a lookaside list */
460 if (pobj
->BaseFlags
& BASEFLAG_LOOKASIDE
)
462 ExFreeToPagedLookasideList(&gpaLookasideList
[objt
], pobj
);
466 ExFreePoolWithTag(pobj
, GDIOBJ_POOL_TAG(objt
));
472 GDIOBJ_vDereferenceObject(POBJ pobj
)
474 ULONG cRefs
, ulIndex
;
476 /* Calculate the index */
477 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
479 /* Check if the object has a handle */
482 /* Decrement reference count */
483 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
484 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
485 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DEREFERENCE
, cRefs
);
487 /* Check if we reached 0 and handle bit is not set */
488 if ((cRefs
& REF_MASK_INUSE
) == 0)
490 /* Make sure it's ok to delete the object */
491 ASSERT(pobj
->BaseFlags
& BASEFLAG_READY_TO_DIE
);
493 /* Check if the handle was process owned */
494 if (gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
495 gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_NONE
)
497 /* Decrement the process handle count */
498 ASSERT(gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
==
499 HandleToUlong(PsGetCurrentProcessId()));
500 DecrementGdiHandleCount();
503 /* Push entry to the free list */
504 ENTRY_vPushFreeEntry(&gpentHmgr
[ulIndex
]);
506 /* Free the object */
507 GDIOBJ_vFreeObject(pobj
);
512 /* Decrement the objects reference count */
513 ASSERT(pobj
->ulShareCount
> 0);
514 cRefs
= InterlockedDecrement((LONG
*)&pobj
->ulShareCount
);
515 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DEREFERENCE
, cRefs
);
517 /* Check if we reached 0 */
520 /* Free the object */
521 GDIOBJ_vFreeObject(pobj
);
528 GDIOBJ_ReferenceObjectByHandle(
535 /* Check if the handle type matches */
536 ASSERT_SHARED_OBJECT_TYPE(objt
);
537 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
539 DPRINT("GDIOBJ: Wrong type. handle=%p, type=%x\n", hobj
, objt
);
543 /* Reference the handle entry */
544 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
547 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
551 /* Get the pointer to the BASEOBJECT */
552 pobj
= pentry
->einfo
.pobj
;
554 /* Check if the object is exclusively locked */
555 if (pobj
->cExclusiveLock
!= 0)
557 DPRINT1("GDIOBJ: Cannot reference oject %p with exclusive lock.\n", hobj
);
558 GDIOBJ_vDereferenceObject(pobj
);
559 DBG_DUMP_EVENT_LIST(&pobj
->slhLog
);
563 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, gpaulRefCount
[pentry
- gpentHmgr
]);
565 /* All is well, return the object */
571 GDIOBJ_vReferenceObjectByPointer(POBJ pobj
)
575 /* Check if the object has a handle */
576 if (GDI_HANDLE_GET_INDEX(pobj
->hHmgr
))
578 /* Increase the handle's reference count */
579 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
580 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
581 cRefs
= InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
585 /* Increase the object's reference count */
586 cRefs
= InterlockedIncrement((LONG
*)&pobj
->ulShareCount
);
589 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, cRefs
);
602 /* Check if the handle type matches */
603 ASSERT_EXCLUSIVE_OBJECT_TYPE(objt
);
604 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
606 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj
, objt
);
610 /* Reference the handle entry */
611 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
614 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
618 /* Get the pointer to the BASEOBJECT */
619 pobj
= pentry
->einfo
.pobj
;
621 /* Check if we already own the lock */
622 dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
623 if (pobj
->dwThreadId
!= dwThreadId
)
625 /* Disable APCs and acquire the push lock */
626 KeEnterCriticalRegion();
627 ExAcquirePushLockExclusive(&pobj
->pushlock
);
629 /* Set us as lock owner */
630 ASSERT(pobj
->dwThreadId
== 0);
631 pobj
->dwThreadId
= dwThreadId
;
634 /* Increase lock count */
635 pobj
->cExclusiveLock
++;
636 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj
);
637 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_LOCK
, 0);
639 /* Return the object */
645 GDIOBJ_vUnlockObject(POBJ pobj
)
647 ULONG cRefs
, ulIndex
;
648 ASSERT(pobj
->cExclusiveLock
> 0);
650 /* Decrease lock count */
651 pobj
->cExclusiveLock
--;
652 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
653 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_UNLOCK
, 0);
655 /* Check if this was the last lock */
656 if (pobj
->cExclusiveLock
== 0)
658 /* Reset lock owner */
659 pobj
->dwThreadId
= 0;
661 /* Release the pushlock and reenable APCs */
662 ExReleasePushLockExclusive(&pobj
->pushlock
);
663 KeLeaveCriticalRegion();
666 /* Calculate the index */
667 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
669 /* Decrement reference count */
670 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
671 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
672 ASSERT(cRefs
& REF_MASK_VALID
);
677 GDIOBJ_hInsertObject(
684 /* Must have no handle and only one reference */
685 ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
) == 0);
686 ASSERT(pobj
->cExclusiveLock
== 0);
687 ASSERT(pobj
->ulShareCount
== 1);
689 /* Get a free handle entry */
690 pentry
= ENTRY_pentPopFreeEntry();
693 DPRINT1("GDIOBJ: Could not get a free entry.\n");
697 /* Make the object exclusively locked */
698 ExInitializePushLock(&pobj
->pushlock
);
699 KeEnterCriticalRegion();
700 ExAcquirePushLockExclusive(&pobj
->pushlock
);
701 pobj
->cExclusiveLock
= 1;
702 pobj
->dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
703 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
705 /* Get object type from the hHmgr field */
706 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0xff;
707 ASSERT(objt
!= GDIObjType_DEF_TYPE
);
709 /* Check if current process is requested owner */
710 if (ulOwner
== GDI_OBJ_HMGR_POWNED
)
712 /* Increment the process handle count */
713 IncrementGdiHandleCount();
716 ulOwner
= HandleToUlong(PsGetCurrentProcessId());
719 /* Insert the object into the handle table */
720 pobj
->hHmgr
= ENTRY_hInsertObject(pentry
, pobj
, objt
, ulOwner
);
722 /* Return the handle */
723 DPRINT("GDIOBJ: Created handle: %p\n", pobj
->hHmgr
);
724 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_CREATE_HANDLE
, 0);
730 GDIOBJ_vSetObjectOwner(
736 /* This is a ugly HACK, needed to fix IntGdiSetDCOwnerEx */
737 if (GDI_HANDLE_IS_STOCKOBJ(pobj
->hHmgr
))
739 DPRINT("Trying to set ownership of stock object %p to %lx\n", pobj
->hHmgr
, ulOwner
);
743 /* Get the handle entry */
744 ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
));
745 pentry
= &gpentHmgr
[GDI_HANDLE_GET_INDEX(pobj
->hHmgr
)];
747 /* Is the current process requested? */
748 if (ulOwner
== GDI_OBJ_HMGR_POWNED
)
751 ulOwner
= HandleToUlong(PsGetCurrentProcessId());
752 if (pentry
->ObjectOwner
.ulObj
!= ulOwner
)
754 IncrementGdiHandleCount();
759 if (ulOwner
== GDI_OBJ_HMGR_NONE
)
760 ulOwner
= GDI_OBJ_HMGR_PUBLIC
;
762 if (ulOwner
== GDI_OBJ_HMGR_PUBLIC
||
763 ulOwner
== GDI_OBJ_HMGR_NONE
)
765 /* Make sure we don't leak user mode memory */
766 ASSERT(pentry
->pUser
== NULL
);
767 if (pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
768 pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_NONE
)
770 DecrementGdiHandleCount();
775 pentry
->ObjectOwner
.ulObj
= ulOwner
;
776 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_SET_OWNER
, 0);
779 /* Locks 2 or 3 objects at a time */
782 GDIOBJ_bLockMultipleObjects(
788 UINT auiIndices
[3] = {0, 1, 2};
791 ASSERT(ulCount
<= 3);
793 /* Sort the handles */
794 for (i
= 0; i
< ulCount
- 1; i
++)
796 for (j
= i
+ 1; j
< ulCount
; j
++)
798 if ((ULONG_PTR
)ahObj
[auiIndices
[i
]] <
799 (ULONG_PTR
)ahObj
[auiIndices
[j
]])
802 auiIndices
[i
] = auiIndices
[j
];
808 /* Lock the objects in safe order */
809 for (i
= 0; i
< ulCount
; i
++)
811 /* Skip NULL handles */
812 if (ahObj
[auiIndices
[i
]] == NULL
)
814 apObj
[auiIndices
[i
]] = NULL
;
818 /* Lock the object */
819 apObj
[auiIndices
[i
]] = GDIOBJ_LockObject(ahObj
[auiIndices
[i
]], objt
);
821 /* Check for failure */
822 if (apObj
[auiIndices
[i
]] == NULL
)
827 if (apObj
[auiIndices
[i
]])
828 GDIOBJ_vUnlockObject(apObj
[auiIndices
[i
]]);
839 GDIOBJ_pvGetObjectAttr(POBJ pobj
)
841 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
842 return gpentHmgr
[ulIndex
].pUser
;
847 GDIOBJ_vSetObjectAttr(POBJ pobj
, PVOID pvObjAttr
)
853 /* Get the handle index */
854 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
856 /* Set pointer to the usermode attribute */
857 gpentHmgr
[ulIndex
].pUser
= pvObjAttr
;
862 GDIOBJ_vDeleteObject(POBJ pobj
)
866 /* Set the object's delete flag */
867 InterlockedOr16((SHORT
*)&pobj
->BaseFlags
, BASEFLAG_READY_TO_DIE
);
868 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DELETE
, 0);
870 /* Get the handle index */
871 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
874 /* Reset the handle valid bit */
875 InterlockedAnd((LONG
*)&gpaulRefCount
[ulIndex
], ~REF_MASK_VALID
);
877 /* Check if the object is exclusively locked */
878 if (pobj
->cExclusiveLock
!= 0)
880 /* Reset lock owner and lock count */
881 pobj
->dwThreadId
= 0;
882 pobj
->cExclusiveLock
= 0;
884 /* Release the pushlock and reenable APCs */
885 ExReleasePushLockExclusive(&pobj
->pushlock
);
886 KeLeaveCriticalRegion();
887 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
891 /* Dereference the object (will take care of deletion) */
892 GDIOBJ_vDereferenceObject(pobj
);
897 GreIsHandleValid(HGDIOBJ hobj
)
901 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
902 if (!pentry
) return FALSE
;
903 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
909 GreDeleteObject(HGDIOBJ hobj
)
913 /* Check for stock objects */
914 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
916 DPRINT1("GreDeleteObject: Cannot delete stock object %p.\n", hobj
);
920 /* Reference the handle entry */
921 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
924 DPRINT1("GreDeleteObject: Trying to delete invalid object %p\n", hobj
);
928 /* Check for public owner */
929 if (pentry
->ObjectOwner
.ulObj
== GDI_OBJ_HMGR_PUBLIC
)
931 DPRINT1("GreDeleteObject: Trying to delete global object %p\n", hobj
);
932 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
936 /* Delete the object */
937 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
943 GreGetObjectOwner(HGDIOBJ hobj
)
945 ULONG ulIndex
, ulOwner
;
947 /* Get the handle index */
948 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
950 /* Check if the handle is valid */
951 if (ulIndex
>= GDI_HANDLE_COUNT
||
952 gpentHmgr
[ulIndex
].Objt
== GDIObjType_DEF_TYPE
||
953 ((ULONG_PTR
)hobj
>> 16) != gpentHmgr
[ulIndex
].FullUnique
)
955 DPRINT1("GreGetObjectOwner: invalid handle 0x%p.\n", hobj
);
956 return GDI_OBJ_HMGR_RESTRICTED
;
959 /* Get the object owner */
960 ulOwner
= gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
;
962 if (ulOwner
== HandleToUlong(PsGetCurrentProcessId()))
963 return GDI_OBJ_HMGR_POWNED
;
965 if (ulOwner
== GDI_OBJ_HMGR_PUBLIC
)
966 return GDI_OBJ_HMGR_PUBLIC
;
968 return GDI_OBJ_HMGR_RESTRICTED
;
979 /* Check for stock objects */
980 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
982 DPRINT("GreSetObjectOwner: Got stock object %p\n", hobj
);
986 /* Reference the handle entry */
987 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
990 DPRINT("GreSetObjectOwner: Invalid handle 0x%p.\n", hobj
);
994 /* Call internal function */
995 GDIOBJ_vSetObjectOwner(pentry
->einfo
.pobj
, ulOwner
);
997 /* Dereference the object */
998 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
1014 /* Verify object type */
1015 objt
= ((ULONG_PTR
)hobj
>> 16) & 0x1f;
1016 if (objt
!= GDIObjType_BRUSH_TYPE
&&
1017 objt
!= GDIObjType_SURF_TYPE
&&
1018 objt
!= GDIObjType_LFONT_TYPE
&&
1019 objt
!= GDIObjType_PAL_TYPE
)
1021 DPRINT1("GreGetObject: Invalid object type\n");
1025 pvObj
= GDIOBJ_ReferenceObjectByHandle(hobj
, objt
);
1028 DPRINT("GreGetObject: Could not lock object\n");
1032 switch (GDI_HANDLE_GET_TYPE(hobj
))
1034 case GDILoObjType_LO_PEN_TYPE
:
1035 case GDILoObjType_LO_EXTPEN_TYPE
:
1036 iResult
= PEN_GetObject(pvObj
, cbCount
, pvBuffer
);
1039 case GDILoObjType_LO_BRUSH_TYPE
:
1040 iResult
= BRUSH_GetObject(pvObj
, cbCount
, pvBuffer
);
1043 case GDILoObjType_LO_BITMAP_TYPE
:
1044 iResult
= BITMAP_GetObject(pvObj
, cbCount
, pvBuffer
);
1047 case GDILoObjType_LO_FONT_TYPE
:
1048 iResult
= FontGetObject(pvObj
, cbCount
, pvBuffer
);
1051 case GDILoObjType_LO_PALETTE_TYPE
:
1052 iResult
= PALETTE_GetObject(pvObj
, cbCount
, pvBuffer
);
1056 DPRINT1("GDI object type of 0x%p not implemented\n", hobj
);
1060 GDIOBJ_vDereferenceObject(pvObj
);
1069 IN INT cjBufferSize
,
1070 OUT LPVOID lpBuffer
)
1072 UINT iResult
, cjMaxSize
;
1076 DIBSECTION dibsection
;
1080 EXTLOGFONTW extlogfontw
;
1081 ENUMLOGFONTEXDVW enumlogfontexdvw
;
1084 /* Normalize to the largest supported object size */
1085 cjMaxSize
= min((UINT
)cjBufferSize
, sizeof(object
));
1087 /* Now do the actual call */
1088 iResult
= GreGetObject(hobj
, cjMaxSize
, lpBuffer
? &object
: NULL
);
1090 /* Check if we have a buffer and data */
1091 if ((lpBuffer
!= NULL
) && (iResult
!= 0))
1093 /* Enter SEH for buffer transfer */
1096 /* Probe the buffer and copy it */
1097 cjMaxSize
= min(cjMaxSize
, iResult
);
1098 ProbeForWrite(lpBuffer
, cjMaxSize
, sizeof(WORD
));
1099 RtlCopyMemory(lpBuffer
, &object
, cjMaxSize
);
1101 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1103 /* Clear the return value.
1104 * Do *NOT* set last error here! */
1110 /* Return the count */
1117 NtGdiCreateClientObj(
1123 /* Allocate a new object */
1124 pObject
= GDIOBJ_AllocateObject(GDIObjType_CLIENTOBJ_TYPE
,
1126 BASEFLAG_LOOKASIDE
);
1129 DPRINT1("NtGdiCreateClientObj: Could not allocate a clientobj.\n");
1133 /* Mask out everything that would change the type in a wrong manner */
1134 ulType
&= (GDI_HANDLE_TYPE_MASK
& ~GDI_HANDLE_BASETYPE_MASK
);
1136 /* Set the real object type */
1137 pObject
->hHmgr
= UlongToHandle(ulType
| GDILoObjType_LO_CLIENTOBJ_TYPE
);
1139 /* Create a handle */
1140 handle
= GDIOBJ_hInsertObject(pObject
, GDI_OBJ_HMGR_POWNED
);
1143 DPRINT1("NtGdiCreateClientObj: Could not create a handle.\n");
1144 GDIOBJ_vFreeObject(pObject
);
1149 GDIOBJ_vUnlockObject(pObject
);
1157 NtGdiDeleteClientObj(
1160 /* We first need to get the real type from the handle */
1161 ULONG ulType
= GDI_HANDLE_GET_TYPE(hobj
);
1163 /* Check if it's really a CLIENTOBJ */
1164 if ((ulType
& GDI_HANDLE_BASETYPE_MASK
) != GDILoObjType_LO_CLIENTOBJ_TYPE
)
1166 /* FIXME: SetLastError? */
1170 return GreDeleteObject(hobj
);
1175 PGDI_HANDLE_TABLE GdiHandleTable
= NULL
;
1178 GDIOBJ_ShareLockObj(HGDIOBJ hObj
, DWORD ExpectedType
)
1180 if (ExpectedType
== GDI_OBJECT_TYPE_DONTCARE
)
1181 ExpectedType
= GDI_HANDLE_GET_TYPE(hObj
);
1182 return GDIOBJ_ReferenceObjectByHandle(hObj
, (ExpectedType
>> 16) & 0x1f);
1185 // This function is not safe to use with concurrent deleting attempts
1186 // That shouldn't be a problem, since we don't have any processes yet,
1187 // that could delete the handle
1190 GDIOBJ_ConvertToStockObj(HGDIOBJ
*phObj
)
1195 /* Reference the handle entry */
1196 pentry
= ENTRY_ReferenceEntryByHandle(*phObj
, 0);
1199 DPRINT1("GDIOBJ: Requested handle 0x%p is not valid.\n", *phObj
);
1203 /* Update the entry */
1204 pentry
->FullUnique
|= GDI_ENTRY_STOCK_MASK
;
1205 pentry
->ObjectOwner
.ulObj
= 0;
1207 /* Get the pointer to the BASEOBJECT */
1208 pobj
= pentry
->einfo
.pobj
;
1210 /* Calculate the new handle */
1211 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)pobj
->hHmgr
| GDI_HANDLE_STOCK_MASK
);
1213 /* Return the new handle */
1214 *phObj
= pobj
->hHmgr
;
1216 /* Dereference the handle */
1217 GDIOBJ_vDereferenceObject(pobj
);
1223 GDIOBJ_AllocObjWithHandle(ULONG ObjectType
, ULONG cjSize
)
1227 UCHAR objt
= (ObjectType
>> 16) & 0xFF;
1229 if ((objt
== GDIObjType_DC_TYPE
&& cjSize
== sizeof(DC
)) ||
1230 (objt
== GDIObjType_PAL_TYPE
&& cjSize
== sizeof(PALETTE
)) ||
1231 (objt
== GDIObjType_RGN_TYPE
&& cjSize
== sizeof(REGION
)) ||
1232 (objt
== GDIObjType_SURF_TYPE
&& cjSize
== sizeof(SURFACE
)) ||
1233 (objt
== GDIObjType_PATH_TYPE
&& cjSize
== sizeof(PATH
)))
1235 fl
|= BASEFLAG_LOOKASIDE
;
1238 pobj
= GDIOBJ_AllocateObject(objt
, cjSize
, fl
);
1244 if (!GDIOBJ_hInsertObject(pobj
, GDI_OBJ_HMGR_POWNED
))
1246 GDIOBJ_vFreeObject(pobj
);
1253 GDI_MapHandleTable(PEPROCESS pProcess
)
1255 PVOID pvMappedView
= NULL
;
1257 LARGE_INTEGER liOffset
;
1258 ULONG cjViewSize
= sizeof(GDI_HANDLE_TABLE
);
1260 liOffset
.QuadPart
= 0;
1262 ASSERT(gpvGdiHdlTblSection
!= NULL
);
1263 ASSERT(pProcess
!= NULL
);
1265 Status
= MmMapViewOfSection(gpvGdiHdlTblSection
,
1276 if (!NT_SUCCESS(Status
))
1279 return pvMappedView
;
1283 GDI_CleanupForProcess(struct _EPROCESS
*Process
)
1290 DPRINT("CleanupForProcess prochandle %p Pid %p\n",
1291 Process
, Process
->UniqueProcessId
);
1293 ASSERT(Process
== PsGetCurrentProcess());
1295 /* Get the current process Id */
1296 dwProcessId
= PtrToUlong(PsGetCurrentProcessId());
1298 /* Loop all handles in the handle table */
1299 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1301 pentry
= &gpentHmgr
[ulIndex
];
1303 /* Check if the object is owned by the process */
1304 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1306 ASSERT(pentry
->einfo
.pobj
->cExclusiveLock
== 0);
1308 /* Reference the object and delete it */
1309 InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
1310 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
1316 DbgGdiHTIntegrityCheck();
1320 ppi
= PsGetCurrentProcessWin32Process();
1321 DPRINT("Completed cleanup for process %p\n", Process
->UniqueProcessId
);
1322 if (ppi
->GDIHandleCount
!= 0)
1324 DPRINT1("Leaking %d handles!\n", ppi
->GDIHandleCount
);
1328 /* Loop all handles in the handle table */
1329 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1331 pentry
= &gpentHmgr
[ulIndex
];
1333 /* Check if the object is owned by the process */
1334 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1336 DPRINT1("Leaking object. Index=%lx, type=0x%x, refcount=%lx\n",
1337 ulIndex
, pentry
->Objt
, gpaulRefCount
[ulIndex
]);
1338 DBG_DUMP_EVENT_LIST(&pentry
->einfo
.pobj
->slhLog
);
1339 //DBG_CLEANUP_EVENT_LIST(&pentry->einfo.pobj->slhLog);