detalloc: Deterministic Real-Time Allocator $PROJECT_NUMBER
C99 constant-time pool allocator for hard real-time
Loading...
Searching...
No Matches
detalloc.h File Reference

Detalloc — Phase 1: Single-Pool, Constant-Time Allocator. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
+ Include dependency graph for detalloc.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  det_config_t
 Phase-1 configuration: one fixed-size pool. More...
 

Macros

#define DETALLOC_VERSION_MAJOR   0
 
#define DETALLOC_VERSION_MINOR   1
 
#define DETALLOC_VERSION_PATCH   0
 
#define DET_ALIGN_UP(sz, a)   (((sz) + ((a)-1)) & ~((a)-1))
 
#define DET_NEW(alloc, type)   ((type *)det_alloc((alloc)))
 
#define DET_FREE(alloc, ptr)
 

Typedefs

typedef struct det_allocator det_allocator_t
 Opaque allocator handle (single pool in Phase 1).
 

Enumerations

enum  det_error_t {
  DET_OK = 0 , DET_ERR_INVALID_PARAM , DET_ERR_OUT_OF_MEMORY , DET_ERR_POOL_FULL ,
  DET_ERR_INVALID_PTR , DET_ERR_NOT_INITIALIZED
}
 Error/status codes returned by Detalloc. More...
 

Functions

DETALLOC_API size_t det_alloc_size (const det_config_t *config)
 Compute required buffer size for a given Phase-1 config.
 
DETALLOC_API det_allocator_tdet_alloc_init (void *memory, size_t size, const det_config_t *config)
 Initialize allocator over a user-provided memory buffer.
 
DETALLOC_API void * det_alloc (det_allocator_t *alloc)
 Allocate a single fixed-size block.
 
DETALLOC_API void * det_calloc (det_allocator_t *alloc)
 Allocate a zero-initialized block.
 
DETALLOC_API void det_free (det_allocator_t *alloc, void *ptr)
 Free a previously allocated block (NULL is a no-op).
 
DETALLOC_API size_t det_alloc_usable_size (det_allocator_t *alloc, void *ptr)
 Return usable size of a block.
 
DETALLOC_API void det_alloc_destroy (det_allocator_t *alloc)
 Destroy allocator structures (metadata cleanup only).
 
DETALLOC_API det_config_t det_default_config (void)
 Return a sensible Phase-1 default config.
 
DETALLOC_API const char * det_version_string (void)
 Get library version string "major.minor.patch".
 

Detailed Description

Detalloc — Phase 1: Single-Pool, Constant-Time Allocator.

Minimal API for a fixed-size, bitmap-based pool allocator with O(1) alloc/free. Designed for hard real-time use; no syscalls after init; uses user-provided memory.

Version
0.1.0
Date
2025

Definition in file detalloc.h.

Macro Definition Documentation

◆ DET_ALIGN_UP

#define DET_ALIGN_UP (   sz,
 
)    (((sz) + ((a)-1)) & ~((a)-1))

Default block size for Phase 1 (can be overridden at init-time). Default alignment for returned blocks (power of two). Align up helper.

Definition at line 80 of file detalloc.h.

◆ DET_FREE

#define DET_FREE (   alloc,
  ptr 
)
Value:
do { \
det_free((alloc), (ptr)); \
(ptr) = NULL; \
} while (0)

Free and null a pointer.

Definition at line 243 of file detalloc.h.

◆ DET_NEW

#define DET_NEW (   alloc,
  type 
)    ((type *)det_alloc((alloc)))

Allocate a typed object (one fixed-size block must fit it).

Definition at line 240 of file detalloc.h.

◆ DETALLOC_VERSION_MAJOR

#define DETALLOC_VERSION_MAJOR   0

Definition at line 61 of file detalloc.h.

◆ DETALLOC_VERSION_MINOR

#define DETALLOC_VERSION_MINOR   1

Definition at line 62 of file detalloc.h.

◆ DETALLOC_VERSION_PATCH

#define DETALLOC_VERSION_PATCH   0

Definition at line 63 of file detalloc.h.

