From 4fad0714e7ca32350c76081d1162a576da28ca00 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 22 Jan 2020 20:52:35 -0600 Subject: [PATCH] Add janet_gcpressure. Address #269. --- src/core/buffer.c | 6 +++--- src/core/gc.c | 5 +++++ src/include/janet.h | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/buffer.c b/src/core/buffer.c index 39d774e8..9691a552 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -32,7 +32,7 @@ JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) { uint8_t *data = NULL; if (capacity > 0) { - janet_vm_next_collection += capacity; + janet_gcpressure(capacity); data = malloc(sizeof(uint8_t) * (size_t) capacity); if (NULL == data) { JANET_OUT_OF_MEMORY; @@ -62,7 +62,7 @@ void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth) if (capacity <= buffer->capacity) return; int64_t big_capacity = ((int64_t) capacity) * growth; capacity = big_capacity > INT32_MAX ? INT32_MAX : (int32_t) big_capacity; - janet_vm_next_collection += capacity - buffer->capacity; + janet_gcpressure(capacity - buffer->capacity); new_data = realloc(old, (size_t) capacity * sizeof(uint8_t)); if (NULL == new_data) { JANET_OUT_OF_MEMORY; @@ -94,7 +94,7 @@ void janet_buffer_extra(JanetBuffer *buffer, int32_t n) { if (new_size > buffer->capacity) { int32_t new_capacity = new_size * 2; uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t)); - janet_vm_next_collection += new_capacity - buffer->capacity; + janet_gcpressure(new_capacity - buffer->capacity); if (NULL == new_data) { JANET_OUT_OF_MEMORY; } diff --git a/src/core/gc.c b/src/core/gc.c index 5201192c..b5d62ddd 100644 --- a/src/core/gc.c +++ b/src/core/gc.c @@ -69,6 +69,11 @@ static void janet_mark_abstract(void *adata); static JANET_THREAD_LOCAL uint32_t depth = JANET_RECURSION_GUARD; static JANET_THREAD_LOCAL size_t orig_rootcount; +/* Hint to the GC that we may need to collect */ +void janet_gcpressure(size_t s) { + janet_vm_next_collection += s; +} + /* Mark a value */ void janet_mark(Janet x) { if (depth) { diff --git a/src/include/janet.h b/src/include/janet.h index 612a08ef..b0742c0d 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -1312,6 +1312,7 @@ JANET_API int janet_gcunroot(Janet root); JANET_API int janet_gcunrootall(Janet root); JANET_API int janet_gclock(void); JANET_API void janet_gcunlock(int handle); +JANET_API void janet_gcpressure(size_t s); /* Functions */ JANET_API JanetFuncDef *janet_funcdef_alloc(void);