LispBM
Loading...
Searching...
No Matches
Functions
lbm_memory.h File Reference
#include "lbm_types.h"
#include <stdint.h>
#include <stddef.h>
Include dependency graph for lbm_memory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

Memory size macros

Convert number of memory blocks to number of memory words The block size is determined from the number of status bitpatterns that fit within one bitmap word.

Status bit patterns are 2 bits, so in 32bits (one word on 32bit platform) you have 16 status patterns => block size is 16.

On 64-bit platforms, LBM_MEMORY_BITMAP_SIZE divides by 2 because one 64-bit bitmap word can track 32 memory words (vs 16 on 32-bit).

LBM_MEMORY is allocated as a number of whole WORDS (not bytes). So the thing that decides the possible sizes are really the bitmap sizes. Bitmaps have to be a multiple of words

Memory sizes that make sense (32-bit): bitmap size words | memory size words | memory size bytes 1 | 1*16 = 16 | 64 2 | 2*16 = 32 | 128 10 | 10*16 = 160 | 640 64 | 64*16 = 1024 | 4096 100 | 100*16 1600 | 6400

There is a minimal viable size of lbm_memory but it depends on space used byt symbol representations and printing stack, gc stack. things like that. The minimal viable is likely about 4KB (to have approx 2KB lbm_mem avail at runtime)

#define LBM_MEMORY_SIZE_BLOCKS_TO_WORDS(X)   (16*(X))
 
#define LBM_MEMORY_SIZE_64BYTES_TIMES_X(X)   (16*(X))
 
#define LBM_MEMORY_BITMAP_SIZE(X)   (X)
 
Legacy Size Macros (deprecated)
Deprecated:
These macros are named for 32-bit byte sizes. On 64-bit platforms, actual byte size is 2× the name. Prefer specifying memory size in bytes directly. Use LBM_MEMORY_SIZE_BLOCKS_TO_WORDS() and LBM_MEMORY_BITMAP_SIZE() for platform-independent sizing.
#define LBM_MEMORY_SIZE_512   LBM_MEMORY_SIZE_64BYTES_TIMES_X(8)
 
#define LBM_MEMORY_SIZE_1K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(16)
 
#define LBM_MEMORY_SIZE_2K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(32)
 
#define LBM_MEMORY_SIZE_4K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(64)
 
#define LBM_MEMORY_SIZE_8K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(128)
 
#define LBM_MEMORY_SIZE_10K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(160)
 
#define LBM_MEMORY_SIZE_12K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(192)
 
#define LBM_MEMORY_SIZE_14K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(224)
 
#define LBM_MEMORY_SIZE_16K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(256)
 
#define LBM_MEMORY_SIZE_32K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(512)
 
#define LBM_MEMORY_SIZE_1M   LBM_MEMORY_SIZE_64BYTES_TIMES_X(16384)
 
#define LBM_MEMORY_BITMAP_SIZE_512   LBM_MEMORY_BITMAP_SIZE(8)
 
#define LBM_MEMORY_BITMAP_SIZE_1K   LBM_MEMORY_BITMAP_SIZE(16)
 
#define LBM_MEMORY_BITMAP_SIZE_2K   LBM_MEMORY_BITMAP_SIZE(32)
 
#define LBM_MEMORY_BITMAP_SIZE_4K   LBM_MEMORY_BITMAP_SIZE(64)
 
#define LBM_MEMORY_BITMAP_SIZE_8K   LBM_MEMORY_BITMAP_SIZE(128)
 
#define LBM_MEMORY_BITMAP_SIZE_10K   LBM_MEMORY_BITMAP_SIZE(160)
 
#define LBM_MEMORY_BITMAP_SIZE_12K   LBM_MEMORY_BITMAP_SIZE(192)
 
#define LBM_MEMORY_BITMAP_SIZE_14K   LBM_MEMORY_BITMAP_SIZE(224)
 
#define LBM_MEMORY_BITMAP_SIZE_16K   LBM_MEMORY_BITMAP_SIZE(256)
 
#define LBM_MEMORY_BITMAP_SIZE_32K   LBM_MEMORY_BITMAP_SIZE(512)
 
#define LBM_MEMORY_BITMAP_SIZE_1M   LBM_MEMORY_BITMAP_SIZE(16384)
 

Functions

