Stop pointer parameters leaking value-pool slots
Create structure parameters before allocating their representation, then keep pointer references in the call variable's inline slot. Add an 8,000-call regression and document the remaining by-value structure escape limitation. Co-authored-by: andrew <andrew@aklabs.net> Co-authored-by: OpenAI Codex (GPT-5) <noreply@openai.com>
This commit is contained in:
@@ -306,7 +306,8 @@ akerr_ErrorContext *akbasic_environment_get(akbasic_Environment *obj, const char
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_environment_create(akbasic_Environment *obj, const char *varname, akbasic_Variable **dest)
|
||||
static akerr_ErrorContext *environment_create_named(akbasic_Environment *obj, const char *varname,
|
||||
akbasic_Variable **dest, bool initialize)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Variable *variable = NULL;
|
||||
@@ -340,12 +341,31 @@ akerr_ErrorContext *akbasic_environment_create(akbasic_Environment *obj, const c
|
||||
PASS(errctx, aksl_strcpy(variable->name, sizeof(variable->name), varname));
|
||||
variable->valuetype = AKBASIC_TYPE_UNDEFINED;
|
||||
variable->mutable_ = true;
|
||||
PASS(errctx, akbasic_variable_init(variable, &obj->runtime->valuepool, sizes, 1));
|
||||
if ( initialize ) {
|
||||
PASS(errctx, akbasic_variable_init(variable, &obj->runtime->valuepool, sizes, 1));
|
||||
}
|
||||
PASS(errctx, akbasic_symtab_set(&obj->variables, varname, variable, 0));
|
||||
*dest = variable;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_environment_create(akbasic_Environment *obj, const char *varname, akbasic_Variable **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
PASS(errctx, environment_create_named(obj, varname, dest, true));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_environment_create_empty(akbasic_Environment *obj, const char *varname,
|
||||
akbasic_Variable **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
PASS(errctx, environment_create_named(obj, varname, dest, false));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Evaluate an lvalue's subscript list, if it has one, into `subscripts`. A bare
|
||||
* identifier yields the single subscript {0}, which is how a scalar is addressed
|
||||
|
||||
@@ -989,12 +989,13 @@ static akerr_ErrorContext *bind_structure_parameter(akbasic_Runtime *obj, akbasi
|
||||
obj->structtypes.types[typeindex].name,
|
||||
obj->structtypes.types[argvalue->structtype].name);
|
||||
|
||||
PASS(errctx, akbasic_environment_create(callenv, param->identifier, &variable));
|
||||
PASS(errctx, akbasic_environment_create_empty(callenv, param->identifier, &variable));
|
||||
sizes[0] = (ispointer ? 1 : obj->structtypes.types[typeindex].slotcount);
|
||||
/* A pointer parameter's own reference cannot escape its call scope. */
|
||||
variable->ispointer = ispointer;
|
||||
PASS(errctx, akbasic_variable_init(variable, &obj->valuepool, sizes, 1));
|
||||
variable->valuetype = AKBASIC_TYPE_STRUCT;
|
||||
variable->structtype = typeindex;
|
||||
variable->ispointer = ispointer;
|
||||
|
||||
if ( ispointer ) {
|
||||
PASS(errctx, akbasic_value_clone(argvalue, &variable->values[0]));
|
||||
|
||||
@@ -99,18 +99,21 @@ akerr_ErrorContext *akbasic_variable_init(akbasic_Variable *obj, akbasic_ValuePo
|
||||
* the run was over, which a game loop reaches in half a minute. TODO.md
|
||||
* section 6 item 30 has the whole reduction.
|
||||
*
|
||||
* **A `@` name is the one exclusion**, and it is the whole of it. A
|
||||
* structure or a pointer to one keeps pool storage because a pointer may
|
||||
* outlive the scope that DIMmed it -- docs/16-structures.md says nothing is
|
||||
* reclaimed and akbasic_runtime_prev_environment() relies on it. The suffix
|
||||
* is the right test rather than `structtype`, which the DIM path sets
|
||||
* **A `@` name is the one exclusion**, except for a one-slot pointer
|
||||
* parameter. A structure or a pointer variable keeps pool storage because
|
||||
* a pointer may outlive the scope that DIMmed it -- docs/16-structures.md
|
||||
* says nothing is reclaimed and akbasic_runtime_prev_environment() relies
|
||||
* on it. A pointer parameter owns only its reference slot; the target is
|
||||
* owned by the caller, so bind_structure_parameter() marks it before this
|
||||
* call and lets that slot use inline storage. The suffix is the right test
|
||||
* for every other case rather than `structtype`, which the DIM path sets
|
||||
* *after* calling this.
|
||||
*
|
||||
* Otherwise: reuse the existing slice when it is already big enough, which
|
||||
* makes a re-DIM to the same or a smaller size free; growing takes fresh
|
||||
* slots and abandons the old ones, as documented on akbasic_ValuePool.
|
||||
*/
|
||||
if ( totalsize == 1 && lastchar != '@' ) {
|
||||
if ( totalsize == 1 && (lastchar != '@' || obj->ispointer) ) {
|
||||
obj->values = &obj->inlinevalue;
|
||||
} else if ( obj->values == NULL || obj->valuecount < (int)totalsize ) {
|
||||
PASS(errctx, akbasic_valuepool_take(pool, (int)totalsize, &obj->values));
|
||||
|
||||
Reference in New Issue
Block a user