Memory management

Memory management module has been introduced:

  • to dynamically allocate memory,
  • to supersede mechanisms from standard C language library,
  • to allow memory alignment for adjusting memory allocation.

Library functions have been written to enable the use of different areas of one memory as independent memory pools in that dynamic memory blocks will be allocated. The module can also join many memories or areas into one, cohesive space.

First aim that should be performed is to prepare the memory pool by using stMemoryInit function. Just after it can be used stMemoryAlloc to dynamically allocate memory blocks and stMemoryFree to release allocated memory blocks in a specified memory pool.

If it is necessary to check free memory, the stMemoryGetInfo can be used. This function is included into compilation, by default. However it can be excluded to decrease output code by setting ST_GET_MEMORY_INFO_FUNC for 0.

Different areas of one memory or even different memories can be joining into one, cohesive memory by using stMemoryExpand function. It doesn't influence the way of memory addressing, but facilitate to allocate new blocks in many different areas. However, it is required that each memory should be addressed by (void *) pointer, or (if it is necessary) by its modified version by specific for compiler modifier (e.g. __far). Such a type of pointer is defined for each architecture and it is PVOID. At first smallest memory areas should be specified and next the biggest, what allows minimizing memory fragmentation. stMemoryExpand is included into compilation, by default, but to reduce the output code, it can be excluded from compilation by setting ST_MEMORY_EXPAND_FUNC for 0.

Architecture specific files defines the default memory alignment for a specific platform. Memory management uses AR_MEMORY_ALIGNMENT constant to align addresses of the allocating memory blocks. addresses are always aligned relatively to address of the memory beginning given in stMemoryInit or stMemoryExpand.

When defining memory pool it should be considered that the value is given by SIZEtype that is specific for architecture. Please remember, that when high addresses of memory (0xFFFFFF00 e.g.) are used as a memory pool beginning, the valid memory size should be given. If not - the address overflow may occurs, what will not be indicated as an error. It may cause unpredictable results.

If the memory management module will not be used, it can be excluded from compilation by setting ST_USE_MEMORY for 0.

SpaceShadow documentation