2015-08-16 21:30:44 -05:00
|
|
|
// 4 december 2014
|
|
|
|
#import <stdlib.h>
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-11-17 11:51:51 -06:00
|
|
|
static NSMutableArray *allocations;
|
2018-05-05 18:46:57 -05:00
|
|
|
NSMutableArray *uiprivDelegates;
|
2015-08-16 21:30:44 -05:00
|
|
|
|
2018-05-05 18:46:57 -05:00
|
|
|
void uiprivInitAlloc(void)
|
2015-08-16 21:30:44 -05:00
|
|
|
{
|
|
|
|
allocations = [NSMutableArray new];
|
2018-05-05 18:46:57 -05:00
|
|
|
uiprivDelegates = [NSMutableArray new];
|
2015-08-16 21:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#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))
|
|
|
|
|
2018-05-05 18:46:57 -05:00
|
|
|
void uiprivUninitAlloc(void)
|
2015-08-16 21:30:44 -05:00
|
|
|
{
|
2016-05-13 19:14:46 -05:00
|
|
|
NSMutableString *str;
|
|
|
|
NSValue *v;
|
2015-08-17 11:57:45 -05:00
|
|
|
|
2018-05-05 18:46:57 -05:00
|
|
|
[uiprivDelegates release];
|
2016-04-25 12:47:24 -05:00
|
|
|
if ([allocations count] == 0) {
|
2015-08-16 21:30:44 -05:00
|
|
|
[allocations release];
|
|
|
|
return;
|
|
|
|
}
|
2016-05-13 19:14:46 -05:00
|
|
|
str = [NSMutableString new];
|
|
|
|
for (v in allocations) {
|
2015-08-16 21:30:44 -05:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = [v pointerValue];
|
2016-05-13 19:14:46 -05:00
|
|
|
[str appendString:[NSString stringWithFormat:@"%p %s\n", ptr, *TYPE(ptr)]];
|
|
|
|
}
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivUserBug("Some data was leaked; either you left a uiControl lying around or there's a bug in libui itself. Leaked data:\n%s", [str UTF8String]);
|
2016-05-13 20:00:12 -05:00
|
|
|
[str release];
|
2015-08-16 21:30:44 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 15:05:24 -05:00
|
|
|
void *uiprivAlloc(size_t size, const char *type)
|
2015-08-16 21:30:44 -05:00
|
|
|
{
|
|
|
|
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 addObject:[NSValue valueWithPointer:out]];
|
|
|
|
return DATA(out);
|
|
|
|
}
|
|
|
|
|
2018-04-15 15:05:24 -05:00
|
|
|
void *uiprivRealloc(void *p, size_t new, const char *type)
|
2015-08-16 21:30:44 -05:00
|
|
|
{
|
|
|
|
void *out;
|
|
|
|
size_t *s;
|
|
|
|
|
|
|
|
if (p == NULL)
|
2018-04-15 15:05:24 -05:00
|
|
|
return uiprivAlloc(new, type);
|
2015-08-16 21:30:44 -05:00
|
|
|
p = BASE(p);
|
|
|
|
out = realloc(p, EXTRA + new);
|
|
|
|
if (out == NULL) {
|
2018-04-15 15:05:24 -05:00
|
|
|
fprintf(stderr, "memory exhausted in uiprivRealloc()\n");
|
2015-08-16 21:30:44 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
s = SIZE(out);
|
2017-02-20 14:14:53 -06:00
|
|
|
if (new > *s)
|
2015-08-16 21:30:44 -05:00
|
|
|
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
|
|
|
|
*s = new;
|
|
|
|
[allocations removeObject:[NSValue valueWithPointer:p]];
|
|
|
|
[allocations addObject:[NSValue valueWithPointer:out]];
|
|
|
|
return DATA(out);
|
|
|
|
}
|
|
|
|
|
2018-04-15 15:05:24 -05:00
|
|
|
void uiprivFree(void *p)
|
2015-08-16 21:30:44 -05:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
2018-04-15 20:46:08 -05:00
|
|
|
uiprivImplBug("attempt to uiprivFree(NULL)");
|
2015-08-16 21:30:44 -05:00
|
|
|
p = BASE(p);
|
|
|
|
free(p);
|
|
|
|
[allocations removeObject:[NSValue valueWithPointer:p]];
|
|
|
|
}
|