Click here to Skip to main content
15,879,348 members
Articles / Desktop Programming / MFC
Article

Low Fragmentation Heap and Function Interception

Rate me:
Please Sign up or sign in to vote.
2.50/5 (8 votes)
14 Jan 20042 min read 41.6K   330   16   4
How to implement a low fragmentation heap by intercepting HeapAlloc.

Introduction

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.

Background

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.

// Set LFH on for this processes heap
SetLFH( GetProcessHeap(), true);

// Allocate using new allocators
pTest = new CTest;
//

Conclusion

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...

History

  • 15th Jan, 2005 - Article posted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Austria Austria
C++/C# developer (MSCD)
SQL DBA (MCDBA)

Comments and Discussions

 
QuestionWhy is this recreated again? Pin
dog_spawn15-Jan-04 7:55
dog_spawn15-Jan-04 7:55 
AnswerRe: Why is this recreated again? Pin
Anonymous15-Jan-04 9:39
Anonymous15-Jan-04 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.