{"id":40783,"date":"2004-02-02T07:00:00","date_gmt":"2004-02-02T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/02\/02\/the-management-of-memory-for-resources-in-16-bit-windows\/"},"modified":"2020-12-19T17:27:54","modified_gmt":"2020-12-20T01:27:54","slug":"20040202-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040202-00\/?p=40783","title":{"rendered":"The management of memory for resources in 16-bit Windows"},"content":{"rendered":"<p><a title=\"The format of string resources\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040130-00\/?p=40813\"> In a previous entry<\/a> I threatened to discuss the way resources were managed in 16-bit Windows.<\/p>\n<p>In 16-bit Windows, resources were not loaded until explicitly requested.<\/p>\n<ul>\n<li>The FindResource function located the entry for the resource in the resource directory and returned it in the form of a HRSRC.<\/li>\n<li>The LoadResource function took that resource handle, allocated some movable memory (HGLOBAL), and loaded the referenced resources off the disk into that memory.<\/li>\n<li>The LockResource function took that global handle and locked it, returning a pointer to the resource bytes themselves.<\/li>\n<li>The UnlockResource function unlocked the global handle.<\/li>\n<li>The FreeResource function freed the memory that had been allocated to hold the resource.<\/li>\n<\/ul>\n<p>Actually, it was more complicated than this. Additional bookkeeping ensure that if two people tried to load the same resource, the same memory block got used for both, and the FreeResource didn&#8217;t actually free the memory until the reference count went back to zero.<\/p>\n<p>Actually, it was even more complicated than this. If the resource was marked DISCARDABLE, then the memory wasn&#8217;t actually freed when the reference count dropped to zero. Instead, the global handle was marked as discardable (GMEM_DISCARDABLE), so the handle remained valid, but when the system came under memory pressure, the memory behind the handle would get freed, and the next time you did a LoadResource, it would get reloaded from disk.<\/p>\n<p>So now you know what that DISCARDABLE keyword in resource files means. Or at least what it used to mean. Win32 doesn&#8217;t do any of this; the DISCARDABLE flag is ignored but remains for compatibility.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A multi-step process.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-40783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>A multi-step process.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40783","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=40783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40783\/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=40783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=40783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=40783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}