Source code of Windows XP (NT5)
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.

202 lines
4.5 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: mcdeng.c
  3. *
  4. * Internal server-side MCD engine functions to perform functions such as
  5. * driver object management, memory allocation, etc.
  6. *
  7. * Copyright (c) 1996 Microsoft Corporation
  8. *
  9. \**************************************************************************/
  10. #include <stddef.h>
  11. #include <stdarg.h>
  12. #include <windef.h>
  13. #include <wingdi.h>
  14. #include <windows.h>
  15. #include <wtypes.h>
  16. #include <winddi.h>
  17. #include <mcdesc.h>
  18. #include "mcdrv.h"
  19. #include <mcd2hack.h>
  20. #include "mcd.h"
  21. #include "mcdint.h"
  22. #include "mcdrvint.h"
  23. WNDOBJ *MCDEngGetWndObj(MCDSURFACE *pMCDSurface)
  24. {
  25. return pMCDSurface->pwo;
  26. }
  27. VOID MCDEngUpdateClipList(WNDOBJ *pwo)
  28. {
  29. return;
  30. }
  31. DRIVEROBJ *MCDEngLockObject(MCDHANDLE hObj)
  32. {
  33. return (DRIVEROBJ *)EngLockDriverObj((HDRVOBJ)hObj);
  34. }
  35. VOID MCDEngUnlockObject(MCDHANDLE hObj)
  36. {
  37. EngUnlockDriverObj((HDRVOBJ)hObj);
  38. }
  39. WNDOBJ *MCDEngCreateWndObj(MCDSURFACE *pMCDSurface, HWND hWnd,
  40. WNDOBJCHANGEPROC pChangeProc)
  41. {
  42. return EngCreateWnd(pMCDSurface->pso,
  43. hWnd,
  44. pChangeProc,
  45. (WO_RGN_CLIENT_DELTA |
  46. WO_RGN_CLIENT |
  47. WO_RGN_SURFACE_DELTA |
  48. WO_RGN_SURFACE |
  49. WO_RGN_UPDATE_ALL
  50. ), 0);
  51. }
  52. MCDHANDLE MCDEngCreateObject(VOID *pObject, FREEOBJPROC pFreeObjFunc,
  53. HDEV hDevEng)
  54. {
  55. return (MCDHANDLE)EngCreateDriverObj(pObject,
  56. pFreeObjFunc,
  57. hDevEng);
  58. }
  59. BOOL MCDEngDeleteObject(MCDHANDLE hObj)
  60. {
  61. return (EngDeleteDriverObj((HDRVOBJ)hObj, TRUE, FALSE) != 0);
  62. }
  63. UCHAR *MCDEngAllocSharedMem(ULONG numBytes)
  64. {
  65. return (UCHAR *)EngAllocUserMem(min(numBytes, MCD_MAX_ALLOC),
  66. MCD_ALLOC_TAG);
  67. }
  68. VOID MCDEngFreeSharedMem(UCHAR *pMem)
  69. {
  70. EngFreeUserMem((VOID *)pMem);
  71. }
  72. //****************************************************************************
  73. // MCDEngGetPtrFromHandle()
  74. //
  75. // Converts a driver handle to a pointer. Note that we lock and unlock
  76. // the object, and do not hold the lock during use of the pointer. This
  77. // simplifies much of the other logic in the driver, especially in
  78. // early- or error-return cases, and is safe since we as single-threaded
  79. // inside the driver.
  80. //****************************************************************************
  81. VOID *MCDEngGetPtrFromHandle(MCDHANDLE handle, MCDHANDLETYPE type)
  82. {
  83. MCDRCOBJ *pRcObject;
  84. DRIVEROBJ *pDrvObj;
  85. pDrvObj = (DRIVEROBJ *)EngLockDriverObj((HDRVOBJ)handle);
  86. if (!pDrvObj)
  87. {
  88. MCDBG_PRINT("GetPtrFromHandle: Couldn't unlock driver object.");
  89. return (PVOID)NULL;
  90. }
  91. else
  92. {
  93. pRcObject = (MCDRCOBJ *)pDrvObj->pvObj;
  94. EngUnlockDriverObj((HDRVOBJ)handle);
  95. if (pRcObject->type != type)
  96. {
  97. MCDBG_PRINT("MCDSrvGetPtrFromHandle: Wrong type: got %d, expected %d.",
  98. pRcObject->type, type);
  99. return (PVOID)NULL;
  100. }
  101. else
  102. return pRcObject;
  103. }
  104. }
  105. ULONG_PTR MCDEngGetProcessID()
  106. {
  107. return (ULONG_PTR)EngGetProcessHandle();
  108. }
  109. #if DBG
  110. ULONG MCDLocalMemSize = 0;
  111. UCHAR *MCDSrvDbgLocalAlloc(UINT flags, UINT size)
  112. {
  113. UCHAR *pRet;
  114. if (pRet = (UCHAR *)EngAllocMem(FL_ZERO_MEMORY, size + sizeof(ULONG),
  115. MCD_ALLOC_TAG)) {
  116. MCDLocalMemSize += size;
  117. *((ULONG *)pRet) = size;
  118. return (pRet + sizeof(ULONG));
  119. } else
  120. return (UCHAR *)NULL;
  121. }
  122. VOID MCDSrvDbgLocalFree(UCHAR *pMem)
  123. {
  124. if (!pMem) {
  125. MCDBG_PRINT("MCDSrvDbgLocalFree: Attempt to free NULL pointer.");
  126. return;
  127. }
  128. pMem -= sizeof(ULONG);
  129. MCDLocalMemSize -= *((ULONG *)pMem);
  130. EngFreeMem((VOID *)pMem);
  131. }
  132. VOID MCDDebugPrint(char *pMessage, ...)
  133. {
  134. char buffer[256];
  135. int len;
  136. va_list ap;
  137. va_start(ap, pMessage);
  138. EngDebugPrint("[MCD] ", pMessage, ap);
  139. EngDebugPrint("", "\n", ap);
  140. va_end(ap);
  141. }
  142. VOID MCDAssertFailed(char *pMessage, char *pFile, int line)
  143. {
  144. MCDDebugPrint("%s(%d): %s", pFile, line, pMessage);
  145. EngDebugBreak();
  146. }
  147. #else
  148. UCHAR *MCDSrvLocalAlloc(UINT flags, UINT size)
  149. {
  150. return (UCHAR *)EngAllocMem(FL_ZERO_MEMORY, size, MCD_ALLOC_TAG);
  151. }
  152. VOID MCDSrvLocalFree(UCHAR *pMem)
  153. {
  154. EngFreeMem((VOID *)pMem);
  155. }
  156. #endif /* DBG */