[GDI32/WIN32K]
[reactos.git] / reactos / win32ss / gdi / ntgdi / gdiobj.c
1 /*
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
7 */
8
9 /*
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.
31 *
32 */
33
34 /* INCLUDES ******************************************************************/
35
36 #include <win32k.h>
37 #define NDEBUG
38 #include <debug.h>
39
40 // Move to gdidbg.h
41 #if DBG
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)
56 #else
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)
61 #endif
62
63 #define MmMapViewInSessionSpace MmMapViewInSystemSpace
64
65 #if defined(_M_IX86) || defined(_M_AMD64)
66 #define InterlockedOr16 _InterlockedOr16
67 #endif
68
69 #define GDIOBJ_POOL_TAG(type) ('00hG' + ((objt & 0x1f) << 24))
70
71 enum
72 {
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,
78 };
79
80 /* GLOBALS *******************************************************************/
81
82 /* Per session handle table globals */
83 static PVOID gpvGdiHdlTblSection = NULL;
84 static PENTRY gpentHmgr;
85 static PULONG gpaulRefCount;
86 ULONG gulFirstFree;
87 ULONG gulFirstUnused;
88 static PPAGED_LOOKASIDE_LIST gpaLookasideList;
89
90 static BOOL NTAPI GDIOBJ_Cleanup(PVOID ObjectBody);
91
92 static const
93 GDICLEANUPPROC
94 apfnCleanup[] =
95 {
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 */
128 };
129
130 /* INTERNAL FUNCTIONS ********************************************************/
131
132 static
133 BOOL NTAPI
134 GDIOBJ_Cleanup(PVOID ObjectBody)
135 {
136 return TRUE;
137 }
138
139 static
140 VOID
141 InitLookasideList(UCHAR objt, ULONG cjSize)
142 {
143 ExInitializePagedLookasideList(&gpaLookasideList[objt],
144 NULL,
145 NULL,
146 0,
147 cjSize,
148 GDITAG_HMGR_LOOKASIDE_START + (objt << 24),
149 0);
150 }
151
152 INIT_FUNCTION
153 NTSTATUS
154 NTAPI
155 InitGdiHandleTable(void)
156 {
157 NTSTATUS status;
158 LARGE_INTEGER liSize;
159 PVOID pvSection;
160 SIZE_T cjViewSize = 0;
161
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,
165 SECTION_ALL_ACCESS,
166 NULL,
167 &liSize,
168 PAGE_READWRITE,
169 SEC_COMMIT,
170 NULL,
171 NULL);
172 if (!NT_SUCCESS(status))
173 {
174 DPRINT1("INITGDI: Could not allocate a GDI handle table.\n");
175 return status;
176 }
177
178 /* Map the section in session space */
179 status = MmMapViewInSessionSpace(gpvGdiHdlTblSection,
180 (PVOID*)&gpentHmgr,
181 &cjViewSize);
182 if (!NT_SUCCESS(status))
183 {
184 DPRINT1("INITGDI: Failed to map handle table section\n");
185 ObDereferenceObject(gpvGdiHdlTblSection);
186 return status;
187 }
188
189 /* Allocate memory for the reference counter table */
190 gpaulRefCount = EngAllocSectionMem(&pvSection,
191 FL_ZERO_MEMORY,
192 GDI_HANDLE_COUNT * sizeof(ULONG),
193 'frHG');
194 if (!gpaulRefCount)
195 {
196 DPRINT1("INITGDI: Failed to allocate reference table.\n");
197 ObDereferenceObject(gpvGdiHdlTblSection);
198 return STATUS_INSUFFICIENT_RESOURCES;
199 }
200
201 gulFirstFree = 0;
202 gulFirstUnused = RESERVE_ENTRIES_COUNT;
203
204 GdiHandleTable = (PVOID)gpentHmgr;
205
206 /* Initialize the lookaside lists */
207 gpaLookasideList = ExAllocatePoolWithTag(NonPagedPool,
208 GDIObjTypeTotal * sizeof(PAGED_LOOKASIDE_LIST),
209 TAG_GDIHNDTBLE);
210 if(!gpaLookasideList)
211 return STATUS_NO_MEMORY;
212
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));
222
223 return STATUS_SUCCESS;
224 }
225
226 FORCEINLINE
227 VOID
228 IncrementGdiHandleCount(void)
229 {
230 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process();
231 if (ppi) InterlockedIncrement((LONG*)&ppi->GDIHandleCount);
232 }
233
234 FORCEINLINE
235 VOID
236 DecrementGdiHandleCount(void)
237 {
238 PPROCESSINFO ppi = PsGetCurrentProcessWin32Process();
239 if (ppi) InterlockedDecrement((LONG*)&ppi->GDIHandleCount);
240 }
241
242 static
243 PENTRY
244 ENTRY_pentPopFreeEntry(VOID)
245 {
246 ULONG iFirst, iNext, iPrev;
247 PENTRY pentFree;
248
249 DPRINT("Enter InterLockedPopFreeEntry\n");
250
251 do
252 {
253 /* Get the index and sequence number of the first free entry */
254 iFirst = gulFirstFree;
255
256 /* Check if we have a free entry */
257 if (!(iFirst & GDI_HANDLE_INDEX_MASK))
258 {
259 /* Increment FirstUnused and get the new index */
260 iFirst = InterlockedIncrement((LONG*)&gulFirstUnused) - 1;
261
262 /* Check if we have unused entries left */
263 if (iFirst >= GDI_HANDLE_COUNT)
264 {
265 DPRINT1("No more GDI handles left!\n");
266 return 0;
267 }
268
269 /* Return the old entry */
270 return &gpentHmgr[iFirst];
271 }
272
273 /* Get a pointer to the first free entry */
274 pentFree = &gpentHmgr[iFirst & GDI_HANDLE_INDEX_MASK];
275
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;
279
280 /* Try to exchange the FirstFree value */
281 iPrev = InterlockedCompareExchange((LONG*)&gulFirstFree,
282 iNext,
283 iFirst);
284 }
285 while (iPrev != iFirst);
286
287 /* Sanity check: is entry really free? */
288 ASSERT(((ULONG_PTR)pentFree->einfo.pobj & ~GDI_HANDLE_INDEX_MASK) == 0);
289
290 return pentFree;
291 }
292
293 /* Pushes an entry of the handle table to the free list,
294 The entry must not have any references left */
295 static
296 VOID
297 ENTRY_vPushFreeEntry(PENTRY pentFree)
298 {
299 ULONG iToFree, iFirst, iPrev, idxToFree;
300
301 DPRINT("Enter ENTRY_vPushFreeEntry\n");
302
303 idxToFree = pentFree - gpentHmgr;
304 ASSERT((gpaulRefCount[idxToFree] & REF_MASK_INUSE) == 0);
305
306 /* Initialize entry */
307 pentFree->Objt = GDIObjType_DEF_TYPE;
308 pentFree->ObjectOwner.ulObj = 0;
309 pentFree->pUser = NULL;
310
311 /* Increase reuse counter in entry and reference counter */
312 InterlockedExchangeAdd((LONG*)&gpaulRefCount[idxToFree], REF_INC_REUSE);
313 pentFree->FullUnique += 0x0100;
314
315 do
316 {
317 /* Get the current first free index and sequence number */
318 iFirst = gulFirstFree;
319
320 /* Set the einfo.pobj member to the index of the first free entry */
321 pentFree->einfo.pobj = UlongToPtr(iFirst & GDI_HANDLE_INDEX_MASK);
322
323 /* Combine new index and increased sequence number in iToFree */
324 iToFree = idxToFree | ((iFirst & ~GDI_HANDLE_INDEX_MASK) + 0x10000);
325
326 /* Try to atomically update the first free entry */
327 iPrev = InterlockedCompareExchange((LONG*)&gulFirstFree,
328 iToFree,
329 iFirst);
330 }
331 while (iPrev != iFirst);
332 }
333
334 static
335 PENTRY
336 ENTRY_ReferenceEntryByHandle(HGDIOBJ hobj, FLONG fl)
337 {
338 ULONG ulIndex, cNewRefs, cOldRefs;
339 PENTRY pentry;
340
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;
344
345 /* Get pointer to the entry */
346 pentry = &gpentHmgr[ulIndex];
347
348 /* Get the current reference count */
349 cOldRefs = gpaulRefCount[ulIndex];
350
351 do
352 {
353 /* Check if the slot is deleted */
354 if ((cOldRefs & REF_MASK_VALID) == 0)
355 {
356 DPRINT("GDIOBJ: Slot is not valid: 0x%lx, hobh=%p\n", cOldRefs, hobj);
357 return NULL;
358 }
359
360 /* Check if the unique value matches */
361 if (pentry->FullUnique != (USHORT)((ULONG_PTR)hobj >> 16))
362 {
363 DPRINT("GDIOBJ: Wrong unique value. Handle: 0x%4x, entry: 0x%4x\n",
364 (USHORT)((ULONG_PTR)hobj >> 16, pentry->FullUnique));
365 return NULL;
366 }
367
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()))
372 {
373 DPRINT("GDIOBJ: Cannot reference foreign handle %p, pentry=%p:%lx.\n",
374 hobj, pentry, pentry->ObjectOwner.ulObj);
375 return NULL;
376 }
377
378 /* Try to atomically increment the reference count */
379 cNewRefs = cOldRefs + 1;
380 cOldRefs = InterlockedCompareExchange((PLONG)&gpaulRefCount[ulIndex],
381 cNewRefs,
382 cOldRefs);
383 }
384 while (cNewRefs != cOldRefs + 1);
385
386 /* Integrity checks */
387 ASSERT((pentry->FullUnique & 0x1f) == pentry->Objt);
388 ASSERT(pentry->einfo.pobj && pentry->einfo.pobj->hHmgr == hobj);
389
390 return pentry;
391 }
392
393 static
394 HGDIOBJ
395 ENTRY_hInsertObject(PENTRY pentry, POBJ pobj, UCHAR objt, ULONG ulOwner)
396 {
397 ULONG ulIndex;
398
399 /* Calculate the handle index */
400 ulIndex = pentry - gpentHmgr;
401
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;
407
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);
411
412 /* Return the handle */
413 return (HGDIOBJ)(((ULONG_PTR)pentry->FullUnique << 16) | ulIndex);
414 }
415
416 POBJ
417 NTAPI
418 GDIOBJ_AllocateObject(UCHAR objt, ULONG cjSize, FLONG fl)
419 {
420 POBJ pobj;
421
422 if (fl & BASEFLAG_LOOKASIDE)
423 {
424 /* Allocate the object from a lookaside list */
425 pobj = ExAllocateFromPagedLookasideList(&gpaLookasideList[objt & 0x1f]);
426 }
427 else
428 {
429 /* Allocate the object from paged pool */
430 pobj = ExAllocatePoolWithTag(PagedPool, cjSize, GDIOBJ_POOL_TAG(objt));
431 }
432
433 if (!pobj) return NULL;
434
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);
443
444 return pobj;
445 }
446
447 VOID
448 NTAPI
449 GDIOBJ_vFreeObject(POBJ pobj)
450 {
451 UCHAR objt;
452
453 DBG_CLEANUP_EVENT_LIST(&pobj->slhLog);
454
455 /* Get the object type */
456 objt = ((ULONG_PTR)pobj->hHmgr >> 16) & 0x1f;
457
458 /* Call the cleanup procedure */
459 ASSERT(apfnCleanup[objt]);
460 apfnCleanup[objt](pobj);
461
462 /* Check if the object is allocated from a lookaside list */
463 if (pobj->BaseFlags & BASEFLAG_LOOKASIDE)
464 {
465 ExFreeToPagedLookasideList(&gpaLookasideList[objt], pobj);
466 }
467 else
468 {
469 ExFreePoolWithTag(pobj, GDIOBJ_POOL_TAG(objt));
470 }
471 }
472
473 VOID
474 NTAPI
475 GDIOBJ_vDereferenceObject(POBJ pobj)
476 {
477 ULONG cRefs, ulIndex;
478
479 /* Check if the object has a handle */
480 if (GDI_HANDLE_GET_INDEX(pobj->hHmgr))
481 {
482 /* Calculate the index */
483 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
484
485 /* Decrement reference count */
486 ASSERT((gpaulRefCount[ulIndex] & REF_MASK_COUNT) > 0);
487 cRefs = InterlockedDecrement((LONG*)&gpaulRefCount[ulIndex]) & REF_MASK_INUSE;
488
489 /* Check if we reached 0 and handle bit is not set */
490 if (cRefs == 0)
491 {
492 /* Check if the handle was process owned */
493 if (gpentHmgr[ulIndex].ObjectOwner.ulObj != GDI_OBJ_HMGR_PUBLIC &&
494 gpentHmgr[ulIndex].ObjectOwner.ulObj != GDI_OBJ_HMGR_NONE)
495 {
496 /* Decrement the process handle count */
497 ASSERT(gpentHmgr[ulIndex].ObjectOwner.ulObj ==
498 HandleToUlong(PsGetCurrentProcessId()));
499 DecrementGdiHandleCount();
500 }
501
502 /* Push entry to the free list */
503 ENTRY_vPushFreeEntry(&gpentHmgr[ulIndex]);
504 }
505 }
506 else
507 {
508 /* Decrement the objects reference count */
509 ASSERT(pobj->ulShareCount > 0);
510 cRefs = InterlockedDecrement((LONG*)&pobj->ulShareCount);
511 }
512
513 DBG_LOGEVENT(&pobj->slhLog, EVENT_DEREFERENCE, cRefs);
514
515 /* Check if we reached 0 */
516 if (cRefs == 0)
517 {
518 /* Make sure it's ok to delete the object */
519 ASSERT(pobj->BaseFlags & BASEFLAG_READY_TO_DIE);
520
521 /* Free the object */
522 GDIOBJ_vFreeObject(pobj);
523 }
524 }
525
526 POBJ
527 NTAPI
528 GDIOBJ_ReferenceObjectByHandle(
529 HGDIOBJ hobj,
530 UCHAR objt)
531 {
532 PENTRY pentry;
533 POBJ pobj;
534
535 /* Check if the handle type matches */
536 ASSERT_SHARED_OBJECT_TYPE(objt);
537 if ((((ULONG_PTR)hobj >> 16) & 0x1f) != objt)
538 {
539 DPRINT("GDIOBJ: Wrong type. handle=%p, type=%x\n", hobj, objt);
540 return NULL;
541 }
542
543 /* Reference the handle entry */
544 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
545 if (!pentry)
546 {
547 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj);
548 return NULL;
549 }
550
551 /* Get the pointer to the BASEOBJECT */
552 pobj = pentry->einfo.pobj;
553
554 /* Check if the object is exclusively locked */
555 if (pobj->cExclusiveLock != 0)
556 {
557 DPRINT1("GDIOBJ: Cannot reference oject %p with exclusive lock.\n", hobj);
558 GDIOBJ_vDereferenceObject(pobj);
559 DBG_DUMP_EVENT_LIST(&pobj->slhLog);
560 return NULL;
561 }
562
563 DBG_LOGEVENT(&pobj->slhLog, EVENT_REFERENCE, gpaulRefCount[pentry - gpentHmgr]);
564
565 /* All is well, return the object */
566 return pobj;
567 }
568
569 VOID
570 NTAPI
571 GDIOBJ_vReferenceObjectByPointer(POBJ pobj)
572 {
573 ULONG cRefs;
574
575 /* Must not be exclusively locked */
576 ASSERT(pobj->cExclusiveLock == 0);
577
578 /* Check if the object has a handle */
579 if (GDI_HANDLE_GET_INDEX(pobj->hHmgr))
580 {
581 /* Increase the handle's reference count */
582 ULONG ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
583 ASSERT((gpaulRefCount[ulIndex] & REF_MASK_COUNT) > 0);
584 cRefs = InterlockedIncrement((LONG*)&gpaulRefCount[ulIndex]);
585 }
586 else
587 {
588 /* Increase the object's reference count */
589 cRefs = InterlockedIncrement((LONG*)&pobj->ulShareCount);
590 }
591
592 DBG_LOGEVENT(&pobj->slhLog, EVENT_REFERENCE, cRefs);
593 }
594
595 PGDIOBJ
596 NTAPI
597 GDIOBJ_LockObject(
598 HGDIOBJ hobj,
599 UCHAR objt)
600 {
601 PENTRY pentry;
602 POBJ pobj;
603 DWORD dwThreadId;
604
605 /* Check if the handle type matches */
606 ASSERT_EXCLUSIVE_OBJECT_TYPE(objt);
607 if ((((ULONG_PTR)hobj >> 16) & 0x1f) != objt)
608 {
609 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj, objt);
610 return NULL;
611 }
612
613 /* Reference the handle entry */
614 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
615 if (!pentry)
616 {
617 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj);
618 return NULL;
619 }
620
621 /* Get the pointer to the BASEOBJECT */
622 pobj = pentry->einfo.pobj;
623
624 /* Check if we already own the lock */
625 dwThreadId = PtrToUlong(PsGetCurrentThreadId());
626 if (pobj->dwThreadId != dwThreadId)
627 {
628 /* Disable APCs and acquire the push lock */
629 KeEnterCriticalRegion();
630 ExAcquirePushLockExclusive(&pobj->pushlock);
631
632 /* Set us as lock owner */
633 ASSERT(pobj->dwThreadId == 0);
634 pobj->dwThreadId = dwThreadId;
635 }
636
637 /* Increase lock count */
638 pobj->cExclusiveLock++;
639 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj);
640 DBG_LOGEVENT(&pobj->slhLog, EVENT_LOCK, 0);
641
642 /* Return the object */
643 return pobj;
644 }
645
646 VOID
647 NTAPI
648 GDIOBJ_vUnlockObject(POBJ pobj)
649 {
650 ASSERT(pobj->cExclusiveLock > 0);
651
652 /* Decrease lock count */
653 pobj->cExclusiveLock--;
654 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj->hHmgr);
655
656 /* Check if this was the last lock */
657 if (pobj->cExclusiveLock == 0)
658 {
659 /* Reset lock owner */
660 pobj->dwThreadId = 0;
661
662 /* Release the pushlock and reenable APCs */
663 ExReleasePushLockExclusive(&pobj->pushlock);
664 KeLeaveCriticalRegion();
665 }
666
667 /* Dereference the object */
668 DBG_LOGEVENT(&pobj->slhLog, EVENT_UNLOCK, 0);
669 GDIOBJ_vDereferenceObject(pobj);
670 }
671
672 HGDIOBJ
673 NTAPI
674 GDIOBJ_hInsertObject(
675 POBJ pobj,
676 ULONG ulOwner)
677 {
678 PENTRY pentry;
679 UCHAR objt;
680
681 /* Must have no handle and only one reference */
682 ASSERT(GDI_HANDLE_GET_INDEX(pobj->hHmgr) == 0);
683 ASSERT(pobj->cExclusiveLock == 0);
684 ASSERT(pobj->ulShareCount == 1);
685
686 /* Get a free handle entry */
687 pentry = ENTRY_pentPopFreeEntry();
688 if (!pentry)
689 {
690 DPRINT1("GDIOBJ: Could not get a free entry.\n");
691 return NULL;
692 }
693
694 /* Make the object exclusively locked */
695 ExInitializePushLock(&pobj->pushlock);
696 KeEnterCriticalRegion();
697 ExAcquirePushLockExclusive(&pobj->pushlock);
698 pobj->cExclusiveLock = 1;
699 pobj->dwThreadId = PtrToUlong(PsGetCurrentThreadId());
700 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj->hHmgr);
701
702 /* Get object type from the hHmgr field */
703 objt = ((ULONG_PTR)pobj->hHmgr >> 16) & 0xff;
704 ASSERT(objt != GDIObjType_DEF_TYPE);
705
706 /* Check if current process is requested owner */
707 if (ulOwner == GDI_OBJ_HMGR_POWNED)
708 {
709 /* Increment the process handle count */
710 IncrementGdiHandleCount();
711
712 /* Use Process id */
713 ulOwner = HandleToUlong(PsGetCurrentProcessId());
714 }
715
716 /* Insert the object into the handle table */
717 pobj->hHmgr = ENTRY_hInsertObject(pentry, pobj, objt, ulOwner);
718
719 /* Return the handle */
720 DPRINT("GDIOBJ: Created handle: %p\n", pobj->hHmgr);
721 DBG_LOGEVENT(&pobj->slhLog, EVENT_CREATE_HANDLE, 0);
722 return pobj->hHmgr;
723 }
724
725 VOID
726 NTAPI
727 GDIOBJ_vSetObjectOwner(
728 POBJ pobj,
729 ULONG ulOwner)
730 {
731 PENTRY pentry;
732
733 /* This is a ugly HACK, needed to fix IntGdiSetDCOwnerEx */
734 if (GDI_HANDLE_IS_STOCKOBJ(pobj->hHmgr))
735 {
736 DPRINT("Trying to set ownership of stock object %p to %lx\n", pobj->hHmgr, ulOwner);
737 return;
738 }
739
740 /* Get the handle entry */
741 ASSERT(GDI_HANDLE_GET_INDEX(pobj->hHmgr));
742 pentry = &gpentHmgr[GDI_HANDLE_GET_INDEX(pobj->hHmgr)];
743
744 /* Is the current process requested? */
745 if (ulOwner == GDI_OBJ_HMGR_POWNED)
746 {
747 /* Use process id */
748 ulOwner = HandleToUlong(PsGetCurrentProcessId());
749 if (pentry->ObjectOwner.ulObj != ulOwner)
750 {
751 IncrementGdiHandleCount();
752 }
753 }
754
755 // HACK
756 if (ulOwner == GDI_OBJ_HMGR_NONE)
757 ulOwner = GDI_OBJ_HMGR_PUBLIC;
758
759 if (ulOwner == GDI_OBJ_HMGR_PUBLIC ||
760 ulOwner == GDI_OBJ_HMGR_NONE)
761 {
762 /* Make sure we don't leak user mode memory */
763 ASSERT(pentry->pUser == NULL);
764 if (pentry->ObjectOwner.ulObj != GDI_OBJ_HMGR_PUBLIC &&
765 pentry->ObjectOwner.ulObj != GDI_OBJ_HMGR_NONE)
766 {
767 DecrementGdiHandleCount();
768 }
769 }
770
771 /* Set new owner */
772 pentry->ObjectOwner.ulObj = ulOwner;
773 }
774
775 /* Locks 2 or 3 objects at a time */
776 BOOL
777 NTAPI
778 GDIOBJ_bLockMultipleObjects(
779 IN ULONG ulCount,
780 IN HGDIOBJ* ahObj,
781 OUT PGDIOBJ* apObj,
782 IN UCHAR objt)
783 {
784 UINT auiIndices[3] = {0, 1, 2};
785 UINT i, j, tmp;
786
787 ASSERT(ulCount <= 3);
788
789 /* Sort the handles */
790 for (i = 0; i < ulCount - 1; i++)
791 {
792 for (j = i + 1; j < ulCount; j++)
793 {
794 if ((ULONG_PTR)ahObj[auiIndices[i]] <
795 (ULONG_PTR)ahObj[auiIndices[j]])
796 {
797 tmp = auiIndices[i];
798 auiIndices[i] = auiIndices[j];
799 auiIndices[j] = tmp;
800 }
801 }
802 }
803
804 /* Lock the objects in safe order */
805 for (i = 0; i < ulCount; i++)
806 {
807 /* Skip NULL handles */
808 if (ahObj[auiIndices[i]] == NULL)
809 {
810 apObj[auiIndices[i]] = NULL;
811 continue;
812 }
813
814 /* Lock the object */
815 apObj[auiIndices[i]] = GDIOBJ_LockObject(ahObj[auiIndices[i]], objt);
816
817 /* Check for failure */
818 if (apObj[auiIndices[i]] == NULL)
819 {
820 /* Cleanup */
821 while (i--)
822 {
823 if (apObj[auiIndices[i]])
824 GDIOBJ_vUnlockObject(apObj[auiIndices[i]]);
825 }
826 return FALSE;
827 }
828 }
829
830 return TRUE;
831 }
832
833 PVOID
834 NTAPI
835 GDIOBJ_pvGetObjectAttr(POBJ pobj)
836 {
837 ULONG ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
838 return gpentHmgr[ulIndex].pUser;
839 }
840
841 VOID
842 NTAPI
843 GDIOBJ_vSetObjectAttr(POBJ pobj, PVOID pvObjAttr)
844 {
845 ULONG ulIndex;
846
847 ASSERT(pobj->hHmgr);
848
849 /* Get the handle index */
850 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
851
852 /* Set pointer to the usermode attribute */
853 gpentHmgr[ulIndex].pUser = pvObjAttr;
854 }
855
856 VOID
857 NTAPI
858 GDIOBJ_vDeleteObject(POBJ pobj)
859 {
860 ULONG ulIndex;
861
862 /* Set the object's delete flag */
863 InterlockedOr16((SHORT*)&pobj->BaseFlags, BASEFLAG_READY_TO_DIE);
864 DBG_LOGEVENT(&pobj->slhLog, EVENT_DELETE, 0);
865
866 /* Get the handle index */
867 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
868 if (ulIndex)
869 {
870 /* Reset the handle valid bit */
871 InterlockedAnd((LONG*)&gpaulRefCount[ulIndex], ~REF_MASK_VALID);
872
873 /* Check if the object is exclusively locked */
874 if (pobj->cExclusiveLock != 0)
875 {
876 /* Reset lock owner and lock count */
877 pobj->dwThreadId = 0;
878 pobj->cExclusiveLock = 0;
879
880 /* Release the pushlock and reenable APCs */
881 ExReleasePushLockExclusive(&pobj->pushlock);
882 KeLeaveCriticalRegion();
883 }
884 }
885
886 /* Dereference the object (will take care of deletion) */
887 GDIOBJ_vDereferenceObject(pobj);
888 }
889
890 BOOL
891 NTAPI
892 GreIsHandleValid(HGDIOBJ hobj)
893 {
894 PENTRY pentry;
895
896 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
897 if (!pentry) return FALSE;
898 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
899 return TRUE;
900 }
901
902 BOOL
903 NTAPI
904 GreDeleteObject(HGDIOBJ hobj)
905 {
906 PENTRY pentry;
907
908 /* Check for stock objects */
909 if (GDI_HANDLE_IS_STOCKOBJ(hobj))
910 {
911 DPRINT1("GreDeleteObject: Cannot delete stock object %p.\n", hobj);
912 return FALSE;
913 }
914
915 /* Reference the handle entry */
916 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
917 if (!pentry)
918 {
919 DPRINT1("GreDeleteObject: Trying to delete invalid object %p\n", hobj);
920 return FALSE;
921 }
922
923 /* Check for public owner */
924 if (pentry->ObjectOwner.ulObj == GDI_OBJ_HMGR_PUBLIC)
925 {
926 DPRINT1("GreDeleteObject: Trying to delete global object %p\n", hobj);
927 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
928 return FALSE;
929 }
930
931 /* Delete the object */
932 GDIOBJ_vDeleteObject(pentry->einfo.pobj);
933 return TRUE;
934 }
935
936 ULONG
937 NTAPI
938 GreGetObjectOwner(HGDIOBJ hobj)
939 {
940 ULONG ulIndex, ulOwner;
941
942 /* Get the handle index */
943 ulIndex = GDI_HANDLE_GET_INDEX(hobj);
944
945 /* Check if the handle is valid */
946 if (ulIndex >= GDI_HANDLE_COUNT ||
947 gpentHmgr[ulIndex].Objt == GDIObjType_DEF_TYPE ||
948 ((ULONG_PTR)hobj >> 16) != gpentHmgr[ulIndex].FullUnique)
949 {
950 DPRINT1("GreGetObjectOwner: invalid handle 0x%p.\n", hobj);
951 return GDI_OBJ_HMGR_RESTRICTED;
952 }
953
954 /* Get the object owner */
955 ulOwner = gpentHmgr[ulIndex].ObjectOwner.ulObj;
956
957 if (ulOwner == HandleToUlong(PsGetCurrentProcessId()))
958 return GDI_OBJ_HMGR_POWNED;
959
960 if (ulOwner == GDI_OBJ_HMGR_PUBLIC)
961 return GDI_OBJ_HMGR_PUBLIC;
962
963 return GDI_OBJ_HMGR_RESTRICTED;
964 }
965
966 BOOL
967 NTAPI
968 GreSetObjectOwner(
969 HGDIOBJ hobj,
970 ULONG ulOwner)
971 {
972 PENTRY pentry;
973
974 /* Check for stock objects */
975 if (GDI_HANDLE_IS_STOCKOBJ(hobj))
976 {
977 DPRINT("GreSetObjectOwner: Got stock object %p\n", hobj);
978 return FALSE;
979 }
980
981 /* Reference the handle entry */
982 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
983 if (!pentry)
984 {
985 DPRINT("GreSetObjectOwner: Invalid handle 0x%p.\n", hobj);
986 return FALSE;
987 }
988
989 /* Call internal function */
990 GDIOBJ_vSetObjectOwner(pentry->einfo.pobj, ulOwner);
991
992 /* Dereference the object */
993 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
994
995 return TRUE;
996 }
997
998 INT
999 NTAPI
1000 GreGetObject(
1001 IN HGDIOBJ hobj,
1002 IN INT cbCount,
1003 IN PVOID pvBuffer)
1004 {
1005 PVOID pvObj;
1006 UCHAR objt;
1007 INT iResult = 0;
1008
1009 /* Verify object type */
1010 objt = ((ULONG_PTR)hobj >> 16) & 0x1f;
1011 if (objt != GDIObjType_BRUSH_TYPE &&
1012 objt != GDIObjType_SURF_TYPE &&
1013 objt != GDIObjType_LFONT_TYPE &&
1014 objt != GDIObjType_PAL_TYPE)
1015 {
1016 DPRINT1("GreGetObject: Invalid object type\n");
1017 return 0;
1018 }
1019
1020 pvObj = GDIOBJ_ReferenceObjectByHandle(hobj, objt);
1021 if (!pvObj)
1022 {
1023 DPRINT("GreGetObject: Could not lock object\n");
1024 return 0;
1025 }
1026
1027 switch (GDI_HANDLE_GET_TYPE(hobj))
1028 {
1029 case GDILoObjType_LO_PEN_TYPE:
1030 case GDILoObjType_LO_EXTPEN_TYPE:
1031 iResult = PEN_GetObject(pvObj, cbCount, pvBuffer);
1032 break;
1033
1034 case GDILoObjType_LO_BRUSH_TYPE:
1035 iResult = BRUSH_GetObject(pvObj, cbCount, pvBuffer);
1036 break;
1037
1038 case GDILoObjType_LO_BITMAP_TYPE:
1039 iResult = BITMAP_GetObject(pvObj, cbCount, pvBuffer);
1040 break;
1041 case GDILoObjType_LO_FONT_TYPE:
1042 iResult = FontGetObject(pvObj, cbCount, pvBuffer);
1043 #if 0
1044 // Fix the LOGFONT structure for the stock fonts
1045 if (FIRST_STOCK_HANDLE <= hobj && hobj <= LAST_STOCK_HANDLE)
1046 {
1047 FixStockFontSizeW(hobj, cbCount, pvBuffer);
1048 }
1049 #endif
1050 break;
1051
1052 case GDILoObjType_LO_PALETTE_TYPE:
1053 iResult = PALETTE_GetObject(pvObj, cbCount, pvBuffer);
1054 break;
1055
1056 default:
1057 DPRINT1("GDI object type of 0x%p not implemented\n", hobj);
1058 break;
1059 }
1060
1061 GDIOBJ_vDereferenceObject(pvObj);
1062 return iResult;
1063 }
1064
1065 W32KAPI
1066 INT
1067 APIENTRY
1068 NtGdiExtGetObjectW(
1069 IN HANDLE hobj,
1070 IN INT cbCount,
1071 OUT LPVOID lpBuffer)
1072 {
1073 INT iRetCount = 0;
1074 INT cbCopyCount;
1075 union
1076 {
1077 BITMAP bitmap;
1078 DIBSECTION dibsection;
1079 LOGPEN logpen;
1080 LOGBRUSH logbrush;
1081 LOGFONTW logfontw;
1082 EXTLOGFONTW extlogfontw;
1083 ENUMLOGFONTEXDVW enumlogfontexdvw;
1084 } object;
1085
1086 /* Normalize to the largest supported object size */
1087 cbCount = min((UINT)cbCount, sizeof(object));
1088
1089 /* Now do the actual call */
1090 iRetCount = GreGetObject(hobj, cbCount, lpBuffer ? &object : NULL);
1091 cbCopyCount = min((UINT)cbCount, (UINT)iRetCount);
1092
1093 /* Make sure we have a buffer and a copy size */
1094 if ((cbCopyCount) && (lpBuffer))
1095 {
1096 /* Enter SEH for buffer transfer */
1097 _SEH2_TRY
1098 {
1099 // Probe the buffer and copy it
1100 ProbeForWrite(lpBuffer, cbCopyCount, sizeof(WORD));
1101 RtlCopyMemory(lpBuffer, &object, cbCopyCount);
1102 }
1103 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1104 {
1105 /* Clear the return value.
1106 * Do *NOT* set last error here! */
1107 iRetCount = 0;
1108 }
1109 _SEH2_END;
1110 }
1111
1112 /* Return the count */
1113 return iRetCount;
1114 }
1115
1116 W32KAPI
1117 HANDLE
1118 APIENTRY
1119 NtGdiCreateClientObj(
1120 IN ULONG ulType)
1121 {
1122 POBJ pObject;
1123 HANDLE handle;
1124
1125 /* Allocate a new object */
1126 pObject = GDIOBJ_AllocateObject(GDIObjType_CLIENTOBJ_TYPE,
1127 sizeof(CLIENTOBJ),
1128 BASEFLAG_LOOKASIDE);
1129 if (!pObject)
1130 {
1131 DPRINT1("NtGdiCreateClientObj: Could not allocate a clientobj.\n");
1132 return NULL;
1133 }
1134
1135 /* Mask out everything that would change the type in a wrong manner */
1136 ulType &= (GDI_HANDLE_TYPE_MASK & ~GDI_HANDLE_BASETYPE_MASK);
1137
1138 /* Set the real object type */
1139 pObject->hHmgr = UlongToHandle(ulType | GDILoObjType_LO_CLIENTOBJ_TYPE);
1140
1141 /* Create a handle */
1142 handle = GDIOBJ_hInsertObject(pObject, GDI_OBJ_HMGR_POWNED);
1143 if (!handle)
1144 {
1145 DPRINT1("NtGdiCreateClientObj: Could not create a handle.\n");
1146 GDIOBJ_vFreeObject(pObject);
1147 return NULL;
1148 }
1149
1150 /* Unlock it */
1151 GDIOBJ_vUnlockObject(pObject);
1152
1153 return handle;
1154 }
1155
1156 W32KAPI
1157 BOOL
1158 APIENTRY
1159 NtGdiDeleteClientObj(
1160 IN HANDLE hobj)
1161 {
1162 /* We first need to get the real type from the handle */
1163 ULONG ulType = GDI_HANDLE_GET_TYPE(hobj);
1164
1165 /* Check if it's really a CLIENTOBJ */
1166 if ((ulType & GDI_HANDLE_BASETYPE_MASK) != GDILoObjType_LO_CLIENTOBJ_TYPE)
1167 {
1168 /* FIXME: SetLastError? */
1169 return FALSE;
1170 }
1171
1172 return GreDeleteObject(hobj);
1173 }
1174
1175
1176
1177 PGDI_HANDLE_TABLE GdiHandleTable = NULL;
1178
1179 PGDIOBJ NTAPI
1180 GDIOBJ_ShareLockObj(HGDIOBJ hObj, DWORD ExpectedType)
1181 {
1182 if (ExpectedType == GDI_OBJECT_TYPE_DONTCARE)
1183 ExpectedType = GDI_HANDLE_GET_TYPE(hObj);
1184 return GDIOBJ_ReferenceObjectByHandle(hObj, (ExpectedType >> 16) & 0x1f);
1185 }
1186
1187 // This function is not safe to use with concurrent deleting attempts
1188 // That shouldn't be a problem, since we don't have any processes yet,
1189 // that could delete the handle
1190 BOOL
1191 NTAPI
1192 GDIOBJ_ConvertToStockObj(HGDIOBJ *phObj)
1193 {
1194 PENTRY pentry;
1195 POBJ pobj;
1196
1197 /* Reference the handle entry */
1198 pentry = ENTRY_ReferenceEntryByHandle(*phObj, 0);
1199 if (!pentry)
1200 {
1201 DPRINT1("GDIOBJ: Requested handle 0x%p is not valid.\n", *phObj);
1202 return FALSE;
1203 }
1204
1205 /* Update the entry */
1206 pentry->FullUnique |= GDI_ENTRY_STOCK_MASK;
1207 pentry->ObjectOwner.ulObj = 0;
1208
1209 /* Get the pointer to the BASEOBJECT */
1210 pobj = pentry->einfo.pobj;
1211
1212 /* Calculate the new handle */
1213 pobj->hHmgr = (HGDIOBJ)((ULONG_PTR)pobj->hHmgr | GDI_HANDLE_STOCK_MASK);
1214
1215 /* Return the new handle */
1216 *phObj = pobj->hHmgr;
1217
1218 /* Dereference the handle */
1219 GDIOBJ_vDereferenceObject(pobj);
1220
1221 return TRUE;
1222 }
1223
1224 POBJ NTAPI
1225 GDIOBJ_AllocObjWithHandle(ULONG ObjectType, ULONG cjSize)
1226 {
1227 POBJ pobj;
1228 FLONG fl = 0;
1229 UCHAR objt = (ObjectType >> 16) & 0xFF;
1230
1231 if ((objt == GDIObjType_DC_TYPE && cjSize == sizeof(DC)) ||
1232 (objt == GDIObjType_PAL_TYPE && cjSize == sizeof(PALETTE)) ||
1233 (objt == GDIObjType_RGN_TYPE && cjSize == sizeof(REGION)) ||
1234 (objt == GDIObjType_SURF_TYPE && cjSize == sizeof(SURFACE)) ||
1235 (objt == GDIObjType_PATH_TYPE && cjSize == sizeof(PATH)))
1236 {
1237 fl |= BASEFLAG_LOOKASIDE;
1238 }
1239
1240 pobj = GDIOBJ_AllocateObject(objt, cjSize, fl);
1241 if (!GDIOBJ_hInsertObject(pobj, GDI_OBJ_HMGR_POWNED))
1242 {
1243 GDIOBJ_vFreeObject(pobj);
1244 return NULL;
1245 }
1246 return pobj;
1247 }
1248
1249 PVOID NTAPI
1250 GDI_MapHandleTable(PEPROCESS pProcess)
1251 {
1252 PVOID pvMappedView = NULL;
1253 NTSTATUS Status;
1254 LARGE_INTEGER liOffset;
1255 ULONG cjViewSize = sizeof(GDI_HANDLE_TABLE);
1256
1257 liOffset.QuadPart = 0;
1258
1259 ASSERT(gpvGdiHdlTblSection != NULL);
1260 ASSERT(pProcess != NULL);
1261
1262 Status = MmMapViewOfSection(gpvGdiHdlTblSection,
1263 pProcess,
1264 &pvMappedView,
1265 0,
1266 0,
1267 &liOffset,
1268 &cjViewSize,
1269 ViewUnmap,
1270 SEC_NO_CHANGE,
1271 PAGE_READONLY);
1272
1273 if (!NT_SUCCESS(Status))
1274 return NULL;
1275
1276 return pvMappedView;
1277 }
1278
1279 BOOL NTAPI
1280 GDI_CleanupForProcess(struct _EPROCESS *Process)
1281 {
1282 PENTRY pentry;
1283 ULONG ulIndex;
1284 DWORD dwProcessId;
1285 PPROCESSINFO ppi;
1286
1287 DPRINT("CleanupForProcess prochandle %x Pid %d\n",
1288 Process, Process->UniqueProcessId);
1289
1290 ASSERT(Process == PsGetCurrentProcess());
1291
1292 /* Get the current process Id */
1293 dwProcessId = PtrToUlong(PsGetCurrentProcessId());
1294
1295 /* Loop all handles in the handle table */
1296 for (ulIndex = RESERVE_ENTRIES_COUNT; ulIndex < gulFirstUnused; ulIndex++)
1297 {
1298 pentry = &gpentHmgr[ulIndex];
1299
1300 /* Check if the object is owned by the process */
1301 if (pentry->ObjectOwner.ulObj == dwProcessId)
1302 {
1303 ASSERT(pentry->einfo.pobj->cExclusiveLock == 0);
1304
1305 /* Reference the object and delete it */
1306 InterlockedIncrement((LONG*)&gpaulRefCount[ulIndex]);
1307 GDIOBJ_vDeleteObject(pentry->einfo.pobj);
1308 }
1309 }
1310
1311 #if DBG
1312 //#ifdef GDI_DEBUG
1313 DbgGdiHTIntegrityCheck();
1314 //#endif
1315 #endif
1316
1317 ppi = PsGetCurrentProcessWin32Process();
1318 DPRINT("Completed cleanup for process %d\n", Process->UniqueProcessId);
1319 if (ppi->GDIHandleCount != 0)
1320 {
1321 DPRINT1("Leaking %d handles!\n", ppi->GDIHandleCount);
1322 ASSERT(FALSE);
1323 }
1324
1325 /* Loop all handles in the handle table */
1326 for (ulIndex = RESERVE_ENTRIES_COUNT; ulIndex < gulFirstUnused; ulIndex++)
1327 {
1328 pentry = &gpentHmgr[ulIndex];
1329
1330 /* Check if the object is owned by the process */
1331 if (pentry->ObjectOwner.ulObj == dwProcessId)
1332 {
1333 DPRINT1("Leaking object. Index=%lx, type=0x%x, refcount=%lx\n",
1334 ulIndex, pentry->Objt, gpaulRefCount[ulIndex]);
1335 DBG_DUMP_EVENT_LIST(&pentry->einfo.pobj->slhLog);
1336 //DBG_CLEANUP_EVENT_LIST(&pentry->einfo.pobj->slhLog);
1337 ASSERT(FALSE);
1338 }
1339 }
1340
1341 return TRUE;
1342 }
1343
1344 /* EOF */