From af872a4cae2509ad1d5663964f581b035ca8dfb6 Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 15 Apr 2024 18:58:50 -0300 Subject: [PATCH] refactor(c-api): Use `size_t` on some `JanetFiber` fields --- src/core/fiber.c | 23 +++++++++++++---------- src/core/util.c | 3 +-- src/include/janet.h | 6 +++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/fiber.c b/src/core/fiber.c index 3ea6c80b..6cb88dec 100644 --- a/src/core/fiber.c +++ b/src/core/fiber.c @@ -48,14 +48,17 @@ static void fiber_reset(JanetFiber *fiber) { janet_fiber_set_status(fiber, JANET_STATUS_NEW); } -static JanetFiber *fiber_alloc(int32_t capacity) { +static JanetFiber *fiber_alloc(size_t capacity) { Janet *data; JanetFiber *fiber = janet_gcalloc(JANET_MEMORY_FIBER, sizeof(JanetFiber)); if (capacity < 32) { capacity = 32; } + if (capacity > JANET_INTMAX_INT64) { + capacity = JANET_INTMAX_INT64; + } fiber->capacity = capacity; - data = janet_malloc(sizeof(Janet) * (size_t) capacity); + data = janet_malloc(sizeof(Janet) * capacity); if (NULL == data) { JANET_OUT_OF_MEMORY; } @@ -93,7 +96,7 @@ JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t } /* Create a new fiber with argn values on the stack. */ -JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv) { +JanetFiber *janet_fiber(JanetFunction *callee, size_t capacity, int32_t argc, const Janet *argv) { return janet_fiber_reset(fiber_alloc(capacity), callee, argc, argv); } @@ -114,9 +117,9 @@ static void janet_fiber_refresh_memory(JanetFiber *fiber) { #endif /* Ensure that the fiber has enough extra capacity */ -void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n) { - int32_t old_size = fiber->capacity; - int32_t diff = n - old_size; +void janet_fiber_setcapacity(JanetFiber *fiber, size_t n) { + size_t old_size = fiber->capacity; + size_t diff = n - old_size; Janet *newData = janet_realloc(fiber->data, sizeof(Janet) * n); if (NULL == newData) { JANET_OUT_OF_MEMORY; @@ -127,8 +130,8 @@ void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n) { } /* Grow fiber if needed */ -static void janet_fiber_grow(JanetFiber *fiber, int32_t needed) { - int32_t cap = needed > (INT32_MAX / 2) ? INT32_MAX : 2 * needed; +static void janet_fiber_grow(JanetFiber *fiber, size_t needed) { + size_t cap = needed > (JANET_INTMAX_INT64 / 2) ? JANET_INTMAX_INT64 : 2 * needed; janet_fiber_setcapacity(fiber, cap); } @@ -178,8 +181,8 @@ void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n) { } /* Create a struct with n values. If n is odd, the last value is ignored. */ -static Janet make_struct_n(const Janet *args, int32_t n) { - int32_t i = 0; +static Janet make_struct_n(const Janet *args, size_t n) { + size_t i = 0; JanetKV *st = janet_struct_begin(n & (~1)); for (; i < n; i += 2) { janet_struct_put(st, args[i], args[i + 1]); diff --git a/src/core/util.c b/src/core/util.c index 235d4394..94bf864a 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -267,8 +267,7 @@ int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len) { /* Calculate next power of 2. May overflow. If n is 0, * will return 0. */ -int32_t janet_tablen(int32_t n) { - if (n < 0) return 0; +size_t janet_tablen(size_t n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; diff --git a/src/include/janet.h b/src/include/janet.h index 247b87cf..08bb48c1 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -924,7 +924,7 @@ struct JanetFiber { int32_t frame; /* Index of the stack frame */ int32_t stackstart; /* Beginning of next args */ int32_t stacktop; /* Top of stack. Where values are pushed and popped from. */ - int32_t capacity; /* How big is the stack memory */ + size_t capacity; /* How big is the stack memory */ int32_t maxstack; /* Arbitrary defined limit for stack overflow */ JanetTable *env; /* Dynamic bindings table (usually current environment). */ Janet *data; /* Dynamically resized stack memory */ @@ -1686,7 +1686,7 @@ JANET_API JanetSymbol janet_symbol_gen(void); #define janet_struct_capacity(t) (janet_struct_head(t)->capacity) #define janet_struct_hash(t) (janet_struct_head(t)->hash) #define janet_struct_proto(t) (janet_struct_head(t)->proto) -JANET_API JanetKV *janet_struct_begin(int32_t count); +JANET_API JanetKV *janet_struct_begin(size_t count); JANET_API void janet_struct_put(JanetKV *st, Janet key, Janet value); JANET_API JanetStruct janet_struct_end(JanetKV *st); JANET_API Janet janet_struct_get(JanetStruct st, Janet key); @@ -1713,7 +1713,7 @@ JANET_API JanetTable *janet_table_clone(JanetTable *table); JANET_API void janet_table_clear(JanetTable *table); /* Fiber */ -JANET_API JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv); +JANET_API JanetFiber *janet_fiber(JanetFunction *callee, size_t capacity, int32_t argc, const Janet *argv); JANET_API JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv); JANET_API JanetFiberStatus janet_fiber_status(JanetFiber *fiber); JANET_API int janet_fiber_can_resume(JanetFiber *fiber);