INF: Minimizing Lock and Unlock Calls in Protected Mode

ID Number: Q74197

3.00 3.10

WINDOWS

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