RTE OSAdapter POSIX 0.1.0 build 41b3eb3
POSIX/Linux OAL OSAdapter for Platform_RTE
Loading...
Searching...
No Matches
rte_posix_osadapter_memory.c File Reference

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

#include "rte/posix_osadapter/rte_posix_osadapter.h"
#include "rte/utils/cast/rte_cast.h"
#include "rte_posix_osadapter_lock.h"
#include "rte_posix_osadapter_report.h"
#include <pthread.h>
#include <stdint.h>
#include "rte/oal/memory/rte_mem_util.h"
Include dependency graph for rte_posix_osadapter_memory.c:

Go to the source code of this file.

Data Structures

struct  posix_mem_pool_state_t

Macros

#define _POSIX_C_SOURCE   200809L
#define RTE_POSIX_MEM_ARENA_SIZE   (1024U * 1024U)
#define RTE_POSIX_MEM_LOCK_TIMEOUT_MS   50U

Typedefs

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

Functions

static rte_status_t osadapter_create (rte_mem_pool_storage_t *storage, const rte_mem_pool_config_t *config, rte_mem_pool_handle_t *out_handle)
static rte_status_t osadapter_acquire (rte_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 rte_status_t osadapter_release (rte_mem_pool_handle_t handle, void *block)
static rte_status_t osadapter_stats (rte_mem_pool_handle_t handle, size_t *out_free_blocks, size_t *out_used_blocks)
static rte_status_t mem_lock (void)
const rte_osadapter_memory_t * rte_posix_osadapter_memory (void)
 The POSIX rte_memory OSAdapter (static arena, no malloc). Its operations return RTE_STATUS_INVALID_STATE until rte_posix_osadapter_memory_init() has succeeded.
rte_status_t rte_posix_osadapter_memory_init (void)
 Creates the memory OSAdapter's pool lock as a priority-inheritance mutex (OSA rule R2, REQ-OAL-MUTEX-004). Composition-root wiring: rte_posix_osadapter_register_all() calls it before it registers the memory OSAdapter; a caller that registers rte_posix_osadapter_memory() on its own calls it first. Call at single-threaded startup, before any thread uses the pools.

Variables

static uint8_t g_arena [RTE_POSIX_MEM_ARENA_SIZE]
static size_t g_arena_used = 0U
static pthread_mutex_t g_mem_lock
static bool g_mem_initialised = false
static const rte_osadapter_memory_t s_posix_memory_osadapter

Detailed Description

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

This is static partitioning, not malloc()/free(): the arena's total size (RTE_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 (RTE_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 RTE_STATUS_INVALID_PARAM rather than silently corrupting adjacent memory.

Concurrency: the global arena cursor (g_arena_used) and every pool's free list are guarded by one process-wide mutex (g_mem_lock). Each critical section is a handful of pointer/counter updates with no allocation and no blocking call inside, so the hold time is bounded. (An earlier version left this unsynchronised and documented the limitation; acquire()/release() from several threads corrupted the free list.) g_mem_lock is a priority-inheritance mutex (OSA rule R2, same as every rte_mutex, REQ-OAL-MUTEX-004), created at init by rte_posix_osadapter_memory_init() (called by rte_posix_osadapter_register_all()), not statically initialised. If the host refuses priority inheritance, init fails and reports the refusal; there is no plain-mutex fallback. Until init has succeeded every vtable operation returns RTE_STATUS_INVALID_STATE without touching the lock. The init flag is written once, at single-threaded startup before any caller thread exists, and only read afterwards.

Every wait for g_mem_lock is bounded (OSA rule R2, REQ-OAL-MEM-015): at most RTE_POSIX_MEM_LOCK_TIMEOUT_MS (default 50 ms), through posix_osa_lock_bounded(), the same wait as rte_mutex_lock_timed() (rte_posix_osadapter_lock.h: Linux pthread_mutex_timedlock(), other hosts a trylock loop without priority-inheritance boost). When the bound expires the operation returns RTE_STATUS_TIMEOUT and leaves the pool unchanged: create claims no arena space, acquire sets *out_block to NULL, release leaves the block with the caller (who may retry), stats leaves its outputs unwritten. No safe-state report on a timeout: the status reaches the caller.

Definition in file rte_posix_osadapter_memory.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 4 of file rte_posix_osadapter_memory.c.

◆ RTE_POSIX_MEM_ARENA_SIZE

#define RTE_POSIX_MEM_ARENA_SIZE   (1024U * 1024U)

Local makros

Definition at line 65 of file rte_posix_osadapter_memory.c.

◆ RTE_POSIX_MEM_LOCK_TIMEOUT_MS

#define RTE_POSIX_MEM_LOCK_TIMEOUT_MS   50U

Definition at line 71 of file rte_posix_osadapter_memory.c.

Typedef Documentation

◆ posix_mem_pool_storage_fits_

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

Definition at line 86 of file rte_posix_osadapter_memory.c.

Function Documentation

◆ block_belongs_to_pool()

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

Definition at line 256 of file rte_posix_osadapter_memory.c.

◆ mem_lock()

rte_status_t mem_lock ( void )
static

Definition at line 323 of file rte_posix_osadapter_memory.c.

◆ osadapter_acquire()

rte_status_t osadapter_acquire ( rte_mem_pool_handle_t handle,
void ** out_block )
static

Definition at line 223 of file rte_posix_osadapter_memory.c.

◆ osadapter_create()

rte_status_t osadapter_create ( rte_mem_pool_storage_t * storage,
const rte_mem_pool_config_t * config,
rte_mem_pool_handle_t * out_handle )
static

Local function declarations

Definition at line 163 of file rte_posix_osadapter_memory.c.

◆ osadapter_release()

rte_status_t osadapter_release ( rte_mem_pool_handle_t handle,
void * block )
static

Definition at line 269 of file rte_posix_osadapter_memory.c.

◆ osadapter_stats()

rte_status_t osadapter_stats ( rte_mem_pool_handle_t handle,
size_t * out_free_blocks,
size_t * out_used_blocks )
static

Definition at line 299 of file rte_posix_osadapter_memory.c.

◆ rte_posix_osadapter_memory()

const rte_osadapter_memory_t * rte_posix_osadapter_memory ( void )

The POSIX rte_memory OSAdapter (static arena, no malloc). Its operations return RTE_STATUS_INVALID_STATE until rte_posix_osadapter_memory_init() has succeeded.

Global variables declarations Global functions

Definition at line 110 of file rte_posix_osadapter_memory.c.

◆ rte_posix_osadapter_memory_init()

rte_status_t rte_posix_osadapter_memory_init ( void )

Creates the memory OSAdapter's pool lock as a priority-inheritance mutex (OSA rule R2, REQ-OAL-MUTEX-004). Composition-root wiring: rte_posix_osadapter_register_all() calls it before it registers the memory OSAdapter; a caller that registers rte_posix_osadapter_memory() on its own calls it first. Call at single-threaded startup, before any thread uses the pools.

Idempotent: once it has succeeded, a further call returns RTE_STATUS_OK and does not create the lock again. There is no fallback to a plain mutex: if the host refuses priority inheritance the pool stays uninitialised (every rte_osadapter_memory_t operation keeps returning RTE_STATUS_INVALID_STATE) and the refusal is reported as DEGRADED / RTE_SAFESTATE_REASON_OSA_RT_ATTRIBUTE.

Returns
RTE_STATUS_OK if the lock exists (created now or by an earlier call); RTE_STATUS_INTERNAL_ERROR if pthread_mutexattr_init(), pthread_mutexattr_setprotocol(PTHREAD_PRIO_INHERIT) or pthread_mutex_init() failed.

Definition at line 115 of file rte_posix_osadapter_memory.c.

Variable Documentation

◆ g_arena

uint8_t g_arena[RTE_POSIX_MEM_ARENA_SIZE]
static

Local variables declarations

Definition at line 99 of file rte_posix_osadapter_memory.c.

◆ g_arena_used

size_t g_arena_used = 0U
static

Definition at line 100 of file rte_posix_osadapter_memory.c.

◆ g_mem_initialised

bool g_mem_initialised = false
static

Definition at line 102 of file rte_posix_osadapter_memory.c.

◆ g_mem_lock

pthread_mutex_t g_mem_lock
static

Definition at line 101 of file rte_posix_osadapter_memory.c.

◆ s_posix_memory_osadapter

const rte_osadapter_memory_t s_posix_memory_osadapter
static
Initial value:
= { osadapter_create, osadapter_acquire, osadapter_release,
osadapter_stats }
static rte_status_t osadapter_create(rte_mem_pool_storage_t *storage, const rte_mem_pool_config_t *config, rte_mem_pool_handle_t *out_handle)

Definition at line 103 of file rte_posix_osadapter_memory.c.