Microsoft KB Archive/93211

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

_vmalloc() May Allocate Larger Memory Block Than Expected

Q93211



The information in this article applies to:


  • The C/C++ Compiler (CL.EXE), included with:
    • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++, versions 1.0, 1.5





SUMMARY

When an application uses the Virtual Memory Manager (VMM) functions _vmalloc() or _vrealloc(), one must consider the memory allocation mechanism the VMM uses. The text below provides the details of these two mechanisms.



MORE INFORMATION

When the VMM requests memory from the virtual memory pool, two allocation schemes are available: block allocation and page allocation. The VMM uses the block allocation scheme when the application requests fewer than 2042 bytes. In the block allocation scheme, the VMM allocates the exact number of bytes requested by the application.

If the application requests 2042 or more bytes, the VMM uses the page allocation scheme to allocate a multiple of 2048 bytes of memory. The VMM adds six bytes for the block header to the size of the allocation and rounds the result up to the next multiple of the virtual page size (2048 bytes). The VMM function _vmsize() returns the number of bytes allocated, not including the block header.

The sample code below illustrates using these functions.

Sample Code

/*
 * Compile options needed: none
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <vmemory.h>

void main()
{
   _vmhnd_t handle, handle2;
   unsigned long block_size;
   if (!_vheapinit(0, _VM_ALLDOS, _VM_XMS | _VM_EMS))
      {
      printf("Could not initialize virtual memory manager.\n");
      exit(-1);
      }

   printf("Requesting 100 bytes of virtual memory.\n");
   if ((handle = _vmalloc(100)) == _VM_NULL)
      {
      _vheapterm();
      exit(-1);
      }

   block_size = _vmsize(handle);
   printf("Received %d bytes of virtual memory.\n", block_size);

   printf("Requesting 2046 bytes of virtual memory.\n");
   if ((handle2 = _vmalloc(2046)) == _VM_NULL)
      {
      _vheapterm();
      exit(-1);
      }

   block_size = _vmsize(handle2);
   printf("Received %d bytes of virtual memory.\n", block_size);

   _vfree(handle2);
   _vheapterm();
} 

Additional query words: kbinf 7.00 1.00 1.50 _vmalloc _vrealloc

Keywords : kb16bitonly
Issue type :
Technology : kbVCsearch kbAudDeveloper kbCVCComp


Last Reviewed: May 8, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.