From 96c346c2dd6521213f60ee5d6ef237e982a8be3b Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 16 May 2019 23:02:03 -0400 Subject: [PATCH] Added uiprivAlloc() and friends and implemented uiNewEvent(). --- common/alloc.c | 31 +++++++++++++++++++++++++++++++ common/events.c | 12 +++++++++++- common/meson.build | 1 + common/uipriv.h | 5 +++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 common/alloc.c diff --git a/common/alloc.c b/common/alloc.c new file mode 100644 index 00000000..ce90c453 --- /dev/null +++ b/common/alloc.c @@ -0,0 +1,31 @@ +// 16 may 2019 +#include +#include +#include "ui.h" +#include "uipriv.h" + +void *uiprivAlloc(size_t n, const char *what) +{ + void *p; + + p = malloc(n); + if (p == NULL) + uiprivInternalError("memory exhausted allocating %s", what); + memset(p, 0, n); + return p; +} + +void *uiprivRealloc(void *p, size_t old, size_t new, const char *what) +{ + p = realloc(p, new); + if (p == NULL) + uiprivInternalError("memory exhausted reallocating %s", what); + if (new > old) + memset(((uint8_t *) p) + old, 0, new - old); + return p; +} + +void uiprivFree(void *p) +{ + free(p); +} diff --git a/common/events.c b/common/events.c index eafd668c..3f690bdc 100644 --- a/common/events.c +++ b/common/events.c @@ -1,11 +1,12 @@ // 15 may 2019 #include +#include #include "ui.h" #include "uipriv.h" struct handler { int id; - uiHandlerFunc f; + uiEventHandler f; void *sender; void *data; bool blocked; @@ -39,10 +40,19 @@ struct uiEvent { uiEvent *uiNewEvent(const uiEventOptions *options) { + uiEvent *e; + if (options == NULL) { uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEventOptions", __func__); return NULL; } + if (options->Size != sizeof (uiEventOptions)) { + uiprivProgrammerError(uiprivProgrammerErrorWrongStructSize, options->Size, "uiEventOptions"); + return NULL; + } + e = (uiEvent *) uiprivAlloc(sizeof (uiEvent), "uiEvent"); + e->opts = *options; + return e; } #define checkEventNonnull(e, ...) if ((e) == NULL) { \ diff --git a/common/meson.build b/common/meson.build index df85ce56..cdc77527 100644 --- a/common/meson.build +++ b/common/meson.build @@ -1,6 +1,7 @@ # 23 march 2019 libui_sources += [ + 'common/alloc.c', 'common/errors.c', 'common/events.c', 'common/init.c', diff --git a/common/uipriv.h b/common/uipriv.h index d54b5708..c4d8ef12 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -10,6 +10,11 @@ extern int uiprivSysInit(void *options, uiInitError *err); extern int uiprivInitReturnError(uiInitError *err, const char *msg); extern int uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...); +// alloc.c +extern void *uiprivAlloc(size_t n, const char *what); +extern void *uiprivRealloc(void *p, size_t old, size_t new, const char *what); +extern void uiprivFree(void *p); + // errors.c extern void uiprivInternalError(const char *fmt, ...); enum {