Files
mirror-processhacker/trunk/ProcessHacker.Native/Memory/Heap.cs
T
wj32 769fb8f12f * fixed copyright notices
* free list is now lock-free

git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1717 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-08-16 00:36:36 +00:00

135 lines
3.8 KiB
C#

/*
* Process Hacker -
* run-time library heap
*
* Copyright (C) 2009 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using ProcessHacker.Native.Api;
namespace ProcessHacker.Native
{
public sealed class Heap
{
public static Heap FromHandle(IntPtr handle)
{
return new Heap(handle);
}
public static Heap GetDefault()
{
return new Heap(Win32.GetProcessHeap());
}
public static Heap[] GetHeaps()
{
IntPtr[] heapAddresses = new IntPtr[64];
int retHeaps;
retHeaps = Win32.RtlGetProcessHeaps(heapAddresses.Length, heapAddresses);
// Reallocate the buffer if it wasn't large enough.
if (retHeaps > heapAddresses.Length)
{
heapAddresses = new IntPtr[retHeaps];
retHeaps = Win32.RtlGetProcessHeaps(heapAddresses.Length, heapAddresses);
}
int numberOfHeaps = Math.Min(heapAddresses.Length, retHeaps);
Heap[] heaps = new Heap[numberOfHeaps];
for (int i = 0; i < numberOfHeaps; i++)
heaps[i] = new Heap(heapAddresses[i]);
return heaps;
}
private IntPtr _heap;
private Heap(IntPtr heap)
{
_heap = heap;
}
public Heap(HeapFlags flags)
: this(flags, 0, 0)
{ }
public Heap(HeapFlags flags, int reserveSize, int commitSize)
{
_heap = Win32.RtlCreateHeap(
flags,
IntPtr.Zero,
reserveSize.ToIntPtr(),
commitSize.ToIntPtr(),
IntPtr.Zero,
IntPtr.Zero
);
if (_heap == IntPtr.Zero)
throw new OutOfMemoryException();
}
public IntPtr Address
{
get { return _heap; }
}
public IntPtr Allocate(HeapFlags flags, int size)
{
IntPtr memory = Win32.RtlAllocateHeap(_heap, flags, size.ToIntPtr());
if (memory == IntPtr.Zero)
throw new OutOfMemoryException();
return memory;
}
public int Compact(HeapFlags flags)
{
return Win32.RtlCompactHeap(_heap, flags).ToInt32();
}
public void Destroy()
{
Win32.RtlDestroyHeap(_heap);
}
public void Free(HeapFlags flags, IntPtr memory)
{
Win32.RtlFreeHeap(_heap, flags, memory);
}
public int GetBlockSize(HeapFlags flags, IntPtr memory)
{
return Win32.RtlSizeHeap(_heap, flags, memory).ToInt32();
}
public IntPtr Reallocate(HeapFlags flags, IntPtr memory, int size)
{
IntPtr newMemory = Win32.RtlReAllocateHeap(_heap, flags, memory, size.ToIntPtr());
if (newMemory == IntPtr.Zero)
throw new OutOfMemoryException();
return newMemory;
}
}
}