diff --git a/include/alisp/sys.h b/include/alisp/sys.h index 999bcd8..dff8b35 100644 --- a/include/alisp/sys.h +++ b/include/alisp/sys.h @@ -10,6 +10,7 @@ #include #include +#include /// System context typedef struct diff --git a/include/alisp/vec.h b/include/alisp/vec.h index 50cb4f9..1bbdb6a 100644 --- a/include/alisp/vec.h +++ b/include/alisp/vec.h @@ -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_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_free(vec_t *); diff --git a/src/allocator.c b/src/allocator.c index 2ba0a7d..f773204 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -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 // 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 *); 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 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 *); total_size += page->data.size; @@ -176,7 +176,7 @@ u64 alloc_cost(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 *); // Iterate through every alloc_node in this page diff --git a/src/lisp.c b/src/lisp.c index 4e4e78e..ec0ec81 100644 --- a/src/lisp.c +++ b/src/lisp.c @@ -170,11 +170,11 @@ void lisp_print(FILE *fp, lisp_t *lisp) #endif 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); - if (i != VEC_SIZE(vec, lisp_t *)) + if (i < VEC_SIZE(vec, lisp_t *)) { fprintf(fp, " "); } diff --git a/src/main.c b/src/main.c index 976d191..2644034 100644 --- a/src/main.c +++ b/src/main.c @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) 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 *); #if VERBOSE_LOGS diff --git a/src/sys.c b/src/sys.c index b324322..2569065 100644 --- a/src/sys.c +++ b/src/sys.c @@ -129,7 +129,7 @@ void lisp_free_rec(sys_t *sys, lisp_t *item) case TAG_VEC: { 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_free_rec(sys, allocated);