TikuOS: Simple. Ubiquitous. Intelligence, Everywhere.

An open-source operating system for microwatt computers.
Deploy-and-forget embedded systems that run for years on a coin cell or indefinitely on harvested energy.

API Reference / Memory Management

Module · Memory Management

Memory Management

TikuOS has no heap. Instead, it provides a small toolbox of static allocators — arenas, pools, write-back caches, and a persistent key-value store — each backed by caller-provided buffers in either SRAM or FRAM. A tier abstraction lets a caller request memory from a specific tier, and an MPU layer protects FRAM from accidental writes. The result: predictable memory behavior, no fragmentation, and safe handling of non-volatile state.

Header: kernel/memory/tiku_mem.h.

Initialization

FunctionDescription
tiku_mem_init()Entry point for the memory subsystem. Initializes the region registry, activates MPU protection, and performs platform-specific memory setup. Call once during boot.

Error Codes

typedef enum {
    TIKU_MEM_OK            =  0,    /* Success                              */
    TIKU_MEM_ERR_INVALID   = -1,    /* Invalid argument                     */
    TIKU_MEM_ERR_NOMEM     = -2,    /* Out of memory                        */
    TIKU_MEM_ERR_FULL      = -3,    /* Store is full                        */
    TIKU_MEM_ERR_NOT_FOUND = -4     /* Key not found                        */
} tiku_mem_err_t;

Memory Tiers

typedef enum {
    TIKU_MEM_SRAM = 0,  /* Fast, volatile                                   */
    TIKU_MEM_NVM  = 1,  /* Persistent, slower writes (FRAM)                 */
    TIKU_MEM_AUTO = 2   /* OS selects: prefers SRAM, falls back to NVM      */
} tiku_mem_tier_t;

Arena Allocator

Bump-pointer allocator: hand out aligned memory from a fixed buffer, reset all allocations at once. There is no individual free operation.

typedef struct {
    uint8_t              *buf;
    tiku_mem_arch_size_t  capacity;
    tiku_mem_arch_size_t  offset;
    tiku_mem_arch_size_t  peak;
    tiku_mem_arch_size_t  count;
    uint8_t               id;
    uint8_t               active;
    tiku_mem_tier_t       tier;
} tiku_arena_t;
FunctionDescription
tiku_arena_create(arena, buf, size, id)Initialize over a caller-provided buffer with region validation.
tiku_arena_create_raw(arena, buf, size)Initialize without region-registry checks (for library code).
tiku_arena_alloc(arena, size)Allocate aligned memory. Returns NULL if full.
tiku_arena_reset(arena)Reset offset to zero. O(1). Preserves peak statistics.
tiku_arena_secure_reset(arena)Zero all memory, then reset. O(n).
tiku_arena_stats(arena, stats)Snapshot of usage statistics.

Pool Allocator

Fixed-size block allocator with an embedded freelist. O(1) allocation and free; zero per-block metadata overhead.

typedef struct {
    uint8_t              *buf;
    tiku_mem_arch_size_t  block_size;
    tiku_mem_arch_size_t  block_count;
    void                 *free_head;
    tiku_mem_arch_size_t  used_count;
    tiku_mem_arch_size_t  peak_count;
    uint8_t               id;
    uint8_t               active;
    tiku_mem_tier_t       tier;
} tiku_pool_t;
FunctionDescription
tiku_pool_create(pool, buf, block_size, block_count, id)Initialize with region validation.
tiku_pool_create_raw(pool, buf, block_size, block_count)Initialize without region checks.
tiku_pool_alloc(pool)Pop a block from the freelist. Returns NULL if empty.
tiku_pool_free(pool, ptr)Push a block back. Validates alignment and range.
tiku_pool_reset(pool)Return all blocks to the freelist. O(n).
tiku_pool_stats(pool, stats)Snapshot of usage statistics.

Region Registry

Platform memory map management and overlap detection. Allows allocators to verify that a given buffer lives in a permitted region (e.g. SRAM, FRAM) and claim exclusive ownership.

FunctionDescription
tiku_region_init(table, count)Register platform memory regions.
tiku_region_contains(ptr, size, type)Check if a range falls within a region of the expected type.
tiku_region_claim(ptr, size, owner_id)Claim a memory range for a subsystem.
tiku_region_unclaim(ptr)Release a previously claimed range.
tiku_region_get_type(ptr, out_type)Look up the region type for an address.

Persistent NVM Key-Value Store

Stores small, named values in FRAM that survive power cycles. Each key is associated with a fixed-size FRAM buffer; reads copy into SRAM and writes go directly to FRAM under MPU control.

