Microsoft KB Archive/93211

{|
 * width="100%"|

_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 */


 * 1) include 
 * 2) include 
 * 3) include 

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

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

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

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

block_size = _vmsize(handle2); printf(&quot;Received %d bytes of virtual memory.\n&quot;, 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