System memory management

During the usual work, the system repeatable allocates and releases memory blocks of the RAM memory. It can be determined whether the blocks should be allocated either in the whole, available RAM memory or only in the specific area that is used by operating system only. Also, it can be decided which of mechanisms, used for memory allocation (dynamic memory allocation or fixed size memory blocks allocation) should be used.

Global and internal memory

While system configuration it should be determined if the system should use the whole RAM memory or only the limited area. It is defined by OS_INTERNAL_MEMORY_SIZE.

If the whole area will be used the OS_INTERNAL_MEMORY_SIZE should be set for 0 and this time a system will use stMemAlloc and stMemFree to allocate and release memory, so the memory will be allocated in user memory area. This configuration is recommended only when it is sure that memory leak in user program will not occur, as it may damage system data.

The system can use an individual memory pool. Therefore its size should be determined in OS_INTERNAL_MEMORY_SIZE. It will be created a global buffer, available for the system only, where memory allocation and release operations will be performed.

In both cases a memory management of Standard Library must be enabled, by setting value of ST_USE_MEMORY to 1. Constant OS_INTERNAL_MEMORY_SIZE is ignored if the value of OS_USE_FIXMEM_POOLS (described below) is set for 1.

Fixed-size memory pools

In case, where Sirius RTOS is used in the real-time (not general purpose) system, it is recommended to use deterministic algorithm for memory allocation like a fixed-size memory blocks allocation algorithm.

The system will use this algorithm during memory allocation and release when the OS_USE_FIXMEM_POOLS is set for 1. During system configuration it is determined how many memory pools will be created. It can be used from 1 to 4 memory pools. The size of the memory pool is defined in OS_FIX_POOL*_SIZE (where * is a memory pool number from 0 to 3). When its size is set to zero, the memory pool is disabled so will not be used. Also the size of the block that will be allocated in the memory pool should be determined. This size is defined in OS_FIX_POOL*_ITEM_SIZE constant, (where * is a memory pool number from 0 to 3).

If one memory pool is defined, the system will allocate the memory only on its area. When more memory pools are defined, the system will use the memory in an optimal way, so it will use the smallest block that is adequate to the pending operation. System tries always to allocate the memory in the first pool. If the size of the block is too small the system will check the next memory pool until find the right one. Therefore the block size in each next memory pool should be bigger than the previous one. It is recommended to use the default setting of block sizes for the memory pools. The aims of memory pools are:

  • Memory pool #0: Block size should be about: 20-32 bytes amount. This block is used when the object is opened by task. When this option is not used, memory pool can be disabled. For more information see system objects management section.
  • Memory pool #1: Block size should be 128-256 bytes. This size blocks are used for small objects, such as events, counting semaphores, timers, etc.
  • Memory pool #2: Block size should 256-512 bytes, depending on system configuration. It is used for the bigger objects such as tasks.
  • Memory pool #3: This pool is dedicated for objects as shared memory, queue of pointers, streams etc. The block size should be big enough to store both the object and its buffer. If, for example the object buffer size is 1 KB, it should be 128-512 + 1024 bytes amount.

This memory pools configuration is recommended, however it can be modified to decrease the use of memory. For example if only the events, mutexes, counting semaphores and tasks are used is a system, each memory pool can be configured to store each object type separately.

When this method is used, a fixed size memory management of Standard Library must be enabled, by setting value of ST_USE_FIXMEM to 1.

SpaceShadow documentation