[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 /* Must not be exclusively locked */
480 ASSERT(pobj->cExclusiveLock == 0);
481
482 /* Check if the object has a handle */
483 if (GDI_HANDLE_GET_INDEX(pobj->hHmgr))
484 {
485 /* Calculate the index */
486 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
487
488 /* Decrement reference count */
489 ASSERT((gpaulRefCount[ulIndex] & REF_MASK_COUNT) > 0);
490 cRefs = InterlockedDecrement((LONG*)&gpaulRefCount[ulIndex]) & REF_MASK_INUSE;
491
492 /* Check if we reached 0 and handle bit is not set */
493 if (cRefs == 0)
494 {
495 /* Check if the handle was process owned */
496 if (gpentHmgr[ulIndex].ObjectOwner.ulObj != GDI_OBJ_HMGR_PUBLIC &&
497 gpentHmgr[ulIndex].ObjectOwner.ulObj != GDI_OBJ_HMGR_NONE)
498 {
499 /* Decrement the process handle count */
500 ASSERT(gpentHmgr[ulIndex].ObjectOwner.ulObj ==
501 HandleToUlong(PsGetCurrentProcessId()));
502 DecrementGdiHandleCount();
503 }
504
505 /* Push entry to the free list */
506 ENTRY_vPushFreeEntry(&gpentHmgr[ulIndex]);
507 }
508 }
509 else
510 {
511 /* Decrement the objects reference count */
512 ASSERT(pobj->ulShareCount > 0);
513 cRefs = InterlockedDecrement((LONG*)&pobj->ulShareCount);
514 }
515
516 DBG_LOGEVENT(&pobj->slhLog, EVENT_DEREFERENCE, cRefs);
517
518 /* Check if we reached 0 */
519 if (cRefs == 0)
520 {
521 /* Make sure it's ok to delete the object */
522 ASSERT(pobj->BaseFlags & BASEFLAG_READY_TO_DIE);
523
524 /* Free the object */
525 GDIOBJ_vFreeObject(pobj);
526 }
527 }
528
529 POBJ
530 NTAPI
531 GDIOBJ_ReferenceObjectByHandle(
532 HGDIOBJ hobj,
533 UCHAR objt)
534 {
535 PENTRY pentry;
536 POBJ pobj;
537
538 /* Check if the handle type matches */
539 ASSERT_SHARED_OBJECT_TYPE(objt);
540 if ((((ULONG_PTR)hobj >> 16) & 0x1f) != objt)
541 {
542 DPRINT("GDIOBJ: Wrong type. handle=%p, type=%x\n", hobj, objt);
543 return NULL;
544 }
545
546 /* Reference the handle entry */
547 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
548 if (!pentry)
549 {
550 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj);
551 return NULL;
552 }
553
554 /* Get the pointer to the BASEOBJECT */
555 pobj = pentry->einfo.pobj;
556
557 /* Check if the object is exclusively locked */
558 if (pobj->cExclusiveLock != 0)
559 {
560 DPRINT1("GDIOBJ: Cannot reference oject %p with exclusive lock.\n", hobj);
561 GDIOBJ_vDereferenceObject(pobj);
562 DBG_DUMP_EVENT_LIST(&pobj->slhLog);
563 return NULL;
564 }
565
566 DBG_LOGEVENT(&pobj->slhLog, EVENT_REFERENCE, gpaulRefCount[pentry - gpentHmgr]);
567
568 /* All is well, return the object */
569 return pobj;
570 }
571
572 VOID
573 NTAPI
574 GDIOBJ_vReferenceObjectByPointer(POBJ pobj)
575 {
576 ULONG cRefs;
577
578 /* Must not be exclusively locked */
579 ASSERT(pobj->cExclusiveLock == 0);
580
581 /* Check if the object has a handle */
582 if (GDI_HANDLE_GET_INDEX(pobj->hHmgr))
583 {
584 /* Increase the handle's reference count */
585 ULONG ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
586 ASSERT((gpaulRefCount[ulIndex] & REF_MASK_COUNT) > 0);
587 cRefs = InterlockedIncrement((LONG*)&gpaulRefCount[ulIndex]);
588 }
589 else
590 {
591 /* Increase the object's reference count */
592 cRefs = InterlockedIncrement((LONG*)&pobj->ulShareCount);
593 }
594
595 DBG_LOGEVENT(&pobj->slhLog, EVENT_REFERENCE, cRefs);
596 }
597
598 PGDIOBJ
599 NTAPI
600 GDIOBJ_LockObject(
601 HGDIOBJ hobj,
602 UCHAR objt)
603 {
604 PENTRY pentry;
605 POBJ pobj;
606 DWORD dwThreadId;
607
608 /* Check if the handle type matches */
609 ASSERT_EXCLUSIVE_OBJECT_TYPE(objt);
610 if ((((ULONG_PTR)hobj >> 16) & 0x1f) != objt)
611 {
612 DPRINT("Wrong object type: hobj=0x%p, objt=0x%x\n", hobj, objt);
613 return NULL;
614 }
615
616 /* Reference the handle entry */
617 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
618 if (!pentry)
619 {
620 DPRINT("GDIOBJ: Requested handle 0x%p is not valid.\n", hobj);
621 return NULL;
622 }
623
624 /* Get the pointer to the BASEOBJECT */
625 pobj = pentry->einfo.pobj;
626
627 /* Check if we already own the lock */
628 dwThreadId = PtrToUlong(PsGetCurrentThreadId());
629 if (pobj->dwThreadId != dwThreadId)
630 {
631 /* Disable APCs and acquire the push lock */
632 KeEnterCriticalRegion();
633 ExAcquirePushLockExclusive(&pobj->pushlock);
634
635 /* Set us as lock owner */
636 ASSERT(pobj->dwThreadId == 0);
637 pobj->dwThreadId = dwThreadId;
638 }
639
640 /* Increase lock count */
641 pobj->cExclusiveLock++;
642 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), hobj);
643 DBG_LOGEVENT(&pobj->slhLog, EVENT_LOCK, 0);
644
645 /* Return the object */
646 return pobj;
647 }
648
649 VOID
650 NTAPI
651 GDIOBJ_vUnlockObject(POBJ pobj)
652 {
653 ULONG cRefs, ulIndex;
654 ASSERT(pobj->cExclusiveLock > 0);
655
656 /* Decrease lock count */
657 pobj->cExclusiveLock--;
658 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj->hHmgr);
659 DBG_LOGEVENT(&pobj->slhLog, EVENT_UNLOCK, 0);
660
661 /* Check if this was the last lock */
662 if (pobj->cExclusiveLock == 0)
663 {
664 /* Reset lock owner */
665 pobj->dwThreadId = 0;
666
667 /* Release the pushlock and reenable APCs */
668 ExReleasePushLockExclusive(&pobj->pushlock);
669 KeLeaveCriticalRegion();
670 }
671
672 /* Calculate the index */
673 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
674
675 /* Decrement reference count */
676 ASSERT((gpaulRefCount[ulIndex] & REF_MASK_COUNT) > 0);
677 cRefs = InterlockedDecrement((LONG*)&gpaulRefCount[ulIndex]);
678 ASSERT(cRefs & REF_MASK_VALID);
679 }
680
681 HGDIOBJ
682 NTAPI
683 GDIOBJ_hInsertObject(
684 POBJ pobj,
685 ULONG ulOwner)
686 {
687 PENTRY pentry;
688 UCHAR objt;
689
690 /* Must have no handle and only one reference */
691 ASSERT(GDI_HANDLE_GET_INDEX(pobj->hHmgr) == 0);
692 ASSERT(pobj->cExclusiveLock == 0);
693 ASSERT(pobj->ulShareCount == 1);
694
695 /* Get a free handle entry */
696 pentry = ENTRY_pentPopFreeEntry();
697 if (!pentry)
698 {
699 DPRINT1("GDIOBJ: Could not get a free entry.\n");
700 return NULL;
701 }
702
703 /* Make the object exclusively locked */
704 ExInitializePushLock(&pobj->pushlock);
705 KeEnterCriticalRegion();
706 ExAcquirePushLockExclusive(&pobj->pushlock);
707 pobj->cExclusiveLock = 1;
708 pobj->dwThreadId = PtrToUlong(PsGetCurrentThreadId());
709 DBG_INCREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj->hHmgr);
710
711 /* Get object type from the hHmgr field */
712 objt = ((ULONG_PTR)pobj->hHmgr >> 16) & 0xff;
713 ASSERT(objt != GDIObjType_DEF_TYPE);
714
715 /* Check if current process is requested owner */
716 if (ulOwner == GDI_OBJ_HMGR_POWNED)
717 {
718 /* Increment the process handle count */
719 IncrementGdiHandleCount();
720
721 /* Use Process id */
722 ulOwner = HandleToUlong(PsGetCurrentProcessId());
723 }
724
725 /* Insert the object into the handle table */
726 pobj->hHmgr = ENTRY_hInsertObject(pentry, pobj, objt, ulOwner);
727
728 /* Return the handle */
729 DPRINT("GDIOBJ: Created handle: %p\n", pobj->hHmgr);
730 DBG_LOGEVENT(&pobj->slhLog, EVENT_CREATE_HANDLE, 0);
731 return pobj->hHmgr;
732 }
733
734 VOID
735 NTAPI
736 GDIOBJ_vSetObjectOwner(
737 POBJ pobj,
738 ULONG ulOwner)
739 {
740 PENTRY pentry;
741
742 /* This is a ugly HACK, needed to fix IntGdiSetDCOwnerEx */
743 if (GDI_HANDLE_IS_STOCKOBJ(pobj->hHmgr))
744 {
745 DPRINT("Trying to set ownership of stock object %p to %lx\n", pobj->hHmgr, ulOwner);
746 return;
747 }
748
749 /* Get the handle entry */
750 ASSERT(GDI_HANDLE_GET_INDEX(pobj->hHmgr));
751 pentry = &gpentHmgr[GDI_HANDLE_GET_INDEX(pobj->hHmgr)];
752
753 /* Is the current process requested? */
754 if (ulOwner == GDI_OBJ_HMGR_POWNED)
755 {
756 /* Use process id */
757 ulOwner = HandleToUlong(PsGetCurrentProcessId());
758 if (pentry->ObjectOwner.ulObj != ulOwner)
759 {
760 IncrementGdiHandleCount();
761 }
762 }
763
764 // HACK
765 if (ulOwner == GDI_OBJ_HMGR_NONE)
766 ulOwner = GDI_OBJ_HMGR_PUBLIC;
767
768 if (ulOwner == GDI_OBJ_HMGR_PUBLIC ||
769 ulOwner == GDI_OBJ_HMGR_NONE)
770 {
771 /* Make sure we don't leak user mode memory */
772 ASSERT(pentry->pUser == NULL);
773 if (pentry->ObjectOwner.ulObj != GDI_OBJ_HMGR_PUBLIC &&
774 pentry->ObjectOwner.ulObj != GDI_OBJ_HMGR_NONE)
775 {
776 DecrementGdiHandleCount();
777 }
778 }
779
780 /* Set new owner */
781 pentry->ObjectOwner.ulObj = ulOwner;
782 }
783
784 /* Locks 2 or 3 objects at a time */
785 BOOL
786 NTAPI
787 GDIOBJ_bLockMultipleObjects(
788 IN ULONG ulCount,
789 IN HGDIOBJ* ahObj,
790 OUT PGDIOBJ* apObj,
791 IN UCHAR objt)
792 {
793 UINT auiIndices[3] = {0, 1, 2};
794 UINT i, j, tmp;
795
796 ASSERT(ulCount <= 3);
797
798 /* Sort the handles */
799 for (i = 0; i < ulCount - 1; i++)
800 {
801 for (j = i + 1; j < ulCount; j++)
802 {
803 if ((ULONG_PTR)ahObj[auiIndices[i]] <
804 (ULONG_PTR)ahObj[auiIndices[j]])
805 {
806 tmp = auiIndices[i];
807 auiIndices[i] = auiIndices[j];
808 auiIndices[j] = tmp;
809 }
810 }
811 }
812
813 /* Lock the objects in safe order */
814 for (i = 0; i < ulCount; i++)
815 {
816 /* Skip NULL handles */
817 if (ahObj[auiIndices[i]] == NULL)
818 {
819 apObj[auiIndices[i]] = NULL;
820 continue;
821 }
822
823 /* Lock the object */
824 apObj[auiIndices[i]] = GDIOBJ_LockObject(ahObj[auiIndices[i]], objt);
825
826 /* Check for failure */
827 if (apObj[auiIndices[i]] == NULL)
828 {
829 /* Cleanup */
830 while (i--)
831 {
832 if (apObj[auiIndices[i]])
833 GDIOBJ_vUnlockObject(apObj[auiIndices[i]]);
834 }
835 return FALSE;
836 }
837 }
838
839 return TRUE;
840 }
841
842 PVOID
843 NTAPI
844 GDIOBJ_pvGetObjectAttr(POBJ pobj)
845 {
846 ULONG ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
847 return gpentHmgr[ulIndex].pUser;
848 }
849
850 VOID
851 NTAPI
852 GDIOBJ_vSetObjectAttr(POBJ pobj, PVOID pvObjAttr)
853 {
854 ULONG ulIndex;
855
856 ASSERT(pobj->hHmgr);
857
858 /* Get the handle index */
859 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
860
861 /* Set pointer to the usermode attribute */
862 gpentHmgr[ulIndex].pUser = pvObjAttr;
863 }
864
865 VOID
866 NTAPI
867 GDIOBJ_vDeleteObject(POBJ pobj)
868 {
869 ULONG ulIndex;
870
871 /* Set the object's delete flag */
872 InterlockedOr16((SHORT*)&pobj->BaseFlags, BASEFLAG_READY_TO_DIE);
873 DBG_LOGEVENT(&pobj->slhLog, EVENT_DELETE, 0);
874
875 /* Get the handle index */
876 ulIndex = GDI_HANDLE_GET_INDEX(pobj->hHmgr);
877 if (ulIndex)
878 {
879 /* Reset the handle valid bit */
880 InterlockedAnd((LONG*)&gpaulRefCount[ulIndex], ~REF_MASK_VALID);
881
882 /* Check if the object is exclusively locked */
883 if (pobj->cExclusiveLock != 0)
884 {
885 /* Reset lock owner and lock count */
886 pobj->dwThreadId = 0;
887 pobj->cExclusiveLock = 0;
888
889 /* Release the pushlock and reenable APCs */
890 ExReleasePushLockExclusive(&pobj->pushlock);
891 KeLeaveCriticalRegion();
892 DBG_DECREASE_LOCK_COUNT(PsGetCurrentProcessWin32Process(), pobj->hHmgr);
893 }
894 }
895
896 /* Dereference the object (will take care of deletion) */
897 GDIOBJ_vDereferenceObject(pobj);
898 }
899
900 BOOL
901 NTAPI
902 GreIsHandleValid(HGDIOBJ hobj)
903 {
904 PENTRY pentry;
905
906 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
907 if (!pentry) return FALSE;
908 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
909 return TRUE;
910 }
911
912 BOOL
913 NTAPI
914 GreDeleteObject(HGDIOBJ hobj)
915 {
916 PENTRY pentry;
917
918 /* Check for stock objects */
919 if (GDI_HANDLE_IS_STOCKOBJ(hobj))
920 {
921 DPRINT1("GreDeleteObject: Cannot delete stock object %p.\n", hobj);
922 return FALSE;
923 }
924
925 /* Reference the handle entry */
926 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
927 if (!pentry)
928 {
929 DPRINT1("GreDeleteObject: Trying to delete invalid object %p\n", hobj);
930 return FALSE;
931 }
932
933 /* Check for public owner */
934 if (pentry->ObjectOwner.ulObj == GDI_OBJ_HMGR_PUBLIC)
935 {
936 DPRINT1("GreDeleteObject: Trying to delete global object %p\n", hobj);
937 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
938 return FALSE;
939 }
940
941 /* Delete the object */
942 GDIOBJ_vDeleteObject(pentry->einfo.pobj);
943 return TRUE;
944 }
945
946 ULONG
947 NTAPI
948 GreGetObjectOwner(HGDIOBJ hobj)
949 {
950 ULONG ulIndex, ulOwner;
951
952 /* Get the handle index */
953 ulIndex = GDI_HANDLE_GET_INDEX(hobj);
954
955 /* Check if the handle is valid */
956 if (ulIndex >= GDI_HANDLE_COUNT ||
957 gpentHmgr[ulIndex].Objt == GDIObjType_DEF_TYPE ||
958 ((ULONG_PTR)hobj >> 16) != gpentHmgr[ulIndex].FullUnique)
959 {
960 DPRINT1("GreGetObjectOwner: invalid handle 0x%p.\n", hobj);
961 return GDI_OBJ_HMGR_RESTRICTED;
962 }
963
964 /* Get the object owner */
965 ulOwner = gpentHmgr[ulIndex].ObjectOwner.ulObj;
966
967 if (ulOwner == HandleToUlong(PsGetCurrentProcessId()))
968 return GDI_OBJ_HMGR_POWNED;
969
970 if (ulOwner == GDI_OBJ_HMGR_PUBLIC)
971 return GDI_OBJ_HMGR_PUBLIC;
972
973 return GDI_OBJ_HMGR_RESTRICTED;
974 }
975
976 BOOL
977 NTAPI
978 GreSetObjectOwner(
979 HGDIOBJ hobj,
980 ULONG ulOwner)
981 {
982 PENTRY pentry;
983
984 /* Check for stock objects */
985 if (GDI_HANDLE_IS_STOCKOBJ(hobj))
986 {
987 DPRINT("GreSetObjectOwner: Got stock object %p\n", hobj);
988 return FALSE;
989 }
990
991 /* Reference the handle entry */
992 pentry = ENTRY_ReferenceEntryByHandle(hobj, 0);
993 if (!pentry)
994 {
995 DPRINT("GreSetObjectOwner: Invalid handle 0x%p.\n", hobj);
996 return FALSE;
997 }
998
999 /* Call internal function */
1000 GDIOBJ_vSetObjectOwner(pentry->einfo.pobj, ulOwner);
1001
1002 /* Dereference the object */
1003 GDIOBJ_vDereferenceObject(pentry->einfo.pobj);
1004
1005 return TRUE;
1006 }
1007
1008 INT
1009 NTAPI
1010 GreGetObject(
1011 IN HGDIOBJ hobj,
1012 IN INT cbCount,
1013 IN PVOID pvBuffer)
1014 {
1015 PVOID pvObj;
1016 UCHAR objt;
1017 INT iResult = 0;
1018
1019 /* Verify object type */
1020 objt = ((ULONG_PTR)hobj >> 16) & 0x1f;
1021 if (objt != GDIObjType_BRUSH_TYPE &&
1022 objt != GDIObjType_SURF_TYPE &&
1023 objt != GDIObjType_LFONT_TYPE &&
1024 objt != GDIObjType_PAL_TYPE)
1025 {
1026 DPRINT1("GreGetObject: Invalid object type\n");
1027 return 0;
1028 }
1029
1030 pvObj = GDIOBJ_ReferenceObjectByHandle(hobj, objt);
1031 if (!pvObj)
1032 {
1033 DPRINT("GreGetObject: Could not lock object\n");
1034 return 0;
1035 }
1036
1037 switch (GDI_HANDLE_GET_TYPE(hobj))
1038 {
1039 case GDILoObjType_LO_PEN_TYPE:
1040 case GDILoObjType_LO_EXTPEN_TYPE:
1041 iResult = PEN_GetObject(pvObj, cbCount, pvBuffer);
1042 break;
1043
1044 case GDILoObjType_LO_BRUSH_TYPE:
1045 iResult = BRUSH_GetObject(pvObj, cbCount, pvBuffer);
1046 break;
1047
1048 case GDILoObjType_LO_BITMAP_TYPE:
1049 iResult = BITMAP_GetObject(pvObj, cbCount, pvBuffer);
1050 break;
1051
1052 case GDILoObjType_LO_FONT_TYPE:
1053 iResult = FontGetObject(pvObj, cbCount, pvBuffer);
1054 break;
1055
1056 case GDILoObjType_LO_PALETTE_TYPE:
1057 iResult = PALETTE_GetObject(pvObj, cbCount, pvBuffer);
1058 break;
1059
1060 default:
1061 DPRINT1("GDI object type of 0x%p not implemented\n", hobj);
1062 break;
1063 }
1064
1065 GDIOBJ_vDereferenceObject(pvObj);
1066 return iResult;
1067 }
1068
1069 W32KAPI
1070 INT
1071 APIENTRY
1072 NtGdiExtGetObjectW(
1073 IN HANDLE hobj,
1074 IN INT cbCount,
1075 OUT LPVOID lpBuffer)
1076 {
1077 INT iRetCount = 0;
1078 INT cbCopyCount;
1079 union
1080 {
1081 BITMAP bitmap;
1082 DIBSECTION dibsection;
1083 LOGPEN logpen;
1084 LOGBRUSH logbrush;
1085 LOGFONTW logfontw;
1086 EXTLOGFONTW extlogfontw;
1087 ENUMLOGFONTEXDVW enumlogfontexdvw;
1088 } object;
1089
1090 /* Normalize to the largest supported object size */
1091 cbCount = min((UINT)cbCount, sizeof(object));
1092
1093 /* Now do the actual call */
1094 iRetCount = GreGetObject(hobj, cbCount, lpBuffer ? &object : NULL);
1095 cbCopyCount = min((UINT)cbCount, (UINT)iRetCount);
1096
1097 /* Make sure we have a buffer and a copy size */
1098 if ((cbCopyCount) && (lpBuffer))
1099 {
1100 /* Enter SEH for buffer transfer */
1101 _SEH2_TRY
1102 {
1103 /* Probe the buffer and copy it */
1104 ProbeForWrite(lpBuffer, cbCopyCount, sizeof(WORD));
1105 RtlCopyMemory(lpBuffer, &object, cbCopyCount);
1106 }
1107 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1108 {
1109 /* Clear the return value.
1110 * Do *NOT* set last error here! */
1111 iRetCount = 0;
1112 }
1113 _SEH2_END;
1114 }
1115
1116 /* Return the count */
1117 return iRetCount;
1118 }
1119
1120 W32KAPI
1121 HANDLE
1122 APIENTRY
1123 NtGdiCreateClientObj(
1124 IN ULONG ulType)
1125 {
1126 POBJ pObject;
1127 HANDLE handle;
1128
1129 /* Allocate a new object */
1130 pObject = GDIOBJ_AllocateObject(GDIObjType_CLIENTOBJ_TYPE,
1131 sizeof(CLIENTOBJ),
1132 BASEFLAG_LOOKASIDE);
1133 if (!pObject)
1134 {
1135 DPRINT1("NtGdiCreateClientObj: Could not allocate a clientobj.\n");
1136 return NULL;
1137 }
1138
1139 /* Mask out everything that would change the type in a wrong manner */
1140 ulType &= (GDI_HANDLE_TYPE_MASK & ~GDI_HANDLE_BASETYPE_MASK);
1141
1142 /* Set the real object type */
1143 pObject->hHmgr = UlongToHandle(ulType | GDILoObjType_LO_CLIENTOBJ_TYPE);
1144
1145 /* Create a handle */
1146 handle = GDIOBJ_hInsertObject(pObject, GDI_OBJ_HMGR_POWNED);
1147 if (!handle)
1148 {
1149 DPRINT1("NtGdiCreateClientObj: Could not create a handle.\n");
1150 GDIOBJ_vFreeObject(pObject);
1151 return NULL;
1152 }
1153
1154 /* Unlock it */
1155 GDIOBJ_vUnlockObject(pObject);
1156
1157 return handle;
1158 }
1159
1160 W32KAPI
1161 BOOL
1162 APIENTRY
1163 NtGdiDeleteClientObj(
1164 IN HANDLE hobj)
1165 {
1166 /* We first need to get the real type from the handle */
1167 ULONG ulType = GDI_HANDLE_GET_TYPE(hobj);
1168
1169 /* Check if it's really a CLIENTOBJ */
1170 if ((ulType & GDI_HANDLE_BASETYPE_MASK) != GDILoObjType_LO_CLIENTOBJ_TYPE)
1171 {
1172 /* FIXME: SetLastError? */
1173 return FALSE;
1174 }
1175
1176 return GreDeleteObject(hobj);
1177 }
1178
1179
1180
1181 PGDI_HANDLE_TABLE GdiHandleTable = NULL;
1182
1183 PGDIOBJ NTAPI
1184 GDIOBJ_ShareLockObj(HGDIOBJ hObj, DWORD ExpectedType)
1185 {
1186 if (ExpectedType == GDI_OBJECT_TYPE_DONTCARE)
1187 ExpectedType = GDI_HANDLE_GET_TYPE(hObj);
1188 return GDIOBJ_ReferenceObjectByHandle(hObj, (ExpectedType >> 16) & 0x1f);
1189 }
1190
1191 // This function is not safe to use with concurrent deleting attempts
1192 // That shouldn't be a problem, since we don't have any processes yet,
1193 // that could delete the handle
1194 BOOL
1195 NTAPI
1196 GDIOBJ_ConvertToStockObj(HGDIOBJ *phObj)
1197 {
1198 PENTRY pentry;
1199 POBJ pobj;
1200
1201 /* Reference the handle entry */
1202 pentry = ENTRY_ReferenceEntryByHandle(*phObj, 0);
1203 if (!pentry)
1204 {
1205 DPRINT1("GDIOBJ: Requested handle 0x%p is not valid.\n", *phObj);
1206 return FALSE;
1207 }
1208
1209 /* Update the entry */
1210 pentry->FullUnique |= GDI_ENTRY_STOCK_MASK;
1211 pentry->ObjectOwner.ulObj = 0;
1212
1213 /* Get the pointer to the BASEOBJECT */
1214 pobj = pentry->einfo.pobj;
1215
1216 /* Calculate the new handle */
1217 pobj->hHmgr = (HGDIOBJ)((ULONG_PTR)pobj->hHmgr | GDI_HANDLE_STOCK_MASK);
1218
1219 /* Return the new handle */
1220 *phObj = pobj->hHmgr;
1221
1222 /* Dereference the handle */
1223 GDIOBJ_vDereferenceObject(pobj);
1224
1225 return TRUE;
1226 }
1227
1228 POBJ NTAPI
1229 GDIOBJ_AllocObjWithHandle(ULONG ObjectType, ULONG cjSize)
1230 {
1231 POBJ pobj;
1232 FLONG fl = 0;
1233 UCHAR objt = (ObjectType >> 16) & 0xFF;
1234
1235 if ((objt == GDIObjType_DC_TYPE && cjSize == sizeof(DC)) ||
1236 (objt == GDIObjType_PAL_TYPE && cjSize == sizeof(PALETTE)) ||
1237 (objt == GDIObjType_RGN_TYPE && cjSize == sizeof(REGION)) ||
1238 (objt == GDIObjType_SURF_TYPE && cjSize == sizeof(SURFACE)) ||
1239 (objt == GDIObjType_PATH_TYPE && cjSize == sizeof(PATH)))
1240 {
1241 fl |= BASEFLAG_LOOKASIDE;
1242 }
1243
1244 pobj = GDIOBJ_AllocateObject(objt, cjSize, fl);
1245 if (!GDIOBJ_hInsertObject(pobj, GDI_OBJ_HMGR_POWNED))
1246 {
1247 GDIOBJ_vFreeObject(pobj);
1248 return NULL;
1249 }
1250 return pobj;
1251 }
1252
1253 PVOID NTAPI
1254 GDI_MapHandleTable(PEPROCESS pProcess)
1255 {
1256 PVOID pvMappedView = NULL;
1257 NTSTATUS Status;
1258 LARGE_INTEGER liOffset;
1259 ULONG cjViewSize = sizeof(GDI_HANDLE_TABLE);
1260
1261 liOffset.QuadPart = 0;
1262
1263 ASSERT(gpvGdiHdlTblSection != NULL);
1264 ASSERT(pProcess != NULL);
1265
1266 Status = MmMapViewOfSection(gpvGdiHdlTblSection,
1267 pProcess,
1268 &pvMappedView,
1269 0,
1270 0,
1271 &liOffset,
1272 &cjViewSize,
1273 ViewUnmap,
1274 SEC_NO_CHANGE,
1275 PAGE_READONLY);
1276
1277 if (!NT_SUCCESS(Status))
1278 return NULL;
1279
1280 return pvMappedView;
1281 }
1282
1283 BOOL NTAPI
1284 GDI_CleanupForProcess(struct _EPROCESS *Process)
1285 {
1286 PENTRY pentry;
1287 ULONG ulIndex;
1288 DWORD dwProcessId;
1289 PPROCESSINFO ppi;
1290
1291 DPRINT("CleanupForProcess prochandle %x Pid %d\n",
1292 Process, Process->UniqueProcessId);
1293
1294 ASSERT(Process == PsGetCurrentProcess());
1295
1296 /* Get the current process Id */
1297 dwProcessId = PtrToUlong(PsGetCurrentProcessId());
1298
1299 /* Loop all handles in the handle table */
1300 for (ulIndex = RESERVE_ENTRIES_COUNT; ulIndex < gulFirstUnused; ulIndex++)
1301 {
1302 pentry = &gpentHmgr[ulIndex];
1303
1304 /* Check if the object is owned by the process */
1305 if (pentry->ObjectOwner.ulObj == dwProcessId)
1306 {
1307 ASSERT(pentry->einfo.pobj->cExclusiveLock == 0);
1308
1309 /* Reference the object and delete it */
1310 InterlockedIncrement((LONG*)&gpaulRefCount[ulIndex]);
1311 GDIOBJ_vDeleteObject(pentry->einfo.pobj);
1312 }
1313 }
1314
1315 #if DBG
1316 //#ifdef GDI_DEBUG
1317 DbgGdiHTIntegrityCheck();
1318 //#endif
1319 #endif
1320
1321 ppi = PsGetCurrentProcessWin32Process();
1322 DPRINT("Completed cleanup for process %d\n", Process->UniqueProcessId);
1323 if (ppi->GDIHandleCount != 0)
1324 {
1325 DPRINT1("Leaking %d handles!\n", ppi->GDIHandleCount);
1326 ASSERT(FALSE);
1327 }
1328
1329 /* Loop all handles in the handle table */
1330 for (ulIndex = RESERVE_ENTRIES_COUNT; ulIndex < gulFirstUnused; ulIndex++)
1331 {
1332 pentry = &gpentHmgr[ulIndex];
1333
1334 /* Check if the object is owned by the process */
1335 if (pentry->ObjectOwner.ulObj == dwProcessId)
1336 {
1337 DPRINT1("Leaking object. Index=%lx, type=0x%x, refcount=%lx\n",
1338 ulIndex, pentry->Objt, gpaulRefCount[ulIndex]);
1339 DBG_DUMP_EVENT_LIST(&pentry->einfo.pobj->slhLog);
1340 //DBG_CLEANUP_EVENT_LIST(&pentry->einfo.pobj->slhLog);
1341 ASSERT(FALSE);
1342 }
1343 }
1344
1345 return TRUE;
1346 }
1347
1348 /* EOF */