FunctionDescription
tiku_persist_init(store)Initialize the store and recover valid entries from NVM.
tiku_persist_register(store, key, fram_buf, capacity)Register an NVM buffer under a key.
tiku_persist_read(store, key, buf, buf_size, out_len)Read a value into an SRAM buffer.
tiku_persist_write(store, key, data, data_len)Write a value from SRAM into NVM.
tiku_persist_delete(store, key)Delete an entry.
tiku_persist_wear_check(store, key, write_count)Check write endurance level.

MPU (Memory Protection Unit)

Protects FRAM from accidental writes. In normal operation the NVM segment is mapped read-only; code that needs to write FRAM must explicitly unlock the MPU, perform the write, and lock it again. Violations can be configured to trigger either a reset or an NMI.

typedef enum { TIKU_MPU_SEG1, TIKU_MPU_SEG2, TIKU_MPU_SEG3 } tiku_mpu_seg_t;

typedef enum {
    TIKU_MPU_READ    = 0x01,
    TIKU_MPU_WRITE   = 0x02,
    TIKU_MPU_EXEC    = 0x04,
    TIKU_MPU_RD_WR   = 0x03,
    TIKU_MPU_RD_EXEC = 0x05,
    TIKU_MPU_ALL     = 0x07
} tiku_mpu_perm_t;
FunctionDescription
tiku_mpu_init()Initialize the MPU with default NVM protection.
tiku_mpu_set_permissions(seg, perm)Set permissions on one segment.
tiku_mpu_unlock_nvm()Unlock NVM for writing. Returns the saved state to be restored later.
tiku_mpu_lock_nvm(saved)Restore MPU state after an NVM write.
tiku_mpu_scoped_write(fn, ctx)Execute fn with NVM unlocked and interrupts disabled.
tiku_mpu_enable_violation_nmi()Switch the MPU violation response from reset to NMI.
tiku_mpu_get_violation_flags()Read per-segment violation flags.
tiku_mpu_clear_violation_flags()Clear all violation flags.

Tier Allocator

Creates arenas and pools backed by a specific memory tier (SRAM or NVM). Useful when a subsystem wants to request "some NVM memory" without caring which buffer it ends up in.

FunctionDescription
tiku_tier_init()Initialize the tier allocator. Call after tiku_mem_init().
tiku_tier_arena_create(arena, tier, size, id)Create a tier-backed arena.
tiku_tier_pool_create(pool, tier, block_size, block_count, id)Create a tier-backed pool.
tiku_tier_get(ptr, out_tier)Query which tier a pointer belongs to.
tiku_tier_stats(tier, stats)Usage statistics for a tier's backing pool.

Write-Back Cache

Pairs an SRAM working copy with a FRAM backing store. Reads and writes hit the fast SRAM copy; dirty regions are flushed to FRAM on demand or before entering low-power mode. This is how frequently-touched persistent state (e.g. boot tables) avoids wearing out the FRAM.

typedef struct {
    uint8_t              *sram_cache;
    uint8_t              *fram_backing;
    tiku_mem_arch_size_t  size;
    uint8_t               dirty;
    uint8_t               active;
} tiku_cached_region_t;
FunctionDescription
tiku_cache_create(region, fram_addr, sram_buf, size)Create a cached region.
tiku_cache_get(region)Get the SRAM pointer (marks dirty).
tiku_cache_mark_dirty(region)Explicitly mark as dirty.
tiku_cache_flush(region)Flush one region to FRAM.
tiku_cache_flush_all()Flush all dirty regions. Call before LPM entry.
tiku_cache_reload(region)Reload SRAM from FRAM.
tiku_cache_destroy(region)Destroy and unregister a cached region.
tiku_cache_get_count()Number of registered cached regions.
tiku_cache_get_region(index)Get a cached region by index.

Process Memory Context

Binds an SRAM scratch arena, an NVM persistent arena, and any attached cached regions to a single process. Lets a process allocate memory that is freed when it exits.

FunctionDescription
tiku_proc_mem_create(pmem, pid, tier, sram_size, nvm_size)Create an isolated memory context.
tiku_proc_mem_destroy(pmem)Flush caches, reset arenas, deactivate.
tiku_proc_alloc(pmem, tier, size)Allocate within a process context.
tiku_proc_mem_attach_cache(pmem, region)Attach a cached region to the context.
tiku_proc_mem_stats(pmem, tier, stats)Stats for a process arena.

Hibernate / Resume

Flushes caches to FRAM, writes a hibernate marker, and later checks that marker on boot to decide whether the system is resuming from a warm state.

FunctionDescription
tiku_mem_hibernate(fram_buf, timestamp)Flush caches and write the hibernate marker to FRAM.
tiku_mem_resume(fram_buf, marker_out)Check for a warm resume and reload caches if the marker is valid.
tiku_mem_hibernate_reset()Reset hibernate state (for test harnesses).