bool lbm_memory_init (lbm_uint *data, lbm_uint data_size, lbm_uint *bitmap, lbm_uint bitmap_size)
 
void lbm_memory_set_reserve (lbm_uint num_words)
 
lbm_uint lbm_memory_get_reserve (void)
 
lbm_uint lbm_memory_num_words (void)
 
lbm_uint lbm_memory_num_free (void)
 
lbm_uint lbm_memory_maximum_used (void)
 
void lbm_memory_update_min_free (void)
 
lbm_uint lbm_memory_longest_free (void)
 
lbm_uintlbm_memory_allocate (lbm_uint num_words)
 
int lbm_memory_free (lbm_uint *ptr)
 
void * lbm_malloc (size_t size)
 
void * lbm_malloc_reserve (size_t size)
 
void lbm_free (void *ptr)
 
int lbm_memory_shrink (lbm_uint *ptr, lbm_uint n)
 
int lbm_memory_ptr_inside (lbm_uint *ptr)
 
lbm_int lbm_memory_address_to_ix (lbm_uint *ptr)
 

Macro Definition Documentation

◆ LBM_MEMORY_BITMAP_SIZE

#define LBM_MEMORY_BITMAP_SIZE (   X)    (X)

◆ LBM_MEMORY_BITMAP_SIZE_10K

#define LBM_MEMORY_BITMAP_SIZE_10K   LBM_MEMORY_BITMAP_SIZE(160)

◆ LBM_MEMORY_BITMAP_SIZE_12K

#define LBM_MEMORY_BITMAP_SIZE_12K   LBM_MEMORY_BITMAP_SIZE(192)

◆ LBM_MEMORY_BITMAP_SIZE_14K

#define LBM_MEMORY_BITMAP_SIZE_14K   LBM_MEMORY_BITMAP_SIZE(224)

◆ LBM_MEMORY_BITMAP_SIZE_16K

#define LBM_MEMORY_BITMAP_SIZE_16K   LBM_MEMORY_BITMAP_SIZE(256)

◆ LBM_MEMORY_BITMAP_SIZE_1K

#define LBM_MEMORY_BITMAP_SIZE_1K   LBM_MEMORY_BITMAP_SIZE(16)

◆ LBM_MEMORY_BITMAP_SIZE_1M

#define LBM_MEMORY_BITMAP_SIZE_1M   LBM_MEMORY_BITMAP_SIZE(16384)

◆ LBM_MEMORY_BITMAP_SIZE_2K

#define LBM_MEMORY_BITMAP_SIZE_2K   LBM_MEMORY_BITMAP_SIZE(32)

◆ LBM_MEMORY_BITMAP_SIZE_32K

#define LBM_MEMORY_BITMAP_SIZE_32K   LBM_MEMORY_BITMAP_SIZE(512)

◆ LBM_MEMORY_BITMAP_SIZE_4K

#define LBM_MEMORY_BITMAP_SIZE_4K   LBM_MEMORY_BITMAP_SIZE(64)

◆ LBM_MEMORY_BITMAP_SIZE_512

#define LBM_MEMORY_BITMAP_SIZE_512   LBM_MEMORY_BITMAP_SIZE(8)

◆ LBM_MEMORY_BITMAP_SIZE_8K

#define LBM_MEMORY_BITMAP_SIZE_8K   LBM_MEMORY_BITMAP_SIZE(128)

◆ LBM_MEMORY_SIZE_10K

#define LBM_MEMORY_SIZE_10K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(160)

◆ LBM_MEMORY_SIZE_12K

#define LBM_MEMORY_SIZE_12K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(192)

◆ LBM_MEMORY_SIZE_14K

#define LBM_MEMORY_SIZE_14K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(224)

◆ LBM_MEMORY_SIZE_16K

#define LBM_MEMORY_SIZE_16K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(256)

◆ LBM_MEMORY_SIZE_1K

#define LBM_MEMORY_SIZE_1K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(16)

◆ LBM_MEMORY_SIZE_1M

#define LBM_MEMORY_SIZE_1M   LBM_MEMORY_SIZE_64BYTES_TIMES_X(16384)

◆ LBM_MEMORY_SIZE_2K

#define LBM_MEMORY_SIZE_2K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(32)

◆ LBM_MEMORY_SIZE_32K

#define LBM_MEMORY_SIZE_32K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(512)

