Merge commit '08da2db380048f9f99cef39bbfa63470550c14f4'

This commit is contained in:
Benjamin Vedder 2022-05-09 16:24:00 +02:00
commit 0d70c06f6b
7 changed files with 69 additions and 42 deletions

View File

@ -147,7 +147,7 @@ extern uint32_t lbm_get_eval_state(void);
* report an error message to the programmer in case
* the extension is used incorrectly.
*
* The error string can be allocates in lbm_memory
* The error string can be allocated in lbm_memory
* and will in that case be freed when the context
* that errored is removed.
* \param error_str

View File

@ -89,6 +89,9 @@
#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)
@ -98,6 +101,9 @@
#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)

View File

@ -268,8 +268,11 @@ extern int lbm_get_num_variables(void);
/**
*
* \return The amount of space occupied by the symbol table in bytes.
* \return The total amount of space occupied by the symbol table in bytes.
*/
extern lbm_uint lbm_get_symbol_table_size(void);
/**
* \return The size in bytes of all symbol strings stored in the symbol table.
*/
extern lbm_uint lbm_get_symbol_table_size_names(void);
#endif

View File

@ -1,5 +1,5 @@
/*
Copyright 2018, 2021 Joel Svensson svenssonjoel@yahoo.se
Copyright 2018, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -587,6 +587,7 @@ int main(int argc, char **argv) {
printf("Memory free: %"PRI_UINT" Words\n", lbm_memory_num_free());
printf("Allocated arrays: %"PRI_UINT"\n", heap_state.num_alloc_arrays);
printf("Symbol table size: %"PRI_UINT" Bytes\n", lbm_get_symbol_table_size());
printf("Symbol names size: %"PRI_UINT" Bytes\n", lbm_get_symbol_table_size_names());
free(str);
} else if (strncmp(str, ":env", 4) == 0) {
lbm_value curr = *lbm_get_env_ptr();

View File

@ -24,8 +24,8 @@
#include "extensions.h"
static int ext_offset = EXTENSION_SYMBOLS_START;
static int ext_max = -1;
static lbm_uint ext_offset = EXTENSION_SYMBOLS_START;
static lbm_uint ext_max = 0;
static extension_fptr *extension_table = NULL;
int lbm_extensions_init(extension_fptr *extension_storage, int extension_storage_size) {
@ -34,35 +34,39 @@ int lbm_extensions_init(extension_fptr *extension_storage, int extension_storage
extension_table = extension_storage;
memset(extension_table, 0, sizeof(extension_fptr) * (unsigned int)extension_storage_size);
ext_max = extension_storage_size;
ext_max = (lbm_uint)extension_storage_size;
return 1;
}
extension_fptr lbm_get_extension(lbm_uint sym) {
int ext_next = (int)sym - ext_offset;
if (ext_next < 0 || ext_next > ext_max) {
lbm_uint ext_next = sym - ext_offset;
if (ext_next >= ext_max) {
return NULL;
}
return extension_table[ext_next];
}
bool lbm_add_extension(char *sym_str, extension_fptr ext) {
lbm_value symbol;
lbm_uint ext_ix = 0;
if (lbm_get_symbol_by_name(sym_str, &symbol)) {
// symbol already exists and may or may not be an extension.
if (lbm_is_extension(symbol)) {
ext_ix = lbm_dec_sym(symbol) - ext_offset;
} else return false;
} else {
int res = lbm_add_extension_symbol_const(sym_str, &symbol);
if (!res) return false;
int ext_next = (int)symbol - ext_offset;
if (ext_next < 0 || ext_next > ext_max) {
return false;
ext_ix = symbol - ext_offset;
}
extension_table[ext_next] = ext;
if (ext_ix >= ext_max) {
return false;
}
extension_table[ext_ix] = ext;
return true;
}

View File

@ -207,11 +207,16 @@ static lbm_uint next_symbol_id = RUNTIME_SYMBOLS_START;
static lbm_uint next_extension_symbol_id = EXTENSION_SYMBOLS_START;
static lbm_uint next_variable_symbol_id = VARIABLE_SYMBOLS_START;
static lbm_uint symbol_table_size_list = 0;
static lbm_uint symbol_table_size_strings = 0;
int lbm_symrepr_init(void) {
symlist = NULL;
next_symbol_id = RUNTIME_SYMBOLS_START;
next_extension_symbol_id = EXTENSION_SYMBOLS_START;
next_variable_symbol_id = VARIABLE_SYMBOLS_START;
symbol_table_size_list = 0;
symbol_table_size_strings = 0;
return 1;
}
@ -283,18 +288,24 @@ int lbm_add_symbol(char *name, lbm_uint* id) {
return 0;
}
char *symbol_name_storage = NULL;;
char *symbol_name_storage = NULL;
lbm_uint alloc_size;
if (n % sizeof(lbm_uint) == 0) {
symbol_name_storage = (char *)lbm_memory_allocate(n/sizeof(lbm_uint));
alloc_size = n/(sizeof(lbm_uint));
} else {
symbol_name_storage = (char *)lbm_memory_allocate((n/sizeof(lbm_uint)) + 1);
alloc_size = (n/(sizeof(lbm_uint))) + 1;
}
symbol_name_storage = (char *)lbm_memory_allocate(alloc_size);
if (symbol_name_storage == NULL) {
lbm_memory_free(m);
return 0;
}
symbol_table_size_list += 3;
symbol_table_size_strings += alloc_size;
strcpy(symbol_name_storage, name);
m[NAME] = (lbm_uint)symbol_name_storage;
@ -325,18 +336,24 @@ int lbm_add_variable_symbol(char *name, lbm_uint* id) {
return 0;
}
char *symbol_name_storage = NULL;;
char *symbol_name_storage = NULL;
lbm_uint alloc_size;
if (n % sizeof(lbm_uint) == 0) {
symbol_name_storage = (char *)lbm_memory_allocate(n/sizeof(lbm_uint));
alloc_size = n/(sizeof(lbm_uint));
} else {
symbol_name_storage = (char *)lbm_memory_allocate((n/sizeof(lbm_uint)) + 1);
alloc_size = (n/(sizeof(lbm_uint))) + 1;
}
symbol_name_storage = (char *)lbm_memory_allocate(alloc_size);
if (symbol_name_storage == NULL) {
lbm_memory_free(m);
return 0;
}
symbol_table_size_list += 3;
symbol_table_size_strings += alloc_size;
strcpy(symbol_name_storage, name);
m[NAME] = (lbm_uint)symbol_name_storage;
@ -363,6 +380,8 @@ int lbm_add_symbol_const(char *name, lbm_uint* id) {
return 0;
}
symbol_table_size_list += 3;
m[NAME] = (lbm_uint)name;
if (symlist == NULL) {
@ -387,6 +406,8 @@ int lbm_add_extension_symbol_const(char *name, lbm_uint* id) {
return 0;
}
symbol_table_size_list += 3;
m[NAME] = (lbm_uint)name;
if (symlist == NULL) {
@ -403,21 +424,13 @@ int lbm_add_extension_symbol_const(char *name, lbm_uint* id) {
lbm_uint lbm_get_symbol_table_size(void) {
lbm_uint n = 0;
lbm_uint *curr = symlist;
while (curr) {
// up to 3 extra bytes are used for string storage if length is not multiple of 4
size_t s = strlen((char *)curr[NAME]);
s ++;
n += s % sizeof(lbm_uint);
n += 12; // sizeof the node in the linked list
curr = (lbm_uint *)curr[NEXT];
}
return n;
return (symbol_table_size_list +
symbol_table_size_strings) * sizeof(lbm_uint);
}
lbm_uint lbm_get_symbol_table_size_names(void) {
return symbol_table_size_strings * sizeof(lbm_uint);
}
int lbm_get_num_variables(void) {
return (int)next_variable_symbol_id - VARIABLE_SYMBOLS_START;

View File

@ -260,14 +260,14 @@ int main(int argc, char **argv) {
return 0;
}
lbm_uint *memory = malloc(sizeof(lbm_uint) * LBM_MEMORY_SIZE_32K);
lbm_uint *memory = malloc(sizeof(lbm_uint) * LBM_MEMORY_SIZE_12K);
if (memory == NULL) return 0;
lbm_uint *bitmap = malloc(sizeof(lbm_uint) * LBM_MEMORY_BITMAP_SIZE_32K);
lbm_uint *bitmap = malloc(sizeof(lbm_uint) * LBM_MEMORY_BITMAP_SIZE_12K);
if (bitmap == NULL) return 0;
res = lbm_memory_init(memory, LBM_MEMORY_SIZE_16K,
bitmap, LBM_MEMORY_BITMAP_SIZE_16K);
res = lbm_memory_init(memory, LBM_MEMORY_SIZE_12K,
bitmap, LBM_MEMORY_BITMAP_SIZE_12K);
if (res)
printf("Memory initialized.\n");
else {