mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	Keep count fo allocated memory via malloc.
We normally only track memory allocated with janet_gcalloc, but if only a few very large normal memory blocks are allocated, the GC will never run. Se simply need to increment a count when we allocate memory so that the next time we enter the VM, we will be able to run a collection if needed.
This commit is contained in:
		| @@ -24,6 +24,7 @@ | ||||
| #include <janet.h> | ||||
| #include "gc.h" | ||||
| #include "util.h" | ||||
| #include "state.h" | ||||
| #endif | ||||
|  | ||||
| #include <string.h> | ||||
| @@ -33,6 +34,7 @@ JanetArray *janet_array(int32_t capacity) { | ||||
|     JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray)); | ||||
|     Janet *data = NULL; | ||||
|     if (capacity > 0) { | ||||
|         janet_vm_next_collection += capacity * sizeof(Janet); | ||||
|         data = (Janet *) malloc(sizeof(Janet) * capacity); | ||||
|         if (NULL == data) { | ||||
|             JANET_OUT_OF_MEMORY; | ||||
| @@ -67,6 +69,7 @@ void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) { | ||||
|     if (NULL == newData) { | ||||
|         JANET_OUT_OF_MEMORY; | ||||
|     } | ||||
|     janet_vm_next_collection += (capacity - array->capacity) * sizeof(Janet); | ||||
|     array->data = newData; | ||||
|     array->capacity = capacity; | ||||
| } | ||||
|   | ||||
| @@ -24,12 +24,14 @@ | ||||
| #include <janet.h> | ||||
| #include "gc.h" | ||||
| #include "util.h" | ||||
| #include "state.h" | ||||
| #endif | ||||
|  | ||||
| /* Initialize a buffer */ | ||||
| JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) { | ||||
|     uint8_t *data = NULL; | ||||
|     if (capacity > 0) { | ||||
|         janet_vm_next_collection += capacity; | ||||
|         data = malloc(sizeof(uint8_t) * capacity); | ||||
|         if (NULL == data) { | ||||
|             JANET_OUT_OF_MEMORY; | ||||
| @@ -59,6 +61,7 @@ void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth) | ||||
|     if (capacity <= buffer->capacity) return; | ||||
|     int64_t big_capacity = capacity * growth; | ||||
|     capacity = big_capacity > INT32_MAX ? INT32_MAX : (int32_t) big_capacity; | ||||
|     janet_vm_next_collection += capacity - buffer->capacity; | ||||
|     new_data = realloc(old, capacity * sizeof(uint8_t)); | ||||
|     if (NULL == new_data) { | ||||
|         JANET_OUT_OF_MEMORY; | ||||
| @@ -90,6 +93,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; | ||||
|         if (NULL == new_data) { | ||||
|             JANET_OUT_OF_MEMORY; | ||||
|         } | ||||
|   | ||||
| @@ -50,6 +50,7 @@ static JanetFiber *fiber_alloc(int32_t capacity) { | ||||
|     if (NULL == data) { | ||||
|         JANET_OUT_OF_MEMORY; | ||||
|     } | ||||
|     janet_vm_next_collection += sizeof(Janet) * capacity; | ||||
|     fiber->data = data; | ||||
|     return fiber; | ||||
| } | ||||
| @@ -201,6 +202,7 @@ static void janet_env_detach(JanetFuncEnv *env) { | ||||
|     if (env) { | ||||
|         size_t s = sizeof(Janet) * env->length; | ||||
|         Janet *vmem = malloc(s); | ||||
|         janet_vm_next_collection += s; | ||||
|         if (NULL == vmem) { | ||||
|             JANET_OUT_OF_MEMORY; | ||||
|         } | ||||
|   | ||||
| @@ -24,6 +24,7 @@ | ||||
| #include <math.h> | ||||
| #include <janet.h> | ||||
| #include "util.h" | ||||
| #include "state.h" | ||||
| #endif | ||||
|  | ||||
| /* Macro fills */ | ||||
| @@ -161,6 +162,7 @@ Janet(janet_wrap_number)(double x) { | ||||
| void *janet_memalloc_empty(int32_t count) { | ||||
|     int32_t i; | ||||
|     void *mem = malloc(count * sizeof(JanetKV)); | ||||
|     janet_vm_next_collection += count * sizeof(JanetKV); | ||||
|     if (NULL == mem) { | ||||
|         JANET_OUT_OF_MEMORY; | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose