_vmalloc() May Allocate Larger Memory Block Than Expected

Last reviewed: July 17, 1997
Article ID: Q93211
7.00 | 1.00 1.50 MS-DOS | WINDOWS kbprg

The information in this article applies to:

  • Microsoft C/C++ Compiler for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 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 reference words: kbinf 7.00 1.00 1.50 _vmalloc _vrealloc
KBCategory: kbprg
KBSubcategory: VirtualMem
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 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.