From aba90a3a6f6c1caee28aaf3dadebb4daba5b5761 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 9 Aug 2025 16:06:32 +0300 Subject: use newer conts --- deps/conts | 2 +- include/gran/vec.h | 46 ------------------------------------ src/bus/simple_bus.c | 16 +++++++------ src/clock_domain.c | 23 ++++++++++-------- src/vec.c | 67 ---------------------------------------------------- 5 files changed, 23 insertions(+), 131 deletions(-) delete mode 100644 include/gran/vec.h delete mode 100644 src/vec.c diff --git a/deps/conts b/deps/conts index 4f647dc..7774ae2 160000 --- a/deps/conts +++ b/deps/conts @@ -1 +1 @@ -Subproject commit 4f647dc8520a9186b367400c5a337b681aec5565 +Subproject commit 7774ae2f8c2dca9ab2d93082856f031e78a1b5f0 diff --git a/include/gran/vec.h b/include/gran/vec.h deleted file mode 100644 index 08a608a..0000000 --- a/include/gran/vec.h +++ /dev/null @@ -1,46 +0,0 @@ -/* SPDX-License-Identifier: copyleft-next-0.3.1 */ - -#ifndef VEC_H -#define VEC_H - -#include - -struct vec { - size_t n; - size_t s; - size_t ns; - void *buf; -}; - -struct vec vec_create(size_t s); -void vec_destroy(struct vec *v); -void vec_reset(struct vec *v); - -size_t vec_len(struct vec *v); -void *vec_at(struct vec *v, size_t i); -void *vec_back(struct vec *v); -void *vec_pop(struct vec *v); -void vec_append(struct vec *v, void *n); - -typedef int (*vec_comp_t)(const void *, const void *); -void vec_sort(struct vec *v, vec_comp_t comp); - -#define foreach_vec(iter, v) \ - for (size_t iter = 0; iter < vec_len(&v); ++iter) - -#define vect_at(type, v, i) \ - *(type *)vec_at(&v, i) - -#define vect_append(type, v, e) \ - vec_append(&v, (type *)(e)) - -#define vect_back(type, v) \ - *(type *)vec_back(&v) - -#define vect_pop(type, v) \ - *(type *)vec_pop(&v) - -#define vec_uninit(v) \ - (v.buf == NULL) - -#endif /* VEC_H */ diff --git a/src/bus/simple_bus.c b/src/bus/simple_bus.c index 1932419..e1e9a88 100644 --- a/src/bus/simple_bus.c +++ b/src/bus/simple_bus.c @@ -3,7 +3,6 @@ #include #include -#include struct bus_region { uint64_t addr; @@ -11,9 +10,13 @@ struct bus_region { struct component *component; }; +#define VEC_NAME regions +#define VEC_TYPE struct bus_region +#include + struct simple_bus { struct component component; - struct vec regions; + struct regions regions; struct component *send; struct packet pkt; @@ -22,8 +25,7 @@ struct simple_bus { static struct bus_region *find_bus_region(struct simple_bus *bus, uint64_t addr) { - foreach_vec(i, bus->regions) { - struct bus_region *r = vec_at(&bus->regions, i); + foreach(regions, r, &bus->regions) { if (addr >= r->addr && addr < (r->addr + r->size)) return r; } @@ -70,7 +72,7 @@ static stat simple_bus_receive(struct simple_bus *bus, struct component *from, s static void simple_bus_destroy(struct simple_bus *bus) { - vec_destroy(&bus->regions); + regions_destroy(&bus->regions); free(bus); } @@ -84,7 +86,7 @@ struct component *create_simple_bus() bus->component.clock = (clock_callback)simple_bus_clock; bus->component.destroy = (destroy_callback)simple_bus_destroy; - bus->regions = vec_create(sizeof(struct bus_region)); + bus->regions = regions_create(0); return (struct component *)bus; } @@ -112,6 +114,6 @@ stat simple_bus_add(struct component *bus, struct component *component, .size = size }; - vect_append(struct bus_region, b->regions, &new); + regions_append(&b->regions, new); return OK; } diff --git a/src/clock_domain.c b/src/clock_domain.c index 74f0f03..c1f0450 100644 --- a/src/clock_domain.c +++ b/src/clock_domain.c @@ -12,10 +12,13 @@ #include #include -#include + +#define VEC_NAME components +#define VEC_TYPE struct component * +#include struct clock_domain { - struct vec components; + struct components components; struct clock_time time; tick period; @@ -39,23 +42,24 @@ struct clock_domain *create_clock_domain(tick period) return NULL; clk->period = period; - clk->components = vec_create(sizeof(struct component *)); + clk->components = components_create(0); return clk; } void destroy_clock_domain(struct clock_domain *clk) { - for (size_t i = 0; i < vec_len(&clk->components); ++i) - destroy(vect_at(struct component *, clk->components, i)); + foreach(components, c, &clk->components) { + destroy(*c); + } - vec_destroy(&clk->components); + components_destroy(&clk->components); free(clk); } stat clock_domain_add(struct clock_domain *clk, struct component *component) { - vect_append(struct component *, clk->components, &component); + components_append(&clk->components, component); return OK; } @@ -69,9 +73,8 @@ static void clocked_component_tick(struct clock_domain *clk, stat clock_domain_tick(struct clock_domain *clk) { - for (size_t i = 0; i < vec_len(&clk->components); ++i) { - clocked_component_tick(clk, - vect_at(struct component *, clk->components, i)); + foreach(components, c, &clk->components) { + clocked_component_tick(clk, *c); } advance_clock(clk); diff --git a/src/vec.c b/src/vec.c deleted file mode 100644 index e56ac41..0000000 --- a/src/vec.c +++ /dev/null @@ -1,67 +0,0 @@ -/* SPDX-License-Identifier: copyleft-next-0.3.1 */ - -#include -#include -#include - -#include - -struct vec vec_create(size_t ns) -{ - return (struct vec) { - .n = 0, - .s = 1, - .ns = ns, - .buf = malloc(ns), - }; -} - -size_t vec_len(struct vec *v) -{ - return v->n; -} - -void *vec_at(struct vec *v, size_t i) -{ - assert(i < v->n && "out of vector bounds"); - return v->buf + i * v->ns; -} - -void *vec_back(struct vec *v) -{ - assert(v->n); - return v->buf + (v->n - 1) * v->ns; -} - -void *vec_pop(struct vec *v) -{ - assert(v->n && "attempting to pop empty vector"); - v->n--; - return v->buf + v->n * v->ns; -} - -void vec_append(struct vec *v, void *n) -{ - v->n++; - if (v->n >= v->s) { - v->s *= 2; - v->buf = realloc(v->buf, v->s * v->ns); - } - - void *p = vec_at(v, v->n - 1); - memcpy(p, n, v->ns); -} - -void vec_reset(struct vec *v) -{ - v->n = 0; -} - -void vec_destroy(struct vec *v) { - free(v->buf); -} - -void vec_sort(struct vec *v, vec_comp_t comp) -{ - qsort(v->buf, v->n, v->ns, comp); -} -- cgit v1.3