From 55293ae396eb3409113c0edf73c2f661d008f9d6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 20 Aug 2025 22:19:14 +0100 Subject: vec -> ivec I'm going to implement a normal stable vector instead of an inline vector so we have both options for use. I think that's smarter than just sticking to one. --- ivec.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ivec.c (limited to 'ivec.c') diff --git a/ivec.c b/ivec.c new file mode 100644 index 0000000..d780adc --- /dev/null +++ b/ivec.c @@ -0,0 +1,76 @@ +/* Copyright (C) 2025 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the Unlicense for details. + + * You may distribute and modify this code under the terms of the Unlicense, + * which you should have received a copy of along with this program. If not, + * please go to . + + * Created: 2025-08-19 + * Description: Inline Vector implementation + */ + +#include +#include + +#include "./alisp.h" + +void ivec_make(void **ptr, u64 size) +{ + if (!ptr) + return; + ivec_t *ivector = calloc(1, sizeof(*ivector) + size); + ivector->size = 0; + ivector->capacity = size; + *ptr = (ivector + 1); +} + +void ivec_free(void **data) +{ + if (!data || !*data) + return; + free(IVEC_GET(*data)); + *data = NULL; +} + +void ivec_ensure_remaining(void **ptr, u64 space) +{ + if (!ptr || !*ptr) + return; + ivec_t *ivec = IVEC_GET(*ptr); + if (ivec->capacity - ivec->size < space) + { + void *new_ivec = NULL; + ivec_make(&new_ivec, MAX(ivec->capacity * IVEC_MULT, ivec->size + space)); + IVEC_SIZE(new_ivec) = ivec->size; + memcpy(new_ivec, *ptr, ivec->size); + ivec_free(ptr); + *ptr = new_ivec; + } +} + +void ivec_append_byte(void **ptr, u8 byte) +{ + ivec_ensure_remaining(ptr, 1); + ivec_t *ivec = IVEC_GET(*ptr); + ivec->bytes[ivec->size++] = byte; +} + +void ivec_append(void **ptr, void *data, u64 size) +{ + ivec_ensure_remaining(ptr, size); + ivec_t *ivec = IVEC_GET(*ptr); + memcpy(*ptr + ivec->size, data, size); + ivec->size += size; +} + +void ivec_clone(void **dest, void **src) +{ + if (!dest || !src || !*src) + return; + ivec_make(dest, IVEC_SIZE(*src)); + memcpy(*dest, *src, IVEC_SIZE(*src)); + IVEC_SIZE(*dest) = IVEC_SIZE(*src); +} -- cgit v1.2.3-13-gbd6f