diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-08-09 16:06:32 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-08-09 16:06:32 +0300 |
| commit | aba90a3a6f6c1caee28aaf3dadebb4daba5b5761 (patch) | |
| tree | 082fc1f5c374f4a0417f345507cee58c23d3a2ad /src | |
| parent | bc920e9de94e884597214da9c20525605cfce6d6 (diff) | |
| download | gran-aba90a3a6f6c1caee28aaf3dadebb4daba5b5761.tar.gz gran-aba90a3a6f6c1caee28aaf3dadebb4daba5b5761.zip | |
use newer conts
Diffstat (limited to 'src')
| -rw-r--r-- | src/bus/simple_bus.c | 16 | ||||
| -rw-r--r-- | src/clock_domain.c | 23 | ||||
| -rw-r--r-- | src/vec.c | 67 |
3 files changed, 22 insertions, 84 deletions
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 <stdbool.h> #include <gran/bus/simple_bus.h> -#include <gran/vec.h> 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 <conts/vec.h> + 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 <assert.h> #include <gran/clock_domain.h> -#include <gran/vec.h> + +#define VEC_NAME components +#define VEC_TYPE struct component * +#include <conts/vec.h> 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 <stdlib.h> -#include <assert.h> -#include <string.h> - -#include <gran/vec.h> - -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); -} |
