mruby/c VM Source Code release 3.4
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 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)

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- Shimane IT Open-Innovation Center.

 This file is distributed under BSD 3-Clause License.

 Memory management for objects in mruby/c.

 STRATEGY
  Using TLSF and FistFit algorithm.

 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

◆ BLOCK_ADRS

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

Definition at line 219 of file alloc.c.

◆ BLOCK_SIZE

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

Definition at line 180 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:230

Definition at line 218 of file alloc.c.

◆ BPOOL_TOP

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

Definition at line 217 of file alloc.c.

◆ FLI

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

Definition at line 103 of file alloc.c.

◆ GET_VM_ID

#define GET_VM_ID ( p)
Value:
0

Definition at line 197 of file alloc.c.

◆ IS_FREE_BLOCK

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

Definition at line 185 of file alloc.c.

◆ IS_PREV_FREE

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

Definition at line 189 of file alloc.c.

◆ IS_PREV_USED

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

Definition at line 188 of file alloc.c.

◆ IS_USED_BLOCK

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

Definition at line 184 of file alloc.c.

◆ MRBC_ALLOC_FLI_BIT_WIDTH

#define MRBC_ALLOC_FLI_BIT_WIDTH   9

Definition at line 80 of file alloc.c.

◆ MRBC_ALLOC_IGNORE_LSBS

#define MRBC_ALLOC_IGNORE_LSBS   4

Definition at line 86 of file alloc.c.

◆ MRBC_ALLOC_SLI_BIT_WIDTH

#define MRBC_ALLOC_SLI_BIT_WIDTH   3

Definition at line 83 of file alloc.c.

◆ MRBC_MIN_MEMORY_BLOCK_SIZE

#define MRBC_MIN_MEMORY_BLOCK_SIZE   sizeof(FREE_BLOCK)

Definition at line 97 of file alloc.c.

◆ MSB_BIT1_FLI

#define MSB_BIT1_FLI   0x8000

Definition at line 221 of file alloc.c.

◆ MSB_BIT1_SLI

#define MSB_BIT1_SLI   0x80

Definition at line 222 of file alloc.c.

◆ NLZ_FLI

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

Definition at line 223 of file alloc.c.

◆ NLZ_SLI

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

Definition at line 224 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:180

Definition at line 181 of file alloc.c.

◆ SET_FREE_BLOCK

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

Definition at line 183 of file alloc.c.

◆ SET_PREV_FREE

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

Definition at line 187 of file alloc.c.

◆ SET_PREV_USED

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

Definition at line 186 of file alloc.c.

◆ SET_USED_BLOCK

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

Definition at line 182 of file alloc.c.

◆ SET_VM_ID

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

Definition at line 196 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 89 of file alloc.c.

◆ SLI

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

Definition at line 104 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 314 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 281 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 422 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 975 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 874 of file alloc.c.

◆ mrbc_cleanup_alloc()

void mrbc_cleanup_alloc ( void )

cleanup memory pool

Definition at line 481 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 438 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 500 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 619 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 674 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 695 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 796 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 246 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 264 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 344 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 400 of file alloc.c.

Here is the caller graph for this function:

Variable Documentation

◆ memory_pool

MEMORY_POOL* memory_pool
static

Definition at line 230 of file alloc.c.