Memory Management

The memory management module has been introduced:

  • to dynamically allocate memory,
  • to supersede standard C library mechanisms,
  • to provide memory alignment control for allocations.

Library functions enable the use of distinct memory areas as independent pools from which dynamic memory blocks are allocated. The module can also combine multiple memory areas into a single, cohesive space.

The first step is to initialize the memory pool using the stMemoryInit function. Subsequently, stMemoryAlloc can be used to dynamically allocate memory blocks, and stMemoryFree to release them within a specified memory pool.

To query available free memory, stMemoryGetInfo can be used. This function is included in the build by default. However, it can be excluded to reduce code size by setting ST_GET_MEMORY_INFO_FUNC to 0.

Distinct memory areas or separate memory regions can be joined into a single, cohesive pool using the stMemoryExpand function. This does not affect memory addressing but facilitates the allocation of new blocks across multiple areas. However, each memory region must be addressable via a (void *) pointer, or a compiler-specific variant (e.g., __far). This pointer type is defined for each architecture as PVOID. Smaller memory areas should be specified first, followed by larger ones, to minimize memory fragmentation. stMemoryExpand is included by default; to reduce code size, it can be excluded from compilation by setting ST_MEMORY_EXPAND_FUNC to 0.

Architecture specific files define the default memory alignment for a specific platform. The memory management module uses the AR_MEMORY_ALIGNMENT constant to align the addresses of allocated memory blocks. Addresses are always aligned relative to the base address provided in stMemoryInit or stMemoryExpand.

When defining a memory pool, note that the size is specified using the SIZE type, which is architecture-specific. If high memory addresses (e.g., 0xFFFFFF00) are used as the start of a memory pool, ensure the specified size is valid. Otherwise, address overflow may occur without error indication, potentially causing unpredictable results.

If the memory management module is not required, it can be excluded from the build by setting ST_USE_MEMORY to 0.

SpaceShadow documentation