Intrinsic-73 Malloc

Top  Previous  Next

73: segment address:= MAlloc(paragraphs);


This intrinsic returns the starting segment address of a block of memory. This is used with EXPL's segment variables to access large blocks of memory. 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.


Segment Variables. Segment Variables are based on the segmented-architecture available on all 80x86 processors. It was originally used to address up to 1 megabyte of memory using 16-bit integers. A segment variable consists of a 16-bit Segment and Offset. The Segment is multiplied by 16 and added to the offset to give an address with a one megabyte range. In the real 80x86 architecture, multiple segments overlap the same block of memory. For example, Segment-1 Offset-32 is overlapped by Segment-2, Offset-16:


Segment 1 * 16 + Offset 32 = 48

Segment 2 * 16 + Offset 16 = 48


EXPL Segment Variable Simulation. EXPL simulates this architecture, which enables it to run legacy programs that use the Segment/Offset feature. The simulation is not exact. Unlike real segments, the EXPL segments don't overlap. In fact each EXPL segment is not guaranteed to be contiguous, and they can be in entirely unrelated blocks of memory.


Memory Allocations. In 16-bit EXPL, the segments and offset are limited to 16-bit values so the maximum allocation is 16 times 64K or 1 megabyte. In 32-bit EXPL, each segment can hold up to 2-gigbytes depending on how much virtual memory is available in the operating system.


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.