C Programming/Platform Reference/alloc.h

This page was imported and needs to be de-wikified.
Books should use wikilinks rather sparsely, and only to reference technical or esoteric terms that are critical to understanding the content. Most if not all wikilinks should simply be removed. Please remove {{dewikify}} after the page is dewikified.

It allocates Size bytes of heap of memory and returns handle. Size is taken as even. Odd sizes are rounded up. Allocated memory is kept in Singly linked list structure. Maximum size is 65520 bytes and minimum size is 6 bytes. It returns H_NULL on failure.

HANDLE HeapAllocThrow (unsigned long Size);

It calls HeapAlloc function and returns the corresponding handle. It throws an error on failure.

HANDLE HeapAllocESTACK (unsigned long Size);

If "Size" bytes memory is not available, it truncates expression stack. Even after reducing stack memory is not available it returns H_NULL.

HANDLE HeapAllocHigh (unsigned long Size);

It allocates the memory at the high end of memory heap and returns Handle. It moves all used blocks of memory down to heap. If memory is not available it returns H_NULL.

HANDLE HeapAllocHighThrow (unsigned long Size);

It calls HeapAllocHigh function and returns the handle. On failure it throws a memory error.

void *HeapDeref (HANDLE Handle);

It dereferences the handle and returns the memory address. To use the memory reserved by a handle, the handle must be dereferenced.

void HeapFree (HANDLE Handle);

It deallocates the memory allocated by the handle.

void HeapFreeIndir (HANDLE *HandlePtr);

It deallocates the block of memory associated with passed Handle and sets the Handle to NULL.

void *HeapAllocPtr (unsigned long Size);

It is very similar to HeapAllocHigh. It does the same job. But instead of returning a handle it returns pointer to an allocated block. It returns NULL on failure.

void *HeapAllocPtrThrow (unsigned long Size);

It calls HeapAllocPtr and returns the corresponding pointer. It throws an error on failure.

A handle represents an allocated, aligned block of memory. Its address can be retrieved by means of HeapDeref function in alloc.h. HANDLE is used for managing the memory allocated by the functions of alloc.h. Some functions from other header files use HANDLE as parameter.

The alloca function is different from the standard malloc function, in that malloc allocates memory in memory heap whereas alloca allocates it in stack memory. Hence, this storage is local. Within a function, if memory is allocated using alloca , it is deallocated automatically at the end of function. alloca is more preferred in applications which require local Dynamic Memory Allocation.

malloc allocates given byte-block in linear order. It requires continuous block of memory. HeapAlloc allocates memory which is stored in linked list structure. It is advantageous.

Many a times, memory blocks are reserved. The memory blocks which are locked for a long time should be moved high in the heap. This reduces interference of such blocks with rest of the system. This can be achieved by using HeapAllocHigh function. This rearranges the heap before requested memory allocation.

When we call malloc, memory manager assigns a pointer known as handle to allocated block. For each block Handles are assigned. So, when we want to use a large number of small size memory blocks, malloc is not quite efficient. Due to large number of Handles, handling becomes difficult. So, alloca is better option in such cases.