Date: | November 8, 2004 / year-entry #387 |
Tags: | history |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20041108-00/?p=37363 |
Comments: | 14 |
Summary: | Now that you know how the 16-bit memory manager handled the global heap, it's time to see how this got transitioned to the new 32-bit world. The GlobalAlloc function continued to emulate all its previous moveability rules, but the return value of GlobalAlloc was no longer a selector since Win32 used the processor in "flat... |
Now that you know how the 16-bit memory manager handled the global heap, it's time to see how this got transitioned to the new 32-bit world. The This means that the old trick of caching a selector and reallocating the memory out from under it no longer worked. Moveability semantics were preserved. Memory blocks still had a lock count, even though it didn't really accomplish anything since Win32 never compacted memory. (Recall that the purpose of the lock count was to prevent memory from moving during a compaction.) Moveable memory and locking could have been eliminated completely, if it weren't for the Aside from that, moveable memory gets you nothing aside from overhead. The Next time, an insight into how locking is implemented (even though it doesn't do anything). |
Comments (14)
Comments are closed. |
QUOTE
Consequently, the charade of locking must be maintained just in case there’s some application that actually snoops at the lock count, or a program that expected the GlobalReAlloc function to fail on a locked block.
/QUOTE
Does that mean that the Win32 team tried to remove locks and found that it broke some applications, or did they decide to emulate them from the start.
How much of this stuff is determined in the design phase, and how much gets found in testing? Just curious really.
Why doesn’t Win32 compact memory? The application would be able to allocate and free big GMEM_MOVEABLE blocks and not worry about fragmentation of the address space.
I would imagine that, with rare exception, fragmentation isn’t an issue anymore. Each process has 4G, after all.
If moveable memory is a thing of the past, what about the clipboard still requires the GMEM_MOVEABLE flag?
Because there are apps that crash if the memory isn’t moveable.
The clipboard doesn’t use the global heap anymore – nowadays, it’s based on the much more mondane shared memory. I guess that, since shared memory requires a nearly identical allocate/lock sequence, there would have been no value in devising an alternate API
Cooney,
each Win32 user process has 4Gb only on AMD64 boxes when they are run in WOW64. Normally, they have 2Gb or 3Gb of addressable virtual memory depending on whether /3Gb switch was used or not.
Aside from that, fragmentation is an issue, you can have problems with fragmentation of the Win32 heap and fragmentation of virtual memory, depending on what memory manager you’re using.
Dare I ask what about non movable memory crashes certain programs? I cannot fathom what sort of practice would result in that, unless someone did something absurd like checking for the flags and deliberately crashing if they were not there.
Then again, I bet Raymond has seen it all by now.
Some programs make undocumented assumptions about how moveable memory happens to be implemented. Here’s an MSDN article that describes the internals of the heap manager:
http://msdn.microsoft.com/library/en-us/dngenlib/html/msdn_heapmm.asp
Instead of using the documented GlobalLock function, those bad apps reach in and dereference the global handle table directly.
You can see what goes wrong if the memory is not actually moveable.
Ivo: You’d have to do more than that. You would have to make sure *every* allocation (especially the small ones) is allocated moveable so that a compaction will move it. Do you really want to go back to the world of locking all memory before using it (and remembering to unlock it when done)?
For one thing, it means no more "new" operator (since "new" allocates non-moveable memory). I doubt many programs would be willing to give up "new".
Andrei: isn’t the 4GB addressable area going to cause backwards compatibility problems for Win32 processes? The reason they’re not given 3GB by default in 32-bit is because programmers were always assuming a 2GB stack i.e. the highest bit being zero.
Darren
IIRC, only applications which are marked "large address aware" are give 4GB on AMD64.
http://www.amd.com/us-en/assets/content_type/DownloadableAssets/Expand_Memory_of_32-bit_App_-_Microsoft_4GT-_6204.pdf
Cooney: let me tell you that CAD/CAM applications are certainly starting to feel some pressure from 2GB/4GB heap fragmentation issues.
Specialized apps like that should build their own memory manager. There’s no reason to mess with one that does the right thing in most circumstances unless there’s a major bug or it’s being redesigned from scratch. MS might work with them, but generally won’t rewrite its OS for one or two small segments of the population.