Win32 Heaps

Started by
1 comment, last by LessBread 14 years, 10 months ago
I've been trying to figure out how Win32 heaps work, but I've hit a stumbling block. Here's an example: If I call _get_heap_handle(), I get 0x00270000. However, when I use new or malloc, the address returned has the base page address of 0x00430000. Even when I enumerate through all the heaps (it shows 3), the default Win32 heap is at 0x00550000 and some mystery heap is at 0x001F0000. Nothing seems to match the page addresses returned by new/malloc. Clearly I'm missing something here concerning the CRT heap and where new/malloc allocate from. Are new/malloc using VirtualAlloc , like how dlMalloc works? What would actually use the CRT heap? Thanks for any help! (I'm using VS2005 and compiling for 32-bit)
Advertisement
A growable heap like the CRT one will use VirtualAlloc to create new blocks when asked for more memory than it has spare. Segments can be, and usually are, strewn all around the process address space. Something like this will give you all the bookkeeping details.
Quote:Original post by GaryNas
I've been trying to figure out how Win32 heaps work, but I've hit a stumbling block. Here's an example:

If I call _get_heap_handle(), I get 0x00270000. However, when I use new or malloc, the address returned has the base page address of 0x00430000.

Even when I enumerate through all the heaps (it shows 3), the default Win32 heap is at 0x00550000 and some mystery heap is at 0x001F0000. Nothing seems to match the page addresses returned by new/malloc.

Clearly I'm missing something here concerning the CRT heap and where new/malloc allocate from. Are new/malloc using VirtualAlloc , like how dlMalloc works? What would actually use the CRT heap?

Thanks for any help!

(I'm using VS2005 and compiling for 32-bit)


The heap handle points to the data structure that describes the heap. That structure need not be contiguous with the actual heap memory (aka the addresses dispensed through that handle). The heap data structure is likely a circular linked list storing pointers that point to the memory dispensed through the various heap functions. A crude real world analogy of this set up would be the difference between a phone book and the entire local phone system. A phone book is a list of all the phone numbers, it's not the actual switches activated when a number is dialed. In this analogy, the phone book is the heap handle and the dispensed addresses are the entire local phone system.

Don't forget HeapAlloc and related functions. [grin]

Have you read this old school doc? Managing Heap Memory in Win32

It doesn't get into that detail, but it's worth checking out if you've never checked it out before.



"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement