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