We help IT Professionals succeed at work.

GlobalAlloc - where does the memory come from?

jimstar
jimstar asked
on
1,413 Views
Last Modified: 2008-01-09
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).
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Here is a link which discusses the topic in greater detail:

http://msdn2.microsoft.com/en-us/library/ms810603.aspx
jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.