Hooking Project

Functions for  heap manipulation

License
Boost License 1.0.
Authors
Denis Shelomovskij

@property Heap  processHeap();

Returns default heap of the calling process.


struct  Heap;

This struct encapsulates heap manipulation functionality.

Mostly it is a convinient wrapper for WinAPI heap functions.


this(HANDLE heapHandle);

Construct a Heap from a heapHandle.


const pure nothrow @property @safe bool  associated();

Returns whether this is associated with a heap handle. It is asserted that no member functions are called for an unassociated Heap struct.

Example:
	assert(processHeap.associated);
	assert(!Heap.init.associated);
	auto h = Heap.init.handle; // assert violation

@property HANDLE  handle();

Gets the handle of the associated heap.


T[]  alloc(T = void)(size_t count, DWORD flags = 0);

Allocates a block of memory.

Also see HeapAlloc.

Throws
Exception if memory allocation failed.

void  realloc(T)(ref T[] array, size_t newCount, DWORD flags = 0);

Reallocates a block of memory (i.e. memory content is preserved).

If array is null, works as alloc.

See also HeapReAlloc.

Throws
Exception if memory reallocation failed.

void  destructiveRealloc(T)(ref T[] array, size_t newCount);

Reallocates a block of memory without preserving its content.

If array is null, works as alloc. Destructive reallocation is often needed when dealing with functions requiring unpredictable buffer size. See example.

Throws
Exception if memory reallocation failed.
Example:
	HMODULE[] enumProcessModulesHelper(HANDLE hProcess)
	{
		HMODULE[] buff;
		DWORD needed = 0;
		do
		{
			processHeap.destructiveRealloc!HMODULE(buff, max(needed + 0x100, buff.length * 2));
			scope(failure) processHeap.free(buff.ptr);
			enforce(EnumProcessModules(hProcess, buff.ptr, buff.length * HMODULE.sizeof, &needed));
			assert(needed % HMODULE.sizeof == 0);
			needed /= HMODULE.sizeof;
		}
		while(needed > buff.length);
		return buff[0 .. needed];
	}

void  free(void* p, DWORD flags = 0);

Frees a block of memory.

See also HeapFree.

Throws
Exception if memory freeing failed.