arraylist, scoped cleanup takes a ptr

main
pantonshire 1 week ago
parent 31e88525ae
commit 1dd5d52bc2

@ -42,6 +42,7 @@ struct owd_event {
struct owd_eventloop { struct owd_eventloop {
struct intrusive_list event_list; struct intrusive_list event_list;
bool valid;
#if defined(OWD_EVENTLOOP_KQUEUE) #if defined(OWD_EVENTLOOP_KQUEUE)
uintptr_t kqueue_timer_next; 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_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); 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) #define EVENTLOOP_STACK_CLOSE(EL) _scoped_eventloop_stack_cleanup(EL)

@ -5,9 +5,9 @@
#include "scoped.h" #include "scoped.h"
static inline void _close_if_nonnegative(int fd) { static inline void _close_if_nonnegative(int *fd) {
if (fd >= 0) if (*fd >= 0)
close(fd); close(*fd);
} }
DEF_SCOPED_TYPE(fd, int, -1, _close_if_nonnegative) DEF_SCOPED_TYPE(fd, int, -1, _close_if_nonnegative)

@ -5,7 +5,11 @@
#include "scoped.h" #include "scoped.h"
#include "typecheck.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_SCOPED(TYPE, VAL) SCOPED(mem, TYPE *, VAL)
#define MEM_FREE(TYPE, MEM) ({ CHECK_TYPE(TYPE **, MEM), (TYPE *)_scoped_mem_cleanup((void **)(MEM)); }) #define MEM_FREE(TYPE, MEM) ({ CHECK_TYPE(TYPE **, MEM), (TYPE *)_scoped_mem_cleanup((void **)(MEM)); })

@ -5,7 +5,7 @@
return (UNOCCUPIED); \ return (UNOCCUPIED); \
} \ } \
static inline void _scoped_##NAME##_cleanup(TYPE *_val) { \ static inline void _scoped_##NAME##_cleanup(TYPE *_val) { \
CLEANUP_FN(*_val); \ CLEANUP_FN(_val); \
*_val = (UNOCCUPIED); \ *_val = (UNOCCUPIED); \
} \ } \
static inline void _scoped_##NAME##_cleanup_raw(void *_val) { \ static inline void _scoped_##NAME##_cleanup_raw(void *_val) { \

@ -43,6 +43,7 @@ sources = [
'src/client.c', 'src/client.c',
'src/str.c', 'src/str.c',
'src/eventloop.c', 'src/eventloop.c',
'src/array_list.c',
eventloop_info['path'], eventloop_info['path'],
] ]

@ -11,6 +11,7 @@ int owd_eventloop_init(struct owd_eventloop *el) {
if ((platform_res = owd_platform_el_init(el))) if ((platform_res = owd_platform_el_init(el)))
return platform_res; return platform_res;
el->valid = true;
return 0; return 0;
} }
@ -18,7 +19,7 @@ void owd_eventloop_cleanup(struct owd_eventloop *el) {
struct intrusive_list *li; struct intrusive_list *li;
struct owd_event *e; struct owd_event *e;
if (!el) if (!el || !el->valid)
return; return;
INTRUSIVE_LIST_FOR_EACH_NO_REMOVE(li, &el->event_list) { 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); owd_platform_el_cleanup(el);
el->valid = false;
} }
int owd_eventloop_wait( int owd_eventloop_wait(

@ -17,10 +17,30 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/socket.h> #include <sys/socket.h>
#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) { static int ev_test(void) {
struct owd_event timer, pipe_ev; struct owd_event timer, pipe_ev;
struct owd_eventloop el; EVENTLOOP_STACK_SCOPED(el);
EVENTLOOP_STACK_SCOPED(el_ptr);
owd_event_id_t id; owd_event_id_t id;
uint32_t flags; uint32_t flags;
@ -41,7 +61,6 @@ static int ev_test(void) {
if (owd_eventloop_init(&el)) if (owd_eventloop_init(&el))
FAIL("failed to init event loop"); FAIL("failed to init event loop");
el_ptr = &el;
if (owd_eventloop_add_timer(&el, &timer, 2000, false)) if (owd_eventloop_add_timer(&el, &timer, 2000, false))
FAIL("failed to add timer"); FAIL("failed to add timer");
@ -83,7 +102,8 @@ int main(int argc, char **argv) {
struct client_conf client; struct client_conf client;
} conf; } conf;
return ev_test(); // return ev_test();
return al_test();
if (argc < 2) if (argc < 2)
return 1; return 1;

Loading…
Cancel
Save