Click here to Skip to main content
Click here to Skip to main content

Low Fragmentation Heap and Function Interception

By , 14 Jan 2004
Rate this:
Please Sign up or sign in to vote.

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

About the Author

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

Comments and Discussions

 
QuestionLFH look-a-like? Pinmemberkpinkert3-Aug-05 9:23 
AnswerRe: LFH look-a-like? Pinmemberbobsheep3-Aug-05 18:01 
QuestionWhy is this recreated again? Pinmemberdog_spawn15-Jan-04 7:55 
Can't you just 'update' the article?
AnswerRe: Why is this recreated again? PinsussAnonymous15-Jan-04 9:39 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 15 Jan 2004
Article Copyright 2004 by bobsheep
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid