mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
alloc.c File Reference

mruby/c memory management. More...

#include "alloc.h"
#include "hal.h"
Include dependency graph for alloc.c:

Go to the source code of this file.

Data Structures

struct  MEMORY_POOL

Macros

#define MRBC_ALLOC_FLI_BIT_WIDTH   9
#define MRBC_ALLOC_SLI_BIT_WIDTH   3
#define MRBC_ALLOC_IGNORE_LSBS   4
#define SIZE_FREE_BLOCKS   ((MRBC_ALLOC_FLI_BIT_WIDTH + 1) * (1 << MRBC_ALLOC_SLI_BIT_WIDTH))
#define MRBC_MIN_MEMORY_BLOCK_SIZE   sizeof(FREE_BLOCK)
#define FLI(x)
#define SLI(x)
#define RETURN_IF_NULL(ptr)
#define BLOCK_SIZE(p)
#define PHYS_NEXT(p)
#define SET_USED_BLOCK(p)
#define SET_FREE_BLOCK(p)
#define IS_USED_BLOCK(p)
#define IS_FREE_BLOCK(p)
#define SET_PREV_USED(p)
#define SET_PREV_FREE(p)
#define IS_PREV_USED(p)
#define IS_PREV_FREE(p)
#define SET_VM_ID(p, id)
#define GET_VM_ID(p)
#define BPOOL_TOP(memory_pool)
#define BPOOL_END(memory_pool)
#define BLOCK_ADRS(p)
#define MSB_BIT1_FLI   0x8000
#define MSB_BIT1_SLI   0x80
#define NLZ_FLI(x)
#define NLZ_SLI(x)
#define alloc_profile()

Typedefs

typedef struct MEMORY_POOL MEMORY_POOL

Functions

static int nlz16 (uint16_t x)
static int nlz8 (uint8_t x)
static unsigned int calc_index (MRBC_ALLOC_MEMSIZE_T alloc_size)
static void add_free_block (MEMORY_POOL *pool, FREE_BLOCK *target)
static void remove_free_block (MEMORY_POOL *pool, FREE_BLOCK *target)
static FREE_BLOCK * split_block (FREE_BLOCK *target, MRBC_ALLOC_MEMSIZE_T size)
static void merge_block (FREE_BLOCK *target, FREE_BLOCK *next)
void mrbc_init_alloc (void *ptr, unsigned int size)
void mrbc_cleanup_alloc (void)
void * mrbc_raw_alloc (unsigned int size)
void * mrbc_raw_alloc_no_free (unsigned int size)
void * mrbc_raw_calloc (unsigned int nmemb, unsigned int size)
void mrbc_raw_free (void *ptr)
void * mrbc_raw_realloc (void *ptr, unsigned int size)
unsigned int mrbc_alloc_usable_size (void *ptr)
void mrbc_alloc_statistics (struct MRBC_ALLOC_STATISTICS *ret)

Variables

static MEMORY_POOLmemory_pool

Detailed Description

mruby/c memory management.

 Copyright (C) 2015-      Kyushu Institute of Technology.
 Copyright (C) 2015-2026  Shimane IT Open-Innovation Center.
 Copyright (C) 2026-      Shimane Institute for Industrial Technology.

 This file is distributed under BSD 3-Clause License.

 Memory management for objects in mruby/c.

 STRATEGY
  Uses the TLSF and first-fit algorithms.

 MEMORY POOL USAGE (see struct MEMORY_POOL)
    | Memory pool header | Memory block pool to provide to application |
    +--------------------+---------------------------------------------+
    | size, bitmap, ...  | USED_BLOCK, FREE_BLOCK, ..., (SentinelBlock)|

 MEMORY BLOCK LINK
     with USED flag and PREV_IN_USE flag in size member's bit 0 and 1.

    |  USED_BLOCK     |  FREE_BLOCK                   |  USED_BLOCK     |...
    +-----------------+-------------------------------+-----------------+---
    |size| (contents) |size|*next|*prev| (empty) |*top|size| (contents) |
USED|   1|            |   0|     |     |         |    |   1|            |
PREV|  1 |            |  1 |     |     |         |    |  0 |            |

                                          Sentinel block at the link tail.
                                     ...              |  USED_BLOCK     |
                                    ------------------+-----------------+
                                                      |size| (contents) |
                                                  USED|   1|            |
                                                  PREV|  ? |            |
   size : block size.
   *next: linked list, pointer to the next free block of same block size.
   *prev: linked list, pointer to the previous free block of same block size.
   *top : pointer to this block's top.

 

Definition in file alloc.c.

Macro Definition Documentation

◆ alloc_profile

#define alloc_profile ( )
Value:
((void)0)

Definition at line 403 of file alloc.c.

◆ BLOCK_ADRS

#define BLOCK_ADRS ( p)
Value:
((void *)((uint8_t *)(p) - sizeof(USED_BLOCK)))

Definition at line 231 of file alloc.c.

◆ BLOCK_SIZE

#define BLOCK_SIZE ( p)
Value:
(((p)->size) & ~0x03)

Definition at line 192 of file alloc.c.

◆ BPOOL_END

#define BPOOL_END ( memory_pool)
Value:
((void *)((uint8_t *)(memory_pool) + ((MEMORY_POOL *)(memory_pool))->size))
static MEMORY_POOL * memory_pool
Definition alloc.c:242

Definition at line 230 of file alloc.c.

◆ BPOOL_TOP

#define BPOOL_TOP ( memory_pool)
Value:
((void *)((uint8_t *)(memory_pool) + sizeof(MEMORY_POOL)))

Definition at line 229 of file alloc.c.

◆ FLI

#define FLI ( x)
Value:
#define MRBC_ALLOC_SLI_BIT_WIDTH
Definition alloc.c:84

Definition at line 104 of file alloc.c.

◆ GET_VM_ID

#define GET_VM_ID ( p)
Value:
0

Definition at line 209 of file alloc.c.

◆ IS_FREE_BLOCK

#define IS_FREE_BLOCK ( p)
Value:
#define IS_USED_BLOCK(p)
Definition alloc.c:196

Definition at line 197 of file alloc.c.

◆ IS_PREV_FREE

#define IS_PREV_FREE ( p)
Value:
#define IS_PREV_USED(p)
Definition alloc.c:200

Definition at line 201 of file alloc.c.

◆ IS_PREV_USED

#define IS_PREV_USED ( p)
Value:
((p)->size & 0x02)

Definition at line 200 of file alloc.c.

◆ IS_USED_BLOCK

#define IS_USED_BLOCK ( p)
Value:
((p)->size & 0x01)

Definition at line 196 of file alloc.c.

◆ MRBC_ALLOC_FLI_BIT_WIDTH

#define MRBC_ALLOC_FLI_BIT_WIDTH   9

Definition at line 81 of file alloc.c.

◆ MRBC_ALLOC_IGNORE_LSBS

#define MRBC_ALLOC_IGNORE_LSBS   4

Definition at line 87 of file alloc.c.

◆ MRBC_ALLOC_SLI_BIT_WIDTH

#define MRBC_ALLOC_SLI_BIT_WIDTH   3

Definition at line 84 of file alloc.c.

◆ MRBC_MIN_MEMORY_BLOCK_SIZE

#define MRBC_MIN_MEMORY_BLOCK_SIZE   sizeof(FREE_BLOCK)

Definition at line 98 of file alloc.c.

◆ MSB_BIT1_FLI

#define MSB_BIT1_FLI   0x8000

Definition at line 233 of file alloc.c.

◆ MSB_BIT1_SLI

#define MSB_BIT1_SLI   0x80

Definition at line 234 of file alloc.c.

◆ NLZ_FLI

#define NLZ_FLI ( x)
Value:
static int nlz16(uint16_t x)
Definition alloc.c:258

Definition at line 235 of file alloc.c.

◆ NLZ_SLI

#define NLZ_SLI ( x)
Value:
nlz8(x)
static int nlz8(uint8_t x)
Definition alloc.c:276

Definition at line 236 of file alloc.c.

◆ PHYS_NEXT

#define PHYS_NEXT ( p)
Value:
((void *)((uint8_t *)(p) + BLOCK_SIZE(p)))
#define BLOCK_SIZE(p)
Definition alloc.c:192

Definition at line 193 of file alloc.c.

◆ RETURN_IF_NULL

#define RETURN_IF_NULL ( ptr)
Value:
(void)0

Definition at line 113 of file alloc.c.

◆ SET_FREE_BLOCK

#define SET_FREE_BLOCK ( p)
Value:
((p)->size &= ~0x01)

Definition at line 195 of file alloc.c.

◆ SET_PREV_FREE

#define SET_PREV_FREE ( p)
Value:
((p)->size &= ~0x02)

Definition at line 199 of file alloc.c.

◆ SET_PREV_USED

#define SET_PREV_USED ( p)
Value:
((p)->size |= 0x02)

Definition at line 198 of file alloc.c.

◆ SET_USED_BLOCK

#define SET_USED_BLOCK ( p)
Value:
((p)->size |= 0x01)

Definition at line 194 of file alloc.c.

◆ SET_VM_ID

#define SET_VM_ID ( p,
id )
Value:
((void)0)

Definition at line 208 of file alloc.c.

◆ SIZE_FREE_BLOCKS

#define SIZE_FREE_BLOCKS   ((MRBC_ALLOC_FLI_BIT_WIDTH + 1) * (1 << MRBC_ALLOC_SLI_BIT_WIDTH))

Definition at line 90 of file alloc.c.

◆ SLI

#define SLI ( x)
Value:
((x) & ((1 << MRBC_ALLOC_SLI_BIT_WIDTH) - 1))

Definition at line 105 of file alloc.c.

Typedef Documentation

◆ MEMORY_POOL

typedef struct MEMORY_POOL MEMORY_POOL

Function Documentation

◆ add_free_block()

void add_free_block ( MEMORY_POOL * pool,
FREE_BLOCK * target )
static

Mark that block free and register it in the free index table.

Parameters
poolPointer to memory pool.
targetPointer to target block.

Definition at line 326 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calc_index()

unsigned int calc_index ( MRBC_ALLOC_MEMSIZE_T alloc_size)
inlinestatic

calc f and s, and returns fli,sli of free_blocks

Parameters
alloc_sizealloc size
Return values
unsignedint index of free_blocks

Definition at line 293 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ merge_block()

void merge_block ( FREE_BLOCK * target,
FREE_BLOCK * next )
inlinestatic

merge target and next block. next will disappear

Parameters
targetpointer to free block 1
nextpointer to free block 2

Definition at line 436 of file alloc.c.

Here is the caller graph for this function:

◆ mrbc_alloc_statistics()

void mrbc_alloc_statistics ( struct MRBC_ALLOC_STATISTICS * ret)

statistics

Parameters
retpointer to return value.

Definition at line 984 of file alloc.c.

◆ mrbc_alloc_usable_size()

unsigned int mrbc_alloc_usable_size ( void * ptr)

allocated memory size

Parameters
ptrReturn value of mrbc_alloc()
Return values
unsignedint pointer to allocated memory.

Definition at line 883 of file alloc.c.

◆ mrbc_cleanup_alloc()

void mrbc_cleanup_alloc ( void )

cleanup memory pool

Definition at line 495 of file alloc.c.

Here is the caller graph for this function:

◆ mrbc_init_alloc()

void mrbc_init_alloc ( void * ptr,
unsigned int size )

initialize

Parameters
ptrpointer to free memory block.
sizesize. (max 64KB. see MRBC_ALLOC_MEMSIZE_T)

Definition at line 452 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_raw_alloc()

void * mrbc_raw_alloc ( unsigned int size)

allocate memory

Parameters
sizerequest size.
Returns
void * pointer to allocated memory.
Return values
NULLerror.

Definition at line 514 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_raw_alloc_no_free()

void * mrbc_raw_alloc_no_free ( unsigned int size)

allocate memory that cannot free and realloc

Parameters
sizerequest size.
Returns
void * pointer to allocated memory.
Return values
NULLerror.

Definition at line 631 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_raw_calloc()

void * mrbc_raw_calloc ( unsigned int nmemb,
unsigned int size )

allocate memory for compatibility with calloc

Parameters
nmembnumber of elements.
sizesize of an element.
Returns
void * pointer to allocated memory.
Return values
NULLerror.

Definition at line 686 of file alloc.c.

Here is the call graph for this function:

◆ mrbc_raw_free()

void mrbc_raw_free ( void * ptr)

release memory

Parameters
ptrReturn value of mrbc_raw_alloc()

Definition at line 707 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_raw_realloc()

void * mrbc_raw_realloc ( void * ptr,
unsigned int size )

re-allocate memory

Parameters
ptrReturn value of mrbc_raw_alloc()
sizerequest size
Returns
void * pointer to allocated memory.
Return values
NULLerror.

Definition at line 806 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nlz16()

int nlz16 ( uint16_t x)
inlinestatic

Number of leading zeros. 16bit version.

Parameters
xtarget (16bit unsigned)
Return values
intnlz value

Definition at line 258 of file alloc.c.

Here is the caller graph for this function:

◆ nlz8()

int nlz8 ( uint8_t x)
inlinestatic

Number of leading zeros. 8bit version.

Parameters
xtarget (8bit unsigned)
Return values
intnlz value

Definition at line 276 of file alloc.c.

◆ remove_free_block()

void remove_free_block ( MEMORY_POOL * pool,
FREE_BLOCK * target )
static

just remove the free_block *target from index

Parameters
poolPointer to memory pool.
targetpointer to target block.

Definition at line 356 of file alloc.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ split_block()

FREE_BLOCK * split_block ( FREE_BLOCK * target,
MRBC_ALLOC_MEMSIZE_T size )
inlinestatic

Split block by size

Parameters
targetpointer to target block
sizesize
Return values
NULLno split.
FREE_BLOCK* pointer to splitted free block.

Definition at line 414 of file alloc.c.

Here is the caller graph for this function:

Variable Documentation

◆ memory_pool

MEMORY_POOL* memory_pool
static

Definition at line 242 of file alloc.c.