OSDN Git Service

チップクラスに「可視の初期値」プロパティを追加。
[strokestylet/CsWin10Desktop3.git] / FDK24 / Memory.cs
1 using System;
2 using System.Diagnostics;
3 using System.Runtime.InteropServices;
4
5 namespace FDK
6 {
7         /// <summary>
8         /// ガベージコレクション対象外のメモリの確保と解放。
9         /// (引用元: https://msdn.microsoft.com/ja-jp/library/aa664786(v=vs.71).aspx )
10         /// </summary>
11         public unsafe class Memory
12         {
13                 // Handle for the process heap. This handle is used in all calls to the
14                 // HeapXXX APIs in the methods below.
15                 static int ph = GetProcessHeap();
16
17                 // Private instance constructor to prevent instantiation.
18                 private Memory()
19                 {
20                 }
21
22                 // Allocates a memory block of the given size. The allocated memory is
23                 // automatically initialized to zero.
24                 public static void* Alloc( int size )
25                 {
26                         void* result = HeapAlloc( ph, HEAP_ZERO_MEMORY, size );
27                         if( result == null ) throw new OutOfMemoryException();
28
29                         IntPtr pr = new IntPtr( result );
30                         //Debug.WriteLine( $"HeapAlloc, {size}bytes, address={pr.ToString()}" );
31
32                         return result;
33                 }
34
35                 // Copies count bytes from src to dst. The source and destination
36                 // blocks are permitted to overlap.
37                 public static void Copy( void* src, void* dst, int count )
38                 {
39                         byte* ps = (byte*) src;
40                         byte* pd = (byte*) dst;
41                         if( ps > pd )
42                         {
43                                 for( ; count != 0; count-- ) *pd++ = *ps++;
44                         }
45                         else if( ps < pd )
46                         {
47                                 for( ps += count, pd += count; count != 0; count-- ) *--pd = *--ps;
48                         }
49                 }
50
51                 // ゼロで埋める。
52                 public static void Zero( void* dst, int count )
53                 {
54                         byte* pd = (byte*) dst;
55                         for( ; count != 0; count-- ) *pd++ = 0;
56                 }
57
58                 // Frees a memory block.
59                 public static void Free( void* block )
60                 {
61                         IntPtr pr = new IntPtr( block );
62                         //Debug.WriteLine( $"HeapFree, address={pr.ToString()}" );
63
64                         if( !HeapFree( ph, 0, block ) ) throw new InvalidOperationException();
65                 }
66
67                 // Re-allocates a memory block. If the reallocation request is for a
68                 // larger size, the additional region of memory is automatically
69                 // initialized to zero.
70                 public static void* ReAlloc( void* block, int size )
71                 {
72                         void* result = HeapReAlloc( ph, HEAP_ZERO_MEMORY, block, size );
73                         if( result == null ) throw new OutOfMemoryException();
74                         return result;
75                 }
76
77                 // Returns the size of a memory block.
78                 public static int SizeOf( void* block )
79                 {
80                         int result = HeapSize( ph, 0, block );
81                         if( result == -1 ) throw new InvalidOperationException();
82                         return result;
83                 }
84
85                 // Heap API flags
86                 const int HEAP_ZERO_MEMORY = 0x00000008;
87
88                 // Heap API functions
89                 [DllImport( "kernel32" )]
90                 static extern int GetProcessHeap();
91
92                 [DllImport( "kernel32" )]
93                 static extern void* HeapAlloc( int hHeap, int flags, int size );
94
95                 [DllImport( "kernel32" )]
96                 static extern bool HeapFree( int hHeap, int flags, void* block );
97
98                 [DllImport( "kernel32" )]
99                 static extern void* HeapReAlloc( int hHeap, int flags, void* block, int size );
100
101                 [DllImport( "kernel32" )]
102                 static extern int HeapSize( int hHeap, int flags, void* block );
103         }
104 }