Intrinsic-85 ReallocMem

Top  Previous  Next

85: segment address:= ReallocMem(Segment Address, New Size (paragraphs));


This intrinsic resizes a block of memory that has been previously allocated with the Malloc intrinsic. To resize a block of memory, you pass the previously allocated segment address and the new size. The intrinsic preservers the content of previously allocated block so the intrinsic can be used to increase the size of arrays as more space is needed.


The size of the memory block allocated is in 16-byte paragraphs, so you must divide the amount of memory by 16 to specify the amount of memory you want. For example:


       Seg:= MAlloc(4000);        \Allocate 64000 bytes


Unlike Reserve, MAlloc does not automatically release memory when returning from a procedure. If MAlloc is called at the beginning of a procedure, and the procedure is repeatedly called, more memory is allocated each time. Allocated memory is automatically released when the program terminates. Allocation can be manually released using the Release intrinsic.


Seg:= MAlloc(4000);        \Allocate 64,000 bytes

Seg:= ReallocMem(Seg,4000);        \Expand to 128,000 bytes


Memory Allocations. The actual amount of memory you can allocate depends on the hardware, the number programs that are running, and the operating system configuration. On a moderately loaded Windows system with at least 2 gigabytes of RAM, you can allocate single blocks of memory up to 1.2 gigabytes without any side effects such as slowing other applications or interfering with the operating system. Allocating many smaller blocks is easier than allocating individual big blocks. Allocations of more than 1.4 gigabytes will usually fail. This is because Windows is using around 600K of memory for Kernel Functions, and these blocks are locked into hardware memory and can't be swapped out.


Memory allocations are optimized for speed. This means that you can do thousands of small allocations in a short period of time. For example, in a test application EXPL was able to do one million reserves, of 500 bytes each, in less than a second. This is less than one microsecond per reserve. Allocating individual blocks beyond 1.2 gigabytes, however will dramatically slow the allocation process because Windows' memory manager usually needs to rearrange memory to create such a large, contiguous block of memory, and this process can take several seconds of disk swapping.


See the MAlloc intrinsic for more information on segment variables.