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 #define ASSERT_TRYLOCK_OBJECT_TYPE(objt) \
56 ASSERT((objt) == GDIObjType_DRVOBJ_TYPE)
58 #define DBG_INCREASE_LOCK_COUNT(ppi, hobj)
59 #define DBG_DECREASE_LOCK_COUNT(x, y)
60 #define ASSERT_SHARED_OBJECT_TYPE(objt)
61 #define ASSERT_EXCLUSIVE_OBJECT_TYPE(objt)
62 #define ASSERT_TRYLOCK_OBJECT_TYPE(objt)
65 #if defined(_M_IX86) || defined(_M_AMD64)
66 #define InterlockedOr16 _InterlockedOr16
69 #define GDIOBJ_POOL_TAG(type) ('00hG' + (((type) & 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
;
88 static PPAGED_LOOKASIDE_LIST gpaLookasideList
;
90 static VOID NTAPI
GDIOBJ_vCleanup(PVOID ObjectBody
);
96 NULL
, /* 00 GDIObjType_DEF_TYPE */
97 DC_vCleanup
, /* 01 GDIObjType_DC_TYPE */
98 NULL
, /* 02 GDIObjType_UNUSED1_TYPE */
99 NULL
, /* 03 GDIObjType_UNUSED2_TYPE */
100 REGION_vCleanup
, /* 04 GDIObjType_RGN_TYPE */
101 SURFACE_vCleanup
, /* 05 GDIObjType_SURF_TYPE */
102 GDIOBJ_vCleanup
, /* 06 GDIObjType_CLIENTOBJ_TYPE */
103 GDIOBJ_vCleanup
, /* 07 GDIObjType_PATH_TYPE */
104 PALETTE_vCleanup
, /* 08 GDIObjType_PAL_TYPE */
105 GDIOBJ_vCleanup
, /* 09 GDIObjType_ICMLCS_TYPE */
106 GDIOBJ_vCleanup
, /* 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_vCleanup
, /* 0e GDIObjType_ICMCXF_TYPE */
111 NULL
, /* 0f GDIObjType_SPRITE_TYPE, unused */
112 BRUSH_vCleanup
, /* 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_vCleanup
,/* 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 ********************************************************/
135 GDIOBJ_vCleanup(PVOID ObjectBody
)
142 InitLookasideList(UCHAR objt
, ULONG cjSize
)
144 ExInitializePagedLookasideList(&gpaLookasideList
[objt
],
149 GDITAG_HMGR_LOOKASIDE_START
+ (objt
<< 24),
156 InitGdiHandleTable(void)
159 LARGE_INTEGER liSize
;
161 SIZE_T cjViewSize
= 0;
163 /* Create a section for the shared handle table */
164 liSize
.QuadPart
= sizeof(GDI_HANDLE_TABLE
); // GDI_HANDLE_COUNT * sizeof(ENTRY);
165 status
= MmCreateSection(&gpvGdiHdlTblSection
,
173 if (!NT_SUCCESS(status
))
175 DPRINT1("INITGDI: Could not allocate a GDI handle table.\n");
179 /* Map the section in session space */
180 status
= MmMapViewInSessionSpace(gpvGdiHdlTblSection
,
183 if (!NT_SUCCESS(status
))
185 DPRINT1("INITGDI: Failed to map handle table section\n");
186 ObDereferenceObject(gpvGdiHdlTblSection
);
190 /* Allocate memory for the reference counter table */
191 gpaulRefCount
= EngAllocSectionMem(&pvSection
,
193 GDI_HANDLE_COUNT
* sizeof(ULONG
),
197 DPRINT1("INITGDI: Failed to allocate reference table.\n");
198 ObDereferenceObject(gpvGdiHdlTblSection
);
199 return STATUS_INSUFFICIENT_RESOURCES
;
203 gulFirstUnused
= RESERVE_ENTRIES_COUNT
;
205 GdiHandleTable
= (PVOID
)gpentHmgr
;
207 /* Initialize the lookaside lists */
208 gpaLookasideList
= ExAllocatePoolWithTag(NonPagedPool
,
209 GDIObjTypeTotal
* sizeof(PAGED_LOOKASIDE_LIST
),
211 if(!gpaLookasideList
)
212 return STATUS_NO_MEMORY
;
214 InitLookasideList(GDIObjType_DC_TYPE
, sizeof(DC
));
215 InitLookasideList(GDIObjType_RGN_TYPE
, sizeof(REGION
));
216 InitLookasideList(GDIObjType_SURF_TYPE
, sizeof(SURFACE
));
217 InitLookasideList(GDIObjType_CLIENTOBJ_TYPE
, sizeof(CLIENTOBJ
));
218 InitLookasideList(GDIObjType_PATH_TYPE
, sizeof(PATH
));
219 InitLookasideList(GDIObjType_PAL_TYPE
, sizeof(PALETTE
));
220 InitLookasideList(GDIObjType_ICMLCS_TYPE
, sizeof(COLORSPACE
));
221 InitLookasideList(GDIObjType_LFONT_TYPE
, sizeof(TEXTOBJ
));
222 InitLookasideList(GDIObjType_BRUSH_TYPE
, sizeof(BRUSH
));
224 return STATUS_SUCCESS
;
229 IncrementCurrentProcessGdiHandleCount(void)
231 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
232 if (ppi
) InterlockedIncrement((LONG
*)&ppi
->GDIHandleCount
);
237 DecrementCurrentProcessGdiHandleCount(void)
239 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
240 if (ppi
) InterlockedDecrement((LONG
*)&ppi
->GDIHandleCount
);
245 IncrementGdiHandleCount(ULONG ulProcessId
)
251 Status
= PsLookupProcessByProcessId(ULongToHandle(ulProcessId
), &pep
);
252 NT_ASSERT(NT_SUCCESS(Status
));
254 ppi
= PsGetProcessWin32Process(pep
);
255 if (ppi
) InterlockedIncrement((LONG
*)&ppi
->GDIHandleCount
);
256 if (NT_SUCCESS(Status
)) ObDereferenceObject(pep
);
261 DecrementGdiHandleCount(ULONG ulProcessId
)
267 Status
= PsLookupProcessByProcessId(ULongToHandle(ulProcessId
), &pep
);
268 NT_ASSERT(NT_SUCCESS(Status
));
270 ppi
= PsGetProcessWin32Process(pep
);
271 if (ppi
) InterlockedDecrement((LONG
*)&ppi
->GDIHandleCount
);
272 if (NT_SUCCESS(Status
)) ObDereferenceObject(pep
);
277 ENTRY_pentPopFreeEntry(VOID
)
279 ULONG iFirst
, iNext
, iPrev
;
282 DPRINT("Enter InterLockedPopFreeEntry\n");
286 /* Get the index and sequence number of the first free entry */
287 iFirst
= gulFirstFree
;
289 /* Check if we have a free entry */
290 if (!(iFirst
& GDI_HANDLE_INDEX_MASK
))
292 /* Increment FirstUnused and get the new index */
293 iFirst
= InterlockedIncrement((LONG
*)&gulFirstUnused
) - 1;
295 /* Check if we have unused entries left */
296 if (iFirst
>= GDI_HANDLE_COUNT
)
298 DPRINT1("No more GDI handles left!\n");
302 /* Return the old entry */
303 return &gpentHmgr
[iFirst
];
306 /* Get a pointer to the first free entry */
307 pentFree
= &gpentHmgr
[iFirst
& GDI_HANDLE_INDEX_MASK
];
309 /* Create a new value with an increased sequence number */
310 iNext
= (USHORT
)(ULONG_PTR
)pentFree
->einfo
.pobj
;
311 iNext
|= (iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000;
313 /* Try to exchange the FirstFree value */
314 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
318 while (iPrev
!= iFirst
);
320 /* Sanity check: is entry really free? */
321 ASSERT(((ULONG_PTR
)pentFree
->einfo
.pobj
& ~GDI_HANDLE_INDEX_MASK
) == 0);
326 /* Pushes an entry of the handle table to the free list,
327 The entry must not have any references left */
330 ENTRY_vPushFreeEntry(PENTRY pentFree
)
332 ULONG iToFree
, iFirst
, iPrev
, idxToFree
;
334 DPRINT("Enter ENTRY_vPushFreeEntry\n");
336 idxToFree
= pentFree
- gpentHmgr
;
337 ASSERT((gpaulRefCount
[idxToFree
] & REF_MASK_INUSE
) == 0);
339 /* Initialize entry */
340 pentFree
->Objt
= GDIObjType_DEF_TYPE
;
341 pentFree
->ObjectOwner
.ulObj
= 0;
342 pentFree
->pUser
= NULL
;
344 /* Increase reuse counter in entry and reference counter */
345 InterlockedExchangeAdd((LONG
*)&gpaulRefCount
[idxToFree
], REF_INC_REUSE
);
346 pentFree
->FullUnique
+= 0x0100;
350 /* Get the current first free index and sequence number */
351 iFirst
= gulFirstFree
;
353 /* Set the einfo.pobj member to the index of the first free entry */
354 pentFree
->einfo
.pobj
= UlongToPtr(iFirst
& GDI_HANDLE_INDEX_MASK
);
356 /* Combine new index and increased sequence number in iToFree */
357 iToFree
= idxToFree
| ((iFirst
& ~GDI_HANDLE_INDEX_MASK
) + 0x10000);
359 /* Try to atomically update the first free entry */
360 iPrev
= InterlockedCompareExchange((LONG
*)&gulFirstFree
,
364 while (iPrev
!= iFirst
);
369 ENTRY_ReferenceEntryByHandle(HGDIOBJ hobj
, FLONG fl
)
371 ULONG ulIndex
, cNewRefs
, cOldRefs
;
374 /* Get the handle index and check if its too big */
375 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
376 if (ulIndex
>= GDI_HANDLE_COUNT
) return NULL
;
378 /* Get pointer to the entry */
379 pentry
= &gpentHmgr
[ulIndex
];
381 /* Get the current reference count */
382 cOldRefs
= gpaulRefCount
[ulIndex
];
386 /* Check if the slot is deleted */
387 if ((cOldRefs
& REF_MASK_VALID
) == 0)
389 DPRINT("GDIOBJ: Slot is not valid: 0x%lx, hobh=%p\n", cOldRefs
, hobj
);
393 /* Check if the unique value matches */
394 if (pentry
->FullUnique
!= (USHORT
)((ULONG_PTR
)hobj
>> 16))
396 DPRINT("GDIOBJ: Wrong unique value. Handle: 0x%4x, entry: 0x%4x\n",
397 (USHORT
)((ULONG_PTR
)hobj
>> 16), pentry
->FullUnique
);
401 /* Check if the object owner is this process or public */
402 if (!(fl
& GDIOBJFLAG_IGNOREPID
) &&
403 pentry
->ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
404 pentry
->ObjectOwner
.ulObj
!= PtrToUlong(PsGetCurrentProcessId()))
406 DPRINT("GDIOBJ: Cannot reference foreign handle %p, pentry=%p:%lx.\n",
407 hobj
, pentry
, pentry
->ObjectOwner
.ulObj
);
411 /* Try to atomically increment the reference count */
412 cNewRefs
= cOldRefs
+ 1;
413 cOldRefs
= InterlockedCompareExchange((PLONG
)&gpaulRefCount
[ulIndex
],
417 while (cNewRefs
!= cOldRefs
+ 1);
419 /* Integrity checks */
420 ASSERT((pentry
->FullUnique
& 0x1f) == pentry
->Objt
);
421 ASSERT(pentry
->einfo
.pobj
&& pentry
->einfo
.pobj
->hHmgr
== hobj
);
428 ENTRY_hInsertObject(PENTRY pentry
, POBJ pobj
, UCHAR objt
, ULONG ulOwner
)
432 /* Calculate the handle index */
433 ulIndex
= pentry
- gpentHmgr
;
435 /* Update the fields in the ENTRY */
436 pentry
->einfo
.pobj
= pobj
;
437 pentry
->Objt
= objt
& 0x1f;
438 pentry
->FullUnique
= (pentry
->FullUnique
& 0xff00) | objt
;
439 pentry
->ObjectOwner
.ulObj
= ulOwner
;
441 /* Make the handle valid with 1 reference */
442 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_INUSE
) == 0);
443 InterlockedOr((LONG
*)&gpaulRefCount
[ulIndex
], REF_MASK_VALID
| 1);
445 /* Return the handle */
446 return (HGDIOBJ
)(((ULONG_PTR
)pentry
->FullUnique
<< 16) | ulIndex
);
451 GDIOBJ_AllocateObject(UCHAR objt
, ULONG cjSize
, FLONG fl
)
455 if (fl
& BASEFLAG_LOOKASIDE
)
457 /* Allocate the object from a lookaside list */
458 pobj
= ExAllocateFromPagedLookasideList(&gpaLookasideList
[objt
& 0x1f]);
462 /* Allocate the object from paged pool */
463 pobj
= ExAllocatePoolWithTag(PagedPool
, cjSize
, GDIOBJ_POOL_TAG(objt
));
466 if (!pobj
) return NULL
;
468 /* Initialize the object */
469 RtlZeroMemory(pobj
, cjSize
);
470 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)objt
<< 16);
471 pobj
->cExclusiveLock
= 0;
472 pobj
->ulShareCount
= 1;
473 pobj
->BaseFlags
= fl
& 0xffff;
474 DBG_INITLOG(&pobj
->slhLog
);
475 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_ALLOCATE
, 0);
482 GDIOBJ_vFreeObject(POBJ pobj
)
486 DBG_CLEANUP_EVENT_LIST(&pobj
->slhLog
);
488 /* Get the object type */
489 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0x1f;
491 /* Call the cleanup procedure */
492 ASSERT(apfnCleanup
[objt
]);
493 apfnCleanup
[objt
](pobj
);
495 /* Check if the object is allocated from a lookaside list */
496 if (pobj
->BaseFlags
& BASEFLAG_LOOKASIDE
)
498 ExFreeToPagedLookasideList(&gpaLookasideList
[objt
], pobj
);
502 ExFreePoolWithTag(pobj
, GDIOBJ_POOL_TAG(objt
));
508 GDIOBJ_vDereferenceObject(POBJ pobj
)
510 ULONG cRefs
, ulIndex
;
512 /* Calculate the index */
513 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
515 /* Check if the object has a handle */
518 /* Decrement reference count */
519 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
520 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
521 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DEREFERENCE
, cRefs
);
523 /* Check if we reached 0 and handle bit is not set */
524 if ((cRefs
& REF_MASK_INUSE
) == 0)
526 /* Make sure it's ok to delete the object */
527 ASSERT(pobj
->BaseFlags
& BASEFLAG_READY_TO_DIE
);
529 /* Check if the handle was process owned */
530 if (gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_PUBLIC
&&
531 gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
!= GDI_OBJ_HMGR_NONE
)
533 /* Decrement the process handle count */
534 ASSERT(gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
==
535 HandleToUlong(PsGetCurrentProcessId()));
536 DecrementCurrentProcessGdiHandleCount();
539 /* Push entry to the free list */
540 ENTRY_vPushFreeEntry(&gpentHmgr
[ulIndex
]);
542 /* Free the object */
543 GDIOBJ_vFreeObject(pobj
);
548 /* Decrement the objects reference count */
549 ASSERT(pobj
->ulShareCount
> 0);
550 cRefs
= InterlockedDecrement((LONG
*)&pobj
->ulShareCount
);
551 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DEREFERENCE
, cRefs
);
553 /* Check if we reached 0 */
556 /* Free the object */
557 GDIOBJ_vFreeObject(pobj
);
564 GDIOBJ_ReferenceObjectByHandle(
571 /* Check if the handle type matches */
572 ASSERT_SHARED_OBJECT_TYPE(objt
);
573 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
575 DPRINT("GDIOBJ: Wrong type. handle=%p, type=%x\n", hobj
, objt
);
579 /* Reference the handle entry */
580 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
583 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
587 /* Get the pointer to the BASEOBJECT */
588 pobj
= pentry
->einfo
.pobj
;
590 /* Check if the object is exclusively locked */
591 if (pobj
->cExclusiveLock
!= 0)
593 DPRINT1("GDIOBJ: Cannot reference oject %p with exclusive lock.\n", hobj
);
594 GDIOBJ_vDereferenceObject(pobj
);
595 DBG_DUMP_EVENT_LIST(&pobj
->slhLog
);
599 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, gpaulRefCount
[pentry
- gpentHmgr
]);
601 /* All is well, return the object */
607 GDIOBJ_vReferenceObjectByPointer(POBJ pobj
)
611 /* Check if the object has a handle */
612 if (GDI_HANDLE_GET_INDEX(pobj
->hHmgr
))
614 /* Increase the handle's reference count */
615 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
616 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
617 cRefs
= InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
621 /* Increase the object's reference count */
622 cRefs
= InterlockedIncrement((LONG
*)&pobj
->ulShareCount
);
625 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_REFERENCE
, cRefs
);
630 GDIOBJ_TryLockObject(
638 /* Check if the handle type matches */
639 ASSERT_TRYLOCK_OBJECT_TYPE(objt
);
640 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
642 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj
, objt
);
646 /* Reference the handle entry */
647 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
650 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
654 /* Get the pointer to the BASEOBJECT */
655 pobj
= pentry
->einfo
.pobj
;
657 /* Check if we already own the lock */
658 dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
659 if (pobj
->dwThreadId
!= dwThreadId
)
661 /* Disable APCs and try acquiring the push lock */
662 KeEnterCriticalRegion();
663 if(!ExTryAcquirePushLockExclusive(&pobj
->pushlock
))
665 ULONG cRefs
, ulIndex
;
666 /* Already owned. Clean up and leave. */
667 KeLeaveCriticalRegion();
669 /* Calculate the index */
670 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
672 /* Decrement reference count */
673 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
674 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
675 ASSERT(cRefs
& REF_MASK_VALID
);
680 /* Set us as lock owner */
681 ASSERT(pobj
->dwThreadId
== 0);
682 pobj
->dwThreadId
= dwThreadId
;
685 /* Increase lock count */
686 pobj
->cExclusiveLock
++;
687 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj
);
688 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_LOCK
, 0);
690 /* Return the object */
704 /* Check if the handle type matches */
705 ASSERT_EXCLUSIVE_OBJECT_TYPE(objt
);
706 if ((((ULONG_PTR
)hobj
>> 16) & 0x1f) != objt
)
708 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj
, objt
);
712 /* Reference the handle entry */
713 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
716 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj
);
720 /* Get the pointer to the BASEOBJECT */
721 pobj
= pentry
->einfo
.pobj
;
723 /* Check if we already own the lock */
724 dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
725 if (pobj
->dwThreadId
!= dwThreadId
)
727 /* Disable APCs and acquire the push lock */
728 KeEnterCriticalRegion();
729 ExAcquirePushLockExclusive(&pobj
->pushlock
);
731 /* Set us as lock owner */
732 ASSERT(pobj
->dwThreadId
== 0);
733 pobj
->dwThreadId
= dwThreadId
;
736 /* Increase lock count */
737 pobj
->cExclusiveLock
++;
738 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj
);
739 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_LOCK
, 0);
741 /* Return the object */
747 GDIOBJ_vUnlockObject(POBJ pobj
)
749 ULONG cRefs
, ulIndex
;
750 ASSERT(pobj
->cExclusiveLock
> 0);
752 /* Decrease lock count */
753 pobj
->cExclusiveLock
--;
754 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
755 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_UNLOCK
, 0);
757 /* Check if this was the last lock */
758 if (pobj
->cExclusiveLock
== 0)
760 /* Reset lock owner */
761 pobj
->dwThreadId
= 0;
763 /* Release the pushlock and reenable APCs */
764 ExReleasePushLockExclusive(&pobj
->pushlock
);
765 KeLeaveCriticalRegion();
768 /* Calculate the index */
769 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
771 /* Decrement reference count */
772 ASSERT((gpaulRefCount
[ulIndex
] & REF_MASK_COUNT
) > 0);
773 cRefs
= InterlockedDecrement((LONG
*)&gpaulRefCount
[ulIndex
]);
774 ASSERT(cRefs
& REF_MASK_VALID
);
779 GDIOBJ_hInsertObject(
786 /* Must have no handle and only one reference */
787 ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
) == 0);
788 ASSERT(pobj
->cExclusiveLock
== 0);
789 ASSERT(pobj
->ulShareCount
== 1);
791 /* Get a free handle entry */
792 pentry
= ENTRY_pentPopFreeEntry();
795 DPRINT1("GDIOBJ: Could not get a free entry.\n");
799 /* Make the object exclusively locked */
800 ExInitializePushLock(&pobj
->pushlock
);
801 KeEnterCriticalRegion();
802 ExAcquirePushLockExclusive(&pobj
->pushlock
);
803 pobj
->cExclusiveLock
= 1;
804 pobj
->dwThreadId
= PtrToUlong(PsGetCurrentThreadId());
805 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
807 /* Get object type from the hHmgr field */
808 objt
= ((ULONG_PTR
)pobj
->hHmgr
>> 16) & 0xff;
809 ASSERT(objt
!= GDIObjType_DEF_TYPE
);
811 /* Check if current process is requested owner */
812 if (ulOwner
== GDI_OBJ_HMGR_POWNED
)
814 /* Increment the process handle count */
815 IncrementCurrentProcessGdiHandleCount();
818 ulOwner
= HandleToUlong(PsGetCurrentProcessId());
821 /* Insert the object into the handle table */
822 pobj
->hHmgr
= ENTRY_hInsertObject(pentry
, pobj
, objt
, ulOwner
);
824 /* Return the handle */
825 DPRINT("GDIOBJ: Created handle: %p\n", pobj
->hHmgr
);
826 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_CREATE_HANDLE
, 0);
832 GDIOBJ_vSetObjectOwner(
839 /* This is a ugly HACK, needed to fix IntGdiSetDCOwnerEx */
840 if (GDI_HANDLE_IS_STOCKOBJ(pobj
->hHmgr
))
842 DPRINT("Trying to set ownership of stock object %p to %lx\n", pobj
->hHmgr
, ulNewOwner
);
846 /* Get the handle entry */
847 NT_ASSERT(GDI_HANDLE_GET_INDEX(pobj
->hHmgr
));
848 pentry
= &gpentHmgr
[GDI_HANDLE_GET_INDEX(pobj
->hHmgr
)];
850 /* Check if the new owner is the same as the old one */
851 ulOldOwner
= pentry
->ObjectOwner
.ulObj
;
852 if (ulOldOwner
== ulNewOwner
)
858 /* Is the current process requested? */
859 if (ulNewOwner
== GDI_OBJ_HMGR_POWNED
)
862 ulNewOwner
= HandleToUlong(PsGetCurrentProcessId());
866 if (ulNewOwner
== GDI_OBJ_HMGR_NONE
)
867 ulNewOwner
= GDI_OBJ_HMGR_PUBLIC
;
869 /* Was the object process owned? */
870 if ((ulOldOwner
!= GDI_OBJ_HMGR_PUBLIC
) &&
871 (ulOldOwner
!= GDI_OBJ_HMGR_NONE
))
873 /* Decrement the previous owners handle count */
874 DecrementGdiHandleCount(ulOldOwner
);
877 /* Is the new owner a process? */
878 if ((ulNewOwner
!= GDI_OBJ_HMGR_PUBLIC
) &&
879 (ulNewOwner
!= GDI_OBJ_HMGR_NONE
))
881 /* Increment the new owners handle count */
882 IncrementGdiHandleCount(ulNewOwner
);
886 /* Make sure we don't leak user mode memory */
887 NT_ASSERT(pentry
->pUser
== NULL
);
891 pentry
->ObjectOwner
.ulObj
= ulNewOwner
;
892 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_SET_OWNER
, 0);
895 /* Locks 2 or 3 objects at a time */
898 GDIOBJ_bLockMultipleObjects(
904 UINT auiIndices
[3] = {0, 1, 2};
907 ASSERT(ulCount
<= 3);
909 /* Sort the handles */
910 for (i
= 0; i
< ulCount
- 1; i
++)
912 for (j
= i
+ 1; j
< ulCount
; j
++)
914 if ((ULONG_PTR
)ahObj
[auiIndices
[i
]] <
915 (ULONG_PTR
)ahObj
[auiIndices
[j
]])
918 auiIndices
[i
] = auiIndices
[j
];
924 /* Lock the objects in safe order */
925 for (i
= 0; i
< ulCount
; i
++)
927 /* Skip NULL handles */
928 if (ahObj
[auiIndices
[i
]] == NULL
)
930 apObj
[auiIndices
[i
]] = NULL
;
934 /* Lock the object */
935 apObj
[auiIndices
[i
]] = GDIOBJ_LockObject(ahObj
[auiIndices
[i
]], objt
);
937 /* Check for failure */
938 if (apObj
[auiIndices
[i
]] == NULL
)
943 if (apObj
[auiIndices
[i
]])
944 GDIOBJ_vUnlockObject(apObj
[auiIndices
[i
]]);
955 GDIOBJ_pvGetObjectAttr(POBJ pobj
)
957 ULONG ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
958 return gpentHmgr
[ulIndex
].pUser
;
963 GDIOBJ_vSetObjectAttr(POBJ pobj
, PVOID pvObjAttr
)
969 /* Get the handle index */
970 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
972 /* Set pointer to the usermode attribute */
973 gpentHmgr
[ulIndex
].pUser
= pvObjAttr
;
978 GDIOBJ_vDeleteObject(POBJ pobj
)
982 /* Set the object's delete flag */
983 InterlockedOr16((SHORT
*)&pobj
->BaseFlags
, BASEFLAG_READY_TO_DIE
);
984 DBG_LOGEVENT(&pobj
->slhLog
, EVENT_DELETE
, 0);
986 /* Get the handle index */
987 ulIndex
= GDI_HANDLE_GET_INDEX(pobj
->hHmgr
);
990 /* Reset the handle valid bit */
991 InterlockedAnd((LONG
*)&gpaulRefCount
[ulIndex
], ~REF_MASK_VALID
);
993 /* Check if the object is exclusively locked */
994 if (pobj
->cExclusiveLock
!= 0)
996 /* Reset lock owner and lock count */
997 pobj
->dwThreadId
= 0;
998 pobj
->cExclusiveLock
= 0;
1000 /* Release the pushlock and reenable APCs */
1001 ExReleasePushLockExclusive(&pobj
->pushlock
);
1002 KeLeaveCriticalRegion();
1003 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj
->hHmgr
);
1007 /* Dereference the object (will take care of deletion) */
1008 GDIOBJ_vDereferenceObject(pobj
);
1013 GreIsHandleValid(HGDIOBJ hobj
)
1017 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
1018 if (!pentry
) return FALSE
;
1019 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
1025 GreDeleteObject(HGDIOBJ hobj
)
1029 /* Check for stock objects */
1030 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
1032 DPRINT1("GreDeleteObject: Cannot delete stock object %p.\n", hobj
);
1036 /* Reference the handle entry */
1037 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, 0);
1040 DPRINT1("GreDeleteObject: Trying to delete invalid object %p\n", hobj
);
1044 /* Check for public owner */
1045 if (pentry
->ObjectOwner
.ulObj
== GDI_OBJ_HMGR_PUBLIC
)
1047 DPRINT1("GreDeleteObject: Trying to delete global object %p\n", hobj
);
1048 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
1052 /* Delete the object */
1053 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
1059 GreGetObjectOwner(HGDIOBJ hobj
)
1061 ULONG ulIndex
, ulOwner
;
1063 /* Get the handle index */
1064 ulIndex
= GDI_HANDLE_GET_INDEX(hobj
);
1066 /* Check if the handle is valid */
1067 if (ulIndex
>= GDI_HANDLE_COUNT
||
1068 gpentHmgr
[ulIndex
].Objt
== GDIObjType_DEF_TYPE
||
1069 ((ULONG_PTR
)hobj
>> 16) != gpentHmgr
[ulIndex
].FullUnique
)
1071 DPRINT1("GreGetObjectOwner: invalid handle 0x%p.\n", hobj
);
1072 return GDI_OBJ_HMGR_RESTRICTED
;
1075 /* Get the object owner */
1076 ulOwner
= gpentHmgr
[ulIndex
].ObjectOwner
.ulObj
;
1078 if (ulOwner
== HandleToUlong(PsGetCurrentProcessId()))
1079 return GDI_OBJ_HMGR_POWNED
;
1081 if (ulOwner
== GDI_OBJ_HMGR_PUBLIC
)
1082 return GDI_OBJ_HMGR_PUBLIC
;
1084 return GDI_OBJ_HMGR_RESTRICTED
;
1089 GreSetObjectOwnerEx(
1096 /* Check for stock objects */
1097 if (GDI_HANDLE_IS_STOCKOBJ(hobj
))
1099 DPRINT("GreSetObjectOwner: Got stock object %p\n", hobj
);
1103 /* Reference the handle entry */
1104 pentry
= ENTRY_ReferenceEntryByHandle(hobj
, Flags
);
1107 DPRINT("GreSetObjectOwner: Invalid handle 0x%p.\n", hobj
);
1111 /* Call internal function */
1112 GDIOBJ_vSetObjectOwner(pentry
->einfo
.pobj
, ulOwner
);
1114 /* Dereference the object */
1115 GDIOBJ_vDereferenceObject(pentry
->einfo
.pobj
);
1126 return GreSetObjectOwnerEx(hobj
, ulOwner
, 0);
1140 /* Verify object type */
1141 objt
= ((ULONG_PTR
)hobj
>> 16) & 0x1f;
1142 if (objt
!= GDIObjType_BRUSH_TYPE
&&
1143 objt
!= GDIObjType_SURF_TYPE
&&
1144 objt
!= GDIObjType_LFONT_TYPE
&&
1145 objt
!= GDIObjType_PAL_TYPE
)
1147 DPRINT1("GreGetObject: Invalid object type\n");
1151 pvObj
= GDIOBJ_ReferenceObjectByHandle(hobj
, objt
);
1154 DPRINT("GreGetObject: Could not lock object\n");
1158 switch (GDI_HANDLE_GET_TYPE(hobj
))
1160 case GDILoObjType_LO_PEN_TYPE
:
1161 case GDILoObjType_LO_EXTPEN_TYPE
:
1162 iResult
= PEN_GetObject(pvObj
, cbCount
, pvBuffer
);
1165 case GDILoObjType_LO_BRUSH_TYPE
:
1166 iResult
= BRUSH_GetObject(pvObj
, cbCount
, pvBuffer
);
1169 case GDILoObjType_LO_BITMAP_TYPE
:
1170 iResult
= BITMAP_GetObject(pvObj
, cbCount
, pvBuffer
);
1173 case GDILoObjType_LO_FONT_TYPE
:
1174 iResult
= FontGetObject(pvObj
, cbCount
, pvBuffer
);
1177 case GDILoObjType_LO_PALETTE_TYPE
:
1178 iResult
= PALETTE_GetObject(pvObj
, cbCount
, pvBuffer
);
1182 DPRINT1("GDI object type of 0x%p not implemented\n", hobj
);
1186 GDIOBJ_vDereferenceObject(pvObj
);
1195 IN INT cjBufferSize
,
1196 OUT LPVOID lpBuffer
)
1198 UINT iResult
, cjMaxSize
;
1202 DIBSECTION dibsection
;
1206 EXTLOGFONTW extlogfontw
;
1207 ENUMLOGFONTEXDVW enumlogfontexdvw
;
1210 /* Normalize to the largest supported object size */
1211 cjMaxSize
= min((UINT
)cjBufferSize
, sizeof(object
));
1213 /* Now do the actual call */
1214 iResult
= GreGetObject(hobj
, cjMaxSize
, lpBuffer
? &object
: NULL
);
1216 /* Check if we have a buffer and data */
1217 if ((lpBuffer
!= NULL
) && (iResult
!= 0))
1219 /* Enter SEH for buffer transfer */
1222 /* Probe the buffer and copy it */
1223 cjMaxSize
= min(cjMaxSize
, iResult
);
1224 ProbeForWrite(lpBuffer
, cjMaxSize
, sizeof(WORD
));
1225 RtlCopyMemory(lpBuffer
, &object
, cjMaxSize
);
1227 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1229 /* Clear the return value.
1230 * Do *NOT* set last error here! */
1236 /* Return the count */
1243 NtGdiCreateClientObj(
1249 /* Allocate a new object */
1250 pObject
= GDIOBJ_AllocateObject(GDIObjType_CLIENTOBJ_TYPE
,
1252 BASEFLAG_LOOKASIDE
);
1255 DPRINT1("NtGdiCreateClientObj: Could not allocate a clientobj.\n");
1259 /* Mask out everything that would change the type in a wrong manner */
1260 ulType
&= (GDI_HANDLE_TYPE_MASK
& ~GDI_HANDLE_BASETYPE_MASK
);
1262 /* Set the real object type */
1263 pObject
->hHmgr
= UlongToHandle(ulType
| GDILoObjType_LO_CLIENTOBJ_TYPE
);
1265 /* Create a handle */
1266 handle
= GDIOBJ_hInsertObject(pObject
, GDI_OBJ_HMGR_POWNED
);
1269 DPRINT1("NtGdiCreateClientObj: Could not create a handle.\n");
1270 GDIOBJ_vFreeObject(pObject
);
1275 GDIOBJ_vUnlockObject(pObject
);
1283 NtGdiDeleteClientObj(
1286 /* We first need to get the real type from the handle */
1287 ULONG ulType
= GDI_HANDLE_GET_TYPE(hobj
);
1289 /* Check if it's really a CLIENTOBJ */
1290 if ((ulType
& GDI_HANDLE_BASETYPE_MASK
) != GDILoObjType_LO_CLIENTOBJ_TYPE
)
1292 /* FIXME: SetLastError? */
1296 return GreDeleteObject(hobj
);
1301 PGDI_HANDLE_TABLE GdiHandleTable
= NULL
;
1304 GDIOBJ_ShareLockObj(HGDIOBJ hObj
, DWORD ExpectedType
)
1306 if (ExpectedType
== GDI_OBJECT_TYPE_DONTCARE
)
1307 ExpectedType
= GDI_HANDLE_GET_TYPE(hObj
);
1308 return GDIOBJ_ReferenceObjectByHandle(hObj
, (ExpectedType
>> 16) & 0x1f);
1311 // This function is not safe to use with concurrent deleting attempts
1312 // That shouldn't be a problem, since we don't have any processes yet,
1313 // that could delete the handle
1316 GDIOBJ_ConvertToStockObj(HGDIOBJ
*phObj
)
1321 /* Reference the handle entry */
1322 pentry
= ENTRY_ReferenceEntryByHandle(*phObj
, 0);
1325 DPRINT1("GDIOBJ: Requested handle 0x%p is not valid.\n", *phObj
);
1329 /* Update the entry */
1330 pentry
->FullUnique
|= GDI_ENTRY_STOCK_MASK
;
1331 pentry
->ObjectOwner
.ulObj
= 0;
1333 /* Get the pointer to the BASEOBJECT */
1334 pobj
= pentry
->einfo
.pobj
;
1336 /* Calculate the new handle */
1337 pobj
->hHmgr
= (HGDIOBJ
)((ULONG_PTR
)pobj
->hHmgr
| GDI_HANDLE_STOCK_MASK
);
1339 /* Return the new handle */
1340 *phObj
= pobj
->hHmgr
;
1342 /* Dereference the handle */
1343 GDIOBJ_vDereferenceObject(pobj
);
1349 GDIOBJ_AllocObjWithHandle(ULONG ObjectType
, ULONG cjSize
)
1353 UCHAR objt
= (ObjectType
>> 16) & 0xFF;
1355 if ((objt
== GDIObjType_DC_TYPE
&& cjSize
== sizeof(DC
)) ||
1356 (objt
== GDIObjType_PAL_TYPE
&& cjSize
== sizeof(PALETTE
)) ||
1357 (objt
== GDIObjType_RGN_TYPE
&& cjSize
== sizeof(REGION
)) ||
1358 (objt
== GDIObjType_SURF_TYPE
&& cjSize
== sizeof(SURFACE
)) ||
1359 (objt
== GDIObjType_PATH_TYPE
&& cjSize
== sizeof(PATH
)))
1361 fl
|= BASEFLAG_LOOKASIDE
;
1364 pobj
= GDIOBJ_AllocateObject(objt
, cjSize
, fl
);
1370 if (!GDIOBJ_hInsertObject(pobj
, GDI_OBJ_HMGR_POWNED
))
1372 GDIOBJ_vFreeObject(pobj
);
1379 GDI_MapHandleTable(PEPROCESS pProcess
)
1381 PVOID pvMappedView
= NULL
;
1383 LARGE_INTEGER liOffset
;
1384 ULONG cjViewSize
= sizeof(GDI_HANDLE_TABLE
);
1386 liOffset
.QuadPart
= 0;
1388 ASSERT(gpvGdiHdlTblSection
!= NULL
);
1389 ASSERT(pProcess
!= NULL
);
1391 Status
= MmMapViewOfSection(gpvGdiHdlTblSection
,
1402 if (!NT_SUCCESS(Status
))
1405 return pvMappedView
;
1409 GDI_CleanupForProcess(struct _EPROCESS
*Process
)
1416 DPRINT("CleanupForProcess prochandle %p Pid %p\n",
1417 Process
, Process
->UniqueProcessId
);
1419 ASSERT(Process
== PsGetCurrentProcess());
1421 /* Get the current process Id */
1422 dwProcessId
= PtrToUlong(PsGetCurrentProcessId());
1424 /* Loop all handles in the handle table */
1425 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1427 pentry
= &gpentHmgr
[ulIndex
];
1429 /* Check if the object is owned by the process */
1430 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1432 ASSERT(pentry
->einfo
.pobj
->cExclusiveLock
== 0);
1434 /* Reference the object and delete it */
1435 InterlockedIncrement((LONG
*)&gpaulRefCount
[ulIndex
]);
1436 GDIOBJ_vDeleteObject(pentry
->einfo
.pobj
);
1442 DbgGdiHTIntegrityCheck();
1446 ppi
= PsGetCurrentProcessWin32Process();
1447 DPRINT("Completed cleanup for process %p\n", Process
->UniqueProcessId
);
1448 if (ppi
->GDIHandleCount
!= 0)
1450 DPRINT1("Leaking %d handles!\n", ppi
->GDIHandleCount
);
1454 /* Loop all handles in the handle table */
1455 for (ulIndex
= RESERVE_ENTRIES_COUNT
; ulIndex
< gulFirstUnused
; ulIndex
++)
1457 pentry
= &gpentHmgr
[ulIndex
];
1459 /* Check if the object is owned by the process */
1460 if (pentry
->ObjectOwner
.ulObj
== dwProcessId
)
1462 DPRINT1("Leaking object. Index=%lx, type=0x%x, refcount=%lx\n",
1463 ulIndex
, pentry
->Objt
, gpaulRefCount
[ulIndex
]);
1464 DBG_DUMP_EVENT_LIST(&pentry
->einfo
.pobj
->slhLog
);
1465 //DBG_CLEANUP_EVENT_LIST(&pentry->einfo.pobj->slhLog);