Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4060 lines
108 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: hmgrapi.cxx
  3. *
  4. * Handle manager API entry points
  5. *
  6. * Created: 08-Dec-1989 23:03:03
  7. * Author: Donald Sidoroff [donalds]
  8. *
  9. * Copyright (c) 1989-1999 Microsoft Corporation
  10. \**************************************************************************/
  11. #include "precomp.hxx"
  12. //
  13. // Synchronization of the handle manager
  14. //
  15. HSEMAPHORE ghsemHmgr;
  16. PDEVCAPS gpGdiDevCaps = NULL;
  17. //
  18. // Points to shared memory including handle table and cfont cache
  19. //
  20. PGDI_SHARED_MEMORY gpGdiSharedMemory;
  21. //
  22. // Points to handle array
  23. //
  24. ENTRY *gpentHmgr=NULL;
  25. //
  26. // Free List of handles
  27. //
  28. HOBJ ghFreeHmgr;
  29. //
  30. // Max handle alloced so far
  31. //
  32. ULONG gcMaxHmgr;
  33. //
  34. // Look aside list headers and maximum allowed entry-size array
  35. PPAGED_LOOKASIDE_LIST pHmgLookAsideList[MAX_TYPE + 1];
  36. ULONG laSize[MAX_TYPE + 1];
  37. #define HMG_LAL_TAG '0alG'
  38. //
  39. //
  40. //
  41. extern "C"
  42. {
  43. HFASTMUTEX ghfmMemory;
  44. }
  45. PVOID gpHmgrSharedHandleSection;
  46. //
  47. // 10 millisecond wait value
  48. //
  49. // We only have a pointer to it since it must be allocated out of Non-paged
  50. // pool
  51. //
  52. PLARGE_INTEGER gpLockShortDelay;
  53. //
  54. // Hydra session flag
  55. //
  56. // Note that this only has bearing on whether process is a hydra session
  57. // or not. It is possible to not be a hydra session, but still be a
  58. // a hydra server (check gIsTerminalServer for that information).
  59. //
  60. // Value is TRUE if not a hydra session.
  61. // Value is FALSE if a hydra session.
  62. //
  63. extern BOOL G_fConsole;
  64. //
  65. // Short term temporary buffer that can be used to put large
  66. // objects for which we have no place in the stack.
  67. //
  68. // The value should only be used through the Alloc\FreeTmpBuffer functions
  69. //
  70. #define TMP_GLOBAL_BUFFER_SIZE 0x1000
  71. PVOID *gpTmpGlobalFree;
  72. PVOID gpTmpGlobal = NULL;
  73. // Number of DCATTR blocks in 1 page
  74. #define ULNUMDC ((PAGE_SIZE)/sizeof(DC_ATTR))
  75. // Number of OBJECTATTR blocks in 1 page
  76. #define ULNUMBRUSH ((PAGE_SIZE)/sizeof(OBJECTATTR))
  77. typedef struct _DCAFLISTENTRY
  78. {
  79. LIST_ENTRY listEntry;
  80. ULONG cFree;
  81. PDC_ATTR* a[ULNUMDC];
  82. } DCAFLISTENTRY, *PDCAFLISTENTRY;
  83. typedef struct _OBAFLISTENTRY
  84. {
  85. LIST_ENTRY listEntry;
  86. ULONG cFree;
  87. POBJECTATTR* a[ULNUMBRUSH];
  88. } OBAFLISTENTRY, *POBAFLISTENTRY;
  89. //
  90. // Prototype for a handy debugging routine.
  91. //
  92. #if DBG
  93. extern "C"
  94. VOID
  95. HmgPrintBadHandle(
  96. HOBJ hobj,
  97. OBJTYPE objt
  98. );
  99. #else
  100. #define HmgPrintBadHandle(hobj, objt)
  101. #endif
  102. //
  103. // During initialization, some things which are really really bad
  104. // (like not being able to allocate the handle manager table) and
  105. // we RIP because we cannot proceed and its completely unexpected.
  106. //
  107. // However, on a Hydra system, some of these are things we need
  108. // to expect. So what would be a RIP on a non-Hydra system can
  109. // be treated as a WARNING on a Hydra system
  110. //
  111. #if DBG
  112. #define NONHYDRA_RIP(a) \
  113. { \
  114. if (G_fConsole) \
  115. RIP(a); \
  116. else \
  117. WARNING(a); \
  118. }
  119. #else
  120. #define NONHYDRA_RIP(a)
  121. #endif
  122. /**************************************************************************\
  123. *
  124. * Fast tempporary memory allocator
  125. *
  126. \**************************************************************************/
  127. PVOID
  128. AllocFreeTmpBuffer(
  129. ULONG size)
  130. {
  131. PVOID tmpPtr = NULL;
  132. if (size <= TMP_GLOBAL_BUFFER_SIZE)
  133. {
  134. tmpPtr = (PVOID) InterlockedExchangePointer((PVOID *)gpTmpGlobalFree, 0);
  135. }
  136. if (!tmpPtr)
  137. {
  138. tmpPtr = AllocThreadBufferWithTag(size,'pmTG');
  139. }
  140. return tmpPtr;
  141. }
  142. VOID
  143. FreeTmpBuffer(
  144. PVOID pv)
  145. {
  146. if (pv == gpTmpGlobal)
  147. {
  148. ASSERTGDI(*gpTmpGlobalFree == NULL, "GRE: gpTmpGlobalFree is inconsistent\n");
  149. *gpTmpGlobalFree = pv;
  150. }
  151. else
  152. {
  153. FreeThreadBufferWithTag(pv);
  154. }
  155. }
  156. /**************************************************************************\
  157. *
  158. * Thread specific memory allocator. Memory is added to tracking list in
  159. * the thread and cleaned up if its still in the list when the thread dies.
  160. *
  161. \**************************************************************************/
  162. PVOID
  163. AllocThreadBufferWithTag(
  164. ULONG size,
  165. ULONG tag)
  166. {
  167. PVOID tmpPtr = NULL;
  168. if (size < MAXULONG - sizeof(LIST_ENTRY))
  169. {
  170. tmpPtr = PALLOCNOZ(size + sizeof(LIST_ENTRY),tag);
  171. if (tmpPtr)
  172. {
  173. PW32THREAD pThread = W32GetCurrentThread();
  174. ASSERTGDI(pThread, "GRE: AllocThreadBuffer W32Thread is NULL\n");
  175. if (pThread)
  176. {
  177. PLIST_ENTRY pEntry = (PLIST_ENTRY)tmpPtr;
  178. InsertHeadList(&pThread->GdiTmpAllocList, pEntry);
  179. tmpPtr = (PVOID)(pEntry + 1);
  180. }
  181. }
  182. }
  183. return tmpPtr;
  184. }
  185. VOID
  186. FreeThreadBufferWithTag(
  187. PVOID pv)
  188. {
  189. PW32THREAD pThread = W32GetCurrentThread();
  190. ASSERTGDI(pThread, "GRE: FreeThreadBuffer W32Thread is NULL\n");
  191. if (pThread)
  192. {
  193. PLIST_ENTRY pFree = (PLIST_ENTRY)pv;
  194. RemoveEntryList(pFree-1);
  195. VFREEMEM(pFree-1);
  196. }
  197. }
  198. /**************************************************************************\
  199. *
  200. * Performance, hit rate, memory size statistics
  201. *
  202. \**************************************************************************/
  203. #if DBG
  204. #define GDI_PERF 1
  205. #endif
  206. #if GDI_PERF
  207. extern "C"
  208. {
  209. // these must be extern "C" so the debugger extensions can see them
  210. ULONG HmgCurrentNumberOfObjects[MAX_TYPE + 1];
  211. ULONG HmgMaximumNumberOfObjects[MAX_TYPE + 1];
  212. ULONG HmgCurrentNumberOfLookAsideObjects[MAX_TYPE + 1];
  213. ULONG HmgMaximumNumberOfLookAsideObjects[MAX_TYPE + 1];
  214. ULONG HmgNumberOfObjectsAllocated[MAX_TYPE + 1];
  215. ULONG HmgNumberOfLookAsideHits[MAX_TYPE + 1];
  216. ULONG HmgCurrentNumberOfHandles[MAX_TYPE + 1];
  217. ULONG HmgMaximumNumberOfHandles[MAX_TYPE + 1];
  218. ULONG HmgNumberOfHandlesAllocated[MAX_TYPE + 1];
  219. };
  220. #endif
  221. /*****************************Exported*Routine*****************************\
  222. * HmgCreate()
  223. *
  224. * Initializes a new handle manager.
  225. *
  226. * History:
  227. *
  228. * Mon 14-Apr-1997 -by- Dan Almosnino [danalm]
  229. * Modified Lookaside initialization to use DaveC S-Lists
  230. * and set the allowable object size on the list based on memory model
  231. *
  232. * Wed 29-Apr-1992 -by- Patrick Haluptzok [patrickh]
  233. * Change to mutex for exclusion, init event here.
  234. *
  235. * Mon 21-Oct-1991 -by- Patrick Haluptzok [patrickh]
  236. * Reserve memory for the handle table so unlock doesn't need semaphore.
  237. *
  238. * Mon 08-Jul-1991 -by- Patrick Haluptzok [patrickh]
  239. * make 0 an invalid handle
  240. *
  241. * Sun 20-Jun-1991 -by- Patrick Haluptzok [patrickh]
  242. * The hheap has gone away, the correct error codes are logged.
  243. *
  244. * 30-Nov-1989 -by- Donald Sidoroff [donalds]
  245. * Wrote it.
  246. \**************************************************************************/
  247. BOOL HmgCreate()
  248. {
  249. ULONG ulType;
  250. ULONG Size;
  251. BOOL LargeSystemSize = FALSE;
  252. #if 0
  253. KdPrint((" MASK CBITS SHIFT\n"));
  254. KdPrint(("INDEX = 0x%8.8lx, %3d %3d \n",INDEX_MASK ,INDEX_BITS ,INDEX_SHIFT));
  255. KdPrint(("TYPE = 0x%8.8lx, %3d %3d \n",TYPE_MASK ,TYPE_BITS ,TYPE_SHIFT ));
  256. KdPrint(("ALTTYPE = 0x%8.8lx, %3d %3d \n",ALTTYPE_MASK ,ALTTYPE_BITS ,ALTTYPE_SHIFT ));
  257. KdPrint(("STOCK = 0x%8.8lx, %3d %3d \n",STOCK_MASK ,STOCK_BITS ,STOCK_SHIFT ));
  258. KdPrint(("UNIQUE = 0x%8.8lx, %3d %3d \n",UNIQUE_MASK ,UNIQUE_BITS ,UNIQUE_SHIFT ));
  259. KdPrint(("LOTYPE = 0x%8.8lx, %3d %3d \n",LOTYPE_MASK ,LOTYPE_BITS ,LOTYPE_SHIFT ));
  260. KdPrint(("FULLTYPE = 0x%8.8lx, %3d %3d \n",FULLTYPE_MASK ,FULLTYPE_BITS ,FULLTYPE_SHIFT ));
  261. KdPrint(("FULLUNIQUE = 0x%8.8lx, %3d %3d \n",FULLUNIQUE_MASK,FULLUNIQUE_BITS,FULLUNIQUE_SHIFT ));
  262. KdPrint(("\n"));
  263. #endif
  264. //
  265. // Initialize exclusion stuff.
  266. //
  267. if ((ghsemHmgr = GreCreateSemaphore()) == NULL)
  268. return FALSE;
  269. //
  270. // Initialize the handle manager allocation database.
  271. //
  272. ghFreeHmgr = 0; // No free handles
  273. gcMaxHmgr = HMGR_HANDLE_BASE; // Initialize with handle index base
  274. //
  275. // Initialize the allocation of lookaside lists for selected objects.
  276. //
  277. // Initialize the maximum allowed entry-size for each object to zero
  278. if (MmMediumSystem <= MmQuerySystemSize())
  279. LargeSystemSize = TRUE;
  280. for (ulType = 0; ulType < MAX_TYPE + 1; ulType++)
  281. {
  282. laSize[ulType] = 0;
  283. }
  284. // Now Initialize the Lookaside lists
  285. Size = (LargeSystemSize == TRUE)? HMG_DC_MAX : HMG_DC_SIZE;
  286. if (HmgInitializeLookAsideList(DC_TYPE, Size, HMG_LAL_TAG, HMG_DC_OBJECTS) == FALSE)
  287. return FALSE;
  288. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_RGN_MAX : HMG_RGN_SIZE);
  289. if (HmgInitializeLookAsideList(RGN_TYPE, Size, HMG_LAL_TAG, HMG_RGN_OBJECTS) == FALSE)
  290. return FALSE;
  291. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_SURF_MAX : HMG_SURF_SIZE);
  292. if (HmgInitializeLookAsideList(SURF_TYPE, Size, HMG_LAL_TAG, HMG_SURF_OBJECTS) == FALSE)
  293. return FALSE;
  294. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_PAL_MAX : HMG_PAL_SIZE);
  295. if (HmgInitializeLookAsideList(PAL_TYPE, Size, HMG_LAL_TAG, HMG_PAL_OBJECTS) == FALSE)
  296. return FALSE;
  297. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_BRUSH_MAX : HMG_BRUSH_SIZE);
  298. if (HmgInitializeLookAsideList(BRUSH_TYPE, Size, HMG_LAL_TAG, HMG_BRUSH_OBJECTS) == FALSE)
  299. return FALSE;
  300. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_LFONT_MAX : HMG_LFONT_SIZE);
  301. if (HmgInitializeLookAsideList(LFONT_TYPE, Size, HMG_LAL_TAG, HMG_LFONT_OBJECTS) == FALSE)
  302. return FALSE;
  303. Size = (ULONG)((LargeSystemSize == TRUE)? HMG_RFONT_MAX : HMG_RFONT_SIZE);
  304. if (HmgInitializeLookAsideList(RFONT_TYPE, Size, HMG_LAL_TAG, HMG_RFONT_OBJECTS) == FALSE)
  305. return FALSE;
  306. //
  307. //
  308. // Init mutex
  309. //
  310. // allocate and initialize memory fast mutex from non-paged pool
  311. //
  312. ghfmMemory = GreCreateFastMutex();
  313. if (ghfmMemory == NULL)
  314. {
  315. NONHYDRA_RIP("HmgCreate failed to create ghfmMemory");
  316. return(FALSE);
  317. }
  318. //
  319. // Create section for shared GDI handle table
  320. //
  321. #if defined(_GDIPLUS_)
  322. {
  323. //
  324. // NOTE: _GDIPLUS_ is some work in progress code.
  325. //
  326. //
  327. // This should probably be a reserve-and-commit
  328. // We ceed to fix clean-up case, too
  329. //
  330. gpGdiSharedMemory = (GDI_SHARED_MEMORY*)
  331. PALLOCMEM(sizeof(GDI_SHARED_MEMORY), ' thG');
  332. if (gpGdiSharedMemory == NULL)
  333. {
  334. RIP("Invalid handle table.");
  335. }
  336. }
  337. #else
  338. {
  339. NTSTATUS Status;
  340. OBJECT_ATTRIBUTES ObjectAttributes;
  341. UNICODE_STRING UnicodeString;
  342. LARGE_INTEGER MaximumSize;
  343. HANDLE hHmgrSharedHandleTable;
  344. ACCESS_MASK DesiredAccess = SECTION_MAP_READ |
  345. SECTION_MAP_WRITE;
  346. ULONG SectionPageProtection = PAGE_READWRITE;
  347. ULONG AllocationAttributes = SEC_COMMIT |
  348. SEC_NO_CHANGE;
  349. MaximumSize.HighPart = 0;
  350. MaximumSize.LowPart = sizeof(GDI_SHARED_MEMORY);
  351. ASSERTGDI((gpHmgrSharedHandleSection == NULL),
  352. "gpHmgrSharedHandleSection MUST be NULL");
  353. Status = Win32CreateSection(&gpHmgrSharedHandleSection,
  354. DesiredAccess,
  355. NULL,
  356. &MaximumSize,
  357. SectionPageProtection,
  358. AllocationAttributes,
  359. NULL,
  360. NULL,
  361. TAG_SECTION_HMGR);
  362. if (!NT_SUCCESS(Status))
  363. {
  364. KdPrint(("Error in HmgCreate: Win32CreateSection returns %lx\n",Status));
  365. NONHYDRA_RIP("Can't continue without shared handle section");
  366. return(FALSE);
  367. }
  368. else
  369. {
  370. SIZE_T ViewSize = 0;
  371. #ifdef _HYDRA_
  372. //
  373. // MmMapViewInSessionSpace is internally promoted to
  374. // MmMapViewInSystemSpace on non-Hydra systems.
  375. //
  376. // Win32MapViewInSessionSpace is included for tracking
  377. // Section objects. It's promoted to MmMapViewInSessionSpace
  378. // after the tracking code.
  379. //
  380. Status = Win32MapViewInSessionSpace(
  381. gpHmgrSharedHandleSection,
  382. (PVOID*)&gpGdiSharedMemory,
  383. &ViewSize);
  384. #else
  385. Status = MmMapViewInSystemSpace(
  386. gpHmgrSharedHandleSection,
  387. (PVOID*)&gpGdiSharedMemory,
  388. &ViewSize);
  389. #endif
  390. if (!NT_SUCCESS(Status))
  391. {
  392. KdPrint(("Error in HmgCreate: MmMapViewInSystemSpace returns %lx\n",Status));
  393. NONHYDRA_RIP("Can't continue without shared handle section");
  394. return(FALSE);
  395. }
  396. }
  397. }
  398. #endif
  399. gpentHmgr = gpGdiSharedMemory->aentryHmgr;
  400. gpGdiDevCaps = &(gpGdiSharedMemory->DevCaps);
  401. //
  402. // Allocate the handle table and commit the initial allocation.
  403. //
  404. // N.B. Committed memory is demand zero.
  405. //
  406. //
  407. if (gpentHmgr == NULL) {
  408. NONHYDRA_RIP("Hmgr-bInit failed to reserve the handle table memory\n");
  409. return(FALSE);
  410. }
  411. //
  412. // allocate and initialize the timeout lock for the handle manager.
  413. //
  414. gpLockShortDelay = (PLARGE_INTEGER) GdiAllocPoolNonPaged(sizeof(LARGE_INTEGER),
  415. 'iniG');
  416. if (gpLockShortDelay == NULL) {
  417. NONHYDRA_RIP("Hmgr-could not allocate memory for delay lock\n");
  418. return(FALSE);
  419. }
  420. gpLockShortDelay->LowPart = (ULONG) -100000;
  421. gpLockShortDelay->HighPart = -1;
  422. //
  423. // Create a short term temporary buffer that can be used to put large
  424. // objects for which we have no place in the stack.
  425. //
  426. // We actually have to allocate the pointer to the buffer in non-paged
  427. // pool so that we can do an Interlocked exchange safely on it
  428. //
  429. gpTmpGlobal = (PVOID)PALLOCNOZ(TMP_GLOBAL_BUFFER_SIZE, 'blgG');
  430. gpTmpGlobalFree = (PVOID *)GdiAllocPoolNonPaged(sizeof(PVOID),
  431. 'iniG');
  432. if ((gpTmpGlobal == NULL) ||
  433. (gpTmpGlobalFree == NULL))
  434. {
  435. NONHYDRA_RIP("Can't allocate process list entry");
  436. return(FALSE);
  437. }
  438. *gpTmpGlobalFree = gpTmpGlobal;
  439. return(TRUE);
  440. }
  441. /******************************Public*Routine******************************\
  442. * InitializeLookAsideList
  443. *
  444. * Initializes a LookAside List for a selected object
  445. * Uses DaveC's S-Lists
  446. *
  447. * History:
  448. * 14-Apr-1997 -by- Dan Almosnino danalm
  449. * Wrote it
  450. \**************************************************************************/
  451. PVOID StubGdiAlloc(
  452. POOL_TYPE PoolType,
  453. SIZE_T uBytes,
  454. ULONG iTag)
  455. {
  456. return GdiAllocPool((ULONG)uBytes, iTag);
  457. UNREFERENCED_PARAMETER(PoolType);
  458. }
  459. // Does anyone want to change this to #define ? if so please do so after win2000.
  460. //
  461. // #define StubGdiFree(p) GdiFreePool((p))
  462. VOID StubGdiFree(
  463. PVOID p)
  464. {
  465. GdiFreePool(p);
  466. }
  467. BOOL
  468. HmgInitializeLookAsideList(
  469. ULONG ulType,
  470. ULONG Size,
  471. ULONG dwTag,
  472. USHORT Number
  473. )
  474. {
  475. #if !defined(_GDIPLUS_)
  476. ULONG Flags = 0;
  477. // Modify tag using type.
  478. dwTag += ulType << 24;
  479. // Set the maximum allowed entry-size for this object
  480. laSize[ulType] = Size;
  481. // Allocate space for the List headers from Non-Paged pool (Otherwise bugcheck occurs
  482. // if Kernel tries to access them for modification of list-depth if they are paged out)
  483. pHmgLookAsideList[ulType] = (PPAGED_LOOKASIDE_LIST)GdiAllocPoolNonPagedNS(
  484. sizeof(PAGED_LOOKASIDE_LIST),
  485. dwTag);
  486. if (pHmgLookAsideList[ulType] == (PPAGED_LOOKASIDE_LIST)NULL)
  487. {
  488. WARNING1("HmgCreate failed to allocate memory for pHmgLookAsideList\n");
  489. return(FALSE);
  490. }
  491. // Now Initialize the Lookaside list
  492. #ifdef _HYDRA_
  493. if (gIsTerminalServer) {
  494. Flags |= gSessionPoolMask;
  495. }
  496. #endif
  497. ExInitializePagedLookasideList(pHmgLookAsideList[ulType],
  498. StubGdiAlloc,
  499. StubGdiFree,
  500. Flags,
  501. Size,
  502. dwTag,
  503. Number);
  504. #endif
  505. return (TRUE);
  506. }
  507. /******************************Public*Routine******************************\
  508. * IncProcessHandleCount - inc process handle count if under limit or
  509. * flag specifies no check.
  510. *
  511. * Arguments:
  512. *
  513. * Return Value:
  514. *
  515. * TRUE if count is incremented.
  516. *
  517. * History:
  518. *
  519. * 30-Apr-1996 -by- Mark Enstrom [marke]
  520. *
  521. \**************************************************************************/
  522. BOOL
  523. HmgIncProcessHandleCount(
  524. W32PID w32pid,
  525. OBJTYPE objt
  526. )
  527. {
  528. BOOL bRet = TRUE;
  529. #if !defined(_GDIPLUS_)
  530. //
  531. // maintain handle count but don't limit quota
  532. // for DCs
  533. // --> changed it to limit quota for DCs. The fact that
  534. // USER create DCs for menu's and borders on behalf of the
  535. // app should not be that bad to prevent us from limiting DCs
  536. // end user can always use 'close' or task manager to kill a bad app.
  537. // If we don't limit DCs, the whole system will be affected.
  538. // [lingyunw]
  539. //
  540. if ((w32pid != OBJECT_OWNER_PUBLIC) && (w32pid != OBJECT_OWNER_NONE))
  541. {
  542. //
  543. // is the PID the current PID
  544. //
  545. if (w32pid == W32GetCurrentPID())
  546. {
  547. PW32PROCESS pw32Current = W32GetCurrentProcess();
  548. //
  549. // if the w32 process is not NULL then use
  550. // the current value, otherwise the process
  551. // has no W32PROCESS, so don't track handle quota
  552. //
  553. if (pw32Current)
  554. {
  555. //
  556. // increment handle count unless call specifies check quota and
  557. // process is already at or above limit.
  558. //
  559. if (pw32Current->GDIHandleCount >= gProcessHandleQuota)
  560. {
  561. WARNING1("GDI Handle Limit reached\n");
  562. bRet = FALSE;
  563. }
  564. else
  565. {
  566. InterlockedIncrement(&pw32Current->GDIHandleCount);
  567. }
  568. }
  569. }
  570. else
  571. {
  572. PW32PROCESS pw32;
  573. PEPROCESS Process;
  574. NTSTATUS Status;
  575. Status = PsLookupProcessByProcessId(LongToHandle(w32pid), &Process);
  576. if (NT_SUCCESS(Status)) {
  577. if (pw32 = (PW32PROCESS)(PsGetProcessWin32Process(Process))) {
  578. if (pw32->GDIHandleCount >= gProcessHandleQuota)
  579. {
  580. WARNING1("GDI Handle Limit reached\n");
  581. bRet = FALSE;
  582. }
  583. else
  584. {
  585. InterlockedIncrement(&pw32->GDIHandleCount);
  586. }
  587. }
  588. ObDereferenceObject(Process);
  589. }
  590. else
  591. {
  592. WARNING1("HmgIncProcessGDIHandleCount: Couldn't find PID owner\n");
  593. }
  594. }
  595. }
  596. #endif
  597. return(bRet);
  598. }
  599. /******************************Public*Routine******************************\
  600. * HmgDecProcessHandleCount - dec process handle count
  601. *
  602. * Arguments:
  603. *
  604. * none
  605. *
  606. * Return Value:
  607. *
  608. * nont
  609. *
  610. * History:
  611. *
  612. * 6-May-1996 -by- Mark Enstrom [marke]
  613. *
  614. \**************************************************************************/
  615. VOID
  616. HmgDecProcessHandleCount(
  617. W32PID w32pid
  618. )
  619. {
  620. #if !defined(_GDIPLUS_)
  621. if ((w32pid != OBJECT_OWNER_PUBLIC) && (w32pid != OBJECT_OWNER_NONE))
  622. {
  623. if (w32pid == W32GetCurrentPID())
  624. {
  625. PW32PROCESS pw32Current = W32GetCurrentProcess();
  626. if (pw32Current) {
  627. //
  628. // use current process
  629. //
  630. InterlockedDecrement(&pw32Current->GDIHandleCount);
  631. if (pw32Current->GDIHandleCount < 0)
  632. {
  633. WARNING("GDI process handle count: decremented below zero");
  634. }
  635. }
  636. }
  637. else
  638. {
  639. PW32PROCESS pw32;
  640. PEPROCESS Process;
  641. NTSTATUS Status;
  642. Status = PsLookupProcessByProcessId(LongToHandle(w32pid), &Process);
  643. if (NT_SUCCESS(Status)) {
  644. if (pw32 = (PW32PROCESS)(PsGetProcessWin32Process(Process))) {
  645. InterlockedDecrement(&pw32->GDIHandleCount);
  646. if (pw32->GDIHandleCount < 0)
  647. {
  648. WARNING("GDI process handle count: decremented below zero");
  649. }
  650. }
  651. ObDereferenceObject(Process);
  652. }
  653. else
  654. {
  655. WARNING1("HmgDeccProcessGDIHandleCount: Couldn't find PID owner\n");
  656. }
  657. }
  658. }
  659. #endif
  660. }
  661. /******************************Public*Routine******************************\
  662. * HmgValidHandle
  663. *
  664. * Returns TRUE if the handle is valid, FALSE if not.
  665. *
  666. * Note we don't need to lock the semaphore, we aren't changing anything,
  667. * we are just peeking in.
  668. *
  669. * History:
  670. * 08-Jul-1991 -by- Patrick Haluptzok patrickh
  671. * Wrote it.
  672. \**************************************************************************/
  673. BOOL
  674. HmgValidHandle(
  675. HOBJ hobj,
  676. OBJTYPE objt)
  677. {
  678. PENTRY pentTmp;
  679. UINT uiIndex = (UINT) (UINT) HmgIfromH(hobj);
  680. if ((uiIndex < gcMaxHmgr) &&
  681. ((pentTmp = &gpentHmgr[uiIndex])->Objt == objt) &&
  682. (pentTmp->FullUnique == HmgUfromH(hobj)))
  683. {
  684. ASSERTGDI(pentTmp->einfo.pobj != (POBJ) NULL, "ERROR how can it be NULL");
  685. return(TRUE);
  686. }
  687. else
  688. {
  689. return(FALSE);
  690. }
  691. }
  692. /******************************Public*Routine******************************\
  693. * HmgInsertObject
  694. *
  695. * This inserts an object into the handle table, returning the handle
  696. * associated with the pointer.
  697. *
  698. * History:
  699. * 13-Oct-1993 -by- Patrick Haluptzok patrickh
  700. * Wrote it.
  701. \**************************************************************************/
  702. HOBJ
  703. HmgInsertObject(
  704. PVOID pv,
  705. FLONG flags, // flags can be a combination of the following :
  706. // HMGR_MAKE_PUBLIC - Allow object to be lockable by
  707. // any process
  708. // HMGR_ALLOC_LOCK - Do an HmgLock on the object and
  709. // return a pointer instead of handle
  710. // HMGR_ALLOC_ALT_LOCK - Do an HmgShareLock on the object
  711. // and return a pointer instead of
  712. // handle
  713. OBJTYPE objt)
  714. {
  715. ASSERTGDI(pv != (PVOID) NULL, "Invalid address");
  716. ASSERTGDI(objt != (OBJTYPE) DEF_TYPE, "objt is bad");
  717. HOBJ h = 0;
  718. BOOL bHandleQuota = TRUE;
  719. W32PID W32Pid = W32GetCurrentPID();
  720. #if defined(_WIN64)
  721. PW32THREAD pW32Thread = W32GetCurrentThread();
  722. PRINTCLIENTID *pClientID = pW32Thread ? (PRINTCLIENTID*)pW32Thread->pClientID : 0;
  723. W32Pid = pClientID ? pClientID->clientPid : W32Pid;
  724. #endif
  725. //
  726. // need the HMGR lock held while getting the handle
  727. //
  728. {
  729. MLOCKFAST mo;
  730. if (!(flags & HMGR_MAKE_PUBLIC))
  731. {
  732. //
  733. // Inc handle count for non-public objects. DC objects
  734. // can be allocated above process limit
  735. //
  736. bHandleQuota = HmgIncProcessHandleCount(W32Pid,objt);
  737. }
  738. if (bHandleQuota)
  739. {
  740. h = hGetFreeHandle((OBJTYPE) objt);
  741. if (h == 0)
  742. {
  743. WARNING1("HmgInsert failed hGetFreeHandle\n");
  744. //
  745. // decrement handle count in case of failure
  746. //
  747. if (!(flags & HMGR_MAKE_PUBLIC))
  748. {
  749. HmgDecProcessHandleCount(W32Pid);
  750. }
  751. }
  752. else
  753. {
  754. // WINBUG #345863 3-17-2001 jasonha Hold ghsemHmgr thru out insert
  755. ((ENTRYOBJ *) &(gpentHmgr[HmgIfromH(h)]))->vSetup((POBJ) pv, objt, (FSHORT) flags);
  756. ((OBJECT *) pv)->hHmgr = (HANDLE) h;
  757. }
  758. }
  759. else
  760. {
  761. WARNING1("HmgInsertObject failed due to handle quota\n");
  762. }
  763. }
  764. return(h);
  765. }
  766. /******************************Public*Routine******************************\
  767. * HmgRemoveObject
  768. *
  769. * Removes an object from the handle table if certain conditions are met.
  770. *
  771. * History:
  772. * 13-Oct-1993 -by- Patrick Haluptzok patrickh
  773. * Wrote it.
  774. \**************************************************************************/
  775. PVOID
  776. HmgRemoveObject(
  777. HOBJ hobj,
  778. LONG cExclusiveLock,
  779. LONG cShareLock,
  780. BOOL bIgnoreUndeletable,
  781. OBJTYPE objt)
  782. {
  783. POBJ pobj;
  784. UINT uiIndex = (UINT) HmgIfromH(hobj);
  785. if (uiIndex < gcMaxHmgr)
  786. {
  787. //
  788. // Must acquire hmgr lock before handle lock
  789. //
  790. GreAcquireHmgrSemaphore();
  791. //
  792. // lock handle
  793. //
  794. PENTRY pentTmp = &gpentHmgr[uiIndex];
  795. HANDLELOCK HandleLock(pentTmp,TRUE);
  796. if (HandleLock.bValid())
  797. {
  798. //
  799. // verify objt and unique
  800. //
  801. if ((pentTmp->Objt == objt) &&
  802. (pentTmp->FullUnique == HmgUfromH(hobj)))
  803. {
  804. pobj = pentTmp->einfo.pobj;
  805. if ((pobj->cExclusiveLock == (USHORT)cExclusiveLock) &&
  806. (pobj->ulShareCount == (ULONG)cShareLock))
  807. {
  808. if (bIgnoreUndeletable || (!(pentTmp->Flags & HMGR_ENTRY_UNDELETABLE)))
  809. {
  810. //
  811. // The undeletable flag is not set or else we are ignoring it.
  812. //
  813. #if GDI_PERF
  814. HmgCurrentNumberOfHandles[objt]--;
  815. #endif
  816. //
  817. // set the handle in the object to NULL
  818. // to prevent/catch accidental decrement of the
  819. // shared reference count
  820. //
  821. pobj->hHmgr = NULL;
  822. //
  823. // free the handle
  824. //
  825. ((ENTRYOBJ *) pentTmp)->vFree(uiIndex);
  826. }
  827. else
  828. {
  829. WARNING1("HmgRemove failed object is undeletable\n");
  830. pobj = NULL;
  831. }
  832. }
  833. else
  834. {
  835. //
  836. // object is busy
  837. //
  838. // WARNING1("HmgRemove failed - object busy elsewhere\n");
  839. pobj = NULL;
  840. }
  841. }
  842. else
  843. {
  844. WARNING1("HmgRemove: bad objt or unique\n");
  845. pobj = NULL;
  846. }
  847. HandleLock.vUnlock();
  848. }
  849. else
  850. {
  851. WARNING1("HmgRemove: failed to lock handle\n");
  852. pobj = NULL;
  853. }
  854. //
  855. // free hmgr lock
  856. //
  857. GreReleaseHmgrSemaphore();
  858. }
  859. else
  860. {
  861. WARNING1("HmgRemove failed invalid index\n");
  862. pobj = NULL;
  863. }
  864. return((PVOID)pobj);
  865. }
  866. /******************************Public*Routine******************************\
  867. * HmgReplace
  868. *
  869. * Change the object pointer. Note this is callable only under very precise
  870. * circumstances:
  871. *
  872. * 1> When the object is exclusively locked.
  873. *
  874. * History:
  875. * Tue 14-Dec-1993 -by- Patrick Haluptzok [patrickh]
  876. * Seperate out lock counts from object structure.
  877. *
  878. * 18-Oct-1993 -by- Eric Kutter [erick]
  879. * Wrote it.
  880. \**************************************************************************/
  881. POBJ HmgReplace(
  882. HOBJ hobj,
  883. POBJ pobjNew,
  884. FLONG flags,
  885. LONG cLock,
  886. OBJTYPE objt)
  887. {
  888. //
  889. // We assume everything is valid, that the handle being replaced is exclusively
  890. // locked and valid.
  891. //
  892. // Note that currently "exclusively locked" can mean that we call this under
  893. // OBJLOCK control which doesn't set the cExclusiveLock
  894. //
  895. POBJ pobj = NULL;
  896. UINT uiIndex = (UINT) HmgIfromH(hobj);
  897. ASSERTGDI(uiIndex != 0, "HmgReplace invalid handle 0");
  898. ASSERTGDI(uiIndex < gcMaxHmgr, "HmgReplace invalid handle");
  899. PENTRY pentTmp = &gpentHmgr[uiIndex];
  900. ASSERTGDI(pentTmp->Objt == objt, "HmgReplace invalid object type");
  901. ASSERTGDI(pentTmp->FullUnique == HmgUfromH(hobj), "HmgReplace invalid uniqueness");
  902. ASSERTGDI((OBJECTOWNER_PID(pentTmp->ObjectOwner) == OBJECT_OWNER_PUBLIC) ||
  903. (OBJECTOWNER_PID(pentTmp->ObjectOwner) == W32GetCurrentPID()), "HmgReplace invalid PID owner");
  904. HANDLELOCK HandleLock(pentTmp,TRUE);
  905. if (HandleLock.bValid())
  906. {
  907. //
  908. // Return the old value. We have to be under the mutex here since the
  909. // counts live in the objects and if we do the switch while someone is
  910. // looking at the old object and the old object gets deleted after this
  911. // call before the original thread gets to run again he may fault or
  912. // wrongly succeed to lock it down.
  913. //
  914. pobj = pentTmp->einfo.pobj;
  915. pentTmp->einfo.pobj = pobjNew;
  916. HandleLock.vUnlock();
  917. }
  918. return((POBJ) pobj);
  919. }
  920. /******************************Public*Routine******************************\
  921. * AllocateObject
  922. *
  923. * Allocates an object through a look a side buffer if possible else just
  924. * allocates out of the heap.
  925. *
  926. * History:
  927. * 14-Apr-1997 -by- Dan Almosnino danalm
  928. * Modify to use DaveC's S-Lists mechanism for Lookaside Lists
  929. * 12-Oct-1993 -by- Patrick Haluptzok patrickh
  930. * Based on DaveC's HmgAlloc look-aside code
  931. \**************************************************************************/
  932. //
  933. // This struct and the following union can be thrown away
  934. // when someone fixes the BASEOBJECT cExclusiveLock and BaseFlags sharing
  935. // the same DWORD.
  936. //
  937. struct SplitLockAndFlags {
  938. USHORT c_cExclusiveLock;
  939. USHORT c_BaseFlags;
  940. };
  941. union SplitOrCombinedLockAndFlags {
  942. SplitLockAndFlags S;
  943. ULONG W;
  944. };
  945. PVOID
  946. AllocateObject(
  947. ULONG cBytes,
  948. ULONG ulType,
  949. BOOL bZero)
  950. {
  951. PVOID pvReturn = NULL;
  952. ASSERTGDI(ulType != DEF_TYPE, "AllocateObject ulType is bad");
  953. ASSERTGDI(cBytes >= sizeof(BASEOBJECT), "AllocateObject cBytes is bad");
  954. //
  955. // Debug check to avoid assert in ExAllocatePool
  956. //
  957. #if DBG
  958. if (cBytes >= (PAGE_SIZE * 10000))
  959. {
  960. WARNING("AllocateObject: cBytes >= 10000 pages");
  961. return(NULL);
  962. }
  963. #endif
  964. //
  965. // If the object type has a lookaside list and the list contains a
  966. // free entry and the requested size is less than or equal to the
  967. // lookaside list size, then attempt to allocate memory from the
  968. // lookaside list.
  969. //
  970. #if !defined(_GDIPLUS_)
  971. if (laSize[ulType] >= (ULONG)cBytes)
  972. {
  973. if((pvReturn = ExAllocateFromPagedLookasideList(pHmgLookAsideList[ulType])) != NULL)
  974. {
  975. if (bZero)
  976. {
  977. RtlZeroMemory(pvReturn, (UINT) cBytes);
  978. }
  979. else
  980. {
  981. //
  982. // At least the BASEOBJECT should be initialized.
  983. //
  984. RtlZeroMemory(pvReturn, (UINT) sizeof(BASEOBJECT));
  985. }
  986. //
  987. // This code is really hideous.
  988. // This is one of a very few places where it is necessary to know
  989. // that the BaseFlags entry in BASEOBJECT is actually the upper
  990. // 16 bits of the DWORD containing the cExclusiveLock.
  991. // If the layout of BASEOBJECT ever changes separating the cExclusiveLock
  992. // and the BaseFlags then this code _WILL_ break.
  993. //
  994. // The InterlockedExchange is required because it inserts appropriate
  995. // memory barriers on Alpha preventing other processors from seeing
  996. // the write delayed.
  997. //
  998. // Using the InterlockedExchange here is probably not necessary
  999. // Before anyone else can access this data it has to be put in the
  1000. // Handle table and the code that does that should have the appropriate
  1001. // mb instruction. However, just to be safe...
  1002. //
  1003. SplitOrCombinedLockAndFlags M;
  1004. M.S.c_cExclusiveLock = ((BASEOBJECT *)pvReturn)->cExclusiveLock;
  1005. M.S.c_BaseFlags = ((BASEOBJECT *)pvReturn)->BaseFlags | HMGR_LOOKASIDE_ALLOC_FLAG;
  1006. InterlockedExchange( (LONG *)& (((BASEOBJECT *)pvReturn)->cExclusiveLock), M.W );
  1007. //
  1008. // This is the code without the InterlockedExchange.
  1009. // If BaseFlags is removed from the DWORD containing the cExclusiveLock, the code
  1010. // can be restored to this (provided all code that puts the object
  1011. // in the handle table uses the HANDLELOCK):
  1012. //
  1013. //((BASEOBJECT *)pvReturn)->BaseFlags |= HMGR_LOOKASIDE_ALLOC_FLAG; //Set Base Object Lookaside Flag
  1014. //
  1015. #if GDI_PERF
  1016. HmgNumberOfLookAsideHits[ulType] += 1;
  1017. HmgCurrentNumberOfLookAsideObjects[ulType] += 1;
  1018. if (HmgCurrentNumberOfLookAsideObjects[ulType] > HmgMaximumNumberOfLookAsideObjects[ulType])
  1019. {
  1020. HmgMaximumNumberOfLookAsideObjects[ulType] = HmgCurrentNumberOfLookAsideObjects[ulType];
  1021. }
  1022. #endif
  1023. }
  1024. }
  1025. #endif
  1026. if (pvReturn == NULL)
  1027. {
  1028. //
  1029. // The attempted allocation from Look-Aside failed
  1030. // (or the object is too big for the lookaside).
  1031. // Attempt to allocate the object from the heap.
  1032. //
  1033. ULONG ulTag = '00hG';
  1034. ulTag += ulType << 24;
  1035. // Inject the Session Id into the tag
  1036. // Sessions with Id's from 0 to 78 will use an ascii character
  1037. // from '0' to '~', respectively. Sessions with higher Id's will
  1038. // all have a ' '.
  1039. ULONG ulSessionId = PsGetCurrentProcessSessionId();
  1040. if (ulSessionId > '~' - '0')
  1041. {
  1042. ulSessionId = ' ' - '0';
  1043. }
  1044. ulTag += ulSessionId << 16;
  1045. //
  1046. // Note: not necessary to clear the HMGR_LOOKASIDE_ALLOC_FLAG
  1047. // in BaseFlags since the BASEOBJECT is always zero-initialized.
  1048. //
  1049. if (bZero)
  1050. {
  1051. pvReturn = PALLOCMEM(cBytes, ulTag);
  1052. }
  1053. else
  1054. {
  1055. pvReturn = PALLOCNOZ(cBytes, ulTag);
  1056. //
  1057. // At least the BASEOBJECT should be initialized.
  1058. //
  1059. if (pvReturn)
  1060. {
  1061. RtlZeroMemory(pvReturn, (UINT) sizeof(BASEOBJECT));
  1062. }
  1063. }
  1064. //
  1065. // If the allocation failed again, then set the extended
  1066. // error status and return an invalid handle.
  1067. //
  1068. if (!pvReturn)
  1069. {
  1070. KdPrint(("GDISRV:AllocateObject failed alloc of %lu bytes\n", (cBytes)));
  1071. SAVE_ERROR_CODE(ERROR_NOT_ENOUGH_MEMORY);
  1072. return NULL;
  1073. }
  1074. }
  1075. #if GDI_PERF
  1076. //
  1077. // Increment the object performance counters.
  1078. //
  1079. HmgCurrentNumberOfObjects[ulType] += 1;
  1080. HmgNumberOfObjectsAllocated[ulType] += 1;
  1081. if (HmgCurrentNumberOfObjects[ulType] > HmgMaximumNumberOfObjects[ulType])
  1082. {
  1083. HmgMaximumNumberOfObjects[ulType] = HmgCurrentNumberOfObjects[ulType];
  1084. }
  1085. #endif
  1086. return(pvReturn);
  1087. }
  1088. /******************************Public*Routine******************************\
  1089. * HmgAlloc
  1090. *
  1091. * Allocate an object from Handle Manager.
  1092. *
  1093. * WARNING:
  1094. * --------
  1095. *
  1096. * If the object is share-lockable via an API, you MUST use HmgInsertObject
  1097. * instead. If the object is only exclusive-lockable via an API, you MUST
  1098. * either use HmgInsertObject or specify HMGR_ALLOC_LOCK.
  1099. *
  1100. * (This is because if you use HmgAlloc, a malicious multi-threaded
  1101. * application could guess the handle and cause it to be dereferenced
  1102. * before you've finished initializing it, possibly causing an access
  1103. * violation.)
  1104. *
  1105. * History:
  1106. * 23-Sep-1991 -by- Patrick Haluptzok patrickh
  1107. * Rewrite to be flat, no memory management under semaphore.
  1108. *
  1109. * 08-Dec-1989 -by- Donald Sidoroff [donalds]
  1110. * Wrote it.
  1111. \**************************************************************************/
  1112. HOBJ
  1113. HmgAlloc(
  1114. ULONGSIZE_T cb,
  1115. OBJTYPE objt,
  1116. FSHORT fs) // fs can be a combination of the following:
  1117. // HMGR_NO_ZERO_INIT - Don't zero initialize
  1118. // HMGR_MAKE_PUBLIC - Allow object to be lockable by
  1119. // any process
  1120. // HMGR_ALLOC_LOCK - Do an HmgLock on the object and
  1121. // return a pointer instead of handle
  1122. // HMGR_ALLOC_ALT_LOCK - Do an HmgShareLock on the object
  1123. // and return a pointer instead of
  1124. // handle
  1125. {
  1126. HOBJ Handle;
  1127. PVOID pv;
  1128. ASSERTGDI(objt != (OBJTYPE) DEF_TYPE, "HmgAlloc objt is bad");
  1129. ASSERTGDI(cb >= 8, "ERROR hmgr writes in first 8 bytes");
  1130. //
  1131. // Allocate a pointer.
  1132. //
  1133. pv = AllocateObject(cb, (ULONG) objt, ((fs & HMGR_NO_ZERO_INIT) == 0));
  1134. if (pv != (PVOID) NULL)
  1135. {
  1136. BOOL bHandleQuota = TRUE;
  1137. W32PID W32Pid = W32GetCurrentPID();
  1138. #if defined(_WIN64)
  1139. PW32THREAD pW32Thread = W32GetCurrentThread();
  1140. PRINTCLIENTID *pClientID = pW32Thread ? (PRINTCLIENTID*)pW32Thread->pClientID : 0;
  1141. W32Pid = pClientID ? pClientID->clientPid : W32Pid;
  1142. #endif
  1143. //
  1144. // Allocate a handle. We need the mutex to access the free list
  1145. //
  1146. GreAcquireHmgrSemaphore();
  1147. if (!(fs & HMGR_MAKE_PUBLIC))
  1148. {
  1149. //
  1150. // increment handle quota on non public objects. DC objects
  1151. // can be allocated over a process quota limit.
  1152. //
  1153. bHandleQuota = HmgIncProcessHandleCount(W32Pid, objt);
  1154. }
  1155. if (bHandleQuota)
  1156. {
  1157. Handle = hGetFreeHandle(objt);
  1158. if (Handle != (HOBJ) 0)
  1159. {
  1160. //
  1161. // Store a pointer to the object in the entry corresponding to the
  1162. // allocated handle and initialize the handle data.
  1163. //
  1164. ((ENTRYOBJ *) &(gpentHmgr[HmgIfromH(Handle)]))->vSetup((POBJ) pv, objt, fs);
  1165. //
  1166. // Store the object handle at the beginning of the object memory.
  1167. //
  1168. ((OBJECT *)pv)->hHmgr = (HANDLE)Handle;
  1169. GreReleaseHmgrSemaphore();
  1170. return ((fs & (HMGR_ALLOC_ALT_LOCK | HMGR_ALLOC_LOCK))
  1171. ? (HOBJ)pv
  1172. : Handle);
  1173. }
  1174. else
  1175. {
  1176. //
  1177. // decrement process handle count if not public
  1178. // (while lock is still held)
  1179. //
  1180. if (!(fs & HMGR_MAKE_PUBLIC))
  1181. {
  1182. HmgDecProcessHandleCount(W32Pid);
  1183. }
  1184. //
  1185. // We just failed a handle allocation. Release the memory.
  1186. //
  1187. GreReleaseHmgrSemaphore();
  1188. FreeObject(pv,(ULONG) objt);
  1189. }
  1190. }
  1191. else
  1192. {
  1193. WARNING("Failed HmgAlloc due to handle quota\n");
  1194. GreReleaseHmgrSemaphore();
  1195. FreeObject(pv,(ULONG) objt);
  1196. }
  1197. }
  1198. return((HOBJ) 0);
  1199. }
  1200. /******************************Public*Routine******************************\
  1201. * FreeObject
  1202. *
  1203. * Frees the object from where it was allocated.
  1204. *
  1205. * History:
  1206. *
  1207. * 14-Apr-1997 -by- Dan Almosnino danalm
  1208. * Modified to use DaveC's S-List mechanism for Lookaside lists
  1209. *
  1210. * 12-Oct-1993 -by- Patrick Haluptzok patrickh
  1211. * Wrote it.
  1212. \**************************************************************************/
  1213. VOID FreeObject(PVOID pvFree, ULONG ulType)
  1214. {
  1215. #if GDI_PERF
  1216. HmgCurrentNumberOfObjects[ulType] -= 1;
  1217. #endif
  1218. // Find if the object originates from a lookaside allocation,
  1219. // if so, return it to the free list.
  1220. if( ((BASEOBJECT *)pvFree)->BaseFlags & HMGR_LOOKASIDE_ALLOC_FLAG )
  1221. {
  1222. #if !defined(_GDIPLUS_)
  1223. ExFreeToPagedLookasideList(pHmgLookAsideList[ulType],pvFree);
  1224. #endif
  1225. #if GDI_PERF
  1226. HmgCurrentNumberOfLookAsideObjects[ulType] -= 1;
  1227. #endif
  1228. }
  1229. else
  1230. {
  1231. // If the object memory was allocated from the general heap, then
  1232. // release the memory to the heap.
  1233. VFREEMEM(pvFree);
  1234. }
  1235. }
  1236. /******************************Public*Routine******************************\
  1237. * HmgFree
  1238. *
  1239. * Free an object from the handle manager.
  1240. *
  1241. * History:
  1242. * 23-Sep-1991 -by- Patrick Haluptzok patrickh
  1243. * Rewrite to be flat, no memory management under semaphore.
  1244. *
  1245. * 08-Dec-1989 -by- Donald Sidoroff [donalds]
  1246. * Wrote it.
  1247. \**************************************************************************/
  1248. VOID HmgFree(HOBJ hobj)
  1249. {
  1250. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1251. POBJ pobjTmp;
  1252. OBJTYPE objtTmp;
  1253. ASSERTGDI(uiIndex != 0, "ERROR HmgFree invalid 0 handle");
  1254. if (uiIndex < gcMaxHmgr)
  1255. {
  1256. PENTRY pentTmp = &gpentHmgr[uiIndex];
  1257. //
  1258. // Acquire the handle manager lock and decrement the count of objects of
  1259. // the specfied type.
  1260. //
  1261. GreAcquireHmgrSemaphore();
  1262. //
  1263. // lock handle
  1264. //
  1265. HANDLELOCK HandleLock(pentTmp,FALSE);
  1266. if (HandleLock.bValid())
  1267. {
  1268. #if GDI_PERF
  1269. HmgCurrentNumberOfHandles[pentTmp->Objt]--;
  1270. #endif
  1271. pobjTmp = pentTmp->einfo.pobj;
  1272. objtTmp = pentTmp->Objt;
  1273. //
  1274. // Free the object handle
  1275. //
  1276. ((ENTRYOBJ *) pentTmp)->vFree(uiIndex);
  1277. HandleLock.vUnlock();
  1278. }
  1279. GreReleaseHmgrSemaphore();
  1280. if (pobjTmp)
  1281. {
  1282. FreeObject((PVOID)pobjTmp, (ULONG) objtTmp);
  1283. }
  1284. }
  1285. else
  1286. {
  1287. WARNING1("HmgFree: bad handle index");
  1288. }
  1289. }
  1290. /******************************Public*Routine******************************\
  1291. * HmgSetOwner - set new object owner
  1292. *
  1293. * Arguments:
  1294. * hobj - handle of object
  1295. * objt - type of object
  1296. * usCurrent - OBJECT_OWNER flag for current owner
  1297. * pw32Current - PW32PROCESS of current owner if not current w32process
  1298. * usNew - OBJECT_OWNER flag for new owner
  1299. * pw32New - PW32PROCESS of new owner if not current w32process
  1300. *
  1301. * Return Value:
  1302. *
  1303. * Status
  1304. *
  1305. * History:
  1306. *
  1307. * 15-May-1996 -by- Mark Enstrom [marke]
  1308. *
  1309. \**************************************************************************/
  1310. BOOL
  1311. HmgSetOwner(
  1312. HOBJ hobj,
  1313. W32PID W32PidNew,
  1314. OBJTYPE objt
  1315. )
  1316. {
  1317. PENTRY pentTmp;
  1318. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1319. BOOL bStatus = FALSE;
  1320. W32PID W32PidCurrent;
  1321. #if defined(_WIN64)
  1322. KERNEL_PVOID pclientID;
  1323. PW32THREAD pW32thread;
  1324. BOOL bWOW64 = FALSE;
  1325. #endif
  1326. if (W32PidNew == OBJECT_OWNER_CURRENT)
  1327. {
  1328. W32PidNew = W32GetCurrentPID();
  1329. }
  1330. if (uiIndex < gcMaxHmgr)
  1331. {
  1332. pentTmp = &gpentHmgr[uiIndex];
  1333. HANDLELOCK HandleLock(pentTmp,FALSE);
  1334. if (HandleLock.bValid())
  1335. {
  1336. //
  1337. // verify objt and unique
  1338. //
  1339. if ((pentTmp->Objt == objt) && (pentTmp->FullUnique == HmgUfromH(hobj)))
  1340. {
  1341. POBJ pobj = pentTmp->einfo.pobj;
  1342. #if defined(_WIN64)
  1343. pW32thread = W32GetCurrentThread();
  1344. //
  1345. // Set the handle owner pid to the wow64 print client pid if
  1346. // the the PRINTCLIENTID pointer in W32TREAD is non zero.
  1347. // In this case, we are doing WOW64 printing via LPC.
  1348. //
  1349. pclientID = pW32thread ? pW32thread->pClientID : 0;
  1350. if(pclientID)
  1351. bWOW64 = TRUE;
  1352. #endif
  1353. if ((pobj->cExclusiveLock == 0) ||
  1354. (pobj->Tid == (PW32THREAD)PsGetCurrentThread())
  1355. #if defined(_WIN64)
  1356. ||
  1357. (bWOW64)
  1358. #endif
  1359. )
  1360. {
  1361. bStatus = TRUE;
  1362. //
  1363. // determine current W32PID
  1364. //
  1365. W32PidCurrent = HandleLock.Pid();
  1366. #if defined(_WIN64)
  1367. if (bWOW64)
  1368. {
  1369. W32PidNew = ((PRINTCLIENTID*)pclientID)->clientPid;
  1370. }
  1371. #endif
  1372. if (W32PidCurrent != W32PidNew)
  1373. {
  1374. bStatus = HmgIncProcessHandleCount(W32PidNew,objt);
  1375. if (bStatus)
  1376. {
  1377. HmgDecProcessHandleCount(W32PidCurrent);
  1378. //
  1379. // set new owner
  1380. //
  1381. HandleLock.Pid(W32PidNew);
  1382. }
  1383. else
  1384. {
  1385. WARNING1("HmgSetOwner: Failed to set owner due to handle quota\n");
  1386. }
  1387. }
  1388. }
  1389. else
  1390. {
  1391. WARNING1("HmgSetOwner, Object is exclusively locked\n");
  1392. }
  1393. }
  1394. else
  1395. {
  1396. WARNING1("HmgSetOwner, object type or unique mismach\n");
  1397. }
  1398. HandleLock.vUnlock();
  1399. }
  1400. }
  1401. else
  1402. {
  1403. WARNING1("HmgSetOwner: bad index\n");
  1404. }
  1405. return(bStatus);
  1406. }
  1407. /*****************************Exported*Routine*****************************\
  1408. * GreGetObjectOwner
  1409. *
  1410. * Get the owner of an object
  1411. *
  1412. * Arguments:
  1413. *
  1414. * hobj - handle to object
  1415. * objt - handle type for verification
  1416. *
  1417. * History:
  1418. *
  1419. * 27-Apr-1994 -by- Johnc
  1420. * Dupped from SetOwner.
  1421. \**************************************************************************/
  1422. W32PID
  1423. GreGetObjectOwner(
  1424. HOBJ hobj,
  1425. DWORD objt)
  1426. {
  1427. W32PID pid = OBJECT_OWNER_ERROR;
  1428. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1429. if (uiIndex < gcMaxHmgr)
  1430. {
  1431. PENTRY pentTmp = &gpentHmgr[uiIndex];
  1432. if ((pentTmp->Objt == objt) && (pentTmp->FullUnique == HmgUfromH(hobj)))
  1433. {
  1434. pid = OBJECTOWNER_PID(pentTmp->ObjectOwner);
  1435. }
  1436. else
  1437. {
  1438. WARNING1("GreGetObjectOwner failed - object type of unique mismatch\n");
  1439. }
  1440. }
  1441. else
  1442. {
  1443. WARNING1("GreGetObjectOwner failed - invalid handle index\n");
  1444. }
  1445. return(pid);
  1446. }
  1447. /******************************Public*Routine******************************\
  1448. *
  1449. * HmgSwapHandleContents locks both handles and verifies lock counts.
  1450. * The handle contents for each element are then swapped.
  1451. *
  1452. * Arguments:
  1453. *
  1454. * hobj1 - handle to object 1
  1455. * cShare1 - share count object 1 must have after handle is locked
  1456. * hobj2 - handle to object 2
  1457. * cShare1 - share count object 2 must have after handle is locked
  1458. * objt - type that both handles must be
  1459. *
  1460. * Return Value:
  1461. *
  1462. * BOOL status
  1463. *
  1464. * History:
  1465. *
  1466. * 24-Jul-1995 -by- Mark Enstrom [marke]
  1467. *
  1468. \**************************************************************************/
  1469. BOOL
  1470. HmgSwapHandleContents(
  1471. HOBJ hobj1,
  1472. ULONG cShare1,
  1473. HOBJ hobj2,
  1474. ULONG cShare2,
  1475. OBJTYPE objt)
  1476. {
  1477. //
  1478. // acquire hmgr resource for this operation to
  1479. // make sure we don't deadlock with two threads
  1480. // trying to swap the same handles in reverse
  1481. // order
  1482. //
  1483. MLOCKFAST mo;
  1484. return(
  1485. HmgSwapLockedHandleContents(
  1486. hobj1,
  1487. cShare1,
  1488. hobj2,
  1489. cShare2,
  1490. objt)
  1491. );
  1492. }
  1493. /******************************Public*Routine******************************\
  1494. *
  1495. * HmgSwapLockedHandleContents locks both handles and verifies lock counts.
  1496. * The handle contents for each element are then swapped. HmgrResource must
  1497. * be owned to prevent deadlock.
  1498. *
  1499. * Arguments:
  1500. *
  1501. * hobj1 - handle to object 1
  1502. * cShare1 - share count object 1 must have after handle is locked
  1503. * hobj2 - handle to object 2
  1504. * cShare1 - share count object 2 must have after handle is locked
  1505. * objt - type that both handles must be
  1506. *
  1507. * Return Value:
  1508. *
  1509. * BOOL status
  1510. *
  1511. * History:
  1512. *
  1513. * 24-Jul-1995 -by- Mark Enstrom [marke]
  1514. *
  1515. \**************************************************************************/
  1516. BOOL
  1517. HmgSwapLockedHandleContents(
  1518. HOBJ hobj1,
  1519. ULONG cShare1,
  1520. HOBJ hobj2,
  1521. ULONG cShare2,
  1522. OBJTYPE objt)
  1523. {
  1524. UINT uiIndex1 = (UINT) HmgIfromH(hobj1);
  1525. UINT uiIndex2 = (UINT) HmgIfromH(hobj2);
  1526. BOOL bResult = FALSE;
  1527. ASSERTGDI(objt != (OBJTYPE) DEF_TYPE, "Bad type");
  1528. //
  1529. // Acquire handle lock on both objects
  1530. //
  1531. PENTRY pentry1 = &gpentHmgr[uiIndex1];
  1532. PENTRY pentry2 = &gpentHmgr[uiIndex2];
  1533. HANDLELOCK HandleLock1(pentry1,FALSE);
  1534. if (HandleLock1.bValid())
  1535. {
  1536. HANDLELOCK HandleLock2(pentry2,FALSE);
  1537. if (HandleLock2.bValid())
  1538. {
  1539. //
  1540. // Verify share lock counts and object types
  1541. //
  1542. if ((HandleLock1.ShareCount() == (USHORT)cShare1) && (pentry1->Objt == objt))
  1543. {
  1544. if ((HandleLock2.ShareCount() == (USHORT)cShare2) && (pentry2->Objt == objt))
  1545. {
  1546. POBJ pobjTemp;
  1547. PVOID pvTmp;
  1548. //
  1549. // swap pobj in handle table
  1550. //
  1551. pobjTemp = pentry1->einfo.pobj;
  1552. pentry1->einfo.pobj = pentry2->einfo.pobj;
  1553. pentry2->einfo.pobj = pobjTemp;
  1554. //
  1555. // swap puser in handle table
  1556. //
  1557. pvTmp = pentry1->pUser;
  1558. pentry1->pUser = pentry2->pUser;
  1559. pentry2->pUser = pvTmp;
  1560. //
  1561. // swap BASEOBJECTS
  1562. //
  1563. BASEOBJECT obj;
  1564. obj = *pentry1->einfo.pobj;
  1565. *pentry1->einfo.pobj = *pentry2->einfo.pobj;
  1566. *pentry2->einfo.pobj = obj;
  1567. //
  1568. // unswap the BASEOBJECT.BaseFlags
  1569. //
  1570. // The BaseFlags field contains information about how
  1571. // the object was allocated (and therefore, how it must
  1572. // be deallocated). Thus, it represents state associated
  1573. // with the actual memory, not the object, and should not
  1574. // be swapped.
  1575. //
  1576. // See comments regarding BASEOBJECT in inc\hmgshare.h
  1577. // for more details. Also, there is swapping code in
  1578. // RGNOBJ::bSwap (rgnobj.cxx).
  1579. //
  1580. USHORT usTemp;
  1581. usTemp = pentry1->einfo.pobj->BaseFlags;
  1582. pentry1->einfo.pobj->BaseFlags = pentry2->einfo.pobj->BaseFlags;
  1583. pentry2->einfo.pobj->BaseFlags = usTemp;
  1584. bResult = TRUE;
  1585. }
  1586. else
  1587. {
  1588. WARNING1("HmgSwapHandleContents: wrong share count or objt for hobj2");
  1589. }
  1590. }
  1591. else
  1592. {
  1593. WARNING1("HmgSwapHandleContents: wrong share count or objt for hobj1");
  1594. }
  1595. HandleLock2.vUnlock();
  1596. }
  1597. HandleLock1.vUnlock();
  1598. }
  1599. return(bResult);
  1600. }
  1601. /*****************************Exported*Routine*****************************\
  1602. * HOBJ HmgNextOwned(hmgr, hobj, pid)
  1603. *
  1604. * Report the next object owned by specified process
  1605. *
  1606. * History:
  1607. * 08-Dec-1989 -by- Donald Sidoroff [donalds]
  1608. * Wrote it.
  1609. \**************************************************************************/
  1610. HOBJ
  1611. FASTCALL
  1612. HmgNextOwned(
  1613. HOBJ hobj,
  1614. W32PID pid)
  1615. {
  1616. MLOCKFAST mo;
  1617. return(HmgSafeNextOwned(hobj, pid));
  1618. }
  1619. /*****************************Exported*Routine*****************************\
  1620. * HOBJ HmgSafeNextOwned
  1621. *
  1622. * Report the next object owned by specified process
  1623. *
  1624. * History:
  1625. * Sat 11-Dec-1993 -by- Patrick Haluptzok [patrickh]
  1626. * Remove function wrapper.
  1627. *
  1628. * 08-Dec-1989 -by- Donald Sidoroff [donalds]
  1629. * Wrote it.
  1630. \**************************************************************************/
  1631. HOBJ
  1632. FASTCALL
  1633. HmgSafeNextOwned(
  1634. HOBJ hobj,
  1635. W32PID pid)
  1636. {
  1637. PENTRYOBJ pentTmp;
  1638. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1639. //
  1640. // If we are passed 0 we inc to 1 because 0 can never be valid.
  1641. // If we are passed != 0 we inc 1 to find the next one valid.
  1642. //
  1643. uiIndex++;
  1644. while (uiIndex < gcMaxHmgr)
  1645. {
  1646. pentTmp = (PENTRYOBJ) &gpentHmgr[uiIndex];
  1647. if (pentTmp->bOwnedBy(pid))
  1648. {
  1649. return((HOBJ) MAKE_HMGR_HANDLE(uiIndex, pentTmp->FullUnique));
  1650. }
  1651. //
  1652. // Advance to next object
  1653. //
  1654. uiIndex++;
  1655. }
  1656. //
  1657. // No objects found
  1658. //
  1659. return((HOBJ) 0);
  1660. }
  1661. /*****************************Exported*Routine*****************************\
  1662. * HOBJ HmgSafeNextObjt
  1663. *
  1664. * Report the next object of a certain type.
  1665. *
  1666. * History:
  1667. * Tue 19-Apr-1994 -by- Patrick Haluptzok [patrickh]
  1668. * Wrote it.
  1669. \**************************************************************************/
  1670. POBJ
  1671. FASTCALL
  1672. HmgSafeNextObjt(
  1673. HOBJ hobj,
  1674. OBJTYPE objt)
  1675. {
  1676. PENTRYOBJ pentTmp;
  1677. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1678. //
  1679. // If we are passed 0 we inc to 1 because 0 can never be valid.
  1680. // If we are passed != 0 we inc 1 to find the next one valid.
  1681. //
  1682. uiIndex++;
  1683. while (uiIndex < gcMaxHmgr)
  1684. {
  1685. pentTmp = (PENTRYOBJ) &gpentHmgr[uiIndex];
  1686. if (pentTmp->Objt == objt)
  1687. {
  1688. return(pentTmp->einfo.pobj);
  1689. }
  1690. //
  1691. // Advance to next object
  1692. //
  1693. uiIndex++;
  1694. }
  1695. //
  1696. // no objects found
  1697. //
  1698. return((POBJ) 0);
  1699. }
  1700. /*******************************Routine************************************\
  1701. * HmgLockAllOwners
  1702. *
  1703. * Description:
  1704. *
  1705. * Acquire an exclusive lock on an object. Note that this routine is
  1706. * different than HmgLock in that the PID owner doesn't need to match the
  1707. * current PID. This is useful in GreRestoreDC where we need to get a lock
  1708. * on the saved DC in order to to copy its information to the primary DC, but
  1709. * don't want to fail if the process has already reached its handle quota
  1710. * because this saved DC is about to be deleted anyway. See bug #223129
  1711. * for details.
  1712. *
  1713. * WARNING: Do not call this routine outside of the above scenario unless
  1714. * there's a very good reason!
  1715. *
  1716. * Arguments:
  1717. *
  1718. * hobj - Handle to lock
  1719. * objt - Check to make sure handle is of expected type
  1720. *
  1721. * Return Value:
  1722. *
  1723. * Pointer to object or NULL
  1724. *
  1725. \**************************************************************************/
  1726. POBJ
  1727. FASTCALL
  1728. HmgLockAllOwners(
  1729. HOBJ hobj,
  1730. OBJTYPE objt
  1731. )
  1732. {
  1733. POBJ pobj = (POBJ)NULL;
  1734. UINT uiIndex = (UINT)HmgIfromH(hobj);
  1735. if (uiIndex < gcMaxHmgr)
  1736. {
  1737. PENTRY pentry = &gpentHmgr[uiIndex];
  1738. //
  1739. // Passing FALSE as the second argument to the HANDLELOCK constructor
  1740. // allows us to obtain a lock regardless of the PID owner of the object.
  1741. //
  1742. HANDLELOCK HandleLock(pentry,FALSE);
  1743. if (HandleLock.bValid())
  1744. {
  1745. if ((pentry->Objt != objt) || (pentry->FullUnique != HmgUfromH(hobj)))
  1746. {
  1747. HmgPrintBadHandle(hobj, objt);
  1748. }
  1749. else
  1750. {
  1751. pobj = pentry->einfo.pobj;
  1752. if ((pobj->cExclusiveLock == 0) || (pobj->Tid == (PW32THREAD)PsGetCurrentThread()))
  1753. {
  1754. pobj->cExclusiveLock++;
  1755. pobj->Tid = (PW32THREAD)PsGetCurrentThread();
  1756. }
  1757. else
  1758. {
  1759. WARNING1("HmgLock: object already locked by another thread");
  1760. pobj = (POBJ)NULL;
  1761. }
  1762. }
  1763. HandleLock.vUnlock();
  1764. }
  1765. else
  1766. {
  1767. HmgPrintBadHandle(hobj, objt);
  1768. }
  1769. }
  1770. else
  1771. {
  1772. HmgPrintBadHandle(hobj, objt);
  1773. }
  1774. return(pobj);
  1775. }
  1776. #if !defined(_X86_) || defined(_GDIPLUS_)
  1777. /*******************************Routine************************************\
  1778. * HmgLock
  1779. *
  1780. * Description:
  1781. *
  1782. * Acquire an exclusive lock on an object, PID owner must match current PID
  1783. * or be a public.
  1784. *
  1785. * Arguments:
  1786. *
  1787. * hobj - Handle to lock
  1788. * objt - Check to make sure handle is of expected type
  1789. *
  1790. * Return Value:
  1791. *
  1792. * Pointer to object or NULL
  1793. *
  1794. \**************************************************************************/
  1795. POBJ
  1796. FASTCALL
  1797. HmgLock(
  1798. HOBJ hobj,
  1799. OBJTYPE objt
  1800. )
  1801. {
  1802. POBJ pobj = (POBJ)NULL;
  1803. UINT uiIndex = (UINT)HmgIfromH(hobj);
  1804. if (uiIndex < gcMaxHmgr)
  1805. {
  1806. PENTRY pentry = &gpentHmgr[uiIndex];
  1807. HANDLELOCK HandleLock(pentry,TRUE);
  1808. if (HandleLock.bValid())
  1809. {
  1810. if ((pentry->Objt != objt) || (pentry->FullUnique != HmgUfromH(hobj)))
  1811. {
  1812. HmgPrintBadHandle(hobj, objt);
  1813. }
  1814. else
  1815. {
  1816. pobj = pentry->einfo.pobj;
  1817. if ((pobj->cExclusiveLock == 0) || (pobj->Tid == (PW32THREAD)PsGetCurrentThread()))
  1818. {
  1819. pobj->cExclusiveLock++;
  1820. pobj->Tid = (PW32THREAD)PsGetCurrentThread();
  1821. }
  1822. else
  1823. {
  1824. WARNING1("HmgLock: object already locked by another thread");
  1825. pobj = (POBJ)NULL;
  1826. }
  1827. }
  1828. HandleLock.vUnlock();
  1829. }
  1830. else
  1831. {
  1832. HmgPrintBadHandle(hobj, objt);
  1833. }
  1834. }
  1835. else
  1836. {
  1837. HmgPrintBadHandle(hobj, objt);
  1838. }
  1839. return(pobj);
  1840. }
  1841. /*******************************Routine************************************\
  1842. * HmgShareCheckLock
  1843. *
  1844. * Description:
  1845. *
  1846. * Acquire a share lock on an object, PID owner must match current PID
  1847. * or be a public.
  1848. *
  1849. * Arguments:
  1850. *
  1851. * hobj - Handle to lock
  1852. * objt - Check to make sure handle is of expected type
  1853. *
  1854. * Return Value:
  1855. *
  1856. * Pointer to object or NULL
  1857. *
  1858. \**************************************************************************/
  1859. POBJ
  1860. FASTCALL
  1861. HmgShareCheckLock(
  1862. HOBJ hobj,
  1863. OBJTYPE objt
  1864. )
  1865. {
  1866. POBJ pobj = (POBJ)NULL;
  1867. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1868. if (uiIndex < gcMaxHmgr)
  1869. {
  1870. PENTRY pentry = &gpentHmgr[uiIndex];
  1871. HANDLELOCK HandleLock(pentry,TRUE);
  1872. if (HandleLock.bValid())
  1873. {
  1874. if ((pentry->Objt == objt) && (pentry->FullUnique == HmgUfromH(hobj)))
  1875. {
  1876. pobj = pentry->einfo.pobj;
  1877. pobj->ulShareCount++;
  1878. }
  1879. else
  1880. {
  1881. HmgPrintBadHandle(hobj, objt);
  1882. }
  1883. HandleLock.vUnlock();
  1884. }
  1885. else
  1886. {
  1887. HmgPrintBadHandle(hobj, objt);
  1888. }
  1889. }
  1890. else
  1891. {
  1892. HmgPrintBadHandle(hobj, objt);
  1893. }
  1894. return(pobj);
  1895. }
  1896. /*******************************Routine************************************\
  1897. * HmgShareLock
  1898. *
  1899. * Description:
  1900. *
  1901. * Acquire a share lock on an object, don't check PID owner
  1902. *
  1903. * Arguments:
  1904. *
  1905. * hobj - Handle to lock
  1906. * objt - Check to make sure handle is of expected type
  1907. *
  1908. * Return Value:
  1909. *
  1910. * Pointer to object or NULL
  1911. *
  1912. \**************************************************************************/
  1913. POBJ FASTCALL
  1914. HmgShareLock(
  1915. HOBJ hobj,
  1916. OBJTYPE objt
  1917. )
  1918. {
  1919. POBJ pobj = (POBJ)NULL;
  1920. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1921. if (uiIndex < gcMaxHmgr)
  1922. {
  1923. PENTRY pentry = &gpentHmgr[uiIndex];
  1924. HANDLELOCK HandleLock(pentry,FALSE);
  1925. if (HandleLock.bValid())
  1926. {
  1927. if ((pentry->Objt == objt) && (pentry->FullUnique == HmgUfromH(hobj)))
  1928. {
  1929. pobj = pentry->einfo.pobj;
  1930. pobj->ulShareCount++;
  1931. }
  1932. else
  1933. {
  1934. HmgPrintBadHandle(hobj, objt);
  1935. }
  1936. HandleLock.vUnlock();
  1937. }
  1938. else
  1939. {
  1940. HmgPrintBadHandle(hobj, objt);
  1941. }
  1942. }
  1943. else
  1944. {
  1945. HmgPrintBadHandle(hobj, objt);
  1946. }
  1947. return(pobj);
  1948. }
  1949. #endif
  1950. /*******************************Routine************************************\
  1951. * HmgShareCheckLockIgnoreStockBit
  1952. *
  1953. * Description:
  1954. *
  1955. * Acquire a share lock on an object, PID owner must match current PID
  1956. * or be a public. We ignore the stock bit.
  1957. *
  1958. * Arguments:
  1959. *
  1960. * hobj - Handle to lock
  1961. * objt - Check to make sure handle is of expected type
  1962. *
  1963. * Return Value:
  1964. *
  1965. * Pointer to object or NULL
  1966. *
  1967. \**************************************************************************/
  1968. POBJ
  1969. FASTCALL
  1970. HmgShareCheckLockIgnoreStockBit(
  1971. HOBJ hobj,
  1972. OBJTYPE objt
  1973. )
  1974. {
  1975. POBJ pobj = (POBJ)NULL;
  1976. UINT uiIndex = (UINT) HmgIfromH(hobj);
  1977. if (uiIndex < gcMaxHmgr)
  1978. {
  1979. PENTRY pentry = &gpentHmgr[uiIndex];
  1980. HANDLELOCK HandleLock(pentry,TRUE);
  1981. if (HandleLock.bValid())
  1982. {
  1983. if ((pentry->Objt == objt) &&
  1984. ((pentry->FullUnique & ~FULLUNIQUE_STOCK_MASK) ==
  1985. (HmgUfromH(hobj) & ~FULLUNIQUE_STOCK_MASK)))
  1986. {
  1987. pobj = pentry->einfo.pobj;
  1988. pobj->ulShareCount++;
  1989. }
  1990. else
  1991. {
  1992. HmgPrintBadHandle(hobj, objt);
  1993. }
  1994. HandleLock.vUnlock();
  1995. }
  1996. else
  1997. {
  1998. HmgPrintBadHandle(hobj, objt);
  1999. }
  2000. }
  2001. else
  2002. {
  2003. HmgPrintBadHandle(hobj, objt);
  2004. }
  2005. return(pobj);
  2006. }
  2007. /*******************************Routine************************************\
  2008. * HmgShareLockIgnoreStockBit
  2009. *
  2010. * Description:
  2011. *
  2012. * Acquire a share lock on an object, don't check PID owner. Ignore stock
  2013. * bit.
  2014. *
  2015. * Arguments:
  2016. *
  2017. * hobj - Handle to lock
  2018. * objt - Check to make sure handle is of expected type
  2019. *
  2020. * Return Value:
  2021. *
  2022. * Pointer to object or NULL
  2023. *
  2024. \**************************************************************************/
  2025. POBJ FASTCALL
  2026. HmgShareLockIgnoreStockBit(
  2027. HOBJ hobj,
  2028. OBJTYPE objt
  2029. )
  2030. {
  2031. POBJ pobj = (POBJ)NULL;
  2032. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2033. if (uiIndex < gcMaxHmgr)
  2034. {
  2035. PENTRY pentry = &gpentHmgr[uiIndex];
  2036. HANDLELOCK HandleLock(pentry,FALSE);
  2037. if (HandleLock.bValid())
  2038. {
  2039. if ((pentry->Objt == objt) &&
  2040. ((pentry->FullUnique & ~FULLUNIQUE_STOCK_MASK) ==
  2041. (HmgUfromH(hobj) & ~FULLUNIQUE_STOCK_MASK)))
  2042. {
  2043. pobj = pentry->einfo.pobj;
  2044. pobj->ulShareCount++;
  2045. }
  2046. else
  2047. {
  2048. HmgPrintBadHandle(hobj, objt);
  2049. }
  2050. HandleLock.vUnlock();
  2051. }
  2052. else
  2053. {
  2054. HmgPrintBadHandle(hobj, objt);
  2055. }
  2056. }
  2057. else
  2058. {
  2059. HmgPrintBadHandle(hobj, objt);
  2060. }
  2061. return(pobj);
  2062. }
  2063. /******************************Public*Routine******************************\
  2064. * HmgShareUnlock
  2065. *
  2066. * Make this a macro once it is debugged
  2067. *
  2068. * Arguments:
  2069. *
  2070. * pobj - pointer to share-locked object
  2071. *
  2072. * Return Value:
  2073. *
  2074. * None
  2075. *
  2076. \**************************************************************************/
  2077. VOID
  2078. HmgShareUnlock(
  2079. POBJ pobj
  2080. )
  2081. {
  2082. PENTRY pentry;
  2083. //
  2084. // decrement shared reference count to object, Handle checks
  2085. // are not done.
  2086. //
  2087. UINT uiIndex = (UINT) HmgIfromH(pobj->hHmgr);
  2088. pentry = &gpentHmgr[uiIndex];
  2089. HANDLELOCK HandleLock(pentry,FALSE);
  2090. if (HandleLock.bValid())
  2091. {
  2092. //
  2093. // decrement use count
  2094. //
  2095. pobj->ulShareCount--;
  2096. HandleLock.vUnlock();
  2097. }
  2098. }
  2099. /*******************************Routine************************************\
  2100. * HmgReferenceCheckLock
  2101. *
  2102. * Description:
  2103. *
  2104. * The routine validates the hanlde passed in and returns a pointer to
  2105. * the object. This routine must only be called when the HmgrReource is
  2106. * held and the pointer to the object is only valid while the Resource
  2107. * is held. No unlock is neccessary since the object is not reference-
  2108. * counted.
  2109. *
  2110. * Arguments:
  2111. *
  2112. * hobj - Handle to lock
  2113. * objt - Check to make sure handle is of expected type
  2114. *
  2115. * Return Value:
  2116. *
  2117. * Pointer to object or NULL
  2118. *
  2119. \**************************************************************************/
  2120. POBJ
  2121. FASTCALL
  2122. HmgReferenceCheckLock(
  2123. HOBJ hobj,
  2124. OBJTYPE objt,
  2125. BOOL bDebugPrint
  2126. )
  2127. {
  2128. PENTRY pentry;
  2129. POBJ pobj = (POBJ)NULL;
  2130. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2131. if (uiIndex < gcMaxHmgr)
  2132. {
  2133. pentry = &gpentHmgr[uiIndex];
  2134. if ((pentry->Objt == objt) && (pentry->FullUnique == HmgUfromH(hobj)))
  2135. {
  2136. pobj = pentry->einfo.pobj;
  2137. }
  2138. else
  2139. {
  2140. if (bDebugPrint)
  2141. {
  2142. HmgPrintBadHandle(hobj, objt);
  2143. }
  2144. }
  2145. }
  2146. else
  2147. {
  2148. if (bDebugPrint)
  2149. {
  2150. HmgPrintBadHandle(hobj, objt);
  2151. }
  2152. }
  2153. return(pobj);
  2154. }
  2155. #if !defined(_X86_) || defined(_GDIPLUS_)
  2156. /******************************Public*Routine******************************\
  2157. * HmgIncrementShareReferenceCount
  2158. *
  2159. * interlocked increment shared reference count
  2160. *
  2161. * Arguments:
  2162. *
  2163. * pObj - pointer to valid object
  2164. *
  2165. * Return Value:
  2166. *
  2167. * none
  2168. *
  2169. * History:
  2170. *
  2171. * 6-Jun-1995 -by- Mark Enstrom [marke]
  2172. *
  2173. \**************************************************************************/
  2174. VOID
  2175. FASTCALL
  2176. HmgIncrementShareReferenceCount(
  2177. POBJ pObj
  2178. )
  2179. {
  2180. HANDLELOCK HandleLock(PENTRY_FROM_POBJ(pObj), FALSE);
  2181. if (HandleLock.bValid())
  2182. {
  2183. pObj->ulShareCount++;
  2184. HandleLock.vUnlock();
  2185. }
  2186. else
  2187. {
  2188. HmgPrintBadHandle((HOBJ)pObj->hHmgr, PENTRY_FROM_POBJ(pObj)->Objt);
  2189. }
  2190. }
  2191. /******************************Public*Routine******************************\
  2192. * HmgDecrementShareReferenceCount
  2193. *
  2194. * interlocked increment shared reference count
  2195. *
  2196. * Arguments:
  2197. *
  2198. * pObj - pointer to valid object
  2199. *
  2200. * Return Value:
  2201. *
  2202. * previous lock count
  2203. *
  2204. * History:
  2205. *
  2206. * 6-Jun-1995 -by- Mark Enstrom [marke]
  2207. *
  2208. \**************************************************************************/
  2209. ULONG
  2210. FASTCALL
  2211. HmgDecrementShareReferenceCount(
  2212. POBJ pObj
  2213. )
  2214. {
  2215. ULONG ulOrigShareCount=0;
  2216. HANDLELOCK HandleLock(PENTRY_FROM_POBJ(pObj), FALSE);
  2217. if (HandleLock.bValid())
  2218. {
  2219. ulOrigShareCount = pObj->ulShareCount;
  2220. pObj->ulShareCount--;
  2221. HandleLock.vUnlock();
  2222. }
  2223. else
  2224. {
  2225. HmgPrintBadHandle((HOBJ)pObj->hHmgr, PENTRY_FROM_POBJ(pObj)->Objt);
  2226. }
  2227. return ulOrigShareCount;
  2228. }
  2229. #endif
  2230. /******************************Public*Routine******************************\
  2231. * HmgMarkDeletable
  2232. *
  2233. * Mark an object as deletable.
  2234. *
  2235. * History:
  2236. * 11-Jun-1993 -by- Patrick Haluptzok patrickh
  2237. * Wrote it.
  2238. \**************************************************************************/
  2239. BOOL
  2240. FASTCALL
  2241. HmgMarkDeletable(
  2242. HOBJ hobj,
  2243. OBJTYPE objt)
  2244. {
  2245. PENTRY pentry;
  2246. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2247. BOOL bStatus = FALSE;
  2248. if (uiIndex < gcMaxHmgr)
  2249. {
  2250. pentry = &gpentHmgr[uiIndex];
  2251. HANDLELOCK HandleLock(pentry,FALSE);
  2252. if (HandleLock.bValid())
  2253. {
  2254. if ((pentry->Objt == objt) && (pentry->FullUnique == HmgUfromH(hobj)))
  2255. {
  2256. pentry->Flags &= ~HMGR_ENTRY_UNDELETABLE;
  2257. bStatus = TRUE;
  2258. }
  2259. else
  2260. {
  2261. WARNING1("HmgMarkDeletable: bad objt or unique");
  2262. }
  2263. HandleLock.vUnlock();
  2264. }
  2265. else
  2266. {
  2267. WARNING1("HmgMarkDeletable failed: not object owner\n");
  2268. }
  2269. }
  2270. else
  2271. {
  2272. WARNING1("HmgMarkDeletable failed: index out of range\n");
  2273. }
  2274. return(bStatus);
  2275. }
  2276. /******************************Public*Routine******************************\
  2277. * HmgMarkUndeletable
  2278. *
  2279. * Mark an object as undeletable.
  2280. *
  2281. * History:
  2282. * 11-Jun-1993 -by- Patrick Haluptzok patrickh
  2283. * Wrote it.
  2284. \**************************************************************************/
  2285. BOOL
  2286. FASTCALL
  2287. HmgMarkUndeletable(HOBJ hobj, OBJTYPE objt)
  2288. {
  2289. PENTRY pentry;
  2290. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2291. BOOL bStatus = FALSE;
  2292. if (uiIndex < gcMaxHmgr)
  2293. {
  2294. pentry = &gpentHmgr[uiIndex];
  2295. HANDLELOCK HandleLock(pentry,TRUE);
  2296. if (HandleLock.bValid())
  2297. {
  2298. if ((pentry->Objt == objt) && (pentry->FullUnique == HmgUfromH(hobj)))
  2299. {
  2300. pentry->Flags |= HMGR_ENTRY_UNDELETABLE;
  2301. bStatus = TRUE;
  2302. }
  2303. else
  2304. {
  2305. WARNING1("HmgMarkUndeletable: bad objt or unique");
  2306. }
  2307. HandleLock.vUnlock();
  2308. }
  2309. else
  2310. {
  2311. WARNING1("HmgMarkUndeletable failed: not object owner\n");
  2312. }
  2313. }
  2314. else
  2315. {
  2316. WARNING1("HmgMarkUndeletable failed: index out of range\n");
  2317. }
  2318. return(bStatus);
  2319. }
  2320. /******************************Public*Routine******************************\
  2321. * BOOL HmgSetLock
  2322. *
  2323. * Set the lock count of the object.
  2324. * This is currently used by process cleanup code to reset object lock count.
  2325. *
  2326. * History:
  2327. * Wed May 25 15:24:33 1994 -by- Hock San Lee [hockl]
  2328. * Wrote it.
  2329. \**************************************************************************/
  2330. BOOL FASTCALL HmgSetLock(HOBJ hobj, ULONG cLock)
  2331. {
  2332. PENTRY pentTmp;
  2333. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2334. if (uiIndex < gcMaxHmgr)
  2335. {
  2336. pentTmp = &gpentHmgr[uiIndex];
  2337. if (pentTmp->FullUnique == HmgUfromH(hobj))
  2338. {
  2339. POBJ pobj = pentTmp->einfo.pobj;
  2340. //
  2341. // This code is really hideous.
  2342. // This is one of a very few places where it is necessary to know
  2343. // that the BaseFlags entry in BASEOBJECT is actually the upper
  2344. // 16 bits of the DWORD containing the cExclusiveLock.
  2345. // If the layout of BASEOBJECT ever changes separating the cExclusiveLock
  2346. // and the BaseFlags then this code _WILL_ break.
  2347. //
  2348. // Also this code only works because nobody actually sets BaseFlags after
  2349. // object allocation. If this fact ever changes then this code needs to
  2350. // be changed to an InterlockedCompareExchange loop and should probably be
  2351. // protected by the HandleLock. Also the BaseFlags modification code
  2352. // would then also require an InterlockedCompareExchange too.
  2353. //
  2354. // The only reason we actually entertain this mess is because this code
  2355. // is only ever used in cleanup. It's bad enough that the code stomps the
  2356. // lock without checking it anyway.
  2357. //
  2358. // The InterlockedExchange is required because it inserts appropriate
  2359. // memory barriers on Alpha preventing other processors from seeing
  2360. // the write delayed.
  2361. //
  2362. SplitOrCombinedLockAndFlags M;
  2363. //
  2364. // get a pointer to the cExclusiveLock, make it a ULONG * and then
  2365. // dereference it getting 32bits from the beginning of the 16bit
  2366. // cExclusiveLock. This will include the BaseFlags which we know won't
  2367. // change (see above and BASEOBJECT).
  2368. //
  2369. M.W = *(ULONG *)&(pobj->cExclusiveLock);
  2370. M.S.c_cExclusiveLock = (USHORT)cLock;
  2371. InterlockedExchange((LONG *)&(pobj->cExclusiveLock), M.W);
  2372. }
  2373. else
  2374. {
  2375. pentTmp = (PENTRY) NULL;
  2376. WARNING1("HmgSetLock failed - Uniqueness does not match\n");
  2377. }
  2378. }
  2379. else
  2380. {
  2381. pentTmp = (PENTRY) NULL;
  2382. WARNING1("HmgSetLock failed - invalid handle index or object type\n");
  2383. }
  2384. return((BOOL) (ULONG_PTR)pentTmp);
  2385. }
  2386. /******************************Public*Routine******************************\
  2387. * HmgQueryLock
  2388. *
  2389. * This returns the number of times an object has been Locked.
  2390. *
  2391. * Expects: A valid handle. The handle should be validated and locked
  2392. * before calling this.
  2393. *
  2394. * Returns: The number of times the object has been locked.
  2395. *
  2396. * History:
  2397. * 28-Jun-1991 -by- Patrick Haluptzok patrickh
  2398. * Wrote it.
  2399. \**************************************************************************/
  2400. ULONG FASTCALL HmgQueryLock(HOBJ hobj)
  2401. {
  2402. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2403. // Note we don't need to grab the semaphore because this call assumes the
  2404. // handle has already been locked down and we are just reading memory.
  2405. ASSERTGDI(uiIndex < gcMaxHmgr, "HmgQueryLock invalid handle");
  2406. return(gpentHmgr[uiIndex].einfo.pobj->cExclusiveLock);
  2407. }
  2408. /******************************Public*Routine******************************\
  2409. * HmgQueryAltLock
  2410. *
  2411. * This returns the number of times an object has been Alt-Locked.
  2412. *
  2413. * Expects: A valid handle. The handle should be validated and locked
  2414. * before calling this.
  2415. *
  2416. * Returns: The number of times the object has been locked.
  2417. *
  2418. * History:
  2419. * 28-Jun-1991 -by- Patrick Haluptzok patrickh
  2420. * Wrote it.
  2421. \**************************************************************************/
  2422. ULONG FASTCALL HmgQueryAltLock(HOBJ hobj)
  2423. {
  2424. UINT uiIndex = (UINT) HmgIfromH(hobj);
  2425. // Note we don't need to grab the semaphore because this call assumes the
  2426. // handle has already been locked down and we are just reading memory.
  2427. ASSERTGDI(uiIndex < gcMaxHmgr, "HmgQueryAltLock invalid handle");
  2428. return((ULONG) gpentHmgr[uiIndex].einfo.pobj->ulShareCount);
  2429. }
  2430. /******************************Public*Routine******************************\
  2431. * HOBJ hGetFreeHandle()
  2432. *
  2433. * Get the next available handle.
  2434. *
  2435. * History:
  2436. * Mon 21-Oct-1991 -by- Patrick Haluptzok [patrickh]
  2437. * Make pent commit memory as needed, add logging of error codes.
  2438. *
  2439. * Sun 20-Oct-1991 -by- Patrick Haluptzok [patrickh]
  2440. * add uniqueness to the handle when getting it.
  2441. *
  2442. * 12-Dec-1989 -by- Donald Sidoroff [donalds]
  2443. * Wrote it.
  2444. \**************************************************************************/
  2445. HOBJ hGetFreeHandle(
  2446. OBJTYPE objt
  2447. )
  2448. {
  2449. #if GDI_PERF
  2450. HmgCurrentNumberOfHandles[objt]++;
  2451. HmgNumberOfHandlesAllocated[objt]++;
  2452. if (HmgCurrentNumberOfHandles[objt] > HmgMaximumNumberOfHandles[objt])
  2453. HmgMaximumNumberOfHandles[objt]++;
  2454. #endif
  2455. LONG_PTR uiIndex;
  2456. if (ghFreeHmgr != (HOBJ) 0)
  2457. {
  2458. PENTRYOBJ pentTmp;
  2459. uiIndex = HmgIfromH(ghFreeHmgr);
  2460. pentTmp = (PENTRYOBJ) &gpentHmgr[uiIndex];
  2461. ghFreeHmgr = pentTmp->einfo.hFree;
  2462. pentTmp->FullUnique = USUNIQUE(pentTmp->FullUnique,objt);
  2463. uiIndex = (LONG_PTR)MAKE_HMGR_HANDLE(uiIndex,pentTmp->FullUnique);
  2464. return((HOBJ) uiIndex);
  2465. }
  2466. if (gcMaxHmgr >= MAX_HANDLE_COUNT)
  2467. {
  2468. WARNING("Hmgr hGetFreeHandle failed to grow\n");
  2469. return ((HOBJ) 0);
  2470. }
  2471. //
  2472. // Allocate a new handle table entry and set the uniqueness value.
  2473. //
  2474. // N.B. All newly committed memory is zeroed.
  2475. //
  2476. uiIndex = USUNIQUE(UNIQUE_INCREMENT,objt);
  2477. gpentHmgr[gcMaxHmgr].FullUnique = (USHORT) uiIndex;
  2478. uiIndex = (LONG_PTR)MAKE_HMGR_HANDLE(gcMaxHmgr,uiIndex);
  2479. gcMaxHmgr++;
  2480. return((HOBJ) uiIndex);
  2481. }
  2482. /******************************Public*Routine******************************\
  2483. * HmgModifyHandleUniqueness()
  2484. *
  2485. * The handle passed in has already been updated to the new handle.
  2486. *
  2487. * This routine adds bits to the uniquess, updating both the handle entry
  2488. * and and the handle in the object it self. This routine assumes that it is
  2489. * safe to be playing with the handle. This means that the handle is locked
  2490. * or it is during initialization and setting the stock bit.
  2491. *
  2492. *
  2493. * History:
  2494. * 18-Nov-1994 -by- Eric Kutter [erick]
  2495. * Wrote it.
  2496. \**************************************************************************/
  2497. HOBJ HmgModifyHandleType(
  2498. HOBJ h
  2499. )
  2500. {
  2501. PENTRY pent = &gpentHmgr[HmgIfromH(h)];
  2502. USHORT usUnique = HmgUfromH(h);
  2503. ASSERTGDI((((ULONG_PTR)pent->einfo.pobj->hHmgr ^ (ULONG_PTR)h) & (UNIQUE_MASK | TYPE_MASK)) == 0,
  2504. "HmgModifyHandleType bad handle\n");
  2505. ASSERTGDI((pent->einfo.pobj->cExclusiveLock > 0) ||
  2506. (pent->einfo.pobj->ulShareCount > 0) ||
  2507. HmgStockObj(h),
  2508. "HmgModifyHandleType not safe\n");
  2509. pent->FullUnique = usUnique;
  2510. pent->einfo.pobj->hHmgr = h;
  2511. return(h);
  2512. }
  2513. /******************************Public*Routine******************************\
  2514. * HmgLockAndModifyHandleType()
  2515. *
  2516. * The handle passed in has already been updated to the new handle.
  2517. *
  2518. * The Handle is locked assuming owned by OBJECT_OWNER_CURRENT
  2519. * or ObJECT_OWNER_PUBLIC.
  2520. *
  2521. * Then this routine adds bits to the uniquess, updating both the handle entry
  2522. * and and the handle in the object it self.
  2523. *
  2524. * History:
  2525. * 31-Oct-2000 -by- Pravin Santiago [pravins]
  2526. * Wrote it.
  2527. \**************************************************************************/
  2528. BOOL HmgLockAndModifyHandleType(
  2529. HOBJ h
  2530. )
  2531. {
  2532. PENTRY pent = &gpentHmgr[HmgIfromH(h)];
  2533. HANDLELOCK HandleLock(pent,TRUE);
  2534. if (HandleLock.bValid())
  2535. {
  2536. HmgModifyHandleType(h);
  2537. HandleLock.vUnlock();
  2538. return(TRUE);
  2539. }
  2540. return(FALSE);
  2541. }
  2542. /******************************Public*Routine******************************\
  2543. * HmgIncUniqueness()
  2544. *
  2545. * Modify the handle uniqueness
  2546. *
  2547. * This is implemented for the brush/pen caching and lazy deletion to make sure:
  2548. *
  2549. * a)a handle will not be cached twice (changing the handle uniqueness gurantees this)
  2550. *
  2551. * b)a handle will not be cached if it is a lazy deletion (cShareLock == 0)
  2552. *
  2553. * History:
  2554. * 8-May-1995 -by- Lingyun Wang [lingyunw]
  2555. *
  2556. \**************************************************************************/
  2557. HOBJ
  2558. HmgIncUniqueness(
  2559. HOBJ hobj,
  2560. OBJTYPE objt
  2561. )
  2562. {
  2563. PENTRY pentry;
  2564. USHORT usUnique = HmgUfromH(hobj);
  2565. POBJ pobj;
  2566. UINT uiIndex= (UINT) HmgIfromH(hobj);
  2567. HOBJ hNew = NULL;
  2568. if (uiIndex < gcMaxHmgr)
  2569. {
  2570. pentry = &gpentHmgr[uiIndex];
  2571. HANDLELOCK HandleLock(pentry,FALSE);
  2572. if (HandleLock.bValid())
  2573. {
  2574. //
  2575. // validate objt and uniqueness, increment unique if correct
  2576. //
  2577. if (
  2578. (pentry->FullUnique == usUnique) &&
  2579. (OBJECTOWNER_PID(pentry->ObjectOwner) == W32GetCurrentPID()) &&
  2580. ((pentry = &gpentHmgr[uiIndex])->Objt == objt)
  2581. )
  2582. {
  2583. pentry->FullUnique += UNIQUE_INCREMENT;
  2584. hNew = (HOBJ)MAKE_HMGR_HANDLE(uiIndex, pentry->FullUnique);
  2585. pentry->einfo.pobj->hHmgr = hNew;
  2586. }
  2587. HandleLock.vUnlock();
  2588. }
  2589. }
  2590. return(hNew);
  2591. }
  2592. /******************************Public*Routine******************************\
  2593. * int NtGdiGetStats(HANDLE,int,int,PVOID,UINT)
  2594. *
  2595. * This function returns information from the handle manager about
  2596. * the gdi objects that are existing.
  2597. *
  2598. * Parameters:
  2599. * hProcess - handle to the process to query for information about
  2600. * iIndex - type of information to return
  2601. * iPidType - whether to query for just the process pointed
  2602. * to by hProcess, or ALL gdi objects or PUBLIC gdi objects
  2603. * pResults - Pointer to the buffer to fill the data into
  2604. * cjResultSize - Size of the buffer to fill
  2605. *
  2606. * The user buffer is expected to be zero'd. Memory trashing
  2607. * may occur if the size parameter is incorrect.
  2608. *
  2609. * returns: Success state
  2610. *
  2611. * History:
  2612. * Wed 7-Jun-1995 -by- Andrew Skowronski [t-andsko]
  2613. * Wrote it.
  2614. \**************************************************************************/
  2615. #define OBJECT_OWNER_IGNORE (0x0001)
  2616. NTSTATUS APIENTRY
  2617. NtGdiGetStats(
  2618. HANDLE hProcess,
  2619. int iIndex,
  2620. int iPidType,
  2621. PVOID pResults,
  2622. UINT cjResultSize
  2623. )
  2624. {
  2625. DWORD * pdwRes = (DWORD *) pResults; //Pointer to the result buffer
  2626. W32PID pid;
  2627. PENTRY pHmgEntry; //Pointer to current entry in the handle manager table
  2628. ULONG ulLoop;
  2629. NTSTATUS iRet = STATUS_SUCCESS;
  2630. //
  2631. // Check permissions flag
  2632. //
  2633. #if !defined(_GDIPLUS_)
  2634. if (!( RtlGetNtGlobalFlags() & FLG_POOL_ENABLE_TAGGING))
  2635. {
  2636. iRet = STATUS_ACCESS_DENIED;
  2637. }
  2638. #endif
  2639. //
  2640. // Validate buffer size
  2641. //
  2642. switch(iIndex)
  2643. {
  2644. case (GS_NUM_OBJS_ALL) :
  2645. if (cjResultSize < ((MAX_TYPE+1)*sizeof(ULONG)))
  2646. iRet = STATUS_BUFFER_TOO_SMALL;
  2647. break;
  2648. #if DBG
  2649. case (GS_HANDOBJ_CURRENT) :
  2650. case (GS_HANDOBJ_MAX) :
  2651. case (GS_HANDOBJ_ALLOC) :
  2652. case (GS_LOOKASIDE_INFO) :
  2653. if (cjResultSize < ((MAX_TYPE+1)*sizeof(ULONG)*2))
  2654. iRet = STATUS_BUFFER_TOO_SMALL;
  2655. break;
  2656. #else
  2657. // This info is not available in non-checked builds
  2658. case (GS_HANDOBJ_CURRENT) :
  2659. case (GS_HANDOBJ_MAX) :
  2660. case (GS_HANDOBJ_ALLOC) :
  2661. case (GS_LOOKASIDE_INFO) :
  2662. break;
  2663. #endif
  2664. default :
  2665. iRet = STATUS_NOT_IMPLEMENTED;
  2666. break;
  2667. }
  2668. if (NT_SUCCESS(iRet))
  2669. {
  2670. __try
  2671. {
  2672. ProbeForWrite(pResults, cjResultSize, sizeof(UCHAR));
  2673. switch(iIndex)
  2674. {
  2675. case (GS_NUM_OBJS_ALL) :
  2676. //
  2677. // Determine the search key.
  2678. //
  2679. if (iPidType == (int) OBJECT_OWNER_CURRENT)
  2680. {
  2681. pid = (W32PID) ((ULONG) ((ULONG_PTR)hProcess & PID_BITS));
  2682. }
  2683. else
  2684. {
  2685. //
  2686. // This takes care of OBJECT_OWNER_PUBLIC
  2687. // and OBJECT_OWNER_IGNORE
  2688. //
  2689. pid = (W32PID) iPidType;
  2690. }
  2691. //
  2692. // Scan through the handle manager table and
  2693. // pick out the relevant objects
  2694. //
  2695. for (ulLoop = 0; ulLoop < gcMaxHmgr; ulLoop++)
  2696. {
  2697. pHmgEntry = &(gpentHmgr[ulLoop]);
  2698. if ((pid == OBJECT_OWNER_IGNORE) ||
  2699. (pid == OBJECTOWNER_PID(pHmgEntry->ObjectOwner)))
  2700. {
  2701. //
  2702. // Now, depending on the iIndex value we
  2703. // analyse the results
  2704. //
  2705. switch (iIndex)
  2706. {
  2707. case (GS_NUM_OBJS_ALL) :
  2708. pdwRes[pHmgEntry->Objt]++;
  2709. break;
  2710. default :
  2711. iRet = STATUS_NOT_IMPLEMENTED;
  2712. break;
  2713. }
  2714. }
  2715. }
  2716. break;
  2717. #if DBG
  2718. case (GS_HANDOBJ_CURRENT) :
  2719. RtlCopyMemory(pdwRes,
  2720. HmgCurrentNumberOfHandles,
  2721. (MAX_TYPE+1)*sizeof(ULONG));
  2722. RtlCopyMemory(&(pdwRes[MAX_TYPE + 1]),
  2723. HmgCurrentNumberOfObjects,
  2724. (MAX_TYPE+1)*sizeof(ULONG));
  2725. break;
  2726. case (GS_HANDOBJ_MAX) :
  2727. RtlCopyMemory(pdwRes,
  2728. HmgMaximumNumberOfHandles,
  2729. (MAX_TYPE+1)*sizeof(ULONG));
  2730. RtlCopyMemory(&(pdwRes[MAX_TYPE + 1]),
  2731. HmgMaximumNumberOfObjects,
  2732. (MAX_TYPE+1)*sizeof(ULONG));
  2733. break;
  2734. case (GS_HANDOBJ_ALLOC) :
  2735. RtlCopyMemory(pdwRes,
  2736. HmgNumberOfHandlesAllocated,
  2737. (MAX_TYPE+1)*sizeof(ULONG));
  2738. RtlCopyMemory(&(pdwRes[MAX_TYPE + 1]),
  2739. HmgNumberOfObjectsAllocated,
  2740. (MAX_TYPE+1)*sizeof(ULONG));
  2741. break;
  2742. case (GS_LOOKASIDE_INFO) :
  2743. RtlCopyMemory(pdwRes,
  2744. HmgNumberOfLookAsideHits,
  2745. (MAX_TYPE+1)*sizeof(ULONG));
  2746. RtlCopyMemory(&(pdwRes[MAX_TYPE + 1]),
  2747. HmgNumberOfObjectsAllocated,
  2748. (MAX_TYPE+1)*sizeof(ULONG));
  2749. break;
  2750. #else
  2751. // This info is not available in non-checked builds
  2752. case (GS_HANDOBJ_CURRENT) :
  2753. case (GS_HANDOBJ_MAX) :
  2754. case (GS_HANDOBJ_ALLOC) :
  2755. case (GS_LOOKASIDE_INFO) :
  2756. break;
  2757. #endif
  2758. default :
  2759. iRet = STATUS_NOT_IMPLEMENTED;
  2760. break;
  2761. }
  2762. }
  2763. __except (EXCEPTION_EXECUTE_HANDLER)
  2764. {
  2765. iRet = STATUS_ACCESS_VIOLATION;
  2766. }
  2767. }
  2768. return iRet;
  2769. }
  2770. /******************************Public*Routine******************************\
  2771. * HmgAllocateSecureUserMemory
  2772. *
  2773. * Allocate and lock 1 page, add as free DC_ATTRs
  2774. *
  2775. * Arguments:
  2776. *
  2777. * None
  2778. *
  2779. * Return Value:
  2780. *
  2781. * Status
  2782. *
  2783. * History:
  2784. *
  2785. * 15-May-1995 -by- Mark Enstrom [marke]
  2786. *
  2787. \**************************************************************************/
  2788. PVOID
  2789. HmgAllocateSecureUserMemory(HANDLE *phSecure)
  2790. {
  2791. PVOID pvRet = NULL;
  2792. NTSTATUS NtStatus;
  2793. //
  2794. // allocate 1 page of user mode memory
  2795. //
  2796. SIZE_T ViewSize = PAGE_SIZE;
  2797. NtStatus = ZwAllocateVirtualMemory(
  2798. NtCurrentProcess(),
  2799. &pvRet,
  2800. 0L,
  2801. &ViewSize,
  2802. MEM_COMMIT | MEM_RESERVE,
  2803. PAGE_READWRITE
  2804. );
  2805. if (NT_SUCCESS(NtStatus))
  2806. {
  2807. //
  2808. // secure virtual memory
  2809. //
  2810. HANDLE hSecure = *phSecure = MmSecureVirtualMemory(pvRet, ViewSize, PAGE_READWRITE);
  2811. if (hSecure != NULL)
  2812. {
  2813. RtlZeroMemory((PVOID)pvRet,ViewSize);
  2814. }
  2815. else
  2816. {
  2817. //
  2818. // free memory
  2819. //
  2820. ZwFreeVirtualMemory(
  2821. NtCurrentProcess(),
  2822. (PVOID*)&pvRet,
  2823. &ViewSize,
  2824. MEM_RELEASE);
  2825. pvRet = NULL;
  2826. }
  2827. }
  2828. return(pvRet);
  2829. }
  2830. /******************************Public*Routine******************************\
  2831. * HmgFreeSecureUserMemory
  2832. *
  2833. * unlock 1 page, and free it
  2834. *
  2835. * Arguments:
  2836. *
  2837. * None
  2838. *
  2839. * Return Value:
  2840. *
  2841. * Status
  2842. *
  2843. * History:
  2844. *
  2845. * 30-Oct-2001 -by- Pravin Santiago[pravins]
  2846. *
  2847. \**************************************************************************/
  2848. VOID
  2849. HmgFreeAllocateSecureUserMemory(PVOID pvAttr, HANDLE hSecure)
  2850. {
  2851. SIZE_T ViewSize = PAGE_SIZE;
  2852. MmUnsecureVirtualMemory(hSecure);
  2853. ZwFreeVirtualMemory(NtCurrentProcess(),
  2854. &pvAttr,
  2855. &ViewSize,
  2856. MEM_RELEASE);
  2857. return;
  2858. }
  2859. /******************************Public*Routine******************************\
  2860. * HmgAllocateDcAttr
  2861. *
  2862. * Look first for free DC_ATTR block on thread, else look on process
  2863. * free list. If none is available, try to allocate user-mode memory.
  2864. *
  2865. * Arguments:
  2866. *
  2867. * None
  2868. *
  2869. * Return Value:
  2870. *
  2871. * pDC_ATTR or NULL
  2872. *
  2873. * History:
  2874. *
  2875. * 24-Aug-1995 -by- Mark Enstrom [marke]
  2876. *
  2877. \**************************************************************************/
  2878. PDC_ATTR
  2879. HmgAllocateDcAttr()
  2880. {
  2881. PW32THREAD pThread = W32GetCurrentThread();
  2882. PDC_ATTR pDCAttr = NULL;
  2883. #if defined(_GDIPLUS_)
  2884. pDCAttr = (DC_ATTR*) PALLOCMEM(sizeof(DC_ATTR), 'zzzG');
  2885. #else
  2886. //
  2887. // look on thread for free dcattr
  2888. //
  2889. if (pThread->pgdiDcattr)
  2890. {
  2891. pDCAttr = (PDC_ATTR)pThread->pgdiDcattr;
  2892. pThread->pgdiDcattr = NULL;
  2893. }
  2894. else
  2895. {
  2896. PW32PROCESS Process = W32GetCurrentProcess();
  2897. GreAcquireHmgrSemaphore();
  2898. //
  2899. // look on process list for free dcattr, if none are
  2900. // available then allocate 1 page of memory and add
  2901. // to list.
  2902. //
  2903. PDC_ATTR *pdcaFreeList = 0;
  2904. PDCAFLISTENTRY pFreeList = 0;
  2905. // If the free list empty try to allocate one:
  2906. if (Process->pDCAttrList == NULL)
  2907. {
  2908. ASSERTGDI(IsListEmpty(&Process->GDIDcAttrFreeList),
  2909. "HmgAllocateDcAttr pDCAttrList is NULL but GDIDcAttrFreeList is not empty\n");
  2910. HANDLE hSecure;
  2911. PDC_ATTR pdca = (PDC_ATTR)HmgAllocateSecureUserMemory(&hSecure);
  2912. if (pdca != (PDC_ATTR)NULL)
  2913. {
  2914. ULONG ulIndex;
  2915. pFreeList = (PDCAFLISTENTRY)PALLOCNOZ(sizeof(DCAFLISTENTRY),
  2916. 'fcdG');
  2917. if (pFreeList == 0)
  2918. {
  2919. HmgFreeAllocateSecureUserMemory(pdca, hSecure);
  2920. GreReleaseHmgrSemaphore();
  2921. return 0;
  2922. }
  2923. InsertHeadList(&Process->GDIDcAttrFreeList,
  2924. (PLIST_ENTRY)pFreeList);
  2925. pFreeList->cFree = ULNUMDC;
  2926. pdcaFreeList = (PDC_ATTR *)(&pFreeList->a[0]);
  2927. //
  2928. // set the new free list pointing to the new dcattrs allocated
  2929. // in user mode memory.
  2930. //
  2931. Process->pDCAttrList = (PVOID)&pdca[ULNUMDC-1];
  2932. for (ulIndex=0;ulIndex<ULNUMDC;ulIndex++)
  2933. {
  2934. pdcaFreeList[ulIndex] = &pdca[ulIndex];
  2935. }
  2936. }
  2937. }
  2938. if (Process->pDCAttrList != NULL)
  2939. {
  2940. ASSERTGDI(!IsListEmpty(&Process->GDIDcAttrFreeList),
  2941. "HmgAllocateDcAttr pDCAttrList != NULL but GDIDcAttrFreeList is empty\n");
  2942. pFreeList = (PDCAFLISTENTRY)Process->GDIDcAttrFreeList.Flink;
  2943. pdcaFreeList = (PDC_ATTR *)(&pFreeList->a[0]);
  2944. ASSERTGDI(Process->pDCAttrList == (PVOID)(pdcaFreeList[pFreeList->cFree-1]),
  2945. "HmgAllocateDcAttr pdcAttrList and pdcaFreeList entries dont match\n");
  2946. //
  2947. // get dcattr and remove from list
  2948. //
  2949. pDCAttr = (PDC_ATTR)Process->pDCAttrList;
  2950. pFreeList->cFree--;
  2951. // If pFreeList has no more free entries remove it from the process
  2952. // free list. Else adjust the free list pointer to next available
  2953. // attr.
  2954. if (pFreeList->cFree == 0)
  2955. {
  2956. RemoveEntryList((PLIST_ENTRY)pFreeList);
  2957. VFREEMEM(pFreeList);
  2958. // If the process free list is not empty, update free list
  2959. // pointer to the next available entry. Else set it to NULL.
  2960. if (!IsListEmpty(&Process->GDIDcAttrFreeList))
  2961. {
  2962. pFreeList = (PDCAFLISTENTRY)Process->GDIDcAttrFreeList.Flink;
  2963. ASSERTGDI(pFreeList->cFree == ULNUMDC,
  2964. "HmgAllocateDcAttr: next available free list does not have cFree of ULNUMDC\n");
  2965. pdcaFreeList = (PDC_ATTR *)(&pFreeList->a[0]);
  2966. Process->pDCAttrList = (PVOID) (pdcaFreeList[pFreeList->cFree-1]);
  2967. }
  2968. else
  2969. {
  2970. Process->pDCAttrList = 0;
  2971. }
  2972. }
  2973. else
  2974. {
  2975. Process->pDCAttrList = (PVOID) (pdcaFreeList[pFreeList->cFree-1]);
  2976. }
  2977. }
  2978. GreReleaseHmgrSemaphore();
  2979. }
  2980. #endif
  2981. return(pDCAttr);
  2982. }
  2983. /******************************Public*Routine******************************\
  2984. * HmgAllocateBrushAttr
  2985. *
  2986. * Look on thread for free brushattr, else look on process free
  2987. * list. If none is available, allocate a dcattr and divide it
  2988. * up.
  2989. *
  2990. * Arguments:
  2991. *
  2992. * none
  2993. *
  2994. * Return Value:
  2995. *
  2996. * pbrushattr or NULL
  2997. *
  2998. * History:
  2999. *
  3000. * 28-Aug-1995 -by- Mark Enstrom [marke]
  3001. *
  3002. \**************************************************************************/
  3003. POBJECTATTR
  3004. HmgAllocateObjectAttr()
  3005. {
  3006. PW32THREAD pThread = W32GetCurrentThread();
  3007. POBJECTATTR pobjattr = NULL;
  3008. #if defined(_GDIPLUS_)
  3009. pobjattr = (OBJECTATTR*) PALLOCMEM(sizeof(OBJECTATTR), 'zzzG');
  3010. #else
  3011. //
  3012. // quick check on thread for free OBJECTATTR
  3013. //
  3014. if (pThread->pgdiBrushAttr)
  3015. {
  3016. pobjattr = (POBJECTATTR)pThread->pgdiBrushAttr;
  3017. pThread->pgdiBrushAttr = NULL;
  3018. }
  3019. else
  3020. {
  3021. PW32PROCESS Process = W32GetCurrentProcess();
  3022. GreAcquireHmgrSemaphore();
  3023. //
  3024. // look on process list for free dcattr, if none are
  3025. // available then allocate 1 page of memory and add
  3026. // to list.
  3027. //
  3028. POBJECTATTR *pobaFreeList = 0;
  3029. POBAFLISTENTRY pFreeList = 0;
  3030. if (Process->pBrushAttrList == NULL)
  3031. {
  3032. ASSERTGDI(IsListEmpty(&Process->GDIBrushAttrFreeList),
  3033. "HmgAllocateObjectAttr pBrushAttrList is NULL but GDIBrushAttrFreeList is not empty\n");
  3034. HANDLE hSecure;
  3035. POBJECTATTR psbr = (POBJECTATTR)HmgAllocateSecureUserMemory(&hSecure);
  3036. if (psbr != (POBJECTATTR)NULL)
  3037. {
  3038. ULONG ulIndex;
  3039. // No free list available. Allocate one which can handle
  3040. // ulNumBrush entries.
  3041. pFreeList = (POBAFLISTENTRY)PALLOCNOZ(sizeof(OBAFLISTENTRY),
  3042. 'fabG');
  3043. if (pFreeList == 0)
  3044. {
  3045. HmgFreeAllocateSecureUserMemory(psbr, hSecure);
  3046. GreReleaseHmgrSemaphore();
  3047. return 0;
  3048. }
  3049. InsertHeadList(&Process->GDIBrushAttrFreeList,
  3050. (PLIST_ENTRY)pFreeList);
  3051. pFreeList->cFree = ULNUMBRUSH;
  3052. pobaFreeList = (POBJECTATTR *)(&pFreeList->a[0]);
  3053. //
  3054. // init process list, then create a linked list of all
  3055. // the new dcattrs on the process list
  3056. //
  3057. Process->pBrushAttrList = (PVOID)&psbr[ULNUMBRUSH-1];
  3058. for (ulIndex=0;ulIndex<ULNUMBRUSH;ulIndex++)
  3059. {
  3060. pobaFreeList[ulIndex] = &psbr[ulIndex];
  3061. }
  3062. }
  3063. }
  3064. if (Process->pBrushAttrList != NULL)
  3065. {
  3066. ASSERTGDI(!IsListEmpty(&Process->GDIBrushAttrFreeList),
  3067. "HmgAllocateObjectAttr pBrushAttrList != NULL but GDIBrushAttrFreeList is empty\n");
  3068. pFreeList = (POBAFLISTENTRY)Process->GDIBrushAttrFreeList.Flink;
  3069. pobaFreeList = (POBJECTATTR*)(&pFreeList->a[0]);
  3070. ASSERTGDI(Process->pBrushAttrList == (PVOID)(pobaFreeList[pFreeList->cFree-1]),
  3071. "HmgAllocateObjectAttr pBrushAttrList and pobjFreeList entries dont match\n");
  3072. //
  3073. // get dcattr and remove from list
  3074. //
  3075. pobjattr = (POBJECTATTR)Process->pBrushAttrList;
  3076. pFreeList->cFree--;
  3077. // If pFreeList has no more free entries remove it from the process
  3078. // free list. Else adjust the free list pointer to next available
  3079. // attr.
  3080. if (pFreeList->cFree == 0)
  3081. {
  3082. RemoveEntryList((PLIST_ENTRY)pFreeList);
  3083. VFREEMEM(pFreeList);
  3084. // If the process free list is not empty, update free list
  3085. // pointer to the next available entry. Else set it to NULL.
  3086. if (!IsListEmpty(&Process->GDIBrushAttrFreeList))
  3087. {
  3088. pFreeList = (POBAFLISTENTRY)Process->GDIBrushAttrFreeList.Flink;
  3089. ASSERTGDI(pFreeList->cFree == ULNUMBRUSH,
  3090. "HmgAllocateObjextAttr: next available free list does not have cFree of ULNUMBRUSH\n");
  3091. pobaFreeList = (POBJECTATTR*)(&pFreeList->a[0]);
  3092. Process->pBrushAttrList = (PVOID) (pobaFreeList[pFreeList->cFree-1]);
  3093. }
  3094. else
  3095. {
  3096. Process->pBrushAttrList = 0;
  3097. }
  3098. }
  3099. else
  3100. {
  3101. Process->pBrushAttrList = (PVOID) (pobaFreeList[pFreeList->cFree-1]);
  3102. }
  3103. }
  3104. GreReleaseHmgrSemaphore();
  3105. }
  3106. #endif
  3107. return(pobjattr);
  3108. }
  3109. /******************************Public*Routine******************************\
  3110. * HmgFreeDcAttr
  3111. *
  3112. * Free the dcattr, try to put it on w32thread, if no space then push onto
  3113. * w32process
  3114. *
  3115. * Arguments:
  3116. *
  3117. * pdcattr
  3118. *
  3119. * Return Value:
  3120. *
  3121. * none
  3122. *
  3123. * History:
  3124. *
  3125. * 28-Aug-1995 -by- Mark Enstrom [marke]
  3126. *
  3127. \**************************************************************************/
  3128. VOID
  3129. HmgFreeDcAttr(
  3130. PDC_ATTR pdca
  3131. )
  3132. {
  3133. PW32THREAD pThread = W32GetCurrentThread();
  3134. #if defined(_GDIPLUS_)
  3135. VFREEMEM(pdca);
  3136. #else
  3137. if ((pdca != NULL) && (pThread != NULL))
  3138. {
  3139. //
  3140. // try to place on thread
  3141. //
  3142. if (pThread->pgdiDcattr == NULL)
  3143. {
  3144. pThread->pgdiDcattr = pdca;
  3145. }
  3146. else
  3147. {
  3148. PW32PROCESS Process = W32GetCurrentProcess();
  3149. //
  3150. // Need to check if W32 process pointer is valid. There are
  3151. // circumstances in which the process can die without setting
  3152. // the W32 process.
  3153. //
  3154. if (Process != NULL)
  3155. {
  3156. GreAcquireHmgrSemaphore();
  3157. //
  3158. // We have to allocate a new free List Entry or add to the one
  3159. // we already have.
  3160. PDCAFLISTENTRY pFreeList = 0;
  3161. PDC_ATTR *pdcaFreeList = 0;
  3162. if (IsListEmpty(&Process->GDIDcAttrFreeList) ||
  3163. ((PDCAFLISTENTRY)Process->GDIDcAttrFreeList.Flink)->cFree == ULNUMDC)
  3164. {
  3165. pFreeList = (PDCAFLISTENTRY)PALLOCNOZ(sizeof(DCAFLISTENTRY),
  3166. 'fcdG');
  3167. if (pFreeList == 0)
  3168. {
  3169. GreReleaseHmgrSemaphore();
  3170. return;
  3171. }
  3172. InsertHeadList(&Process->GDIDcAttrFreeList,
  3173. (PLIST_ENTRY)pFreeList);
  3174. pFreeList->cFree = 0;
  3175. }
  3176. else
  3177. {
  3178. pFreeList = (PDCAFLISTENTRY)Process->GDIDcAttrFreeList.Flink;
  3179. }
  3180. pdcaFreeList = (PDC_ATTR *)(&pFreeList->a[0]);
  3181. // place on process single linked list
  3182. pFreeList->cFree++;
  3183. pdcaFreeList[pFreeList->cFree-1] = pdca;
  3184. Process->pDCAttrList = pdca;
  3185. GreReleaseHmgrSemaphore();
  3186. }
  3187. }
  3188. }
  3189. #endif
  3190. }
  3191. /******************************Public*Routine******************************\
  3192. * HmgFreeObjectAttr
  3193. *
  3194. * Free the objattr, try to put it on w32thread, if no space then
  3195. * check other part(s) of block that make up a dcattr. If they are
  3196. * free then place whole dcattr back on dcattr list.
  3197. *
  3198. * Arguments:
  3199. *
  3200. * pobjattr
  3201. *
  3202. * Return Value:
  3203. *
  3204. * none
  3205. *
  3206. * History:
  3207. *
  3208. * 28-Aug-1995 -by- Mark Enstrom [marke]
  3209. *
  3210. \**************************************************************************/
  3211. VOID
  3212. HmgFreeObjectAttr(
  3213. POBJECTATTR pobjattr
  3214. )
  3215. {
  3216. PW32THREAD pThread = W32GetCurrentThread();
  3217. #if defined(_GDIPLUS_)
  3218. VFREEMEM(pobjattr);
  3219. #else
  3220. if ((pobjattr != NULL) && (pThread != NULL))
  3221. {
  3222. //
  3223. // try to place on thread
  3224. //
  3225. if (pThread->pgdiBrushAttr == NULL)
  3226. {
  3227. pThread->pgdiBrushAttr = pobjattr;
  3228. }
  3229. else
  3230. {
  3231. PW32PROCESS Process = W32GetCurrentProcess();
  3232. //
  3233. // Need to check if W32 process pointer is valid. There are
  3234. // circumstances in which the process can die without setting
  3235. // the W32 process.
  3236. //
  3237. if (Process != NULL)
  3238. {
  3239. GreAcquireHmgrSemaphore();
  3240. //
  3241. // We have to allocate a new free List Entry or add to the one
  3242. // we alread have.
  3243. POBAFLISTENTRY pFreeList = 0;
  3244. POBJECTATTR *pobaFreeList = 0;
  3245. if (IsListEmpty(&Process->GDIBrushAttrFreeList) ||
  3246. ((POBAFLISTENTRY)Process->GDIBrushAttrFreeList.Flink)->cFree == ULNUMBRUSH)
  3247. {
  3248. pFreeList = (POBAFLISTENTRY)PALLOCNOZ(sizeof(OBAFLISTENTRY),
  3249. 'fabG');
  3250. if (pFreeList == 0)
  3251. {
  3252. GreReleaseHmgrSemaphore();
  3253. return;
  3254. }
  3255. InsertHeadList(&Process->GDIBrushAttrFreeList,
  3256. (PLIST_ENTRY)pFreeList);
  3257. pFreeList->cFree = 0;
  3258. }
  3259. else
  3260. {
  3261. pFreeList = (POBAFLISTENTRY)Process->GDIBrushAttrFreeList.Flink;
  3262. }
  3263. pobaFreeList = (POBJECTATTR*)(&pFreeList->a[0]);
  3264. //
  3265. // place on process single linked list
  3266. //
  3267. pFreeList->cFree++;
  3268. pobaFreeList[pFreeList->cFree-1] = pobjattr;
  3269. Process->pBrushAttrList = pobjattr;
  3270. GreReleaseHmgrSemaphore();
  3271. }
  3272. }
  3273. }
  3274. #endif
  3275. }
  3276. /******************************Public*Routine******************************\
  3277. * GreIncQuotaCount GreDecQuotaCount
  3278. *
  3279. * This is called by user Inc or Dec cursor bitmaps against the creating process
  3280. *
  3281. * Notice that cursor bitmaps are marked as PUBLIC by USER but charged against
  3282. * the creating process's handle quota
  3283. *
  3284. * History:
  3285. * 2/10/98 - by Lingyun Wang [lingyunw]
  3286. *
  3287. \**************************************************************************/
  3288. VOID GreIncQuotaCount(PW32PROCESS pW32)
  3289. {
  3290. InterlockedIncrement(&pW32->GDIHandleCount);
  3291. }
  3292. VOID GreDecQuotaCount(PW32PROCESS pW32)
  3293. {
  3294. InterlockedDecrement(&pW32->GDIHandleCount);
  3295. }
  3296. /******************************Public*Routine******************************\
  3297. * HmgPrintBadHandle
  3298. *
  3299. * Simple routine that prints out a warning when a handle manager
  3300. * lock fails due to a bad handle.
  3301. *
  3302. \**************************************************************************/
  3303. #if DBG
  3304. CONST CHAR* aszType[] = {
  3305. "hdef", // DEF_TYPE
  3306. "HDC", // DC_TYPE
  3307. "hunused", // DD_DIRECTDRAW_TYPE
  3308. "hunused", // DD_SURFACE_TYPE
  3309. "HRGN", // RGN_TYPE
  3310. "HBITMAP", // SURF_TYPE
  3311. "HCLIENT", // CLIENTOBJ_TYPE
  3312. "hpath", // PATH_TYPE
  3313. "HPALETTE", // PAL_TYPE
  3314. "HCOLORSPACE", // ICMLCS_TYPE
  3315. "HFONT", // LFONT_TYPE
  3316. "hrfont", // RFONT_TYPE
  3317. "hpfe", // PFE_TYPE
  3318. "hpft", // PFT_TYPE
  3319. "hicmcxf", // ICMCXF_TYPE
  3320. "HSPRITE", // SPRITE_TYPE
  3321. "HBRUSH", // BRUSH_TYPE
  3322. "hunused", // D3D_HANDLE_TYPE
  3323. "hunused", // DD_VIDEOPORT_TYPE
  3324. "hspace", // SPACE_TYPE
  3325. "hunused", // DD_MOTIONCOMP_TYPE
  3326. "HENHMETAFILE", // META_TYPE
  3327. "hefstate", // EFSTATE_TYPE
  3328. "hunused", // BMFD_TYPE
  3329. "hunused", // VTFD_TYPE
  3330. "hunused", // TTFD_TYPE
  3331. "hunused", // RC_TYPE
  3332. "hunused", // TEMP_TYPE
  3333. "HDRVOBJ", // DRVOBJ_TYPE
  3334. "hunused", // DCIOBJ_TYPE
  3335. "hspool", // SPOOL_TYPE
  3336. "hunused", //
  3337. };
  3338. VOID
  3339. HmgPrintBadHandle(
  3340. HOBJ hobj,
  3341. OBJTYPE objt
  3342. )
  3343. {
  3344. static CHAR *szSystem = "System";
  3345. static CHAR *szUnknown = "???";
  3346. CHAR *pszImage;
  3347. {
  3348. PETHREAD pet;
  3349. PEPROCESS pep;
  3350. if (pep = PsGetCurrentProcess())
  3351. {
  3352. pszImage = (CHAR *)PsGetProcessImageFileName(pep);
  3353. if (*pszImage == '\0')
  3354. {
  3355. pszImage = szSystem;
  3356. }
  3357. }
  3358. else
  3359. {
  3360. pszImage = szUnknown;
  3361. }
  3362. }
  3363. KdPrint(("GDI: %s or DLL gave bad handle 0x%p as an %s.\n",
  3364. pszImage, hobj, aszType[objt]));
  3365. // Private debug code to catch invalid hpath handle in DC
  3366. // 6/24/98 - davidx
  3367. if (hobj != NULL && objt == PATH_TYPE)
  3368. {
  3369. RIP("Private debug breakpoint. Please contact ntgdi.");
  3370. }
  3371. }
  3372. #endif