Skip to content

Commit f923b3a

Browse files
auto scaling of string temp arena
1 parent a1badc8 commit f923b3a

5 files changed

Lines changed: 56 additions & 31 deletions

File tree

include/basic/context.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ typedef struct basic_ctx {
385385
*/
386386
char* string_gc_storage;
387387

388+
/**
389+
* @brief Size of string_gc_storage bump allocation.
390+
* This grows when a previous run hits a high water mark when
391+
* the gc() function runs
392+
*/
393+
size_t string_gc_storage_size;
394+
388395
/**
389396
* @brief Pointer to the next free position in the garbage-collected string storage.
390397
*/

include/string.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#pragma once
88
#include "basic.h"
99

10+
/**
11+
* Initial string bump allocator area size for storing temporary
12+
* strings used in expressions.
13+
*/
1014
#define STRING_GC_AREA_SIZE (1024 * 1024 * 4)
1115

1216
/**

src/basic/function.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,15 @@ const char* basic_eval_str_fn(const char* fn_name, struct basic_ctx* ctx)
458458
}
459459
}
460460

461-
ctx->int_variables = atomic->int_variables;
462-
ctx->str_variables = atomic->str_variables;
463-
ctx->double_variables = atomic->double_variables;
464-
ctx->sounds = atomic->sounds;
465-
ctx->data_offset = atomic->data_offset;
466-
ctx->graphics_colour = atomic->graphics_colour;
467-
ctx->active_restrictions = atomic->active_restrictions;
468-
ctx->child_restrictions = atomic->child_restrictions;
461+
ctx->int_variables = atomic->int_variables;
462+
ctx->str_variables = atomic->str_variables;
463+
ctx->double_variables = atomic->double_variables;
464+
ctx->sounds = atomic->sounds;
465+
ctx->data_offset = atomic->data_offset;
466+
ctx->graphics_colour = atomic->graphics_colour;
467+
ctx->active_restrictions = atomic->active_restrictions;
468+
ctx->child_restrictions = atomic->child_restrictions;
469+
ctx->string_gc_storage_size = atomic->string_gc_storage_size;
469470

470471
memcpy(ctx->audio_streams, atomic->audio_streams, sizeof(ctx->audio_streams));
471472
memcpy(ctx->envelopes, atomic->envelopes, sizeof(ctx->envelopes));
@@ -614,14 +615,15 @@ int64_t basic_eval_int_fn(const char* fn_name, struct basic_ctx* ctx)
614615
}
615616
rv = (int64_t)atomic->fn_return;
616617

617-
ctx->int_variables = atomic->int_variables;
618-
ctx->str_variables = atomic->str_variables;
619-
ctx->double_variables = atomic->double_variables;
620-
ctx->sounds = atomic->sounds;
621-
ctx->data_offset = atomic->data_offset;
622-
ctx->graphics_colour = atomic->graphics_colour;
623-
ctx->active_restrictions = atomic->active_restrictions;
624-
ctx->child_restrictions = atomic->child_restrictions;
618+
ctx->int_variables = atomic->int_variables;
619+
ctx->str_variables = atomic->str_variables;
620+
ctx->double_variables = atomic->double_variables;
621+
ctx->sounds = atomic->sounds;
622+
ctx->data_offset = atomic->data_offset;
623+
ctx->graphics_colour = atomic->graphics_colour;
624+
ctx->active_restrictions = atomic->active_restrictions;
625+
ctx->child_restrictions = atomic->child_restrictions;
626+
ctx->string_gc_storage_size = atomic->string_gc_storage_size;
625627

626628
memcpy(ctx->audio_streams, atomic->audio_streams, sizeof(ctx->audio_streams));
627629
memcpy(ctx->envelopes, atomic->envelopes, sizeof(ctx->envelopes));
@@ -693,14 +695,15 @@ void basic_eval_double_fn(const char* fn_name, struct basic_ctx* ctx, double* re
693695
*res = *((double*)atomic->fn_return);
694696
}
695697

696-
ctx->int_variables = atomic->int_variables;
697-
ctx->str_variables = atomic->str_variables;
698-
ctx->double_variables = atomic->double_variables;
699-
ctx->sounds = atomic->sounds;
700-
ctx->data_offset = atomic->data_offset;
701-
ctx->graphics_colour = atomic->graphics_colour;
702-
ctx->active_restrictions = atomic->active_restrictions;
703-
ctx->child_restrictions = atomic->child_restrictions;
698+
ctx->int_variables = atomic->int_variables;
699+
ctx->str_variables = atomic->str_variables;
700+
ctx->double_variables = atomic->double_variables;
701+
ctx->sounds = atomic->sounds;
702+
ctx->data_offset = atomic->data_offset;
703+
ctx->graphics_colour = atomic->graphics_colour;
704+
ctx->active_restrictions = atomic->active_restrictions;
705+
ctx->child_restrictions = atomic->child_restrictions;
706+
ctx->string_gc_storage_size = atomic->string_gc_storage_size;
704707

705708
memcpy(ctx->audio_streams, atomic->audio_streams, sizeof(ctx->audio_streams));
706709
memcpy(ctx->envelopes, atomic->envelopes, sizeof(ctx->envelopes));

src/basic/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ struct basic_ctx *basic_init(const char *program, uint32_t pid, const char *file
336336
*error = "Out of memory";
337337
return NULL;
338338
}
339-
ctx->string_gc_storage = buddy_malloc(ctx->allocator, STRING_GC_AREA_SIZE);
339+
ctx->string_gc_storage_size = STRING_GC_AREA_SIZE;
340+
ctx->string_gc_storage = buddy_malloc(ctx->allocator, ctx->string_gc_storage_size);
340341
if (!ctx->string_gc_storage) {
341342
buddy_free(ctx->allocator, ctx->program_ptr);
342343
kfree_null(&ctx);
@@ -560,6 +561,7 @@ struct basic_ctx *basic_clone(struct basic_ctx *old) {
560561
ctx->string_array_variables = old->string_array_variables;
561562
ctx->double_array_variables = old->double_array_variables;
562563
ctx->string_gc_storage = old->string_gc_storage;
564+
ctx->string_gc_storage_size = old->string_gc_storage_size;
563565
ctx->string_gc_storage_next = old->string_gc_storage_next;
564566
ctx->lines = old->lines;
565567
ctx->highest_line = old->highest_line;

src/string.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ char* strdup(const char* string)
189189

190190
const char* gc_try_concat(struct basic_ctx *ctx, const char* s1, const char* s2) {
191191
char* dest = ctx->string_gc_storage_next;
192-
char* end = ctx->string_gc_storage + STRING_GC_AREA_SIZE;
192+
char* end = ctx->string_gc_storage + ctx->string_gc_storage_size;
193193
char* out = dest;
194194

195195
while (*s1) {
@@ -220,7 +220,7 @@ const char* gc_strdup(basic_ctx* ctx, const char* string)
220220
return ctx->string_gc_storage;
221221
}
222222
char* dest = ctx->string_gc_storage_next;
223-
size_t remaining = (size_t)((ctx->string_gc_storage + STRING_GC_AREA_SIZE) - dest);
223+
size_t remaining = (size_t)((ctx->string_gc_storage + ctx->string_gc_storage_size) - dest);
224224
size_t len = strlcpy(dest, string, remaining);
225225
if (len >= remaining) {
226226
tokenizer_error_printf(ctx, "Out of string area allocator space storing '%s'", string);
@@ -232,7 +232,7 @@ const char* gc_strdup(basic_ctx* ctx, const char* string)
232232

233233
const char* gc_from_tokenizer_string(struct basic_ctx *ctx) {
234234
char* dest = ctx->string_gc_storage_next;
235-
char* end = ctx->string_gc_storage + STRING_GC_AREA_SIZE;
235+
char* end = ctx->string_gc_storage + ctx->string_gc_storage_size;
236236
char* out = dest;
237237

238238
if (*ctx->ptr == '"') {
@@ -255,10 +255,19 @@ const char* gc_from_tokenizer_string(struct basic_ctx *ctx) {
255255
}
256256

257257
int gc(basic_ctx* ctx) {
258-
if (ctx->string_gc_storage && ctx->string_gc_storage_next) {
259-
/* Strings start at +1 as the base ptr is for empties */
260-
ctx->string_gc_storage_next = ctx->string_gc_storage + 1;
258+
size_t used = ctx->string_gc_storage_next - ctx->string_gc_storage;
259+
if (used >= (ctx->string_gc_storage_size * 75) / 100 && !ctx->errored && !ctx->ended) {
260+
size_t new_size = ctx->string_gc_storage_size * 2;
261+
char* new_storage = buddy_realloc(ctx->allocator, ctx->string_gc_storage, new_size);
262+
if (new_storage) {
263+
ctx->string_gc_storage = new_storage;
264+
ctx->string_gc_storage_size = new_size;
265+
*ctx->string_gc_storage = 0;
266+
} else {
267+
tokenizer_error_printf(ctx, "Out of memory growing string area to %lu MB", new_size / 1024 / 1024);
268+
}
261269
}
270+
ctx->string_gc_storage_next = ctx->string_gc_storage + 1;
262271
return 1;
263272
}
264273

0 commit comments

Comments
 (0)