SafeAPI Backend POSIX
POSIX/Linux OAL backend for safeAPIFreamwork
Loading...
Searching...
No Matches
sapi_posix_backend_memory.c File Reference

POSIX sapi_memory backend: a single fixed-size static byte arena with a per-pool intrusive free list carved out of it at sapi_mem_pool_create() time (ADR-018 section 2.3). More...

#include "safeapi/posix_backend/sapi_posix_backend.h"
#include "safeapi/utils/cast/sapi_cast.h"
#include <stdint.h>
#include "safeapi/oal/memory/sapi_mem_util.h"
Include dependency graph for sapi_posix_backend_memory.c:

Go to the source code of this file.

Data Structures

struct  posix_mem_pool_state_t

Macros

#define SAPI_POSIX_MEM_ARENA_SIZE   (1024U * 1024U)

Typedefs

typedef char posix_mem_pool_storage_fits_[(sizeof(posix_mem_pool_state_t)<=sizeof(sapi_mem_pool_storage_t)) ? 1 :-1]

Functions

static sapi_status_t backend_create (sapi_mem_pool_storage_t *storage, const sapi_mem_pool_config_t *config, sapi_mem_pool_handle_t *out_handle)
static sapi_status_t backend_acquire (sapi_mem_pool_handle_t handle, void **out_block)
static bool block_belongs_to_pool (const posix_mem_pool_state_t *state, const void *block)
static sapi_status_t backend_release (sapi_mem_pool_handle_t handle, void *block)
static sapi_status_t backend_stats (sapi_mem_pool_handle_t handle, size_t *out_free_blocks, size_t *out_used_blocks)
const sapi_mem_pool_backend_t * sapi_posix_backend_memory (void)
 The POSIX sapi_memory backend (static arena, no malloc).

Variables

static uint8_t g_arena [SAPI_POSIX_MEM_ARENA_SIZE]
static size_t g_arena_used = 0U
static const sapi_mem_pool_backend_t s_posix_memory_backend

Detailed Description

POSIX sapi_memory backend: a single fixed-size static byte arena with a per-pool intrusive free list carved out of it at sapi_mem_pool_create() time (ADR-018 section 2.3).

This is static partitioning, not malloc()/free(): the arena's total size (SAPI_POSIX_MEM_ARENA_SIZE, default 1 MiB) is a compile-time constant, every pool's claim against it is a one-way bump allocation checked against remaining arena space (SAPI_STATUS_RESOURCE_EXHAUSTED rather than growing anything), and no memory is ever returned to the arena once a pool claims it - only individual blocks are recycled within their own pool's free list. Matches REQ-OAL-MEM-001: pools are expected to be reserved during system initialization.

Known constraint: the free list is intrusive (a free block's first sizeof(void*) bytes store the pointer to the next free block in the same pool), so block_size must be >= sizeof(void*). A pool requesting a smaller block_size is rejected with SAPI_STATUS_INVALID_PARAM rather than silently corrupting adjacent memory.

Known simplification: the global arena cursor (g_arena_used) and each pool's free-list head are not protected by a lock. REQ-OAL-MEM-001 frames pool reservation as an initialization-time activity (a single thread, before concurrent operation begins), so backend_create() being unsynchronized matches that expectation; acquire()/release() on an already-created pool being called concurrently from multiple threads is NOT safe with this backend and is a documented limitation, not an oversight - a production backend needing concurrent acquire/release would need a lock or a lock-free free-list (e.g. compare-and-swap), deliberately left out of this first cut.

Definition in file sapi_posix_backend_memory.c.

Macro Definition Documentation

◆ SAPI_POSIX_MEM_ARENA_SIZE

#define SAPI_POSIX_MEM_ARENA_SIZE   (1024U * 1024U)

Local makros

Definition at line 41 of file sapi_posix_backend_memory.c.

Typedef Documentation

◆ posix_mem_pool_storage_fits_

typedef char posix_mem_pool_storage_fits_[(sizeof(posix_mem_pool_state_t)<=sizeof(sapi_mem_pool_storage_t)) ? 1 :-1]

Definition at line 56 of file sapi_posix_backend_memory.c.

Function Documentation

◆ backend_acquire()

sapi_status_t backend_acquire ( sapi_mem_pool_handle_t handle,
void ** out_block )
static

Definition at line 137 of file sapi_posix_backend_memory.c.

◆ backend_create()

sapi_status_t backend_create ( sapi_mem_pool_storage_t * storage,
const sapi_mem_pool_config_t * config,
sapi_mem_pool_handle_t * out_handle )
static

Local function declarations

Definition at line 85 of file sapi_posix_backend_memory.c.

◆ backend_release()

sapi_status_t backend_release ( sapi_mem_pool_handle_t handle,
void * block )
static

Definition at line 174 of file sapi_posix_backend_memory.c.

◆ backend_stats()

sapi_status_t backend_stats ( sapi_mem_pool_handle_t handle,
size_t * out_free_blocks,
size_t * out_used_blocks )
static

Definition at line 193 of file sapi_posix_backend_memory.c.

◆ block_belongs_to_pool()

bool block_belongs_to_pool ( const posix_mem_pool_state_t * state,
const void * block )
static

Definition at line 161 of file sapi_posix_backend_memory.c.

◆ sapi_posix_backend_memory()

const sapi_mem_pool_backend_t * sapi_posix_backend_memory ( void )

The POSIX sapi_memory backend (static arena, no malloc).

Global variables declarations Global functions

Definition at line 77 of file sapi_posix_backend_memory.c.

Variable Documentation

◆ g_arena

uint8_t g_arena[SAPI_POSIX_MEM_ARENA_SIZE]
static

Local variables declarations

Definition at line 68 of file sapi_posix_backend_memory.c.

◆ g_arena_used

size_t g_arena_used = 0U
static

Definition at line 69 of file sapi_posix_backend_memory.c.

◆ s_posix_memory_backend

const sapi_mem_pool_backend_t s_posix_memory_backend
static
Initial value:
= { backend_create, backend_acquire, backend_release,
backend_stats }
static sapi_status_t backend_create(sapi_mem_pool_storage_t *storage, const sapi_mem_pool_config_t *config, sapi_mem_pool_handle_t *out_handle)

Definition at line 70 of file sapi_posix_backend_memory.c.