Link to home
Create AccountLog in
Avatar of jimstar
jimstar

asked on

GlobalAlloc - where does the memory come from?

I'm looking for an explanation of where the memory returned from GlobalAlloc comes from. Based on the fact that this memory can be accessed across module and process boundaries, it seems that this must be a system-wide heap that is mapped into the same virtual address for all processes. Is this essentially how it works?

If this is the case, then the pages that malloc allocates from the OS must come from a different heap since those addresses are not valid across modules. However, when reading about heap management in win32, it looks like there are *not* two different heaps (though back in Win 3.1 there used to be the docs say).
SOLUTION
Avatar of Anthony2000
Anthony2000
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Here is a link which discusses the topic in greater detail:

http://msdn2.microsoft.com/en-us/library/ms810603.aspx
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of jimstar
jimstar

ASKER

Thanks for your comments. A few clarifications:

1. Does Windows pre-allocate a chunk of memory for use as the heap at startup, and then offer processes memory from those pages when requested, or does it actually allocate the heap memory as processes request it? I'd think for optimization purposes it'd have a pre-allocated pool to use, and increase it as needed, though I can't find docs to prove this theory.

2. Unless I'm mistaken, Windows would need to keep process-specific heap memory (such as memory requested through malloc) in a page entirely dedicated to that process - since the VMM maps full pages, it wouldn't be possible to store Process A and Process B's malloc'd data on the same page. This would seem to mean it has several pages dedicated to globally-accessible heap data, which are then mapped into each process at the same VM address. Is this how it works?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of jimstar

ASKER

Ok, I'm getting there. Let's say I have two processes - Process A and Process B.

Sequence of Events:
1. Process A allocates pBuffer1 (8 bytes) using malloc.
2. Process A allocates pBuffer2 (8 bytes) using GlobalAlloc.
3. Process A allocates pBuffer3 (8 bytes) using malloc.

--> At this point, Process B should be able to access pBuffer2 in its address space, but it should not be able to access pBuffer1 or pBuffer3. To accomplish this, it would seem that pBuffer1 and pBuffer3 need to be on a different memory page than pBuffer2 - right? Since memory is mapped/protected by page, malloc data and GlobalAlloc data cannot share a page, otherwise malloc data could potentially be mapped into another process when the GlobalAlloc data is mapped, if they happen to fall on the same page.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.