diff --git a/lispBM/lispBM/include/eval_cps.h b/lispBM/lispBM/include/eval_cps.h index 55e5c15e..3f828fff 100644 --- a/lispBM/lispBM/include/eval_cps.h +++ b/lispBM/lispBM/include/eval_cps.h @@ -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 diff --git a/lispBM/lispBM/include/lbm_memory.h b/lispBM/lispBM/include/lbm_memory.h index e20682b6..baca8e00 100644 --- a/lispBM/lispBM/include/lbm_memory.h +++ b/lispBM/lispBM/include/lbm_memory.h @@ -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) diff --git a/lispBM/lispBM/include/symrepr.h b/lispBM/lispBM/include/symrepr.h index e4d00eec..726c1d6a 100644 --- a/lispBM/lispBM/include/symrepr.h +++ b/lispBM/lispBM/include/symrepr.h @@ -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 diff --git a/lispBM/lispBM/repl/repl.c b/lispBM/lispBM/repl/repl.c index f7aab4be..31952799 100644 --- a/lispBM/lispBM/repl/repl.c +++ b/lispBM/lispBM/repl/repl.c @@ -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(); diff --git a/lispBM/lispBM/src/extensions.c b/lispBM/lispBM/src/extensions.c index 2b0bf3b4..083f9865 100644 --- a/lispBM/lispBM/src/extensions.c +++ b/lispBM/lispBM/src/extensions.c @@ -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; - int res = lbm_add_extension_symbol_const(sym_str, &symbol); - if (!res) return false; + lbm_uint ext_ix = 0; - int ext_next = (int)symbol - ext_offset; - - if (ext_next < 0 || ext_next > ext_max) { - return false; + 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; + ext_ix = symbol - ext_offset; } - extension_table[ext_next] = ext; - + if (ext_ix >= ext_max) { + return false; + } + extension_table[ext_ix] = ext; return true; } diff --git a/lispBM/lispBM/src/symrepr.c b/lispBM/lispBM/src/symrepr.c index e852aa3c..a0a8d5ed 100644 --- a/lispBM/lispBM/src/symrepr.c +++ b/lispBM/lispBM/src/symrepr.c @@ -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; diff --git a/lispBM/lispBM/tests/test_lisp_code_cps.c b/lispBM/lispBM/tests/test_lisp_code_cps.c index 056cdcc2..ca35c857 100644 --- a/lispBM/lispBM/tests/test_lisp_code_cps.c +++ b/lispBM/lispBM/tests/test_lisp_code_cps.c @@ -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 {