From cc56e2cece682f95a9bf5f091583b58087514178 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 4 May 2015 20:07:34 -0400 Subject: [PATCH] Implemented clearing realloc() on the OS X backend. --- darwin/alloc.m | 36 ++++++++++++++++++++++++++---------- darwin/init.m | 2 ++ darwin/uipriv_darwin.h | 3 +++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/darwin/alloc.m b/darwin/alloc.m index 736bfe9e..25e9ba21 100644 --- a/darwin/alloc.m +++ b/darwin/alloc.m @@ -1,38 +1,54 @@ // 4 december 2014 -#import +#import #import "uipriv_darwin.h" +void initAlloc(void) +{ +} + +#define UINT8(p) ((uint8_t *) (p)) +#define PVOID(p) ((void *) (p)) +#define DATA(p) PVOID(UINT8(p) + sizeof (size_t)) +#define BASE(p) PVOID(UINT8(p) - sizeof (size_t)) +#define SIZE(p) ((size_t *) (p)) + void *uiAlloc(size_t size) { void *out; - out = malloc(size); + out = malloc(sizeof (size_t) + size); if (out == NULL) { fprintf(stderr, "memory exhausted in uiAlloc()\n"); abort(); } - memset(out, 0, size); - return out; + memset(DATA(out), 0, size); + *SIZE(out) = size; + return DATA(out); } -void *uiRealloc(void *p, size_t size) +void *uiRealloc(void *p, size_t new) { void *out; + size_t *s; if (p == NULL) - return uiAlloc(size); - out = realloc(p, size); + return uiAlloc(new); + p = BASE(p); + out = realloc(p, sizeof (size_t) + new); if (out == NULL) { fprintf(stderr, "memory exhausted in uiRealloc()\n"); abort(); } - // TODO zero the extra memory - return out; + s = SIZE(out); + if (new <= *s) + memset(((uint8_t *) DATA(out)) + *s, 0, new - *s); + *s = new; + return DATA(out); } void uiFree(void *p) { if (p == NULL) return; - free(p); + free(BASE(p)); } diff --git a/darwin/init.m b/darwin/init.m index b620f1a5..738c8c6e 100644 --- a/darwin/init.m +++ b/darwin/init.m @@ -53,6 +53,8 @@ const char *uiInit(uiInitOptions *o) [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setDelegate:[appDelegate new]]; + initAlloc(); + // always do this so we always have an application menu appDelegate().menuManager = [menuManager new]; [NSApp setMainMenu:[appDelegate().menuManager makeMenubar]]; diff --git a/darwin/uipriv_darwin.h b/darwin/uipriv_darwin.h index 2d8fb897..bd486ca8 100644 --- a/darwin/uipriv_darwin.h +++ b/darwin/uipriv_darwin.h @@ -46,3 +46,6 @@ extern void finishNewTextField(NSTextField *, BOOL); // window.m extern uiWindow *windowFromNSWindow(NSWindow *); + +// alloc.m +extern void initAlloc(void);