Memory.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.SystemLayer
  5. {
  6. /// <summary>
  7. /// Contains methods for allocating, freeing, and performing operations on memory
  8. /// that is fixed (pinned) in memory.
  9. /// </summary>
  10. [CLSCompliant(false)]
  11. public unsafe static class Memory
  12. {
  13. private static IntPtr hHeap;
  14. static Memory()
  15. {
  16. hHeap = SafeNativeMethods.HeapCreate(0, IntPtr.Zero, IntPtr.Zero);
  17. uint info = 2;
  18. try
  19. {
  20. // Enable the low-fragmentation heap (LFH)
  21. SafeNativeMethods.HeapSetInformation(hHeap,
  22. NativeConstants.HeapCompatibilityInformation,
  23. (void*)&info,
  24. sizeof(uint));
  25. }
  26. catch (Exception)
  27. {
  28. // If that method isn't available, like on Win2K, don't worry about it.
  29. }
  30. Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
  31. }
  32. /// <summary>
  33. /// Gets the total amount of physical memory (RAM) in the system.
  34. /// </summary>
  35. public static ulong TotalPhysicalBytes
  36. {
  37. get
  38. {
  39. NativeStructs.MEMORYSTATUSEX mse = new NativeStructs.MEMORYSTATUSEX();
  40. mse.dwLength = (uint)sizeof(NativeStructs.MEMORYSTATUSEX);
  41. bool result = NativeMethods.GlobalMemoryStatusEx(ref mse);
  42. if (!result)
  43. {
  44. NativeMethods.ThrowOnWin32Error("GlobalMemoryStatusEx");
  45. }
  46. return mse.ullTotalPhys;
  47. }
  48. }
  49. private static void DestroyHeap()
  50. {
  51. IntPtr hHeap2 = hHeap;
  52. hHeap = IntPtr.Zero;
  53. SafeNativeMethods.HeapDestroy(hHeap2);
  54. }
  55. private static void Application_ApplicationExit(object sender, EventArgs e)
  56. {
  57. DestroyHeap();
  58. }
  59. /// <summary>
  60. /// Allocates a block of memory at least as large as the amount requested.
  61. /// </summary>
  62. /// <param name="bytes">The number of bytes you want to allocate.</param>
  63. /// <returns>A pointer to a block of memory at least as large as <b>bytes</b>.</returns>
  64. /// <exception cref="OutOfMemoryException">Thrown if the memory manager could not fulfill the request for a memory block at least as large as <b>bytes</b>.</exception>
  65. public static IntPtr Allocate(ulong bytes)
  66. {
  67. if (hHeap == IntPtr.Zero)
  68. {
  69. throw new InvalidOperationException("heap has already been destroyed");
  70. }
  71. else
  72. {
  73. IntPtr block = SafeNativeMethods.HeapAlloc(hHeap, 0, new UIntPtr(bytes));
  74. if (block == IntPtr.Zero)
  75. {
  76. throw new OutOfMemoryException("HeapAlloc returned a null pointer");
  77. }
  78. if (bytes > 0)
  79. {
  80. GC.AddMemoryPressure((long)bytes);
  81. }
  82. return block;
  83. }
  84. }
  85. /// <summary>
  86. /// Allocates a block of memory at least as large as the amount requested.
  87. /// </summary>
  88. /// <param name="bytes">The number of bytes you want to allocate.</param>
  89. /// <returns>A pointer to a block of memory at least as large as bytes</returns>
  90. /// <remarks>
  91. /// This method uses an alternate method for allocating memory (VirtualAlloc in Windows). The allocation
  92. /// granularity is the page size of the system (usually 4K). Blocks allocated with this method may also
  93. /// be protected using the ProtectBlock method.
  94. /// </remarks>
  95. public static IntPtr AllocateLarge(ulong bytes)
  96. {
  97. IntPtr block = SafeNativeMethods.VirtualAlloc(IntPtr.Zero, new UIntPtr(bytes),
  98. NativeConstants.MEM_COMMIT, NativeConstants.PAGE_READWRITE);
  99. if (block == IntPtr.Zero)
  100. {
  101. throw new OutOfMemoryException("VirtualAlloc returned a null pointer");
  102. }
  103. if (bytes > 0)
  104. {
  105. GC.AddMemoryPressure((long)bytes);
  106. }
  107. return block;
  108. }
  109. /// <summary>
  110. /// Allocates a bitmap of the given height and width. Pixel data may be read/written directly,
  111. /// and it may be drawn to the screen using PdnGraphics.DrawBitmap().
  112. /// </summary>
  113. /// <param name="width">The width of the bitmap to allocate.</param>
  114. /// <param name="height">The height of the bitmap to allocate.</param>
  115. /// <param name="handle">Receives a handle to the bitmap.</param>
  116. /// <returns>A pointer to the bitmap's pixel data.</returns>
  117. /// <remarks>
  118. /// The following invariants may be useful for implementors:
  119. /// * The bitmap is always 32-bits per pixel, BGRA.
  120. /// * Stride for the bitmap is always width * 4.
  121. /// * The upper-left pixel of the bitmap (0,0) is located at the first memory location pointed to by the returned pointer.
  122. /// * The bitmap is top-down ("memory correct" ordering).
  123. /// * The 'handle' may be any type of data you want, but must be unique for the lifetime of the bitmap, and must not be IntPtr.Zero.
  124. /// * The handle's value must be understanded by PdnGraphics.DrawBitmap().
  125. /// * The bitmap is always modified by directly reading and writing to the memory pointed to by the return value.
  126. /// * PdnGraphics.DrawBitmap() must always render from this memory location (i.e. it must treat the memory as 'volatile')
  127. /// </remarks>
  128. public static IntPtr AllocateBitmap(int width, int height, out IntPtr handle)
  129. {
  130. NativeStructs.BITMAPINFO bmi = new NativeStructs.BITMAPINFO();
  131. bmi.bmiHeader.biSize = (uint)sizeof(NativeStructs.BITMAPINFOHEADER);
  132. bmi.bmiHeader.biWidth = width;
  133. bmi.bmiHeader.biHeight = -height;
  134. bmi.bmiHeader.biPlanes = 1;
  135. bmi.bmiHeader.biBitCount = 32;
  136. bmi.bmiHeader.biCompression = NativeConstants.BI_RGB;
  137. bmi.bmiHeader.biSizeImage = 0;
  138. bmi.bmiHeader.biXPelsPerMeter = 96;
  139. bmi.bmiHeader.biYPelsPerMeter = 96;
  140. bmi.bmiHeader.biClrUsed = 0;
  141. bmi.bmiHeader.biClrImportant = 0;
  142. IntPtr pvBits;
  143. IntPtr hBitmap = SafeNativeMethods.CreateDIBSection(
  144. IntPtr.Zero,
  145. ref bmi,
  146. NativeConstants.DIB_RGB_COLORS,
  147. out pvBits,
  148. IntPtr.Zero,
  149. 0);
  150. if (hBitmap == IntPtr.Zero)
  151. {
  152. throw new OutOfMemoryException("CreateDIBSection returned NULL (" + Marshal.GetLastWin32Error().ToString() + ") while attempting to allocate " + width + "x" + height + " bitmap");
  153. }
  154. handle = hBitmap;
  155. long bytes = (long)width * (long)height * 4;
  156. if (bytes > 0)
  157. {
  158. GC.AddMemoryPressure(bytes);
  159. }
  160. return pvBits;
  161. }
  162. /// <summary>
  163. /// Frees a bitmap previously allocated with AllocateBitmap.
  164. /// </summary>
  165. /// <param name="handle">The handle that was returned from a previous call to AllocateBitmap.</param>
  166. /// <param name="width">The width of the bitmap, as specified in the original call to AllocateBitmap.</param>
  167. /// <param name="height">The height of the bitmap, as specified in the original call to AllocateBitmap.</param>
  168. public static void FreeBitmap(IntPtr handle, long width, long height)
  169. {
  170. long bytes = (long)width * (long)height * 4;
  171. bool bResult = SafeNativeMethods.DeleteObject(handle);
  172. if (!bResult)
  173. {
  174. NativeMethods.ThrowOnWin32Error("DeleteObject returned false");
  175. }
  176. if (bytes > 0)
  177. {
  178. GC.RemoveMemoryPressure(bytes);
  179. }
  180. }
  181. /// <summary>
  182. /// Frees a block of memory previously allocated with Allocate().
  183. /// </summary>
  184. /// <param name="block">The block to free.</param>
  185. /// <exception cref="InvalidOperationException">There was an error freeing the block.</exception>
  186. public static void Free(IntPtr block)
  187. {
  188. if (Memory.hHeap != IntPtr.Zero)
  189. {
  190. long bytes = (long)SafeNativeMethods.HeapSize(hHeap, 0, block);
  191. bool result = SafeNativeMethods.HeapFree(hHeap, 0, block);
  192. if (!result)
  193. {
  194. int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
  195. throw new InvalidOperationException("HeapFree returned an error: " + error.ToString());
  196. }
  197. if (bytes > 0)
  198. {
  199. GC.RemoveMemoryPressure(bytes);
  200. }
  201. }
  202. else
  203. {
  204. #if REPORTLEAKS
  205. throw new InvalidOperationException("memory leak! check the debug output for more info, and http://blogs.msdn.com/ricom/archive/2004/12/10/279612.aspx to track it down");
  206. #endif
  207. }
  208. }
  209. /// <summary>
  210. /// Frees a block of memory previous allocated with AllocateLarge().
  211. /// </summary>
  212. /// <param name="block">The block to free.</param>
  213. /// <param name="bytes">The size of the block.</param>
  214. public static void FreeLarge(IntPtr block, ulong bytes)
  215. {
  216. bool result = SafeNativeMethods.VirtualFree(block, UIntPtr.Zero, NativeConstants.MEM_RELEASE);
  217. if (!result)
  218. {
  219. //int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
  220. //throw new InvalidOperationException("VirtualFree returned an error: " + error.ToString());
  221. }
  222. if (bytes > 0)
  223. {
  224. GC.RemoveMemoryPressure((long)bytes);
  225. }
  226. }
  227. /// <summary>
  228. /// Sets protection on a block previously allocated with AllocateLarge.
  229. /// </summary>
  230. /// <param name="block">The starting memory address to set protection for.</param>
  231. /// <param name="size">The size of the block.</param>
  232. /// <param name="readAccess">Whether to allow read access.</param>
  233. /// <param name="writeAccess">Whether to allow write access.</param>
  234. /// <remarks>
  235. /// You may not specify false for read access without also specifying false for write access.
  236. /// Note to implementors: This method is not guaranteed to actually set read/write-ability
  237. /// on a block of memory, and may instead be implemented as a no-op after parameter validation.
  238. /// </remarks>
  239. public static void ProtectBlockLarge(IntPtr block, ulong size, bool readAccess, bool writeAccess)
  240. {
  241. uint flOldProtect;
  242. uint flNewProtect;
  243. if (readAccess && writeAccess)
  244. {
  245. flNewProtect = NativeConstants.PAGE_READWRITE;
  246. }
  247. else if (readAccess && !writeAccess)
  248. {
  249. flNewProtect = NativeConstants.PAGE_READONLY;
  250. }
  251. else if (!readAccess && !writeAccess)
  252. {
  253. flNewProtect = NativeConstants.PAGE_NOACCESS;
  254. }
  255. else
  256. {
  257. throw new InvalidOperationException("May not specify a page to be write-only");
  258. }
  259. #if DEBUGSPEW
  260. Tracing.Ping("ProtectBlockLarge: block #" + block.ToString() + ", read: " + readAccess + ", write: " + writeAccess);
  261. #endif
  262. SafeNativeMethods.VirtualProtect(block, new UIntPtr(size), flNewProtect, out flOldProtect);
  263. }
  264. /// <summary>
  265. /// Copies bytes from one area of memory to another. Since this function only
  266. /// takes pointers, it can not do any bounds checking.
  267. /// </summary>
  268. /// <param name="dst">The starting address of where to copy bytes to.</param>
  269. /// <param name="src">The starting address of where to copy bytes from.</param>
  270. /// <param name="length">The number of bytes to copy</param>
  271. public static void Copy(IntPtr dst, IntPtr src, ulong length)
  272. {
  273. Copy(dst.ToPointer(), src.ToPointer(), length);
  274. }
  275. /// <summary>
  276. /// Copies bytes from one area of memory to another. Since this function only
  277. /// takes pointers, it can not do any bounds checking.
  278. /// </summary>
  279. /// <param name="dst">The starting address of where to copy bytes to.</param>
  280. /// <param name="src">The starting address of where to copy bytes from.</param>
  281. /// <param name="length">The number of bytes to copy</param>
  282. public static void Copy(void* dst, void* src, ulong length)
  283. {
  284. SafeNativeMethods.memcpy(dst, src, new UIntPtr(length));
  285. }
  286. public static void SetToZero(IntPtr dst, ulong length)
  287. {
  288. SetToZero(dst.ToPointer(), length);
  289. }
  290. public static void SetToZero(void* dst, ulong length)
  291. {
  292. SafeNativeMethods.memset(dst, 0, new UIntPtr(length));
  293. }
  294. }
  295. }