2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 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.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2017-11-27 19:03:34 +00:00
|
|
|
#include "symcache.h"
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* GC State */
|
|
|
|
void *dst_vm_blocks;
|
2017-12-30 21:46:59 +00:00
|
|
|
uint32_t dst_vm_gc_interval;
|
2017-11-06 03:05:47 +00:00
|
|
|
uint32_t dst_vm_next_collection;
|
2018-01-28 20:29:47 +00:00
|
|
|
int dst_vm_gc_suspend = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Roots */
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst *dst_vm_roots;
|
2017-12-21 04:03:34 +00:00
|
|
|
uint32_t dst_vm_root_count;
|
|
|
|
uint32_t dst_vm_root_capacity;
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Helpers for marking the various gc types */
|
|
|
|
static void dst_mark_funcenv(DstFuncEnv *env);
|
2017-11-06 03:05:47 +00:00
|
|
|
static void dst_mark_funcdef(DstFuncDef *def);
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_function(DstFunction *func);
|
|
|
|
static void dst_mark_array(DstArray *array);
|
|
|
|
static void dst_mark_table(DstTable *table);
|
2018-01-05 21:17:55 +00:00
|
|
|
static void dst_mark_struct(const DstKV *st);
|
2018-01-06 16:09:15 +00:00
|
|
|
static void dst_mark_tuple(const Dst *tuple);
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_buffer(DstBuffer *buffer);
|
|
|
|
static void dst_mark_string(const uint8_t *str);
|
2017-11-06 03:05:47 +00:00
|
|
|
static void dst_mark_fiber(DstFiber *fiber);
|
2018-01-04 02:36:10 +00:00
|
|
|
static void dst_mark_abstract(void *adata);
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Mark a value */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dst_mark(Dst x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(x)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
default: break;
|
|
|
|
case DST_STRING:
|
2017-11-28 23:27:55 +00:00
|
|
|
case DST_SYMBOL: dst_mark_string(dst_unwrap_string(x)); break;
|
|
|
|
case DST_FUNCTION: dst_mark_function(dst_unwrap_function(x)); break;
|
|
|
|
case DST_ARRAY: dst_mark_array(dst_unwrap_array(x)); break;
|
|
|
|
case DST_TABLE: dst_mark_table(dst_unwrap_table(x)); break;
|
|
|
|
case DST_STRUCT: dst_mark_struct(dst_unwrap_struct(x)); break;
|
|
|
|
case DST_TUPLE: dst_mark_tuple(dst_unwrap_tuple(x)); break;
|
|
|
|
case DST_BUFFER: dst_mark_buffer(dst_unwrap_buffer(x)); break;
|
|
|
|
case DST_FIBER: dst_mark_fiber(dst_unwrap_fiber(x)); break;
|
2018-01-04 02:36:10 +00:00
|
|
|
case DST_ABSTRACT: dst_mark_abstract(dst_unwrap_abstract(x)); break;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_string(const uint8_t *str) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(dst_string_raw(str));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_buffer(DstBuffer *buffer) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(buffer);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
static void dst_mark_abstract(void *adata) {
|
2018-01-17 04:18:45 +00:00
|
|
|
if (dst_gc_reachable(dst_abstract_header(adata)))
|
|
|
|
return;
|
2018-01-04 02:36:10 +00:00
|
|
|
dst_gc_mark(dst_abstract_header(adata));
|
2018-01-17 04:18:45 +00:00
|
|
|
if (dst_abstract_header(adata)->type->gcmark) {
|
|
|
|
dst_abstract_header(adata)->type->gcmark(adata, dst_abstract_size(adata));
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark a bunch of items in memory */
|
2018-01-06 16:09:15 +00:00
|
|
|
static void dst_mark_many(const Dst *values, int32_t n) {
|
|
|
|
const Dst *end = values + n;
|
2017-11-01 21:53:43 +00:00
|
|
|
while (values < end) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_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 */
|
|
|
|
static void dst_mark_kvs(const DstKV *kvs, int32_t n) {
|
|
|
|
const DstKV *end = kvs + n;
|
|
|
|
while (kvs < end) {
|
|
|
|
dst_mark(kvs->key);
|
|
|
|
dst_mark(kvs->value);
|
|
|
|
kvs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_array(DstArray *array) {
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(array))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(array);
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_mark_many(array->data, array->count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dst_mark_table(DstTable *table) {
|
2018-03-10 18:34:46 +00:00
|
|
|
recur: /* Manual tail recursion */
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(table))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(table);
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_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-01-05 21:17:55 +00:00
|
|
|
static void dst_mark_struct(const DstKV *st) {
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(dst_struct_raw(st)))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(dst_struct_raw(st));
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_mark_kvs(st, dst_struct_capacity(st));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 16:09:15 +00:00
|
|
|
static void dst_mark_tuple(const Dst *tuple) {
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(dst_tuple_raw(tuple)))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(dst_tuple_raw(tuple));
|
|
|
|
dst_mark_many(tuple, dst_tuple_length(tuple));
|
2017-05-03 23:57:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Helper to mark function environments */
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_funcenv(DstFuncEnv *env) {
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(env))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(env);
|
|
|
|
if (env->offset) {
|
|
|
|
/* On stack */
|
|
|
|
dst_mark_fiber(env->as.fiber);
|
|
|
|
} else {
|
|
|
|
/* Not on stack */
|
|
|
|
dst_mark_many(env->as.values, env->length);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GC helper to mark a FuncDef */
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_funcdef(DstFuncDef *def) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t i;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(def))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(def);
|
2018-01-04 02:36:10 +00:00
|
|
|
dst_mark_many(def->constants, def->constants_length);
|
|
|
|
for (i = 0; i < def->defs_length; ++i) {
|
|
|
|
dst_mark_funcdef(def->defs[i]);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-12-17 04:11:51 +00:00
|
|
|
if (def->source)
|
|
|
|
dst_mark_string(def->source);
|
|
|
|
if (def->sourcepath)
|
|
|
|
dst_mark_string(def->sourcepath);
|
2018-03-22 00:53:39 +00:00
|
|
|
if (def->name)
|
|
|
|
dst_mark_string(def->name);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
static void dst_mark_function(DstFunction *func) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t numenvs;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(func))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(func);
|
|
|
|
numenvs = func->def->environments_length;
|
2018-02-13 21:14:55 +00:00
|
|
|
for (i = 0; i < numenvs; ++i) {
|
|
|
|
dst_mark_funcenv(func->envs[i]);
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_mark_funcdef(func->def);
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
static void dst_mark_fiber(DstFiber *fiber) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, j;
|
2017-11-06 03:05:47 +00:00
|
|
|
DstStackFrame *frame;
|
2018-03-11 19:35:23 +00:00
|
|
|
recur:
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_gc_reachable(fiber))
|
2017-11-01 21:53:43 +00:00
|
|
|
return;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_gc_mark(fiber);
|
2018-03-11 19:35:23 +00:00
|
|
|
|
|
|
|
if (fiber->flags & DST_FIBER_FLAG_NEW)
|
|
|
|
dst_mark_function(fiber->root);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
i = fiber->frame;
|
2018-01-04 02:36:10 +00:00
|
|
|
j = fiber->stackstart - DST_FRAME_SIZE;
|
2017-11-28 23:27:55 +00:00
|
|
|
while (i > 0) {
|
2017-11-06 03:05:47 +00:00
|
|
|
frame = (DstStackFrame *)(fiber->data + i - DST_FRAME_SIZE);
|
|
|
|
if (NULL != frame->func)
|
|
|
|
dst_mark_function(frame->func);
|
2018-02-12 21:43:59 +00:00
|
|
|
if (NULL != frame->env)
|
|
|
|
dst_mark_funcenv(frame->env);
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Mark all values in the stack frame */
|
|
|
|
dst_mark_many(fiber->data + i, j - i);
|
|
|
|
j = i - DST_FRAME_SIZE;
|
|
|
|
i = frame->prevframe;
|
|
|
|
}
|
|
|
|
|
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 */
|
2017-11-06 03:05:47 +00:00
|
|
|
static void dst_deinit_block(DstGCMemoryHeader *block) {
|
2017-11-01 21:53:43 +00:00
|
|
|
void *mem = ((char *)(block + 1));
|
2018-01-04 02:36:10 +00:00
|
|
|
DstAbstractHeader *h = (DstAbstractHeader *)mem;
|
2017-11-06 03:05:47 +00:00
|
|
|
switch (block->flags & DST_MEM_TYPEBITS) {
|
2017-07-02 01:51:16 +00:00
|
|
|
default:
|
2018-02-13 21:14:55 +00:00
|
|
|
case DST_MEMORY_FUNCTION:
|
2017-11-01 21:53:43 +00:00
|
|
|
break; /* Do nothing for non gc types */
|
2017-11-27 19:03:34 +00:00
|
|
|
case DST_MEMORY_SYMBOL:
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_symbol_deinit((const uint8_t *)mem + 2 * sizeof(int32_t));
|
2017-02-23 22:21:13 +00:00
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_MEMORY_ARRAY:
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_array_deinit((DstArray*) mem);
|
2017-02-23 22:21:13 +00:00
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_MEMORY_TABLE:
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_table_deinit((DstTable*) mem);
|
2017-03-07 20:29:40 +00:00
|
|
|
break;
|
2017-11-06 03:05:47 +00:00
|
|
|
case DST_MEMORY_FIBER:
|
2018-01-05 21:17:55 +00:00
|
|
|
free(((DstFiber *)mem)->data);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_MEMORY_BUFFER:
|
2017-11-27 19:03:34 +00:00
|
|
|
dst_buffer_deinit((DstBuffer *) mem);
|
2017-11-01 21:53:43 +00:00
|
|
|
break;
|
2018-01-04 02:36:10 +00:00
|
|
|
case DST_MEMORY_ABSTRACT:
|
2018-01-16 01:14:21 +00:00
|
|
|
if (h->type->gc) {
|
|
|
|
if (h->type->gc((void *)(h + 1), h->size)) {
|
|
|
|
/* finalizer failed. try again later? */
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 22:21:13 +00:00
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_MEMORY_FUNCENV:
|
|
|
|
{
|
|
|
|
DstFuncEnv *env = (DstFuncEnv *)mem;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (0 == env->offset)
|
|
|
|
free(env->as.values);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-11-01 21:53:43 +00:00
|
|
|
case DST_MEMORY_FUNCDEF:
|
2017-09-09 18:39:51 +00:00
|
|
|
{
|
2017-11-06 03:05:47 +00:00
|
|
|
DstFuncDef *def = (DstFuncDef *)mem;
|
2017-11-01 21:53:43 +00:00
|
|
|
/* TODO - get this all with one alloc and one free */
|
2018-01-05 21:17:55 +00:00
|
|
|
free(def->defs);
|
2017-11-06 03:05:47 +00:00
|
|
|
free(def->environments);
|
|
|
|
free(def->constants);
|
|
|
|
free(def->bytecode);
|
2017-12-17 04:11:51 +00:00
|
|
|
free(def->sourcemap);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
2017-04-26 14:21:03 +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. */
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_sweep() {
|
|
|
|
DstGCMemoryHeader *previous = NULL;
|
|
|
|
DstGCMemoryHeader *current = dst_vm_blocks;
|
|
|
|
DstGCMemoryHeader *next;
|
2017-11-27 19:03:34 +00:00
|
|
|
while (NULL != current) {
|
2017-02-23 22:21:13 +00:00
|
|
|
next = current->next;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (current->flags & (DST_MEM_REACHABLE | DST_MEM_DISABLED)) {
|
|
|
|
previous = current;
|
2017-11-27 19:03:34 +00:00
|
|
|
current->flags &= ~DST_MEM_REACHABLE;
|
2017-11-01 21:53:43 +00:00
|
|
|
} else {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_deinit_block(current);
|
2017-11-27 19:03:34 +00:00
|
|
|
if (NULL != previous) {
|
2017-02-23 22:21:13 +00:00
|
|
|
previous->next = next;
|
|
|
|
} else {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_blocks = next;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
free(current);
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Allocate some memory that is tracked for garbage collection */
|
2018-02-02 01:09:22 +00:00
|
|
|
void *dst_gcalloc(enum DstMemoryType type, size_t size) {
|
2017-11-06 03:05:47 +00:00
|
|
|
DstGCMemoryHeader *mdata;
|
|
|
|
size_t total = size + sizeof(DstGCMemoryHeader);
|
2017-11-06 14:44:10 +00:00
|
|
|
|
|
|
|
/* Make sure everything is inited */
|
2017-12-15 00:33:45 +00:00
|
|
|
dst_assert(NULL != dst_vm_cache, "please initialize dst before use");
|
2017-11-06 03:05:47 +00:00
|
|
|
void *mem = malloc(total);
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Check for bad malloc */
|
|
|
|
if (NULL == mem) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
mdata = (DstGCMemoryHeader *)mem;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Configure block */
|
|
|
|
mdata->flags = type;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Prepend block to heap list */
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_next_collection += size;
|
|
|
|
mdata->next = dst_vm_blocks;
|
|
|
|
dst_vm_blocks = mdata;
|
2017-02-23 22:21:13 +00:00
|
|
|
|
2018-02-02 01:09:22 +00:00
|
|
|
return (char *) mem + sizeof(DstGCMemoryHeader);
|
2017-03-22 04:27:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Run garbage collection */
|
2018-02-02 01:09:22 +00:00
|
|
|
void dst_collect(void) {
|
2017-12-21 04:03:34 +00:00
|
|
|
uint32_t i;
|
2018-01-28 20:29:47 +00:00
|
|
|
if (dst_vm_gc_suspend) return;
|
2017-12-21 04:03:34 +00:00
|
|
|
for (i = 0; i < dst_vm_root_count; i++)
|
|
|
|
dst_mark(dst_vm_roots[i]);
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_sweep();
|
|
|
|
dst_vm_next_collection = 0;
|
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-01-06 16:09:15 +00:00
|
|
|
void dst_gcroot(Dst root) {
|
2017-12-21 04:03:34 +00:00
|
|
|
uint32_t newcount = dst_vm_root_count + 1;
|
|
|
|
if (newcount > dst_vm_root_capacity) {
|
|
|
|
uint32_t newcap = 2 * newcount;
|
2018-01-06 16:09:15 +00:00
|
|
|
dst_vm_roots = realloc(dst_vm_roots, sizeof(Dst) * newcap);
|
2017-12-21 04:03:34 +00:00
|
|
|
if (NULL == dst_vm_roots) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
dst_vm_root_capacity = newcap;
|
|
|
|
}
|
|
|
|
dst_vm_roots[dst_vm_root_count] = root;
|
|
|
|
dst_vm_root_count = newcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a root value from the GC. This allows the gc to potentially reclaim
|
|
|
|
* a value and all its children. */
|
2018-01-06 16:09:15 +00:00
|
|
|
int dst_gcunroot(Dst root) {
|
|
|
|
Dst *vtop = dst_vm_roots + dst_vm_root_count;
|
|
|
|
Dst *v = dst_vm_roots;
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Search from top to bottom as access is most likely LIFO */
|
|
|
|
for (v = dst_vm_roots; v < vtop; v++) {
|
|
|
|
if (dst_equals(root, *v)) {
|
|
|
|
*v = dst_vm_roots[--dst_vm_root_count];
|
|
|
|
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-01-06 16:09:15 +00:00
|
|
|
int dst_gcunrootall(Dst root) {
|
|
|
|
Dst *vtop = dst_vm_roots + dst_vm_root_count;
|
|
|
|
Dst *v = dst_vm_roots;
|
2017-12-30 21:46:59 +00:00
|
|
|
int ret = 0;
|
|
|
|
/* Search from top to bottom as access is most likely LIFO */
|
|
|
|
for (v = dst_vm_roots; v < vtop; v++) {
|
|
|
|
if (dst_equals(root, *v)) {
|
|
|
|
*v = dst_vm_roots[--dst_vm_root_count];
|
|
|
|
vtop--;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:21:13 +00:00
|
|
|
/* Free all allocated memory */
|
2018-02-02 01:09:22 +00:00
|
|
|
void dst_clear_memory(void) {
|
2017-11-06 03:05:47 +00:00
|
|
|
DstGCMemoryHeader *current = dst_vm_blocks;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (NULL != current) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_deinit_block(current);
|
|
|
|
DstGCMemoryHeader *next = current->next;
|
2017-11-01 21:53:43 +00:00
|
|
|
free(current);
|
2017-02-23 22:21:13 +00:00
|
|
|
current = next;
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_vm_blocks = NULL;
|
2017-03-01 01:20:29 +00:00
|
|
|
}
|