Minimizing Lock and Unlock Calls in Protected Mode

Last reviewed: July 22, 1997
Article ID: Q74197
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0

SUMMARY

When Microsoft Windows is running in protected mode (standard mode or enhanced mode), the selector of a movable memory block does not change even though Windows may move the block in physical (standard mode, enhanced mode) or linear (enhanced mode) memory.

Applications that run only in protected mode can take advantage of this behavior to minimize the number of GlobalLock() and GlobalUnlock() calls.

MORE INFORMATION

The following code demonstrates how to allocate a global memory object:

   HANDLE hMem;
   LPSTR lpstr;

   if ((hMem = GlobalAlloc(GMEM_MOVEABLE, cb)) != NULL)
      if ((lpstr = GlobalLock(hMem)) != (LPSTR)NULL)
         {
         // use the memory
         }

The following code demonstrates how to free the global memory object allocated above:

   if (GlobalUnlock(hMem) != (HANDLE)NULL)
      GlobalFree(hMem);

Leaving movable global memory objects locked does not impose a memory consumption penalty in protected mode.

This technique is not appropriate for an application that runs in real mode under versions of Windows prior to version 3.1. In real mode, Windows manages a limited amount of memory (less than 640K) by moving and discarding memory blocks. When an application locks a memory block, Windows cannot move or discard the block. Therefore, an application that runs in real mode must lock an object only while the object is in use.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: KrMm
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.