1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-27 17:00:27 +00:00

Add janet_gcpressure. Address #269.

This commit is contained in:
Calvin Rose 2020-01-22 20:52:35 -06:00
parent ca17eb4a2b
commit 4fad0714e7
3 changed files with 9 additions and 3 deletions

View File

@ -32,7 +32,7 @@
JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) { JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) {
uint8_t *data = NULL; uint8_t *data = NULL;
if (capacity > 0) { if (capacity > 0) {
janet_vm_next_collection += capacity; janet_gcpressure(capacity);
data = malloc(sizeof(uint8_t) * (size_t) capacity); data = malloc(sizeof(uint8_t) * (size_t) capacity);
if (NULL == data) { if (NULL == data) {
JANET_OUT_OF_MEMORY; 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; if (capacity <= buffer->capacity) return;
int64_t big_capacity = ((int64_t) capacity) * growth; int64_t big_capacity = ((int64_t) capacity) * growth;
capacity = big_capacity > INT32_MAX ? INT32_MAX : (int32_t) big_capacity; 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)); new_data = realloc(old, (size_t) capacity * sizeof(uint8_t));
if (NULL == new_data) { if (NULL == new_data) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
@ -94,7 +94,7 @@ void janet_buffer_extra(JanetBuffer *buffer, int32_t n) {
if (new_size > buffer->capacity) { if (new_size > buffer->capacity) {
int32_t new_capacity = new_size * 2; int32_t new_capacity = new_size * 2;
uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t)); 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) { if (NULL == new_data) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
} }

View File

@ -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 uint32_t depth = JANET_RECURSION_GUARD;
static JANET_THREAD_LOCAL size_t orig_rootcount; 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 */ /* Mark a value */
void janet_mark(Janet x) { void janet_mark(Janet x) {
if (depth) { if (depth) {

View File

@ -1312,6 +1312,7 @@ JANET_API int janet_gcunroot(Janet root);
JANET_API int janet_gcunrootall(Janet root); JANET_API int janet_gcunrootall(Janet root);
JANET_API int janet_gclock(void); JANET_API int janet_gclock(void);
JANET_API void janet_gcunlock(int handle); JANET_API void janet_gcunlock(int handle);
JANET_API void janet_gcpressure(size_t s);
/* Functions */ /* Functions */
JANET_API JanetFuncDef *janet_funcdef_alloc(void); JANET_API JanetFuncDef *janet_funcdef_alloc(void);