Somewhat implemented uninitAlloc() on OS X.

This commit is contained in:
Pietro Gagliardi 2015-05-08 17:51:05 -04:00
parent 33f41d6c37
commit c079e43784
3 changed files with 23 additions and 1 deletions

View File

@ -2,8 +2,11 @@
#import <stdlib.h> #import <stdlib.h>
#import "uipriv_darwin.h" #import "uipriv_darwin.h"
NSMutableArray *allocations;
void initAlloc(void) void initAlloc(void)
{ {
allocations = [NSMutableArray new];
} }
#define UINT8(p) ((uint8_t *) (p)) #define UINT8(p) ((uint8_t *) (p))
@ -17,6 +20,20 @@ void initAlloc(void)
void uninitAlloc(void) void uninitAlloc(void)
{ {
if ([allocations count] == 0) {
[allocations release];
return;
}
fprintf(stderr, "[libui] leaked allocations:\n");
[allocations enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSValue *v;
void *ptr;
v = (NSValue *) obj;
ptr = [v pointerValue];
fprintf(stderr, "[libui] %p %s\n", ptr, *TYPE(ptr));
}];
complain("either you left something around or there's a bug in libui");
} }
void *uiAlloc(size_t size, const char *type) void *uiAlloc(size_t size, const char *type)
@ -31,6 +48,7 @@ void *uiAlloc(size_t size, const char *type)
memset(DATA(out), 0, size); memset(DATA(out), 0, size);
*SIZE(out) = size; *SIZE(out) = size;
*TYPE(out) = type; *TYPE(out) = type;
[allocations addObject:[NSValue valueWithPointer:out]];
return DATA(out); return DATA(out);
} }
@ -51,6 +69,8 @@ void *uiRealloc(void *p, size_t new, const char *type)
if (new <= *s) if (new <= *s)
memset(((uint8_t *) DATA(out)) + *s, 0, new - *s); memset(((uint8_t *) DATA(out)) + *s, 0, new - *s);
*s = new; *s = new;
[allocations removeObject:[NSValue valueWithPointer:p]];
[allocations addObject:[NSValue valueWithPointer:out]];
return DATA(out); return DATA(out);
} }
@ -59,4 +79,5 @@ void uiFree(void *p)
if (p == NULL) if (p == NULL)
complain("attempt to uiFree(NULL); there's a bug somewhere"); complain("attempt to uiFree(NULL); there's a bug somewhere");
free(BASE(p)); free(BASE(p));
[allocations removeObject:[NSValue valueWithPointer:p]];
} }

View File

@ -67,7 +67,7 @@ void uiUninit(void)
{ {
// TODO free menus // TODO free menus
// TODO free application delegate // TODO free application delegate
// TODO uninit alloc uninitAlloc();
} }
void uiFreeInitError(const char *err) void uiFreeInitError(const char *err)

View File

@ -49,3 +49,4 @@ extern uiWindow *windowFromNSWindow(NSWindow *);
// alloc.m // alloc.m
extern void initAlloc(void); extern void initAlloc(void);
extern void uninitAlloc(void);