2015-04-28 12:49:20 -05:00
|
|
|
// 4 december 2014
|
2015-05-04 19:07:34 -05:00
|
|
|
#import <stdlib.h>
|
2015-04-28 12:49:20 -05:00
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-05-04 19:07:34 -05:00
|
|
|
void initAlloc(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UINT8(p) ((uint8_t *) (p))
|
|
|
|
#define PVOID(p) ((void *) (p))
|
2015-05-08 16:29:36 -05:00
|
|
|
#define EXTRA (sizeof (size_t) + sizeof (const char **))
|
|
|
|
#define DATA(p) PVOID(UINT8(p) + EXTRA)
|
|
|
|
#define BASE(p) PVOID(UINT8(p) - EXTRA)
|
2015-05-04 19:07:34 -05:00
|
|
|
#define SIZE(p) ((size_t *) (p))
|
2015-05-08 16:29:36 -05:00
|
|
|
#define CCHAR(p) ((const char **) (p))
|
|
|
|
#define TYPE(p) CCHAR(UINT8(p) + sizeof (size_t))
|
2015-05-04 19:07:34 -05:00
|
|
|
|
2015-05-08 16:29:36 -05:00
|
|
|
void uninitAlloc(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *uiAlloc(size_t size, const char *type)
|
2015-04-28 12:49:20 -05:00
|
|
|
{
|
|
|
|
void *out;
|
|
|
|
|
2015-05-08 16:29:36 -05:00
|
|
|
out = malloc(EXTRA + size);
|
2015-04-28 12:49:20 -05:00
|
|
|
if (out == NULL) {
|
2015-05-03 18:58:12 -05:00
|
|
|
fprintf(stderr, "memory exhausted in uiAlloc()\n");
|
2015-04-28 12:49:20 -05:00
|
|
|
abort();
|
|
|
|
}
|
2015-05-04 19:07:34 -05:00
|
|
|
memset(DATA(out), 0, size);
|
|
|
|
*SIZE(out) = size;
|
2015-05-08 16:29:36 -05:00
|
|
|
*TYPE(out) = type;
|
2015-05-04 19:07:34 -05:00
|
|
|
return DATA(out);
|
2015-04-28 12:49:20 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 16:29:36 -05:00
|
|
|
void *uiRealloc(void *p, size_t new, const char *type)
|
2015-04-28 12:49:20 -05:00
|
|
|
{
|
|
|
|
void *out;
|
2015-05-04 19:07:34 -05:00
|
|
|
size_t *s;
|
2015-04-28 12:49:20 -05:00
|
|
|
|
|
|
|
if (p == NULL)
|
2015-05-08 16:29:36 -05:00
|
|
|
return uiAlloc(new, type);
|
2015-05-04 19:07:34 -05:00
|
|
|
p = BASE(p);
|
2015-05-08 16:29:36 -05:00
|
|
|
out = realloc(p, EXTRA + new);
|
2015-04-28 12:49:20 -05:00
|
|
|
if (out == NULL) {
|
2015-05-03 18:58:12 -05:00
|
|
|
fprintf(stderr, "memory exhausted in uiRealloc()\n");
|
2015-04-28 12:49:20 -05:00
|
|
|
abort();
|
|
|
|
}
|
2015-05-04 19:07:34 -05:00
|
|
|
s = SIZE(out);
|
|
|
|
if (new <= *s)
|
|
|
|
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
|
|
|
*s = new;
|
|
|
|
return DATA(out);
|
2015-04-28 12:49:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiFree(void *p)
|
|
|
|
{
|
|
|
|
if (p == NULL)
|
2015-05-07 16:45:34 -05:00
|
|
|
complain("attempt to uiFree(NULL); there's a bug somewhere");
|
2015-05-04 19:07:34 -05:00
|
|
|
free(BASE(p));
|
2015-04-28 12:49:20 -05:00
|
|
|
}
|