vec: FOR_VEC macro

This commit is contained in:
2026-03-05 19:47:46 +00:00
parent 99448f6702
commit b925a68986
6 changed files with 11 additions and 8 deletions

View File

@@ -10,6 +10,7 @@
#include <alisp/allocator.h> #include <alisp/allocator.h>
#include <alisp/lisp.h> #include <alisp/lisp.h>
#include <alisp/symtable.h>
/// System context /// System context
typedef struct typedef struct

View File

@@ -31,6 +31,8 @@ static_assert(sizeof(vec_t) == 64, "vec_t has to be 64 bytes as part of SBO");
#define VEC_GET(V, I, T) (((T *)vec_data(V))[I]) #define VEC_GET(V, I, T) (((T *)vec_data(V))[I])
#define VEC_SIZE(V, T) ((V)->size / (sizeof(T))) #define VEC_SIZE(V, T) ((V)->size / (sizeof(T)))
#define FOR_VEC(INDEX, V, T) \
for (size_t INDEX = 0; INDEX < VEC_SIZE(V, T); ++INDEX)
void vec_init(vec_t *, u64); void vec_init(vec_t *, u64);
void vec_free(vec_t *); void vec_free(vec_t *);

View File

@@ -135,7 +135,7 @@ lisp_t *alloc_make(alloc_t *alloc, tag_t type)
// We couldn't get anything from the free vector, so try to allocate a fresh // We couldn't get anything from the free vector, so try to allocate a fresh
// one against one of the pages. // one against one of the pages.
for (u64 i = 0; i < VEC_SIZE(&alloc->pages, page_t *); ++i) FOR_VEC(i, &alloc->pages, page_t *)
{ {
page_t *page = VEC_GET(&alloc->pages, i, page_t *); page_t *page = VEC_GET(&alloc->pages, i, page_t *);
node = make_node(page, type); node = make_node(page, type);
@@ -166,7 +166,7 @@ void alloc_delete(alloc_t *alloc, lisp_t *lisp)
u64 alloc_cost(alloc_t *alloc) u64 alloc_cost(alloc_t *alloc)
{ {
u64 total_size = alloc->pages.size; u64 total_size = alloc->pages.size;
for (u64 i = 0; i < VEC_SIZE(&alloc->pages, page_t *); ++i) FOR_VEC(i, &alloc->pages, page_t *)
{ {
page_t *page = VEC_GET(&alloc->pages, i, page_t *); page_t *page = VEC_GET(&alloc->pages, i, page_t *);
total_size += page->data.size; total_size += page->data.size;
@@ -176,7 +176,7 @@ u64 alloc_cost(alloc_t *alloc)
void alloc_free(alloc_t *alloc) void alloc_free(alloc_t *alloc)
{ {
for (u64 i = 0; i < VEC_SIZE(&alloc->pages, page_t *); ++i) FOR_VEC(i, &alloc->pages, page_t *)
{ {
page_t *page = VEC_GET(&alloc->pages, i, page_t *); page_t *page = VEC_GET(&alloc->pages, i, page_t *);
// Iterate through every alloc_node in this page // Iterate through every alloc_node in this page

View File

@@ -170,11 +170,11 @@ void lisp_print(FILE *fp, lisp_t *lisp)
#endif #endif
vec_t *vec = as_vec(lisp); vec_t *vec = as_vec(lisp);
for (u64 i = 1; i <= VEC_SIZE(vec, lisp_t *); ++i) FOR_VEC(i, vec, lisp_t *)
{ {
lisp_t *item = VEC_GET(vec, i - 1, lisp_t *); lisp_t *item = VEC_GET(vec, i, lisp_t *);
lisp_print(fp, item); lisp_print(fp, item);
if (i != VEC_SIZE(vec, lisp_t *)) if (i < VEC_SIZE(vec, lisp_t *))
{ {
fprintf(fp, " "); fprintf(fp, " ");
} }

View File

@@ -45,7 +45,7 @@ int main(int argc, char *argv[])
VEC_SIZE(&ast, lisp_t *) == 1 ? "expr" : "exprs"); VEC_SIZE(&ast, lisp_t *) == 1 ? "expr" : "exprs");
{ {
for (u64 i = 0; i < VEC_SIZE(&ast, lisp_t *); ++i) FOR_VEC(i, &ast, lisp_t *)
{ {
lisp_t *expr = VEC_GET(&ast, i, lisp_t *); lisp_t *expr = VEC_GET(&ast, i, lisp_t *);
#if VERBOSE_LOGS #if VERBOSE_LOGS

View File

@@ -129,7 +129,7 @@ void lisp_free_rec(sys_t *sys, lisp_t *item)
case TAG_VEC: case TAG_VEC:
{ {
vec_t *vec = as_vec(item); vec_t *vec = as_vec(item);
for (size_t i = 0; i < VEC_SIZE(vec, lisp_t **); ++i) FOR_VEC(i, vec, lisp_t *)
{ {
lisp_t *allocated = VEC_GET(vec, i, lisp_t *); lisp_t *allocated = VEC_GET(vec, i, lisp_t *);
lisp_free_rec(sys, allocated); lisp_free_rec(sys, allocated);