libui/alloc_darwin.m

44 lines
890 B
Mathematica
Raw Normal View History

// 4 december 2014
2015-04-07 22:40:18 -05:00
#import <stdio.h>
#import "uipriv_darwin.h"
2015-04-06 19:01:14 -05:00
// TODO is there a better alternative to NSCAssert()? preferably a built-in allocator that panics on out of memory for us?
2015-04-07 22:40:18 -05:00
void *uiAlloc(size_t size, const char *type)
{
void *out;
out = malloc(size);
2015-04-06 19:01:14 -05:00
NSCAssert(out != NULL, @"out of memory in uiAlloc()");
memset(out, 0, size);
2015-04-07 22:40:18 -05:00
#ifdef uiLogAllocations
fprintf(stderr, "%p alloc %s\n", out, type);
#endif
return out;
}
2015-04-07 22:40:18 -05:00
void *uiRealloc(void *p, size_t size, const char *type)
{
void *out;
if (p == NULL)
2015-04-07 22:40:18 -05:00
return uiAlloc(size, type);
out = realloc(p, size);
2015-04-06 19:01:14 -05:00
NSCAssert(out != NULL, @"out of memory in uiRealloc()");
// TODO zero the extra memory
2015-04-07 22:40:18 -05:00
#ifdef uiLogAllocations
fprintf(stderr, "%p realloc %p\n", p, out);
#endif
return out;
}
void uiFree(void *p)
{
if (p == NULL)
return;
free(p);
2015-04-07 22:40:18 -05:00
#ifdef uiLogAllocations
fprintf(stderr, "%p free\n", p);
#endif
}