diff --git a/src/verto.c b/src/verto.c index 71eaffa..643474e 100644 --- a/src/verto.c +++ b/src/verto.c @@ -583,6 +583,8 @@ verto_set_allocator(void *(*resize)(void *mem, size_t size), void verto_free(verto_ctx *ctx) { + verto_ev *cur, *next; + if (!ctx) return; @@ -591,8 +593,12 @@ verto_free(verto_ctx *ctx) return; /* Cancel all pending events */ - while (ctx->events) - verto_del(ctx->events); + next = NULL; + for (cur = ctx->events; cur != NULL; cur = next) { + next = cur->next; + verto_del(cur); + } + ctx->events = NULL; /* Free the private */ if (!ctx->deflt || !ctx->module->funcs->ctx_default) @@ -658,20 +664,28 @@ verto_break(verto_ctx *ctx) int verto_reinitialize(verto_ctx *ctx) { - verto_ev *tmp, *next; + verto_ev *next, *prev, *cur; int error = 1; if (!ctx) return 0; /* Delete all events, but keep around the forkable ev structs */ - for (tmp = ctx->events; tmp; tmp = next) { - next = tmp->next; + prev = NULL; + for (cur = ctx->events; cur != NULL; cur = next) { + next = cur->next; + + if (cur->flags & VERTO_EV_FLAG_REINITIABLE) { + ctx->module->funcs->ctx_del(ctx->ctx, cur, cur->ev); + prev = cur; + continue; + } - if (tmp->flags & VERTO_EV_FLAG_REINITIABLE) - ctx->module->funcs->ctx_del(ctx->ctx, tmp, tmp->ev); - else - verto_del(tmp); + verto_del(cur); + if (prev) + prev->next = next; + if (cur == ctx->events) + ctx->events = next; } /* Reinit the loop */ @@ -679,10 +693,10 @@ verto_reinitialize(verto_ctx *ctx) ctx->module->funcs->ctx_reinitialize(ctx->ctx); /* Recreate events that were marked forkable */ - for (tmp = ctx->events; tmp; tmp = tmp->next) { - tmp->actual = make_actual(tmp->flags); - tmp->ev = ctx->module->funcs->ctx_add(ctx->ctx, tmp, &tmp->actual); - if (!tmp->ev) + for (cur = ctx->events; cur != NULL; cur = cur->next) { + cur->actual = make_actual(cur->flags); + cur->ev = ctx->module->funcs->ctx_add(ctx->ctx, cur, &cur->actual); + if (!cur->ev) error = 0; }