Implemented clearing realloc() on the OS X backend.

This commit is contained in:
Pietro Gagliardi 2015-05-04 20:07:34 -04:00
parent ed9539de94
commit cc56e2cece
3 changed files with 31 additions and 10 deletions

View File

@ -1,38 +1,54 @@
// 4 december 2014 // 4 december 2014
#import <stdio.h> #import <stdlib.h>
#import "uipriv_darwin.h" #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 *uiAlloc(size_t size)
{ {
void *out; void *out;
out = malloc(size); out = malloc(sizeof (size_t) + size);
if (out == NULL) { if (out == NULL) {
fprintf(stderr, "memory exhausted in uiAlloc()\n"); fprintf(stderr, "memory exhausted in uiAlloc()\n");
abort(); abort();
} }
memset(out, 0, size); memset(DATA(out), 0, size);
return out; *SIZE(out) = size;
return DATA(out);
} }
void *uiRealloc(void *p, size_t size) void *uiRealloc(void *p, size_t new)
{ {
void *out; void *out;
size_t *s;
if (p == NULL) if (p == NULL)
return uiAlloc(size); return uiAlloc(new);
out = realloc(p, size); p = BASE(p);
out = realloc(p, sizeof (size_t) + new);
if (out == NULL) { if (out == NULL) {
fprintf(stderr, "memory exhausted in uiRealloc()\n"); fprintf(stderr, "memory exhausted in uiRealloc()\n");
abort(); abort();
} }
// TODO zero the extra memory s = SIZE(out);
return out; if (new <= *s)
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
*s = new;
return DATA(out);
} }
void uiFree(void *p) void uiFree(void *p)
{ {
if (p == NULL) if (p == NULL)
return; return;
free(p); free(BASE(p));
} }

View File

@ -53,6 +53,8 @@ const char *uiInit(uiInitOptions *o)
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setDelegate:[appDelegate new]]; [NSApp setDelegate:[appDelegate new]];
initAlloc();
// always do this so we always have an application menu // always do this so we always have an application menu
appDelegate().menuManager = [menuManager new]; appDelegate().menuManager = [menuManager new];
[NSApp setMainMenu:[appDelegate().menuManager makeMenubar]]; [NSApp setMainMenu:[appDelegate().menuManager makeMenubar]];

View File

@ -46,3 +46,6 @@ extern void finishNewTextField(NSTextField *, BOOL);
// window.m // window.m
extern uiWindow *windowFromNSWindow(NSWindow *); extern uiWindow *windowFromNSWindow(NSWindow *);
// alloc.m
extern void initAlloc(void);