diff --git a/include/eventloop.h b/include/eventloop.h index bec59d1..578bc7d 100644 --- a/include/eventloop.h +++ b/include/eventloop.h @@ -42,6 +42,7 @@ struct owd_event { struct owd_eventloop { struct intrusive_list event_list; + bool valid; #if defined(OWD_EVENTLOOP_KQUEUE) uintptr_t kqueue_timer_next; @@ -66,7 +67,7 @@ void owd_eventloop_remove(struct owd_eventloop *el, struct owd_event *e); int owd_eventloop_add_timer(struct owd_eventloop *el, struct owd_event *e, uint64_t millis, bool oneshot); int owd_eventloop_add_fd(struct owd_eventloop *el, struct owd_event *e, int fd, uint32_t flags); -DEF_SCOPED_TYPE(eventloop_stack, struct owd_eventloop *, NULL, owd_eventloop_cleanup) +DEF_SCOPED_TYPE(eventloop_stack, struct owd_eventloop, (struct owd_eventloop) { .valid = false }, owd_eventloop_cleanup) -#define EVENTLOOP_STACK_SCOPED(VAL) SCOPED(eventloop_stack, struct owd_eventloop *, VAL) +#define EVENTLOOP_STACK_SCOPED(VAL) SCOPED(eventloop_stack, struct owd_eventloop, VAL) #define EVENTLOOP_STACK_CLOSE(EL) _scoped_eventloop_stack_cleanup(EL) diff --git a/include/io.h b/include/io.h index 901e2ff..55efec4 100644 --- a/include/io.h +++ b/include/io.h @@ -5,9 +5,9 @@ #include "scoped.h" -static inline void _close_if_nonnegative(int fd) { - if (fd >= 0) - close(fd); +static inline void _close_if_nonnegative(int *fd) { + if (*fd >= 0) + close(*fd); } DEF_SCOPED_TYPE(fd, int, -1, _close_if_nonnegative) diff --git a/include/mem.h b/include/mem.h index 8d8c7ba..29ebd75 100644 --- a/include/mem.h +++ b/include/mem.h @@ -5,7 +5,11 @@ #include "scoped.h" #include "typecheck.h" -DEF_SCOPED_TYPE(mem, void *, NULL, free) +static inline void _mem_free(void **p) { + free(*p); +} + +DEF_SCOPED_TYPE(mem, void *, NULL, _mem_free) #define MEM_SCOPED(TYPE, VAL) SCOPED(mem, TYPE *, VAL) #define MEM_FREE(TYPE, MEM) ({ CHECK_TYPE(TYPE **, MEM), (TYPE *)_scoped_mem_cleanup((void **)(MEM)); }) diff --git a/include/scoped.h b/include/scoped.h index 6899bd2..219b67d 100644 --- a/include/scoped.h +++ b/include/scoped.h @@ -5,7 +5,7 @@ return (UNOCCUPIED); \ } \ static inline void _scoped_##NAME##_cleanup(TYPE *_val) { \ - CLEANUP_FN(*_val); \ + CLEANUP_FN(_val); \ *_val = (UNOCCUPIED); \ } \ static inline void _scoped_##NAME##_cleanup_raw(void *_val) { \ diff --git a/meson.build b/meson.build index 37af9df..2f2b971 100644 --- a/meson.build +++ b/meson.build @@ -43,6 +43,7 @@ sources = [ 'src/client.c', 'src/str.c', 'src/eventloop.c', + 'src/array_list.c', eventloop_info['path'], ] diff --git a/src/eventloop.c b/src/eventloop.c index 48e677c..6e363b5 100644 --- a/src/eventloop.c +++ b/src/eventloop.c @@ -11,6 +11,7 @@ int owd_eventloop_init(struct owd_eventloop *el) { if ((platform_res = owd_platform_el_init(el))) return platform_res; + el->valid = true; return 0; } @@ -18,7 +19,7 @@ void owd_eventloop_cleanup(struct owd_eventloop *el) { struct intrusive_list *li; struct owd_event *e; - if (!el) + if (!el || !el->valid) return; INTRUSIVE_LIST_FOR_EACH_NO_REMOVE(li, &el->event_list) { @@ -27,6 +28,7 @@ void owd_eventloop_cleanup(struct owd_eventloop *el) { } owd_platform_el_cleanup(el); + el->valid = false; } int owd_eventloop_wait( diff --git a/src/main.c b/src/main.c index bd325a9..892b2aa 100644 --- a/src/main.c +++ b/src/main.c @@ -17,10 +17,30 @@ #include #include +#include "array_list.h" + +static void test_cleanup(int *i) { + printf("cleanup %d\n", *i); +} + +DEF_ARRAY_LIST_TYPE(test, int, 0, test_cleanup) + +static int al_test(void) { + SCOPED(array_list_test, struct array_list, al); + + if (array_list_test_alloc(&al, 16)) + return 1; + array_list_test_push_noalloc_byval(&al, 2); + array_list_test_push_noalloc_byval(&al, 4); + array_list_test_push_noalloc_byval(&al, 6); + array_list_test_push_noalloc_byval(&al, 8); + + return 0; +} + static int ev_test(void) { struct owd_event timer, pipe_ev; - struct owd_eventloop el; - EVENTLOOP_STACK_SCOPED(el_ptr); + EVENTLOOP_STACK_SCOPED(el); owd_event_id_t id; uint32_t flags; @@ -41,7 +61,6 @@ static int ev_test(void) { if (owd_eventloop_init(&el)) FAIL("failed to init event loop"); - el_ptr = ⪙ if (owd_eventloop_add_timer(&el, &timer, 2000, false)) FAIL("failed to add timer"); @@ -83,7 +102,8 @@ int main(int argc, char **argv) { struct client_conf client; } conf; - return ev_test(); + // return ev_test(); + return al_test(); if (argc < 2) return 1;