3/07/2009

03-07-09 - Malloc

Arseny Kapoulkine has a nice new blog post about replacing CRT malloc .

I looked into it a while ago and gathered these links on the subject :

What your mother never told you about graphics development Fighting against CRT heap and winning
TRET The Reverse Engineering Toolkit
The Bag of Holding
somecode Memtracer
Practical Efficient Memory Management EntBlog
New Fun Blog - Scott Bilas Figuring out how to override malloc and new
MemTracer strikes back .mischief.mayhem.soap.
Lightning Engine � Blog Archive � Tracking Memory Allocations
Gamasutra - Monitoring Your PC's Memory Usage For Game Development
Fast File Loading EntBlog
Detours - Microsoft Research
CodeProject Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++. Free source code and programming help
A Cross-Platform Memory Leak Detector

then I decided it was a pain in the ass and I wasn't going to do it. This is one place where C++ got it so much better - you can just override new/delete and it all works. I never use malloc/free anyway, so WTF the few people who do use them can go to the CRT heap, what do I care?

The new cblib has a simple lock-free paged small block allocator. It's very primitive and not as fast or efficient as it could be (it's very fast for single threaded but has very poor cache line sharing behavior), but on the plus side it is nice and simple and self-contained unlike tcmalloc or hoard or something which are like a huge mess of code.

BTW related to this all and the threading stuff is : LeapHeap

Hmm these were in that link bag and don't belong, but they are quite fun -
HAKMEM -- CONTENTS -- DRAFT, NOT YET PROOFED
Hacker's Delight

5 comments:

Autodidactic Asphyxiation said...

Another linky that belongs in your non-belonging link bag:

http://graphics.stanford.edu/~seander/bithacks.html

ent said...

Hi charles,

Thanks for linking back two of my articles.

The problem with only overloading the new operator is that you lose all the allocations happening in 3rdparties linked as dll.

It is a pity because if you want the full tracking memory scenario you need to resort to this hacks...

Tom said...

We've gone with the Scott Bilas approach of taking bits out of the runtime library, but I ended up managing to remove ALL the memory allocation functions from the library. Equivalents are then provided in the game.

I think I started out by looking through the C runtime source for all the functions that had the word "alloc" in their names, and created a new library with their objects removed. Then I compiled the game, linking it with my new libraries, and wrote equivalent functions in the game, following the linker errors.

There are some internal allocation routines that need implementing (Bilas mentions __recalloc_crt, but I think there were a couple more too...), if you're linking with the debug libraries then you'll need _malloc_dbg and the like, and I'm sure there were a couple more surprises along the way. But this is all details; MS provide the runtime source code, so it's easy enough to work out what each function needs to do.

"A million dependencies" is over-egging things, if you ask me, but that's not to say it's as easy as gcc -wrap. I think it took about 2-3 hours from first having the idea to getting it basically working, then another hour a couple of days later because I'd managed to miss something.

(The C++ side is easier. The C++ RTL has one .obj that contains new/new[]/delete/delete[], as I recall, and once you remove that you're golden.)

cbloom said...

"But this is all details; MS provide the runtime source code, so it's easy enough to work out what each function needs to do."

I've always wondered if you could actually take the MS CRT source and just compile it and have your own CRT (and thus be able to change or remove things you don't like).

eg. is it complete or did they leave out huge pieces? Is there some evil license on it?

Tom said...

I'm sure I remember reading somewhere in the online help that there are some FP support routines that you don't get the code for -- they're top secret, or something like that.

When I looked again just now, I couldn't find anything about it though. Did find this page with some licence-related notes:

http://benjamin.smedbergs.us/blog/2008-01-10/patching-the-windows-crt/

Sounds like our PC version can ship as-is. (But we statically link with all the libraries anyway, so it's not like anybody would know :)

old rants