This article shows how to implement the Low Fragmentation Heap available in Windows 2003 Server/ XP and Windows 2000 SP5.
I originally posted this article yesterday but then discovered Detours so I've re-posted it.
The project makes use of the Detours project available from Detours from which you can download the source. I have already done this and repackaged it into a project which you can build using the IDE as the original project uses NMake.
The essential elements are that the LFH.Dll is loaded by a call to
SetLFH. During processing the attached DLL modifies the calling program's code to intercept the calls to
HeapAlloc so that all the calls are now processed by the LFH.Dll version of this function.
I'm not going to explain anything about Detours as everything you need can be downloaded from the link given above.
There's not very much to this program really as all the meat and potatoes is performed by Detours. All LFH does is maintain a linked list of heap handles for which LFH is set on and optimize the memory block size for each allocation to be a multiple of 8, 16, 32, 64, 128, 512.
Using the Code
Include the header allocator.h in the stdafx.h file and add LFH.Lib to the linker input. Two TestHarness sample programs are supplied.
SetLFH( GetProcessHeap(), true);
pTest = new CTest;
I would expect this to work on versions of Windows, pre-Win2K but I've not tested it. If you do not have Visual Studio 2003 then you need to create a Windows DLL that exports symbols and copy allocator.h, allocator.cpp to your newly created project. You will have to do the same for Detours or use the code from the Microsoft download link above.
Detours also opens up a range of program instrumentation possibilities as it's very easy to intercept any function call and produce diagnostics, tracing, performance statistics.
If you find any bugs please let me know (nicely please). My code is just as good as anybody else's... It has bugs too...
- 15th Jan, 2005 - Article posted.