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.

232 lines
5.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: mmodel.cxx
  7. //
  8. // Contents: CMemoryModel
  9. //
  10. // History: 29-Sep-94 DrewB Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.cxx"
  14. #pragma hdrstop
  15. CMemoryModel16 mmodel16Public(TRUE);
  16. CMemoryModel16 mmodel16Owned(FALSE);
  17. CMemoryModel32 mmodel32;
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Method: CMemoryModel16::AllocMemory
  21. //
  22. // Synopsis: Allocates memory
  23. //
  24. // Arguments: [cb] - Size of block to allocate
  25. //
  26. // Returns: New address of block or NULL
  27. //
  28. // History: 7-05-94 BobDay (Bob Day) Created
  29. //
  30. //----------------------------------------------------------------------------
  31. DWORD CMemoryModel16::AllocMemory(DWORD cb)
  32. {
  33. VPVOID vpv;
  34. HMEM16 hmem16;
  35. thkAssert(cb > 0);
  36. vpv = WgtAllocLock(GMEM_MOVEABLE, cb, &hmem16);
  37. if (vpv == 0)
  38. {
  39. //
  40. // Not able to allocate a 16-bit memory block!
  41. //
  42. thkDebugOut((DEB_ERROR,
  43. "CMemoryModel16::AllocMemory, "
  44. "Allocation failed, size %08lX\n",
  45. cb));
  46. return 0;
  47. }
  48. if (_fPublic)
  49. {
  50. SetOwnerPublicHMEM16(hmem16);
  51. }
  52. return vpv;
  53. }
  54. //+---------------------------------------------------------------------------
  55. //
  56. // Method: CMemoryModel16::FreeMemory
  57. //
  58. // Synopsis: Deallocates a block of memory previously allocated
  59. //
  60. // Arguments: [dwMem] - Address of memory block to free
  61. //
  62. // History: 7-05-94 BobDay (Bob Day) Created
  63. //
  64. //----------------------------------------------------------------------------
  65. void CMemoryModel16::FreeMemory(DWORD dwMem)
  66. {
  67. thkAssert(dwMem != 0);
  68. WgtUnlockFree(dwMem);
  69. }
  70. //+---------------------------------------------------------------------------
  71. //
  72. // Method: CMemoryModel16::ResolvePtr
  73. //
  74. // Synopsis: Returns a resolved pointer given an abstract pointer
  75. //
  76. // Arguments: [dwMem] - Address to get pointer from
  77. // [cb] - Length, starting at given address, to make valid
  78. // pointers for.
  79. //
  80. // Returns: LPVOID - A real pointer equivalent to the abstract pointer.
  81. //
  82. // History: 7-05-94 BobDay (Bob Day) Created
  83. //
  84. // Notes: Be careful of alignment issues
  85. //
  86. //----------------------------------------------------------------------------
  87. LPVOID CMemoryModel16::ResolvePtr(DWORD dwMem, DWORD cb)
  88. {
  89. LPVOID pv;
  90. thkAssert(dwMem != 0 && cb > 0);
  91. pv = (LPVOID)WOWFIXVDMPTR(dwMem, cb);
  92. if (pv == NULL)
  93. {
  94. thkDebugOut((DEB_ERROR,
  95. "CMemoryModel16::ResolvePtr, "
  96. "WOWGetVDMPointer failed on %08lX, size %08lX\n",
  97. dwMem, cb));
  98. }
  99. return pv;
  100. }
  101. //+---------------------------------------------------------------------------
  102. //
  103. // Member: CMemoryModel16::ReleasePtr, public
  104. //
  105. // Synopsis: Releases a resolved pointer
  106. //
  107. // Arguments: [dwMem] - Abstract pointer to release
  108. //
  109. // History: 10-Oct-94 DrewB Created
  110. //
  111. //----------------------------------------------------------------------------
  112. void CMemoryModel16::ReleasePtr(DWORD dwMem)
  113. {
  114. thkAssert(dwMem != 0);
  115. WOWRELVDMPTR(dwMem);
  116. }
  117. //+---------------------------------------------------------------------------
  118. //
  119. // Method: CMemoryModel32::AllocMemory
  120. //
  121. // Synopsis: Allocates memory
  122. //
  123. // Arguments: [cb] - Size of block to allocate
  124. //
  125. // Returns: New address of block or NULL
  126. //
  127. // History: 7-05-94 BobDay (Bob Day) Created
  128. //
  129. //----------------------------------------------------------------------------
  130. DWORD CMemoryModel32::AllocMemory(DWORD cb)
  131. {
  132. DWORD dwMem;
  133. thkAssert(cb > 0);
  134. dwMem = (DWORD)CoTaskMemAlloc(cb);
  135. if (dwMem == 0)
  136. {
  137. //
  138. // Not able to allocate a 32-bit memory block!
  139. //
  140. thkDebugOut((DEB_ERROR,
  141. "CMemoryModel32::AllocBlock, "
  142. "CoTaskMemAlloc failed size %08lX\n",
  143. cb));
  144. return 0;
  145. }
  146. return dwMem;
  147. }
  148. //+---------------------------------------------------------------------------
  149. //
  150. // Method: CMemoryModel32::FreeMemory
  151. //
  152. // Synopsis: Deallocates a block of memory previously allocated
  153. //
  154. // Arguments: [dwMem] - Address of memory block to free
  155. //
  156. // History: 7-05-94 BobDay (Bob Day) Created
  157. //
  158. //----------------------------------------------------------------------------
  159. void CMemoryModel32::FreeMemory(DWORD dwMem)
  160. {
  161. thkAssert(dwMem != 0);
  162. CoTaskMemFree((LPVOID)dwMem);
  163. }
  164. //+---------------------------------------------------------------------------
  165. //
  166. // Method: CMemoryModel32::ResolvePtr
  167. //
  168. // Synopsis: Returns a resolved pointer given an abstract pointer
  169. //
  170. // Arguments: [dwMem] - Address to get pointer from
  171. // [cb] - Length, starting at given address, to make valid
  172. // pointers for.
  173. //
  174. // Returns: LPVOID - A real pointer equivalent to the abstract pointer.
  175. //
  176. // History: 7-05-94 BobDay (Bob Day) Created
  177. //
  178. // Notes: Be careful of alignment issues
  179. //
  180. //----------------------------------------------------------------------------
  181. LPVOID CMemoryModel32::ResolvePtr(DWORD dwMem, DWORD cb)
  182. {
  183. thkAssert(dwMem != 0 && cb > 0);
  184. return (LPVOID)dwMem;
  185. }
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CMemoryModel32::ReleasePtr, public
  189. //
  190. // Synopsis: Releases a resolved pointer
  191. //
  192. // Arguments: [dwMem] - Abstract pointer to release
  193. //
  194. // History: 10-Oct-94 DrewB Created
  195. //
  196. //----------------------------------------------------------------------------
  197. void CMemoryModel32::ReleasePtr(DWORD dwMem)
  198. {
  199. thkAssert(dwMem != 0);
  200. }