{"id":37363,"date":"2004-11-08T07:00:00","date_gmt":"2004-11-08T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/11\/08\/a-history-of-globallock-part-3-transitioning-to-win32\/"},"modified":"2004-11-08T07:00:00","modified_gmt":"2004-11-08T07:00:00","slug":"a-history-of-globallock-part-3-transitioning-to-win32","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20041108-00\/?p=37363","title":{"rendered":"A history of GlobalLock, part 3: Transitioning to Win32"},"content":{"rendered":"<p>Now that you know how the 16-bit memory manager handled the global heap, it&#8217;s time to see how this got transitioned to the new 32-bit world.<\/p>\n<p> <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/memory\/base\/globalalloc.asp\"> The <code>GlobalAlloc<\/code> function<\/a> continued to emulate all its previous moveability rules, but the return value of <code>GlobalAlloc<\/code> was no longer a selector since Win32 used the processor in &#8220;flat mode&#8221;. <\/p>\n<p> This means that the old trick of caching a selector and reallocating the memory out from under it no longer worked. <\/p>\n<p> Moveability semantics were preserved.  Memory blocks still had a lock count, even though it didn&#8217;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.) <\/p>\n<p> Moveable memory and locking could have been eliminated completely, if it weren&#8217;t for <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/memory\/base\/globalflags.asp\"> the <code>GlobalFlags<\/code> function<\/a>.  This function returns several strange bits of information&mdash;now entirely irrelevant&mdash;the most troubling of which is the lock count.  Consequently, the charade of locking must be maintained just in case there&#8217;s some application that actually snoops at the lock count, or a program that expected <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/memory\/base\/globalrealloc.asp\"> the <code>GlobalReAlloc<\/code> function<\/a> to fail on a locked block. <\/p>\n<p> Aside from that, moveable memory gets you nothing aside from overhead. <\/p>\n<p> <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/memory\/base\/localalloc.asp\"> The <code>LocalAlloc<\/code> function<\/a> also carries the moveability overhead, but since local memory was never passed between DLLs in Win16, the local heap functions don&#8217;t carry as much 16-bit compatibility overhead as the global heap functions. <code>LocalAlloc<\/code> is preferred over <code>GlobalAlloc<\/code> in Win32 for that reason.  (Of course, many functions require a specific type of memory allocation, in which case you don&#8217;t have any choice. The clipboard, for example, requires moveable global handles, and COM requires use of the task allocator.) <\/p>\n<p> <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/11\/09\/254441.aspx\"> Next time<\/a>, an insight into how locking is implemented (even though it doesn&#8217;t do anything). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that you know how the 16-bit memory manager handled the global heap, it&#8217;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 &#8220;flat [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-37363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Now that you know how the 16-bit memory manager handled the global heap, it&#8217;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 &#8220;flat [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=37363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=37363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=37363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=37363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}