From 9f2796ebac59794514bb3dc7fd6f2f76f4af3856 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 2 Jun 2019 10:06:50 -0400 Subject: [PATCH] Added uiEventFree(). Now to add it to the tests and see what AddressSanitizer says afterward. --- common/events.c | 21 +++++++++++++++++++++ common/programmererrors.h | 6 ++++++ doc/events.md | 10 ++++++++++ ui.h | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/common/events.c b/common/events.c index 932aa3b6..f0481b20 100644 --- a/common/events.c +++ b/common/events.c @@ -24,6 +24,7 @@ static int handlerCmp(const void *a, const void *b) struct uiEvent { uiEventOptions opts; + bool internal; uiprivArray handlers; uiprivArray unusedIDs; bool firing; @@ -59,6 +60,26 @@ uiEvent *uiNewEvent(const uiEventOptions *options) return ret; \ } +void uiFreeEvent(uiEvent *e) +{ + if (!uiprivCheckInitializedAndThread()) + return; + checkEventNonnull(e, /* nothing */); + if (e->internal) { + uiprivProgrammerErrorFreeingInternalEvent(); + return; + } + checkEventNotFiring(e, /* nothing */); + if (e->handlers.len != 0) { + uiprivProgrammerErrorFreeingEventInUse(); + return; + } + + uiprivArrayFree(e->unusedIDs); + uiprivArrayFree(e->handlers); + uiprivFree(e); +} + static bool checkEventSender(const uiEvent *e, void *sender, const char *func) { if (e->opts.Global && sender != NULL) { diff --git a/common/programmererrors.h b/common/programmererrors.h index a8292b9d..cfa61ed3 100644 --- a/common/programmererrors.h +++ b/common/programmererrors.h @@ -36,3 +36,9 @@ #define uiprivProgrammerErrorRecursiveEventFire() \ uiprivProgrammerError("attempt to fire a uiEvent while it is already being fired") + +#define uiprivProgrammerErrorFreeingInternalEvent() \ + uiprivProgrammerError("attempt to free a libui-provided uiEvent") + +#define uiprivProgrammerErrorFreeingEventInUse() \ + uiprivProgrammerError("attempt to free a uiEvent that still has handlers registered") diff --git a/doc/events.md b/doc/events.md index d0647ec6..27279d17 100644 --- a/doc/events.md +++ b/doc/events.md @@ -50,6 +50,16 @@ uiEvent *uiNewEvent(const uiEventOptions *options); It is a programmer error to specify `NULL` for `options`. +### `uiFreeEvent()` + +```c +uiprivExtern void uiFreeEvent(uiEvent *e); +``` + +Frees the given event. The event must not be a libui-provided event, and must not have any handlers registered to it. + +It is a programmer error to pass `NULL` for the given event, or to pass in an event that is currently firing. + ### `uiEventAddHandler()` ```c diff --git a/ui.h b/ui.h index 161a783b..23c02427 100644 --- a/ui.h +++ b/ui.h @@ -54,7 +54,7 @@ struct uiEventOptions { }; uiprivExtern uiEvent *uiNewEvent(const uiEventOptions *options); -// TODO uiFreeEvent() +uiprivExtern void uiFreeEvent(uiEvent *e); uiprivExtern int uiEventAddHandler(uiEvent *e, uiEventHandler handler, void *sender, void *data); uiprivExtern void uiEventDeleteHandler(uiEvent *e, int id); uiprivExtern void uiEventFire(uiEvent *e, void *sender, void *args);