Skip to content

Provide unmanaged memory allocation, memory heap creation.

License

Notifications You must be signed in to change notification settings

10sa/Unmanaged-Memory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unmanaged Memory

This library provides memory that can not be collected by the CLR gc and Memory heap allocation.

You should think twice when you need like this functions. Unmanaged Memory Allocation is isn't REALLY good idea in GC Environment. I recommend use this code only for study. If you need reason, Reference this stackoverflow question.

Memory areas allocated using this library are not collected by gc and must be released by the programmer. A memory leak occurs when all references are disconnected without being released.

This library also uses WinAPI. It doesn't work on non-Windows platforms.

For the WinAPI used in this library, please refer to the "List of Used WinAPIs" below.

Examples

/// Allocation Unmanaged memory
Memory memroy = Memory.Allocation(4); // 4Bytes Memory allocation.
/// Access Memory
byte data = memory[0];
memory[0] = 2;

byte[] datas = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF};
memory.WriteBytes(datas, 0, datas.Length);
/// Release Memory
memory.Free();
/// Since VS2017, C++ Version Memory release
delete memory; // memory : Address (in C#, IntPtr type variable.)
/// Dispose Free
using (Memory memory = Memory.Allocation(4))
{
  memory.IsFreeOnDispose = true; // This code will be make automatic memory release.
  ...
}
/// Allocation Memory heap and Use.
Heap memoryHeap = Heap.CreateHeap();
Memory memory = Memory.Allocation(memoryHeap, 8); // 8Bytes Memory allocation on "memoryHeap" Heap.
/// Destory Memory heap
memoryHeap.Destory();

List of Used WinAPIs

List of Data types

  • HeapFlags : A 32-bit unsigned integer, Bit Flags.
  • IntPtr : Variable. (x32, 4Byte / x64, 8Byte)

IntPtr GetProcessHeap()

uint GetProcessHeaps(uint, IntPtr[])

IntPtr HeapAlloc(IntPtr, HeapFlags, uint)

IntPtr HeapFree(IntPtr, HeapFlags, IntPtr);

IntPtr CopyMemory(IntPtr, IntPtr, uint)

IntPtr CreateHeap(HeapFlags, uint, uint)