Typedef Documentation

◆ det_allocator_t

typedef struct det_allocator det_allocator_t

Opaque allocator handle (single pool in Phase 1).

Implementation maintains:

  • user buffer base/limit
  • bitmap for free/used slots
  • fixed block size & count
  • optional lock if thread_safe=true

Definition at line 110 of file detalloc.h.

Enumeration Type Documentation

◆ det_error_t

Error/status codes returned by Detalloc.

Enumerator
DET_OK 

Operation successful

DET_ERR_INVALID_PARAM 

A parameter is invalid

DET_ERR_OUT_OF_MEMORY 

Buffer cannot accommodate configuration

DET_ERR_POOL_FULL 

Pool has no free blocks

DET_ERR_INVALID_PTR 

Pointer not owned by allocator/pool

DET_ERR_NOT_INITIALIZED 

Allocator not initialized

Definition at line 89 of file detalloc.h.

Function Documentation

◆ det_alloc()

DETALLOC_API void * det_alloc ( det_allocator_t alloc)

Allocate a single fixed-size block.

Parameters
allocAllocator handle
Returns
Pointer to block on success, or NULL if pool is full
Note
Returned pointer is aligned to config.align.
Complexity
O(1) worst-case.

◆ det_alloc_destroy()

DETALLOC_API void det_alloc_destroy ( det_allocator_t alloc)

Destroy allocator structures (metadata cleanup only).

The user-provided memory buffer is not freed.

Parameters
allocAllocator handle
Complexity
O(1).

◆ det_alloc_init()

DETALLOC_API det_allocator_t * det_alloc_init ( void *  memory,
size_t  size,
const det_config_t config 
)

Initialize allocator over a user-provided memory buffer.

No dynamic allocation; all metadata lives inside memory.

Parameters
memoryPointer to pre-allocated memory buffer (non-NULL)
sizeSize of memory buffer in bytes (>= det_alloc_size(config))
configNon-NULL Phase-1 configuration
Returns
Allocator handle on success, or NULL on error
Note
The buffer must remain valid for the allocator lifetime.
Complexity
O(1).

◆ det_alloc_size()

DETALLOC_API size_t det_alloc_size ( const det_config_t config)

Compute required buffer size for a given Phase-1 config.

Calculates metadata + bitmap + aligned payload area.

Parameters
configNon-NULL configuration
Returns
Required bytes, or 0 on error
Complexity
O(1).

◆ det_alloc_usable_size()

DETALLOC_API size_t det_alloc_usable_size ( det_allocator_t alloc,
void *  ptr 
)

Return usable size of a block.

Parameters
allocAllocator handle
ptrPointer to allocated block
Returns
Fixed block size (or 0 if invalid)
Complexity
O(1).

◆ det_calloc()

DETALLOC_API void * det_calloc ( det_allocator_t alloc)

Allocate a zero-initialized block.

Parameters
allocAllocator handle
Returns
Pointer to zeroed block, or NULL if pool is full
Complexity
O(block_size) for zeroing, O(1) for allocation.

◆ det_default_config()

DETALLOC_API det_config_t det_default_config ( void  )

Return a sensible Phase-1 default config.

Defaults:

  • block_size = DET_DEFAULT_BLOCK_SIZE
  • num_blocks = 0 (must be set by user)
  • align = DET_DEFAULT_ALIGN
  • thread_safe = false

Definition at line 5 of file detalloc.c.

References det_config_t::align, det_config_t::block_size, det_config_t::num_blocks, and det_config_t::thread_safe.

◆ det_free()

DETALLOC_API void det_free ( det_allocator_t alloc,
void *  ptr 
)

Free a previously allocated block (NULL is a no-op).

Parameters
allocAllocator handle
ptrPointer returned by det_alloc()/det_calloc()
Warning
Undefined behavior if ptr was not allocated by this allocator.
Complexity
O(1) worst-case.

◆ det_version_string()

DETALLOC_API const char * det_version_string ( void  )

Get library version string "major.minor.patch".

Definition at line 3 of file detalloc.c.