arraylist, scoped cleanup takes a ptr
This commit is contained in:
1 parent
31e88525ae
commit
1dd5d52bc2
7 files changed
+40
-12
No files matched your search
+3
-2
@@ -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)
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+5
-1
@@ -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)); })
|
||||
|
||||
+1
-1
@@ -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) { \
|
||||
|
||||
@@ -43,6 +43,7 @@ sources = [
|
||||
'src/client.c',
|
||||
'src/str.c',
|
||||
'src/eventloop.c',
|
||||
'src/array_list.c',
|
||||
eventloop_info['path'],
|
||||
]
|
||||
|
||||
|
||||
+3
-1
@@ -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(
|
||||
|
||||
+24
-4
@@ -17,10 +17,30 @@
|
||||
#include <fcntl.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) {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user