Added uiprivAlloc() and friends and implemented uiNewEvent().

This commit is contained in:
Pietro Gagliardi 2019-05-16 23:02:03 -04:00
parent bd84da7179
commit 96c346c2dd
4 changed files with 48 additions and 1 deletions

31
common/alloc.c Normal file
View File

@ -0,0 +1,31 @@
// 16 may 2019
#include <stdlib.h>
#include <string.h>
#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);
}

View File

@ -1,11 +1,12 @@
// 15 may 2019
#include <stdlib.h>
#include <string.h>
#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) { \

View File

@ -1,6 +1,7 @@
# 23 march 2019
libui_sources += [
'common/alloc.c',
'common/errors.c',
'common/events.c',
'common/init.c',

View File

@ -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 {