From ff9dcc6d58662622711d98fb51615b516d87a983 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 17 Nov 2015 12:51:51 -0500 Subject: [PATCH] Wrote the Haiku allocator and fixed some leftovers in other allocators. --- darwin/alloc.m | 2 +- haiku/GNUmakeinc.mk | 1 + haiku/alloc.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ haiku/main.cpp | 2 ++ haiku/uipriv_haiku.hpp | 4 +++ unix/alloc.c | 1 - 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 haiku/alloc.cpp diff --git a/darwin/alloc.m b/darwin/alloc.m index 3421c62e..2134735a 100644 --- a/darwin/alloc.m +++ b/darwin/alloc.m @@ -2,7 +2,7 @@ #import #import "uipriv_darwin.h" -NSMutableArray *allocations; +static NSMutableArray *allocations; NSMutableArray *delegates; void initAlloc(void) diff --git a/haiku/GNUmakeinc.mk b/haiku/GNUmakeinc.mk index 71b1faf8..676b4d11 100644 --- a/haiku/GNUmakeinc.mk +++ b/haiku/GNUmakeinc.mk @@ -1,6 +1,7 @@ # 22 april 2015 CXXFILES += \ + haiku/alloc.cpp \ haiku/main.cpp HFILES += \ diff --git a/haiku/alloc.cpp b/haiku/alloc.cpp new file mode 100644 index 00000000..6e044911 --- /dev/null +++ b/haiku/alloc.cpp @@ -0,0 +1,82 @@ +// 4 december 2014 +#include +#include +#include +#include "uipriv_haiku.hpp" +using namepsace std; + +static set allocations; + +void initAlloc(void) +{ +} + +#define UINT8(p) ((uint8_t *) (p)) +#define PVOID(p) ((void *) (p)) +#define EXTRA (sizeof (size_t) + sizeof (const char **)) +#define DATA(p) PVOID(UINT8(p) + EXTRA) +#define BASE(p) PVOID(UINT8(p) - EXTRA) +#define SIZE(p) ((size_t *) (p)) +#define CCHAR(p) ((const char **) (p)) +#define TYPE(p) CCHAR(UINT8(p) + sizeof (size_t)) + +void uninitAlloc(void) +{ + set::const_iterator i; + + if (allocations.size() == 0) + return; + fprintf(stderr, "[libui] leaked allocations:\n"); + for (i = allocations.begin(); i != allocations.end(); i++) + fprintf(stderr, "[libui] %p %s\n", *i, *TYPE(*i)); + complain("either you left something around or there's a bug in libui"); +} + +void *uiAlloc(size_t size, const char *type) +{ + void *out; + + out = malloc(EXTRA + size); + if (out == NULL) { + fprintf(stderr, "memory exhausted in uiAlloc()\n"); + abort(); + } + memset(DATA(out), 0, size); + *SIZE(out) = size; + *TYPE(out) = type; + allocations.insert(out); + return DATA(out); +} + +void *uiRealloc(void *p, size_t new, const char *type) +{ + void *out; + size_t *s; + + if (p == NULL) + return uiAlloc(new, type); + p = BASE(p); + out = realloc(p, EXTRA + new); + if (out == NULL) { + fprintf(stderr, "memory exhausted in uiRealloc()\n"); + abort(); + } + s = SIZE(out); + if (new <= *s) + memset(((uint8_t *) DATA(out)) + *s, 0, new - *s); + *s = new; + // TODO check this + allocations.erase(p); + allocations.insert(out); + return DATA(out); +} + +void uiFree(void *p) +{ + if (p == NULL) + complain("attempt to uiFree(NULL); there's a bug somewhere"); + p = BASE(p); + free(p); + // TODO check this + allocations.erase(p); +} diff --git a/haiku/main.cpp b/haiku/main.cpp index 75dddac1..2f3c1763 100644 --- a/haiku/main.cpp +++ b/haiku/main.cpp @@ -17,12 +17,14 @@ const char *uiInit(uiInitOptions *o) // TODO return "fail"; } + initAlloc(); return NULL; } void uiUninit(void) { delete app; + uninitAlloc(); } void uiFreeInitError(const char *err) diff --git a/haiku/uipriv_haiku.hpp b/haiku/uipriv_haiku.hpp index d7763c70..65449dfe 100644 --- a/haiku/uipriv_haiku.hpp +++ b/haiku/uipriv_haiku.hpp @@ -4,3 +4,7 @@ #include "../ui.h" #include "../ui_haiku.hpp" #include "../common/uipriv.h" + +// alloc.c +extern void initAlloc(void); +extern void uninitAlloc(void); diff --git a/unix/alloc.c b/unix/alloc.c index c04728b7..cb37fa10 100644 --- a/unix/alloc.c +++ b/unix/alloc.c @@ -23,7 +23,6 @@ static void uninitComplain(gpointer ptr, gpointer data) fprintf(stderr, "[libui] %p %s\n", ptr, *TYPE(ptr)); } -// TODO bring back the type names for this void uninitAlloc(void) { if (allocations->len == 0) {