Locking and Unlocking Blocks

To retain access to a block for an arbitrarily long period of time, use the _vlock function. Like _vload, _vlock takes a handle, copies the associated block of virtual memory into DOS memory, and returns a far pointer to it. However, _vlock locks a block of memory so that it remains in DOS memory even if you make subsequent calls to the virtual memory manager. A locked block remains in DOS memory until it is unlocked with _vunlock. You can lock a block multiple times; the block is not swapped out until you have unlocked it an equal number of times. The number of locks currently held on a virtual memory block can be determined by calling _vlockcnt.

You must also specify a clean or dirty flag when you unlock a locked block of virtual memory with _vunlock. With this function, you specify the flag after you have accessed the block instead of before, as was the case with _vload. For a block that has been locked more than once, different clean or dirty flags can be specified for the _vunlock calls. If _VM_DIRTY is specified with any of the _vunlock calls, the block is treated as dirty.

You can lock a block that has already been loaded into DOS memory. If you do so, the virtual memory manager may relocate the block within DOS memory, so you should use the pointer returned by _vlock rather than the one previously returned by _vload.

Both _vload and _vlock return NULL if they are unable to load or lock a block of virtual memory. Always test the return value of these functions before using it as a pointer.

Summary: Keep as few blocks locked as possible.

Having a large number of blocks locked at any one time can interfere with the virtual memory manager's ability to swap blocks in and out of DOS memory. Therefore, you should always keep as few blocks locked as possible.