◆ LBM_MEMORY_SIZE_4K

#define LBM_MEMORY_SIZE_4K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(64)

◆ LBM_MEMORY_SIZE_512

#define LBM_MEMORY_SIZE_512   LBM_MEMORY_SIZE_64BYTES_TIMES_X(8)

◆ LBM_MEMORY_SIZE_64BYTES_TIMES_X

#define LBM_MEMORY_SIZE_64BYTES_TIMES_X (   X)    (16*(X))

◆ LBM_MEMORY_SIZE_8K

#define LBM_MEMORY_SIZE_8K   LBM_MEMORY_SIZE_64BYTES_TIMES_X(128)

◆ LBM_MEMORY_SIZE_BLOCKS_TO_WORDS

#define LBM_MEMORY_SIZE_BLOCKS_TO_WORDS (   X)    (16*(X))

Function Documentation

◆ lbm_free()

void lbm_free ( void *  ptr)

Free memory allocated with lbm_malloc

Parameters
Pointerto array to free

◆ lbm_malloc()

void * lbm_malloc ( size_t  size)

Malloc like interface to lbm_memory

Parameters
sizeSize in bytes of memory to allocate.
Returns
Pointer to array or NULL.

◆ lbm_malloc_reserve()

void * lbm_malloc_reserve ( size_t  size)

Allocate memory potentially from the reserved memory.

Parameters
sizeSize in bytes of memory to allocate.
Returns
Pointer to array or NULL.

◆ lbm_memory_address_to_ix()

lbm_int lbm_memory_address_to_ix ( lbm_uint ptr)

◆ lbm_memory_allocate()

lbm_uint * lbm_memory_allocate ( lbm_uint  num_words)

Allocate a number of words from the symbols and arrays memory.

Parameters
num_wordsNumber of words to allocate.
Returns
pointer to allocated array or NULL.

◆ lbm_memory_free()

int lbm_memory_free ( lbm_uint ptr)

Free an allocated array in the symbols and arrays memory.

Parameters
ptrPointer to array to free.
Returns
1 on success and 0 on failure.

◆ lbm_memory_get_reserve()

lbm_uint lbm_memory_get_reserve ( void  )

Get the number of words of memory that is treated as reserve.

Returns
Number of words that are reserved

◆ lbm_memory_init()

bool lbm_memory_init ( lbm_uint data,
lbm_uint  data_size,
lbm_uint bitmap,
lbm_uint  bitmap_size 
)

Initialize the symbols and arrays memory

Parameters
dataPointer to an array of uint32_t for data storage.
data_sizeThe size of the data storage array in number of uint32_t elements.
bitmapPointer to an array of uint32_t for memory allocator meta-data.
bitmap_sizeThe size of the meta-data in number of uint32_t elements.
Returns
true on success and false otherwise.

◆ lbm_memory_longest_free()

lbm_uint lbm_memory_longest_free ( void  )

Find the length of the longest run of consecutire free indices in the LBM memory.

◆ lbm_memory_maximum_used()

lbm_uint lbm_memory_maximum_used ( void  )

Get the maximum of memory usage.

Returns
Maximal memory usage.

◆ lbm_memory_num_free()

lbm_uint lbm_memory_num_free ( void  )
Returns
The number of free words in the symbols and arrays memory.

◆ lbm_memory_num_words()

lbm_uint lbm_memory_num_words ( void  )

Size of of the symbols and arrays memory in uint32_t chunks.

Returns
Number of uint32_t words.

◆ lbm_memory_ptr_inside()

int lbm_memory_ptr_inside ( lbm_uint ptr)

Check if a pointer points into the lbm_memory

Parameters
ptr
Returns
1 for yes and 0 for no.

◆ lbm_memory_set_reserve()

void lbm_memory_set_reserve ( lbm_uint  num_words)

Set the size of the memory reserve in words.

Parameters
num_wordsNumber of words to treat as reserve.

◆ lbm_memory_shrink()

int lbm_memory_shrink ( lbm_uint ptr,
lbm_uint  n 
)

Shrink an allocated array.

Parameters
ptrPointer to array to shrink
nNew smaller size of array
Returns
1 on success and 0 on failure.

◆ lbm_memory_update_min_free()

void lbm_memory_update_min_free ( void  )

Update memory usage statistics. called by GC automatically