Squashed 'lispBM/lispBM/' changes from ad49766e..1dc61723

1dc61723 bug fix in sort, reordering of operations to keep sublist safe from GC

git-subtree-dir: lispBM/lispBM
git-subtree-split: 1dc61723743b6300130ea4e666a9bb1921d5e8ec
This commit is contained in:
Benjamin Vedder 2023-12-23 10:56:27 +01:00
parent 62867b118b
commit 890cda9c53
2 changed files with 20 additions and 11 deletions

View File

@ -609,7 +609,7 @@ int main(int argc, char **argv) {
pthread_t lispbm_thd;
lbm_heap_state_t heap_state;
unsigned int heap_size = 8192;//2048;
unsigned int heap_size = 2048;
lbm_cons_t *heap_storage = NULL;
lbm_const_heap_t const_heap;

View File

@ -2619,8 +2619,9 @@ static void apply_sort(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
lbm_value a = list_copy;
lbm_value b = lbm_cdr(a);
lbm_value rest = lbm_cdr(b);
lbm_set_cdr(a, b);
lbm_set_cdr(b, ENC_SYM_NIL);
// Do not terminate b. keep rest of list safe from GC in the following
// closure extraction.
//lbm_set_cdr(a, b); // This is void
lbm_value cl[3]; // Comparator closure
extract_n(lbm_cdr(args[0]), cl, 3);
@ -2641,6 +2642,9 @@ static void apply_sort(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) {
}
lbm_value cmp = cl[CLO_BODY];
// Terminate the comparator argument list.
lbm_set_cdr(b, ENC_SYM_NIL);
lbm_stack_drop(&ctx->K, 3); //TODO: optimize drop 3, alloc 20 into alloc 17
lbm_uint *sptr = stack_reserve(ctx, 20);
sptr[0] = cmp;
@ -3140,13 +3144,15 @@ static void cont_merge_rest(eval_context_t *ctx) {
}
// merge_layer stack contents
// s[sp-7] = cmp
// s[sp-6] = cmp_env
// s[sp-5] = par1
// s[sp-4] = par2
// s[sp-3] = acc
// s[sp-2] = rest;
// s[sp-1] = layer
// s[sp-9] = cmp
// s[sp-8] = cmp_env
// s[sp-7] = par1
// s[sp-6] = par2
// s[sp-5] = acc - first cell
// s[sp-4] = acc - last cell
// s[sp-3] = rest;
// s[sp-2] = layer
// s[sp-1] = length or original list
//
// ctx->r merged sublist
static void cont_merge_layer(eval_context_t *ctx) {
@ -3180,7 +3186,7 @@ static void cont_merge_layer(eval_context_t *ctx) {
lbm_stack_drop(&ctx->K, 9);
return;
} else {
// Setup for marges of the next layer
// Setup for merges of the next layer
layer = layer * 2;
sptr[7] = lbm_enc_i(layer);
layer_rest = sptr[4]; // continue on the accumulation of all sublists.
@ -3230,6 +3236,9 @@ static void cont_merge_layer(eval_context_t *ctx) {
lbm_value a = a_list;
lbm_value b = b_list;
lbm_set_cdr(a, b);
// Terminating the b list would be incorrect here
// if there was any chance that the environment update below
// performs GC.
lbm_set_cdr(b, ENC_SYM_NIL);
lbm_value cmp_body = sptr[0];