Loads a virtual memory block into DOS memory and locks it.
#include <vmemory.h>
void __far *__far _vlock( _vmhnd_t handle );
handle | Handle to previously allocated virtual memory block |
The _vlock function loads a virtual memory block into DOS memory, locks it, and returns a far pointer to it. The argument handle points to a virtual memory block previously allocated through a call to _vmalloc or _vrealloc.
A locked virtual memory block will not be swapped out until it is unlocked. A virtual memory block can be locked up to 255 times. The pointer returned by _vlock remains valid until an equal number of unlock operations is performed.
Since DOS memory may be scarce, try to keep the number of blocks locked at one time to a minimum and use _vunlock to unlock them as soon as possible.
The _vlock function returns a far pointer to DOS memory if the virtual memory block is successfully loaded and locked. If insufficient DOS memory is available, _vload returns NULL.
Standards:None
16-Bit:DOS
32-Bit:None
/* VLOCK.C: This program locks a block of virtual memory using _vlock,
* writes to it, loads in a new block with _vload, and then verifies
* that the contents of the locked block are still accessible. It then
* unlocks the block with _vunlock.
*/
#include <stdio.h>
#include <stdlib.h>
#include <vmemory.h>
void main( void )
{
int i, flag;
_vmhnd_t handle1,
handle2;
int __far *buffer1;
int __far *buffer2;
if ( !_vheapinit( 0, _VM_ALLDOS, _VM_XMS | _VM_EMS ) )
{
printf( "Could not initialize virtual memory manager. \n" );
exit( -1 );
}
if ( ( (handle1 = _vmalloc( 100 * sizeof(int) )) == _VM_NULL ) ||
( (handle2 = _vmalloc( 100 * sizeof(int) )) == _VM_NULL ) )
{
_vheapterm();
exit( -1 );
}
printf( "Two blocks of virtual memory allocated.\n" );
if ( (buffer1 = (int __far *)_vlock( handle1 )) == NULL )
{
_vheapterm();
exit( -1 );
}
printf( "buffer1 locked: valid until unlocked.\n" );
for ( i = 0; i 100; i++ ) // write to buffer1
buffer1[i] = i;
if ( (buffer2 = (int __far *)_vload( handle2, _VM_DIRTY )) == NULL )
{
_vheapterm();
exit( -1 );
}
printf( "buffer2 loaded. buffer 1 still valid.\n" );
flag = 0;
for ( i = 0; i 100; i++ )
if ( buffer1[i] != i )
flag = 1;
if ( !flag )
printf( "Contents of buffer1 verified.\n" );
_vunlock( handle1, _VM_DIRTY );
_vfree( handle1 );
_vfree( handle2 );
_vheapterm();
exit( 0 );
}
Two blocks of virtual memory allocated.
buffer1 locked: valid until unlocked.
buffer2 loaded. buffer 1 still valid.
Contents of buffer1 verified.