2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2022-03-21 23:22:59 +00:00
|
|
|
* Copyright (c) 2022 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-03-31 20:40:36 +00:00
|
|
|
#include "state.h"
|
2017-11-27 19:03:34 +00:00
|
|
|
#include "symcache.h"
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2019-04-27 19:45:28 +00:00
|
|
|
#include "util.h"
|
2020-03-17 23:55:32 +00:00
|
|
|
#include "fiber.h"
|
2020-07-05 22:26:17 +00:00
|
|
|
#include "vector.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Helpers for marking the various gc types */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_funcenv(JanetFuncEnv *env);
|
|
|
|
static void janet_mark_funcdef(JanetFuncDef *def);
|
|
|
|
static void janet_mark_function(JanetFunction *func);
|
|
|
|
static void janet_mark_array(JanetArray *array);
|
|
|
|
static void janet_mark_table(JanetTable *table);
|
|
|
|
static void janet_mark_struct(const JanetKV *st);
|
|
|
|
static void janet_mark_tuple(const Janet *tuple);
|
|
|
|
static void janet_mark_buffer(JanetBuffer *buffer);
|
|
|
|
static void janet_mark_string(const uint8_t *str);
|
|
|
|
static void janet_mark_fiber(JanetFiber *fiber);
|
|
|
|
static void janet_mark_abstract(void *adata);
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2020-01-03 04:02:57 +00:00
|
|
|
/* Local state that is only temporary for gc */
|
2018-09-23 21:45:05 +00:00
|
|
|
static JANET_THREAD_LOCAL uint32_t depth = JANET_RECURSION_GUARD;
|
2020-01-03 04:12:07 +00:00
|
|
|
static JANET_THREAD_LOCAL size_t orig_rootcount;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2020-01-23 02:52:35 +00:00
|
|
|
/* Hint to the GC that we may need to collect */
|
|
|
|
void janet_gcpressure(size_t s) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += s;
|
2020-01-23 02:52:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Mark a value */
|
2018-09-23 21:10:41 +00:00
|
|
|
void janet_mark(Janet x) {
|
2018-09-23 21:45:05 +00:00
|
|
|
if (depth) {
|
|
|
|
depth--;
|
2018-09-23 21:10:41 +00:00
|
|
|
switch (janet_type(x)) {
|
2019-02-20 01:51:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
2018-09-23 21:10:41 +00:00
|
|
|
case JANET_STRING:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
janet_mark_string(janet_unwrap_string(x));
|
|
|
|
break;
|
|
|
|
case JANET_FUNCTION:
|
|
|
|
janet_mark_function(janet_unwrap_function(x));
|
|
|
|
break;
|
|
|
|
case JANET_ARRAY:
|
|
|
|
janet_mark_array(janet_unwrap_array(x));
|
|
|
|
break;
|
|
|
|
case JANET_TABLE:
|
|
|
|
janet_mark_table(janet_unwrap_table(x));
|
|
|
|
break;
|
|
|
|
case JANET_STRUCT:
|
|
|
|
janet_mark_struct(janet_unwrap_struct(x));
|
|
|
|
break;
|
|
|
|
case JANET_TUPLE:
|
|
|
|
janet_mark_tuple(janet_unwrap_tuple(x));
|
|
|
|
break;
|
|
|
|
case JANET_BUFFER:
|
|
|
|
janet_mark_buffer(janet_unwrap_buffer(x));
|
|
|
|
break;
|
|
|
|
case JANET_FIBER:
|
|
|
|
janet_mark_fiber(janet_unwrap_fiber(x));
|
|
|
|
break;
|
|
|
|
case JANET_ABSTRACT:
|
|
|
|
janet_mark_abstract(janet_unwrap_abstract(x));
|
|
|
|
break;
|
2018-09-23 21:10:41 +00:00
|
|
|
}
|
2018-09-23 21:45:05 +00:00
|
|
|
depth++;
|
2018-09-23 21:10:41 +00:00
|
|
|
} else {
|
2018-09-23 21:45:05 +00:00
|
|
|
janet_gcroot(x);
|
2018-09-23 21:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_string(const uint8_t *str) {
|
2019-02-21 16:22:29 +00:00
|
|
|
janet_gc_mark(janet_string_head(str));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_buffer(JanetBuffer *buffer) {
|
|
|
|
janet_gc_mark(buffer);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_abstract(void *adata) {
|
2021-08-20 01:56:48 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
/* Check if abstract type is a threaded abstract type. If it is, marking means
|
|
|
|
* updating the threaded_abstract table. */
|
|
|
|
if ((janet_abstract_head(adata)->gc.flags & JANET_MEM_TYPEBITS) == JANET_MEMORY_THREADED_ABSTRACT) {
|
|
|
|
janet_table_put(&janet_vm.threaded_abstracts, janet_wrap_abstract(adata), janet_wrap_true());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-27 19:45:28 +00:00
|
|
|
if (janet_gc_reachable(janet_abstract_head(adata)))
|
2018-01-17 04:18:45 +00:00
|
|
|
return;
|
2019-04-27 19:45:28 +00:00
|
|
|
janet_gc_mark(janet_abstract_head(adata));
|
|
|
|
if (janet_abstract_head(adata)->type->gcmark) {
|
|
|
|
janet_abstract_head(adata)->type->gcmark(adata, janet_abstract_size(adata));
|
2018-01-17 04:18:45 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark a bunch of items in memory */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_many(const Janet *values, int32_t n) {
|
2021-09-12 01:49:16 +00:00
|
|
|
if (values == NULL)
|
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
const Janet *end = values + n;
|
2018-09-23 21:53:55 +00:00
|
|
|
while (values < end) {
|
|
|
|
janet_mark(*values);
|
|
|
|
values += 1;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Mark a bunch of key values items in memory */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_kvs(const JanetKV *kvs, int32_t n) {
|
|
|
|
const JanetKV *end = kvs + n;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (kvs < end) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark(kvs->key);
|
|
|
|
janet_mark(kvs->value);
|
2018-01-05 21:17:55 +00:00
|
|
|
kvs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_array(JanetArray *array) {
|
|
|
|
if (janet_gc_reachable(array))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(array);
|
|
|
|
janet_mark_many(array->data, array->count);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_table(JanetTable *table) {
|
2019-02-20 01:51:34 +00:00
|
|
|
recur: /* Manual tail recursion */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_reachable(table))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(table);
|
|
|
|
janet_mark_kvs(table->data, table->capacity);
|
2018-03-10 18:34:46 +00:00
|
|
|
if (table->proto) {
|
|
|
|
table = table->proto;
|
|
|
|
goto recur;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_struct(const JanetKV *st) {
|
2021-10-29 21:42:34 +00:00
|
|
|
recur:
|
2019-02-21 16:22:29 +00:00
|
|
|
if (janet_gc_reachable(janet_struct_head(st)))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2019-02-21 16:22:29 +00:00
|
|
|
janet_gc_mark(janet_struct_head(st));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_kvs(st, janet_struct_capacity(st));
|
2021-10-29 21:42:34 +00:00
|
|
|
st = janet_struct_proto(st);
|
|
|
|
if (st) goto recur;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_tuple(const Janet *tuple) {
|
2019-02-21 16:22:29 +00:00
|
|
|
if (janet_gc_reachable(janet_tuple_head(tuple)))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2019-02-21 16:22:29 +00:00
|
|
|
janet_gc_mark(janet_tuple_head(tuple));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_many(tuple, janet_tuple_length(tuple));
|
2017-05-03 23:57:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Helper to mark function environments */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_funcenv(JanetFuncEnv *env) {
|
|
|
|
if (janet_gc_reachable(env))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(env);
|
2020-03-17 23:55:32 +00:00
|
|
|
/* If closure env references a dead fiber, we can just copy out the stack frame we need so
|
|
|
|
* we don't need to keep around the whole dead fiber. */
|
|
|
|
janet_env_maybe_detach(env);
|
2020-04-06 15:58:47 +00:00
|
|
|
if (env->offset > 0) {
|
2017-11-06 03:05:47 +00:00
|
|
|
/* On stack */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_fiber(env->as.fiber);
|
2017-11-06 03:05:47 +00:00
|
|
|
} else {
|
|
|
|
/* Not on stack */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_many(env->as.values, env->length);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GC helper to mark a FuncDef */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_funcdef(JanetFuncDef *def) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_reachable(def))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(def);
|
|
|
|
janet_mark_many(def->constants, def->constants_length);
|
2018-01-04 02:36:10 +00:00
|
|
|
for (i = 0; i < def->defs_length; ++i) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_funcdef(def->defs[i]);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-12-17 04:11:51 +00:00
|
|
|
if (def->source)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_string(def->source);
|
2018-03-22 00:53:39 +00:00
|
|
|
if (def->name)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_string(def->name);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_function(JanetFunction *func) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t numenvs;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_reachable(func))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(func);
|
2020-12-06 22:32:23 +00:00
|
|
|
if (NULL != func->def) {
|
|
|
|
/* this should always be true, except if function is only partially constructed */
|
|
|
|
numenvs = func->def->environments_length;
|
|
|
|
for (i = 0; i < numenvs; ++i) {
|
|
|
|
janet_mark_funcenv(func->envs[i]);
|
|
|
|
}
|
|
|
|
janet_mark_funcdef(func->def);
|
2018-02-13 21:14:55 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_mark_fiber(JanetFiber *fiber) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, j;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetStackFrame *frame;
|
2018-03-11 19:35:23 +00:00
|
|
|
recur:
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_reachable(fiber))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_gc_mark(fiber);
|
2018-12-30 23:23:29 +00:00
|
|
|
|
2021-01-03 22:17:36 +00:00
|
|
|
janet_mark(fiber->last_value);
|
|
|
|
|
2018-12-30 23:23:29 +00:00
|
|
|
/* Mark values on the argument stack */
|
|
|
|
janet_mark_many(fiber->data + fiber->stackstart,
|
2019-02-20 01:51:34 +00:00
|
|
|
fiber->stacktop - fiber->stackstart);
|
2018-12-30 23:23:29 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
i = fiber->frame;
|
2018-09-06 02:18:42 +00:00
|
|
|
j = fiber->stackstart - JANET_FRAME_SIZE;
|
2017-11-28 23:27:55 +00:00
|
|
|
while (i > 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (NULL != frame->func)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_function(frame->func);
|
2018-02-12 21:43:59 +00:00
|
|
|
if (NULL != frame->env)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_funcenv(frame->env);
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Mark all values in the stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_mark_many(fiber->data + i, j - i);
|
|
|
|
j = i - JANET_FRAME_SIZE;
|
2017-11-06 03:05:47 +00:00
|
|
|
i = frame->prevframe;
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:41:45 +00:00
|
|
|
if (fiber->env)
|
|
|
|
janet_mark_table(fiber->env);
|
|
|
|
|
2021-01-07 05:19:22 +00:00
|
|
|
#ifdef JANET_EV
|
2021-01-13 03:35:28 +00:00
|
|
|
if (fiber->supervisor_channel) {
|
|
|
|
janet_mark_abstract(fiber->supervisor_channel);
|
2021-01-07 05:19:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-17 17:34:11 +00:00
|
|
|
/* Explicit tail recursion */
|
2018-03-11 19:35:23 +00:00
|
|
|
if (fiber->child) {
|
|
|
|
fiber = fiber->child;
|
|
|
|
goto recur;
|
|
|
|
}
|
2017-04-12 14:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Deinitialize a block of memory */
|
2019-02-21 16:22:29 +00:00
|
|
|
static void janet_deinit_block(JanetGCObject *mem) {
|
|
|
|
switch (mem->flags & JANET_MEM_TYPEBITS) {
|
2017-07-02 01:51:16 +00:00
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_FUNCTION:
|
2018-05-20 01:16:00 +00:00
|
|
|
break; /* Do nothing for non gc types */
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_SYMBOL:
|
2019-02-21 16:22:29 +00:00
|
|
|
janet_symbol_deinit(((JanetStringHead *) mem)->data);
|
2017-02-23 22:21:13 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_ARRAY:
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(((JanetArray *) mem)->data);
|
2017-02-23 22:21:13 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_TABLE:
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(((JanetTable *) mem)->data);
|
2017-03-07 20:29:40 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_FIBER:
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(((JanetFiber *)mem)->data);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_MEMORY_BUFFER:
|
|
|
|
janet_buffer_deinit((JanetBuffer *) mem);
|
2018-05-20 01:16:00 +00:00
|
|
|
break;
|
2019-02-21 16:22:29 +00:00
|
|
|
case JANET_MEMORY_ABSTRACT: {
|
|
|
|
JanetAbstractHead *head = (JanetAbstractHead *)mem;
|
|
|
|
if (head->type->gc) {
|
|
|
|
janet_assert(!head->type->gc(head->data, head->size), "finalizer failed");
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
2019-02-21 16:22:29 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_MEMORY_FUNCENV: {
|
|
|
|
JanetFuncEnv *env = (JanetFuncEnv *)mem;
|
|
|
|
if (0 == env->offset)
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(env->as.values);
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_MEMORY_FUNCDEF: {
|
|
|
|
JanetFuncDef *def = (JanetFuncDef *)mem;
|
|
|
|
/* TODO - get this all with one alloc and one free */
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(def->defs);
|
|
|
|
janet_free(def->environments);
|
|
|
|
janet_free(def->constants);
|
|
|
|
janet_free(def->bytecode);
|
|
|
|
janet_free(def->sourcemap);
|
|
|
|
janet_free(def->closure_bitset);
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over all allocated memory, and free memory that is not
|
|
|
|
* marked as reachable. Flip the gc color flag for next sweep. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_sweep() {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject *previous = NULL;
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetGCObject *current = janet_vm.blocks;
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject *next;
|
2017-11-27 19:03:34 +00:00
|
|
|
while (NULL != current) {
|
2021-10-22 00:48:37 +00:00
|
|
|
next = current->data.next;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (current->flags & (JANET_MEM_REACHABLE | JANET_MEM_DISABLED)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
previous = current;
|
2018-09-06 02:18:42 +00:00
|
|
|
current->flags &= ~JANET_MEM_REACHABLE;
|
2017-11-01 21:53:43 +00:00
|
|
|
} else {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.block_count--;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_deinit_block(current);
|
2017-11-27 19:03:34 +00:00
|
|
|
if (NULL != previous) {
|
2021-10-22 00:48:37 +00:00
|
|
|
previous->data.next = next;
|
2017-02-23 22:21:13 +00:00
|
|
|
} else {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.blocks = next;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(current);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
current = next;
|
|
|
|
}
|
2021-08-20 01:56:48 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
/* Sweep threaded abstract types for references to decrement */
|
|
|
|
JanetKV *items = janet_vm.threaded_abstracts.data;
|
|
|
|
for (int32_t i = 0; i < janet_vm.threaded_abstracts.capacity; i++) {
|
|
|
|
if (janet_checktype(items[i].key, JANET_ABSTRACT)) {
|
|
|
|
|
|
|
|
/* If item was not visited during the mark phase, then this
|
|
|
|
* abstract type isn't present in the heap and needs its refcount
|
|
|
|
* decremented, and shouuld be removed from table. If the refcount is
|
|
|
|
* then 0, the item will be collected. This ensures that only one interpreter
|
|
|
|
* will clean up the threaded abstract. */
|
|
|
|
|
|
|
|
/* If not visited... */
|
|
|
|
if (!janet_truthy(items[i].value)) {
|
|
|
|
void *abst = janet_unwrap_abstract(items[i].key);
|
|
|
|
if (0 == janet_abstract_decref(abst)) {
|
|
|
|
/* Run finalizer */
|
|
|
|
JanetAbstractHead *head = janet_abstract_head(abst);
|
|
|
|
if (head->type->gc) {
|
|
|
|
janet_assert(!head->type->gc(head->data, head->size), "finalizer failed");
|
|
|
|
}
|
|
|
|
/* Mark as tombstone in place */
|
|
|
|
items[i].key = janet_wrap_nil();
|
|
|
|
items[i].value = janet_wrap_false();
|
|
|
|
janet_vm.threaded_abstracts.deleted++;
|
|
|
|
janet_vm.threaded_abstracts.count--;
|
|
|
|
/* Free memory */
|
|
|
|
janet_free(janet_abstract_head(abst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset for next sweep */
|
|
|
|
items[i].value = janet_wrap_false();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Allocate some memory that is tracked for garbage collection */
|
2018-09-06 02:18:42 +00:00
|
|
|
void *janet_gcalloc(enum JanetMemoryType type, size_t size) {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject *mem;
|
2017-11-06 14:44:10 +00:00
|
|
|
|
|
|
|
/* Make sure everything is inited */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_assert(NULL != janet_vm.cache, "please initialize janet before use");
|
2021-03-23 10:00:48 +00:00
|
|
|
mem = janet_malloc(size);
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Check for bad malloc */
|
|
|
|
if (NULL == mem) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Configure block */
|
2019-02-21 16:22:29 +00:00
|
|
|
mem->flags = type;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Prepend block to heap list */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += size;
|
2021-10-22 00:48:37 +00:00
|
|
|
mem->data.next = janet_vm.blocks;
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.blocks = mem;
|
|
|
|
janet_vm.block_count++;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
return (void *)mem;
|
2017-03-22 04:27:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
static void free_one_scratch(JanetScratch *s) {
|
|
|
|
if (NULL != s->finalize) {
|
|
|
|
s->finalize((char *) s->mem);
|
|
|
|
}
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(s);
|
2019-11-28 04:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
/* Free all allocated scratch memory */
|
|
|
|
static void janet_free_all_scratch(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
for (size_t i = 0; i < janet_vm.scratch_len; i++) {
|
|
|
|
free_one_scratch(janet_vm.scratch_mem[i]);
|
2019-11-28 04:26:45 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.scratch_len = 0;
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
static JanetScratch *janet_mem2scratch(void *mem) {
|
|
|
|
JanetScratch *s = (JanetScratch *)mem;
|
|
|
|
return s - 1;
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Run garbage collection */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_collect(void) {
|
2018-09-23 21:53:55 +00:00
|
|
|
uint32_t i;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.gc_suspend) return;
|
2018-09-23 21:45:05 +00:00
|
|
|
depth = JANET_RECURSION_GUARD;
|
2020-06-27 21:51:20 +00:00
|
|
|
/* Try and prevent many major collections back to back.
|
2021-07-17 01:59:03 +00:00
|
|
|
* A full collection will take O(janet_vm.block_count) time.
|
2020-06-27 21:51:20 +00:00
|
|
|
* If we have a large heap, make sure our interval is not too
|
|
|
|
* small so we won't make many collections over it. This is just a
|
|
|
|
* heuristic for automatically changing the gc interval */
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.block_count * 8 > janet_vm.gc_interval) {
|
|
|
|
janet_vm.gc_interval = janet_vm.block_count * sizeof(JanetGCObject);
|
2020-06-27 21:51:20 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
orig_rootcount = janet_vm.root_count;
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
janet_ev_mark();
|
2020-02-03 15:29:51 +00:00
|
|
|
#endif
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_mark_fiber(janet_vm.root_fiber);
|
2018-09-23 21:45:05 +00:00
|
|
|
for (i = 0; i < orig_rootcount; i++)
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_mark(janet_vm.roots[i]);
|
|
|
|
while (orig_rootcount < janet_vm.root_count) {
|
|
|
|
Janet x = janet_vm.roots[--janet_vm.root_count];
|
2018-09-23 21:10:41 +00:00
|
|
|
janet_mark(x);
|
2018-09-22 18:59:50 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_sweep();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection = 0;
|
2019-06-02 03:31:39 +00:00
|
|
|
janet_free_all_scratch();
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Add a root value to the GC. This prevents the GC from removing a value
|
|
|
|
* and all of its children. If gcroot is called on a value n times, unroot
|
|
|
|
* must also be called n times to remove it as a gc root. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_gcroot(Janet root) {
|
2021-07-17 01:59:03 +00:00
|
|
|
size_t newcount = janet_vm.root_count + 1;
|
|
|
|
if (newcount > janet_vm.root_capacity) {
|
2020-01-12 16:19:51 +00:00
|
|
|
size_t newcap = 2 * newcount;
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.roots = janet_realloc(janet_vm.roots, sizeof(Janet) * newcap);
|
|
|
|
if (NULL == janet_vm.roots) {
|
2018-09-23 21:53:55 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.root_capacity = newcap;
|
2018-09-23 21:53:55 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.roots[janet_vm.root_count] = root;
|
|
|
|
janet_vm.root_count = newcount;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 02:55:45 +00:00
|
|
|
/* Identity equality for GC purposes */
|
2018-09-06 02:18:42 +00:00
|
|
|
static int janet_gc_idequals(Janet lhs, Janet rhs) {
|
|
|
|
if (janet_type(lhs) != janet_type(rhs))
|
2018-07-17 02:55:45 +00:00
|
|
|
return 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
switch (janet_type(lhs)) {
|
2019-03-13 18:50:25 +00:00
|
|
|
case JANET_BOOLEAN:
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_NUMBER:
|
2018-12-27 18:36:27 +00:00
|
|
|
/* These values don't really matter to the gc so returning 1 all the time is fine. */
|
2018-07-17 02:55:45 +00:00
|
|
|
return 1;
|
|
|
|
default:
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_unwrap_pointer(lhs) == janet_unwrap_pointer(rhs);
|
2018-07-17 02:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Remove a root value from the GC. This allows the gc to potentially reclaim
|
|
|
|
* a value and all its children. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_gcunroot(Janet root) {
|
2021-07-17 01:59:03 +00:00
|
|
|
Janet *vtop = janet_vm.roots + janet_vm.root_count;
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Search from top to bottom as access is most likely LIFO */
|
2021-07-17 01:59:03 +00:00
|
|
|
for (Janet *v = janet_vm.roots; v < vtop; v++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_idequals(root, *v)) {
|
2021-07-17 01:59:03 +00:00
|
|
|
*v = janet_vm.roots[--janet_vm.root_count];
|
2017-12-21 04:03:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Remove a root value from the GC. This sets the effective reference count to 0. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_gcunrootall(Janet root) {
|
2021-07-17 01:59:03 +00:00
|
|
|
Janet *vtop = janet_vm.roots + janet_vm.root_count;
|
2017-12-30 21:46:59 +00:00
|
|
|
int ret = 0;
|
|
|
|
/* Search from top to bottom as access is most likely LIFO */
|
2021-07-17 01:59:03 +00:00
|
|
|
for (Janet *v = janet_vm.roots; v < vtop; v++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_gc_idequals(root, *v)) {
|
2021-07-17 01:59:03 +00:00
|
|
|
*v = janet_vm.roots[--janet_vm.root_count];
|
2017-12-30 21:46:59 +00:00
|
|
|
vtop--;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Free all allocated memory */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_clear_memory(void) {
|
2021-08-20 02:51:53 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
JanetKV *items = janet_vm.threaded_abstracts.data;
|
|
|
|
for (int32_t i = 0; i < janet_vm.threaded_abstracts.capacity; i++) {
|
|
|
|
if (janet_checktype(items[i].key, JANET_ABSTRACT)) {
|
|
|
|
void *abst = janet_unwrap_abstract(items[i].key);
|
|
|
|
if (0 == janet_abstract_decref(abst)) {
|
|
|
|
JanetAbstractHead *head = janet_abstract_head(abst);
|
|
|
|
if (head->type->gc) {
|
|
|
|
janet_assert(!head->type->gc(head->data, head->size), "finalizer failed");
|
|
|
|
}
|
|
|
|
janet_free(janet_abstract_head(abst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetGCObject *current = janet_vm.blocks;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (NULL != current) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_deinit_block(current);
|
2021-10-22 00:48:37 +00:00
|
|
|
JanetGCObject *next = current->data.next;
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(current);
|
2017-02-23 22:21:13 +00:00
|
|
|
current = next;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.blocks = NULL;
|
2019-06-02 03:31:39 +00:00
|
|
|
janet_free_all_scratch();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_free(janet_vm.scratch_mem);
|
2017-03-01 01:20:29 +00:00
|
|
|
}
|
2018-03-31 20:40:36 +00:00
|
|
|
|
|
|
|
/* Primitives for suspending GC. */
|
2019-02-20 01:51:34 +00:00
|
|
|
int janet_gclock(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
return janet_vm.gc_suspend++;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
void janet_gcunlock(int handle) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.gc_suspend = handle;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
2019-06-02 03:31:39 +00:00
|
|
|
|
|
|
|
/* Scratch memory API */
|
|
|
|
|
|
|
|
void *janet_smalloc(size_t size) {
|
2021-03-23 10:00:48 +00:00
|
|
|
JanetScratch *s = janet_malloc(sizeof(JanetScratch) + size);
|
2020-01-29 05:38:52 +00:00
|
|
|
if (NULL == s) {
|
2019-06-02 03:31:39 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2020-01-29 05:38:52 +00:00
|
|
|
s->finalize = NULL;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.scratch_len == janet_vm.scratch_cap) {
|
|
|
|
size_t newcap = 2 * janet_vm.scratch_cap + 2;
|
|
|
|
JanetScratch **newmem = (JanetScratch **) janet_realloc(janet_vm.scratch_mem, newcap * sizeof(JanetScratch));
|
2019-06-02 03:31:39 +00:00
|
|
|
if (NULL == newmem) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.scratch_cap = newcap;
|
|
|
|
janet_vm.scratch_mem = newmem;
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.scratch_mem[janet_vm.scratch_len++] = s;
|
2020-01-29 05:38:52 +00:00
|
|
|
return (char *)(s->mem);
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 23:10:17 +00:00
|
|
|
void *janet_scalloc(size_t nmemb, size_t size) {
|
2020-01-29 05:38:52 +00:00
|
|
|
if (nmemb && size > SIZE_MAX / nmemb) {
|
2020-01-02 23:10:17 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
size_t n = nmemb * size;
|
|
|
|
void *p = janet_smalloc(n);
|
|
|
|
memset(p, 0, n);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
void *janet_srealloc(void *mem, size_t size) {
|
|
|
|
if (NULL == mem) return janet_smalloc(size);
|
2020-01-29 05:38:52 +00:00
|
|
|
JanetScratch *s = janet_mem2scratch(mem);
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.scratch_len) {
|
|
|
|
for (size_t i = janet_vm.scratch_len - 1; ; i--) {
|
|
|
|
if (janet_vm.scratch_mem[i] == s) {
|
2021-03-23 10:00:48 +00:00
|
|
|
JanetScratch *news = janet_realloc(s, size + sizeof(JanetScratch));
|
2020-01-29 05:38:52 +00:00
|
|
|
if (NULL == news) {
|
2019-06-02 03:31:39 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.scratch_mem[i] = news;
|
2020-01-29 05:38:52 +00:00
|
|
|
return (char *)(news->mem);
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|
|
|
|
if (i == 0) break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 17:00:01 +00:00
|
|
|
JANET_EXIT("invalid janet_srealloc");
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
void janet_sfinalizer(void *mem, JanetScratchFinalizer finalizer) {
|
|
|
|
JanetScratch *s = janet_mem2scratch(mem);
|
|
|
|
s->finalize = finalizer;
|
2019-11-28 04:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
void janet_sfree(void *mem) {
|
|
|
|
if (NULL == mem) return;
|
2020-01-29 05:38:52 +00:00
|
|
|
JanetScratch *s = janet_mem2scratch(mem);
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.scratch_len) {
|
|
|
|
for (size_t i = janet_vm.scratch_len - 1; ; i--) {
|
|
|
|
if (janet_vm.scratch_mem[i] == s) {
|
|
|
|
janet_vm.scratch_mem[i] = janet_vm.scratch_mem[--janet_vm.scratch_len];
|
2020-01-29 05:38:52 +00:00
|
|
|
free_one_scratch(s);
|
2019-06-02 03:31:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i == 0) break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 17:00:01 +00:00
|
|
|
JANET_EXIT("invalid janet_sfree");
|
2019-06-02 03:31:39 +00:00
|
|
|
}
|