Memory Management

XWOS memory management

XWOS provides four memory management algorithms.

Simple Memory Allocator

The simple memory allocator only allocates the best-fit memory block and does not reclaim memory.

  • Advantages:
    • Simple, with very small code size;
    • Stable runtime.
  • Disadvantages: Once memory is allocated, it is never reclaimed; the free function is merely a Dummy function;
  • Applicability: Simple RTOS application scenarios or scenarios where memory reclamation is not needed.
  • Context safety: Safe in any context (interrupt, bottom half (BH), thread).
  • CAPI reference: Header <xwos/mm/sma.h>

Memory Slice Allocator

The memory slice allocator divides memory into equal-sized blocks, like cards, and links them together into a queue. Each allocation takes one block, and upon reclamation, the memory slice is returned to the queue.

  • Advantages:
    • Simple, with relatively small code size;
    • Supports free operations;
    • Runtime of allocation and free operations is stable;
    • Repeated allocation and free will not cause memory fragmentation.
  • Disadvantages:
    • Memory block size is fixed; if the requested memory is too small, memory is wasted; if the requested memory exceeds the block size, the request cannot be satisfied;
    • Two consecutively allocated memory blocks are not guaranteed to be contiguous.
  • Applicability: RTOS application scenarios with high requirements for time stability.
  • Context safety: Safe in any context (interrupt, bottom half (BH), thread).
  • CAPI reference: Header <xwos/mm/memslice.h>

Buddy Allocator

The buddy allocator, when allocating, continuously divides memory into halves until the smallest block that meets the requirement is obtained. When freeing, it checks whether the adjacent block of equal length (called a buddy) is also free; if so, it merges with the “buddy” into a larger memory block, and then continues checking whether the merged block also has a buddy that can be merged, merging upward until no further merging is possible.

  • Advantages:
    • Supports free operations;
    • Supports memory allocation requests of varying sizes;
    • Repeated allocation and free will not cause memory fragmentation.
  • Disadvantages:
    • Code is slightly more complex;
    • Due to the loop of merging and splitting, the time required for allocation and free operations is not particularly stable;
    • Memory size is fixed to powers of 2; if the requested memory is too small, memory is wasted.
  • Applicability: Application scenarios with high demand for memory reuse.
  • Context safety: Safe in any context (interrupt, bottom half (BH), thread).
  • CAPI reference: Header <xwos/mm/bma.h>

Memory Pool

The memory pool is an algorithm that combines the memory slice allocator and the buddy allocator. The basic unit for memory management is the page, where one page of memory is 4096 bytes. Page memory is managed using the buddy allocator. The memory pool also uses an object cache algorithm to create blocks of various small sizes: 8 bytes, 16 bytes, 32 bytes, 64 bytes, 96 bytes, 128 bytes, 160 bytes, 192 bytes, 256 bytes, 320 bytes, 384 bytes, 512 bytes, 768 bytes, 1024 bytes, 2048 bytes. When the requested memory is larger than 2048 bytes, page memory is allocated directly; when the requested memory is less than or equal to 2048 bytes, a block is allocated from the best-fit object cache allocator.

  • Advantages:
    • Supports free operations;
    • Supports memory allocation requests of varying sizes;
    • Repeated allocation and free will not cause memory fragmentation;
    • Small memory blocks will not waste excessive memory.
  • Disadvantages:
    • Code is complex;
    • Time required for allocation and free operations is not stable.
  • Applicability: Memory management for larger external SRAM or SDRAM, object pools for C++ and Lua VMs.
  • Context safety: Safe in any context (interrupt, bottom half (BH), thread).
  • CAPI reference: Header <xwos/mm/mempool.h>