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).
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
http://msdn2.microsoft.com/en-us/library/ms810603.aspx