1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-10 18:59:54 +00:00

refactor(c-api): Dogfooding usage of size types and limits

This commit is contained in:
GrayJack 2024-04-16 16:22:28 -03:00
parent 3f54b282dd
commit b483c9e2e4
14 changed files with 65 additions and 62 deletions

View File

@ -70,7 +70,7 @@ void num_array_put(void *p, Janet key, Janet value) {
if (!janet_checktype(value, JANET_NUMBER))
janet_panic("expected number value");
index = (size_t)janet_unwrap_integer(key);
index = janet_unwrap_size(key);
if (index < array->size) {
array->data[index] = janet_unwrap_number(value);
}
@ -96,7 +96,7 @@ int num_array_get(void *p, Janet key, Janet *out) {
return janet_getmethod(janet_unwrap_keyword(key), methods, out);
if (!janet_checkint(key))
janet_panic("expected integer key");
index = (size_t)janet_unwrap_integer(key);
index = janet_unwrap_size(key);
if (index >= array->size) {
return 0;
} else {

View File

@ -77,7 +77,7 @@ void janet_array_ensure(JanetArray *array, size_t capacity, size_t growth) {
Janet *old = array->data;
if (capacity <= array->capacity) return;
size_t new_capacity = (capacity) * growth;
if (new_capacity > JANET_INTMAX_INT64) new_capacity = JANET_INTMAX_INT64;
if (new_capacity > JANET_INTMAX_SIZE) new_capacity = JANET_INTMAX_SIZE;
capacity = new_capacity;
newData = janet_realloc(old, capacity * sizeof(Janet));
if (NULL == newData) {
@ -102,7 +102,7 @@ void janet_array_setcount(JanetArray *array, size_t count) {
/* Push a value to the top of the array */
void janet_array_push(JanetArray *array, Janet x) {
if (array->count == JANET_INTMAX_INT64) {
if (array->count == JANET_INTMAX_SIZE) {
janet_panic("array overflow");
}
size_t newcount = array->count + 1;
@ -199,7 +199,7 @@ JANET_CORE_FN(cfun_array_push,
"Push all the elements of xs to the end of an array. Modifies the input array and returns it.") {
janet_arity(argc, 1, -1);
JanetArray *array = janet_getarray(argv, 0);
if ((size_t) JANET_INTMAX_INT64 - argc + 1 <= array->count) {
if ((size_t) JANET_INTMAX_SIZE - argc + 1 <= array->count) {
janet_panic("array overflow");
}
size_t newcount = array->count - 1 + argc;
@ -281,15 +281,15 @@ JANET_CORE_FN(cfun_array_insert,
size_t chunksize, restsize;
janet_arity(argc, 2, -1);
JanetArray *array = janet_getarray(argv, 0);
int32_t at = janet_getinteger(argv, 1);
ssize_t at = janet_getssize(argv, 1);
if (at < 0) {
at = array->count + at + 1;
}
if (at < 0 || (size_t) at > array->count)
janet_panicf("insertion index %d out of range [0,%d]", at, array->count);
janet_panicf("insertion index %d out of range [0,%d]", at, array->count);
chunksize = (argc - 2) * sizeof(Janet);
restsize = (array->count - at) * sizeof(Janet);
if ((size_t) JANET_INTMAX_INT64 - (argc - 2) < array->count) {
if ((size_t) JANET_INTMAX_SIZE - (argc - 2) < array->count) {
janet_panic("array overflow");
}
janet_array_ensure(array, array->count + argc - 2, 2);
@ -311,7 +311,7 @@ JANET_CORE_FN(cfun_array_remove,
"Returns the array.") {
janet_arity(argc, 2, 3);
JanetArray *array = janet_getarray(argv, 0);
int32_t at = janet_getinteger(argv, 1);
ssize_t at = janet_getssize(argv, 1);
size_t n = 1;
if (at < 0) {
at = array->count + at;

View File

@ -595,7 +595,7 @@ static JanetAssembleResult janet_asm1(JanetAssembler *parent, Janet source, int
x = janet_get1(s, janet_ckeywordv("constants"));
if (janet_indexed_view(x, &arr, &count)) {
def->constants_length = count;
def->constants = janet_malloc(sizeof(Janet) * (size_t) count);
def->constants = janet_malloc(sizeof(Janet) * count);
if (NULL == def->constants) {
JANET_OUT_OF_MEMORY;
}

View File

@ -39,8 +39,8 @@ static void janet_buffer_can_realloc(JanetBuffer *buffer) {
static JanetBuffer *janet_buffer_init_impl(JanetBuffer *buffer, size_t capacity) {
uint8_t *data = NULL;
if (capacity < 4) capacity = 4;
if (capacity > JANET_INTMAX_INT64)
capacity = JANET_INTMAX_INT64;
if (capacity > JANET_INTMAX_SIZE)
capacity = JANET_INTMAX_SIZE;
janet_gcpressure(capacity);
data = janet_malloc(sizeof(uint8_t) * capacity);
if (NULL == data) {
@ -91,7 +91,7 @@ void janet_buffer_ensure(JanetBuffer *buffer, size_t capacity, size_t growth) {
if (capacity <= buffer->capacity) return;
janet_buffer_can_realloc(buffer);
size_t big_capacity = (capacity) * growth;
capacity = big_capacity > JANET_INTMAX_INT64 ? JANET_INTMAX_INT64 : big_capacity;
capacity = big_capacity > JANET_INTMAX_SIZE ? JANET_INTMAX_SIZE : big_capacity;
janet_gcpressure(capacity - buffer->capacity);
new_data = janet_realloc(old, capacity * sizeof(uint8_t));
if (NULL == new_data) {
@ -115,13 +115,13 @@ void janet_buffer_setcount(JanetBuffer *buffer, size_t count) {
* next n bytes pushed to the buffer will not cause a reallocation */
void janet_buffer_extra(JanetBuffer *buffer, size_t n) {
/* Check for buffer overflow */
if (n + buffer->count > JANET_INTMAX_INT64) {
if (n + buffer->count > JANET_INTMAX_SIZE) {
janet_panic("buffer overflow");
}
size_t new_size = buffer->count + n;
if (new_size > buffer->capacity) {
janet_buffer_can_realloc(buffer);
size_t new_capacity = (new_size > (JANET_INTMAX_INT64 / 2)) ? JANET_INTMAX_INT64 : (new_size * 2);
size_t new_capacity = (new_size > (JANET_INTMAX_SIZE / 2)) ? JANET_INTMAX_SIZE : (new_size * 2);
uint8_t *new_data = janet_realloc(buffer->data, new_capacity * sizeof(uint8_t));
janet_gcpressure(new_capacity - buffer->capacity);
if (NULL == new_data) {
@ -618,8 +618,8 @@ JANET_CORE_FN(cfun_buffer_blit,
} else {
length_src = src.len - offset_src;
}
int64_t last = (int64_t) offset_dest + length_src;
if (last > JANET_INTMAX_INT64)
size_t last = offset_dest + length_src;
if (last > JANET_INTMAX_SIZE)
janet_panic("buffer blit out of range");
size_t last32 = (size_t) last;
janet_buffer_ensure(dest, last32, 2);

View File

@ -232,19 +232,19 @@ const char *janet_getcbytes(const Janet *argv, int32_t n) {
char *new_string = janet_smalloc(b->count + 1);
memcpy(new_string, b->data, b->count);
new_string[b->count] = 0;
if (strlen(new_string) != (size_t) b->count) goto badzeros;
if (strlen(new_string) != b->count) goto badzeros;
return new_string;
} else {
/* Ensure trailing 0 */
janet_buffer_push_u8(b, 0);
b->count--;
if (strlen((char *)b->data) != (size_t) b->count) goto badzeros;
if (strlen((char *)b->data) != b->count) goto badzeros;
return (const char *) b->data;
}
}
JanetByteView view = janet_getbytes(argv, n);
const char *cstr = (const char *)view.bytes;
if (strlen(cstr) != (size_t) view.len) goto badzeros;
if (strlen(cstr) != view.len) goto badzeros;
return cstr;
badzeros:
@ -337,7 +337,7 @@ size_t janet_getsize(const Janet *argv, int32_t n) {
if (!janet_checksize(x)) {
janet_panicf("bad slot #%d, expected size, got %v", n, x);
}
return (size_t) janet_unwrap_number(x);
return janet_unwrap_size(x);
}
ssize_t janet_getssize(const Janet *argv, int32_t n) {
@ -345,7 +345,7 @@ ssize_t janet_getssize(const Janet *argv, int32_t n) {
if (!janet_checkssize(x)) {
janet_panicf("bad slot #%d, expected ssize, got %v", n, x);
}
return (ssize_t) janet_unwrap_number(x);
return janet_unwrap_ssize(x);
}
int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which) {

View File

@ -92,7 +92,7 @@ static const char *janet_dyncstring(const char *name, const char *dflt) {
}
const uint8_t *jstr = janet_unwrap_string(x);
const char *cstr = (const char *)jstr;
if (strlen(cstr) != (size_t) janet_string_length(jstr)) {
if (strlen(cstr) != janet_string_length(jstr)) {
janet_panicf("string %v contains embedded 0s", x);
}
return cstr;

View File

@ -107,7 +107,7 @@ static const JanetFFIPrimInfo janet_ffi_type_info[] = {
struct JanetFFIType {
JanetFFIStruct *st;
JanetFFIPrimType prim;
int32_t array_count;
ssize_t array_count;
};
typedef struct {
@ -438,7 +438,7 @@ static JanetFFIType decode_ffi_type(Janet x) {
if (janet_checktype(x, JANET_ARRAY)) {
if (len != 2 && len != 1) janet_panicf("array type must be of form @[type count], got %v", x);
ret = decode_ffi_type(els[0]);
int32_t array_count = len == 1 ? 0 : janet_getnat(els, 1);
ssize_t array_count = len == 1 ? 0 : janet_getssize(els, 1);
ret.array_count = array_count;
} else {
ret.st = build_struct_type(len, els);
@ -1308,7 +1308,7 @@ JANET_CORE_FN(cfun_ffi_jitfn,
JanetByteView bytes = janet_getbytes(argv, 0);
/* Quick hack to align to page boundary, we should query OS. FIXME */
size_t alloc_size = ((size_t) bytes.len + FFI_PAGE_MASK) & ~FFI_PAGE_MASK;
size_t alloc_size = (bytes.len + FFI_PAGE_MASK) & ~FFI_PAGE_MASK;
#ifdef JANET_FFI_JIT
#ifdef JANET_EV
@ -1408,14 +1408,14 @@ JANET_CORE_FN(cfun_ffi_buffer_read,
janet_sandbox_assert(JANET_SANDBOX_FFI_USE);
janet_arity(argc, 2, 3);
JanetFFIType type = decode_ffi_type(argv[0]);
size_t offset = (size_t) janet_optnat(argv, argc, 2, 0);
size_t offset = janet_optsize(argv, argc, 2, 0);
if (janet_checktype(argv[1], JANET_POINTER)) {
uint8_t *ptr = janet_unwrap_pointer(argv[1]);
return janet_ffi_read_one(ptr + offset, type, JANET_FFI_MAX_RECUR);
} else {
size_t el_size = type_size(type);
JanetByteView bytes = janet_getbytes(argv, 1);
if ((size_t) bytes.len < offset + el_size) janet_panic("read out of range");
if (bytes.len < offset + el_size) janet_panic("read out of range");
return janet_ffi_read_one(bytes.bytes + offset, type, JANET_FFI_MAX_RECUR);
}
}
@ -1523,8 +1523,8 @@ JANET_CORE_FN(cfun_ffi_pointer_buffer,
janet_sandbox_assert(JANET_SANDBOX_FFI_USE);
janet_arity(argc, 2, 4);
void *pointer = janet_getpointer(argv, 0);
int32_t capacity = janet_getnat(argv, 1);
int32_t count = janet_optnat(argv, argc, 2, 0);
size_t capacity = janet_getsize(argv, 1);
size_t count = janet_optsize(argv, argc, 2, 0);
int64_t offset = janet_optinteger64(argv, argc, 3, 0);
uint8_t *offset_pointer = ((uint8_t *) pointer) + offset;
return janet_wrap_buffer(janet_pointer_buffer_unsafe(offset_pointer, capacity, count));

View File

@ -54,8 +54,8 @@ static JanetFiber *fiber_alloc(size_t capacity) {
if (capacity < 32) {
capacity = 32;
}
if (capacity > JANET_INTMAX_INT64) {
capacity = JANET_INTMAX_INT64;
if (capacity > JANET_INTMAX_SIZE) {
capacity = JANET_INTMAX_SIZE;
}
fiber->capacity = capacity;
data = janet_malloc(sizeof(Janet) * capacity);
@ -131,7 +131,7 @@ void janet_fiber_setcapacity(JanetFiber *fiber, size_t n) {
/* Grow fiber if needed */
static void janet_fiber_grow(JanetFiber *fiber, size_t needed) {
size_t cap = needed > (JANET_INTMAX_INT64 / 2) ? JANET_INTMAX_INT64 : 2 * needed;
size_t cap = needed > (JANET_INTMAX_SIZE / 2) ? JANET_INTMAX_SIZE : 2 * needed;
janet_fiber_setcapacity(fiber, cap);
}
@ -171,7 +171,7 @@ void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z) {
/* Push an array on the next stack frame */
void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n) {
if (fiber->stacktop > (size_t) INT32_MAX - n) janet_panic("stack overflow");
if (fiber->stacktop > (size_t) JANET_INTMAX_SIZE - n) janet_panic("stack overflow");
size_t newtop = fiber->stacktop + n;
if (newtop > fiber->capacity) {
janet_fiber_grow(fiber, newtop);

View File

@ -325,7 +325,7 @@ static EnvBlock os_execute_env(int32_t argc, const Janet *argv) {
memcpy(ret, temp->data, temp->count);
return ret;
#else
char **envp = janet_smalloc(sizeof(char *) * ((size_t)dict.len + 1));
char **envp = janet_smalloc(sizeof(char *) * (dict.len + 1));
int32_t j = 0;
for (size_t i = 0; i < dict.cap; i++) {
const JanetKV *kv = dict.kvs + i;
@ -333,18 +333,18 @@ static EnvBlock os_execute_env(int32_t argc, const Janet *argv) {
if (!janet_checktype(kv->value, JANET_STRING)) continue;
const uint8_t *keys = janet_unwrap_string(kv->key);
const uint8_t *vals = janet_unwrap_string(kv->value);
int32_t klen = janet_string_length(keys);
int32_t vlen = janet_string_length(vals);
size_t klen = janet_string_length(keys);
size_t vlen = janet_string_length(vals);
/* Check keys has no embedded 0s or =s. */
int skip = 0;
for (int32_t k = 0; k < klen; k++) {
for (size_t k = 0; k < klen; k++) {
if (keys[k] == '\0' || keys[k] == '=') {
skip = 1;
break;
}
}
if (skip) continue;
char *envitem = janet_smalloc((size_t) klen + (size_t) vlen + 2);
char *envitem = janet_smalloc(klen + vlen + 2);
memcpy(envitem, keys, klen);
envitem[klen] = '=';
memcpy(envitem + klen + 1, vals, vlen);
@ -1246,7 +1246,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
/* Result */
int status = 0;
const char **child_argv = janet_smalloc(sizeof(char *) * ((size_t) exargs.len + 1));
const char **child_argv = janet_smalloc(sizeof(char *) * (exargs.len + 1));
for (size_t i = 0; i < exargs.len; i++)
child_argv[i] = janet_getcstring(exargs.items, i);
child_argv[exargs.len] = NULL;

View File

@ -205,12 +205,11 @@ JANET_CORE_FN(cfun_string_repeat,
"Returns a string that is `n` copies of `bytes` concatenated.") {
janet_fixarity(argc, 2);
JanetByteView view = janet_getbytes(argv, 0);
int32_t rep = janet_getinteger(argv, 1);
if (rep < 0) janet_panic("expected non-negative number of repetitions");
size_t rep = janet_getsize(argv, 1);
if (rep == 0) return janet_cstringv("");
int64_t mulres = (int64_t) rep * view.len;
if (mulres > JANET_INTMAX_INT64) janet_panic("result string is too long");
uint8_t *newbuf = janet_string_begin((int32_t) mulres);
size_t mulres = rep * view.len;
if (mulres > JANET_INTMAX_SIZE) janet_panic("result string is too long");
uint8_t *newbuf = janet_string_begin(mulres);
uint8_t *end = newbuf + mulres;
for (uint8_t *p = newbuf; p < end; p += view.len) {
safe_memcpy(p, view.bytes, view.len);
@ -506,7 +505,7 @@ JANET_CORE_FN(cfun_string_join,
}
/* Check args */
size_t i;
int64_t finallen = 0;
size_t finallen = 0;
for (i = 0; i < parts.len; i++) {
const uint8_t *chunk;
size_t chunklen = 0;
@ -515,11 +514,11 @@ JANET_CORE_FN(cfun_string_join,
}
if (i) finallen += joiner.len;
finallen += chunklen;
if (finallen > INT32_MAX)
if (finallen > JANET_INTMAX_SIZE)
janet_panic("result string too long");
}
uint8_t *buf, *out;
out = buf = janet_string_begin((size_t) finallen);
out = buf = janet_string_begin(finallen);
for (i = 0; i < parts.len; i++) {
const uint8_t *chunk = NULL;
size_t chunklen = 0;

View File

@ -49,12 +49,12 @@ JanetKV *janet_struct_begin(size_t count) {
* specialized to structs (slightly more compact). */
const JanetKV *janet_struct_find(const JanetKV *st, Janet key) {
size_t cap = janet_struct_capacity(st);
int32_t index = janet_maphash(cap, janet_hash(key));
size_t index = (size_t) janet_maphash(cap, janet_hash(key));
size_t i;
for (i = index; i < cap; i++)
if (janet_checktype(st[i].key, JANET_NIL) || janet_equals(st[i].key, key))
return st + i;
for (i = 0; i < (size_t) index; i++)
for (i = 0; i < index; i++)
if (janet_checktype(st[i].key, JANET_NIL) || janet_equals(st[i].key, key))
return st + i;
return NULL;
@ -71,9 +71,9 @@ const JanetKV *janet_struct_find(const JanetKV *st, Janet key) {
void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) {
size_t cap = janet_struct_capacity(st);
int32_t hash = janet_hash(key);
int32_t index = janet_maphash(cap, hash);
int32_t i, j, dist;
int32_t bounds[4] = {index, cap, 0, index};
size_t index = (size_t) janet_maphash(cap, hash);
size_t i, j, dist;
size_t bounds[4] = {index, cap, 0, index};
if (janet_checktype(key, JANET_NIL) || janet_checktype(value, JANET_NIL)) return;
if (janet_checktype(key, JANET_NUMBER) && isnan(janet_unwrap_number(key))) return;
/* Avoid extra items */
@ -82,7 +82,7 @@ void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) {
for (i = bounds[j]; i < bounds[j + 1]; i++, dist++) {
int status;
int32_t otherhash;
int32_t otherindex, otherdist;
size_t otherindex, otherdist;
JanetKV *kv = st + i;
/* We found an empty slot, so just add key and value */
if (janet_checktype(kv->key, JANET_NIL)) {
@ -99,7 +99,7 @@ void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) {
* will compare properly - i.e., {1 2 3 4} should equal {3 4 1 2}.
* Collisions are resolved via an insertion sort insertion. */
otherhash = janet_hash(kv->key);
otherindex = janet_maphash(cap, otherhash);
otherindex = (size_t) janet_maphash(cap, otherhash);
otherdist = (i + cap - otherindex) & (cap - 1);
if (dist < otherdist)
status = -1;
@ -237,18 +237,18 @@ JANET_CORE_FN(cfun_struct_flatten,
JanetStruct st = janet_getstruct(argv, 0);
/* get an upper bounds on the number of items in the final struct */
int64_t pair_count = 0;
size_t pair_count = 0;
JanetStruct cursor = st;
while (cursor) {
pair_count += janet_struct_length(cursor);
cursor = janet_struct_proto(cursor);
}
if (pair_count > JANET_INTMAX_INT64) {
if (pair_count > JANET_INTMAX_SIZE) {
janet_panic("struct too large");
}
JanetKV *accum = janet_struct_begin((size_t) pair_count);
JanetKV *accum = janet_struct_begin(pair_count);
cursor = st;
while (cursor) {
for (size_t i = 0; i < janet_struct_capacity(cursor); i++) {

View File

@ -32,7 +32,7 @@
static void *janet_memalloc_empty_local(size_t count) {
size_t i;
void *mem = janet_smalloc((size_t) count * sizeof(JanetKV));
void *mem = janet_smalloc(count * sizeof(JanetKV));
JanetKV *mmem = (JanetKV *)mem;
for (i = 0; i < count; i++) {
JanetKV *kv = mmem + i;

View File

@ -667,7 +667,7 @@ size_t janet_length(Janet x) {
const JanetAbstractType *type = janet_abstract_type(abst);
if (type->length != NULL) {
size_t len = type->length(abst, janet_abstract_size(abst));
if (len > JANET_INTMAX_INT64) {
if (len > JANET_INTMAX_SIZE) {
janet_panicf("invalid integer length %u", len);
}
return len;
@ -771,7 +771,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
case JANET_ARRAY: {
JanetArray *array = janet_unwrap_array(ds);
size_t index = getter_checksize(type, key, JANET_INTMAX_INT64 - 1);
size_t index = getter_checksize(type, key, JANET_INTMAX_SIZE - 1);
if (index >= array->count) {
janet_array_setcount(array, index + 1);
}
@ -780,7 +780,7 @@ void janet_put(Janet ds, Janet key, Janet value) {
}
case JANET_BUFFER: {
JanetBuffer *buffer = janet_unwrap_buffer(ds);
size_t index = getter_checksize(type, key, JANET_INTMAX_INT64 - 1);
size_t index = getter_checksize(type, key, JANET_INTMAX_SIZE - 1);
if (!janet_checkint(value))
janet_panicf("can only put integers in buffers, got %v", value);
if (index >= buffer->count) {

View File

@ -380,7 +380,11 @@ typedef struct JanetOSRWLock JanetOSRWLock;
#include <stdio.h>
/* signed size */
#ifdef _SSIZE_T
typedef ssize_t ssize_t;
#else
typedef ptrdiff_t ssize_t;
#endif
/* What to do when out of memory */
#ifndef JANET_OUT_OF_MEMORY