Changed the allocation logging from a compile-time setting to the first initialization option.

This commit is contained in:
Pietro Gagliardi 2015-04-09 22:38:11 -04:00
parent d9315e6d8f
commit 50a227058a
11 changed files with 59 additions and 44 deletions

View File

@ -11,9 +11,8 @@ void *uiAlloc(size_t size, const char *type)
out = malloc(size); out = malloc(size);
NSCAssert(out != NULL, @"out of memory in uiAlloc()"); NSCAssert(out != NULL, @"out of memory in uiAlloc()");
memset(out, 0, size); memset(out, 0, size);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p alloc %s\n", out, type); fprintf(stderr, "%p alloc %s\n", out, type);
#endif
return out; return out;
} }
@ -26,9 +25,8 @@ void *uiRealloc(void *p, size_t size, const char *type)
out = realloc(p, size); out = realloc(p, size);
NSCAssert(out != NULL, @"out of memory in uiRealloc()"); NSCAssert(out != NULL, @"out of memory in uiRealloc()");
// TODO zero the extra memory // TODO zero the extra memory
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p realloc %p\n", p, out); fprintf(stderr, "%p realloc %p\n", p, out);
#endif
return out; return out;
} }
@ -37,7 +35,6 @@ void uiFree(void *p)
if (p == NULL) if (p == NULL)
return; return;
free(p); free(p);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", p); fprintf(stderr, "%p free\n", p);
#endif
} }

View File

@ -7,9 +7,8 @@ void *uiAlloc(size_t size, const char *type)
void *out; void *out;
out = g_malloc0(size); out = g_malloc0(size);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p alloc %s\n", out, type); fprintf(stderr, "%p alloc %s\n", out, type);
#endif
return out; return out;
} }
@ -21,16 +20,14 @@ void *uiRealloc(void *p, size_t size, const char *type)
return uiAlloc(size, type); return uiAlloc(size, type);
// TODO fill with 0s // TODO fill with 0s
out = g_realloc(p, size); out = g_realloc(p, size);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p realloc %p\n", p, out); fprintf(stderr, "%p realloc %p\n", p, out);
#endif
return out; return out;
} }
void uiFree(void *p) void uiFree(void *p)
{ {
g_free(p); g_free(p);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", p); fprintf(stderr, "%p free\n", p);
#endif
} }

View File

@ -15,9 +15,8 @@ void *uiAlloc(size_t size, const char *type)
if (out == NULL) if (out == NULL)
abort(); // TODO figure this part out abort(); // TODO figure this part out
ZeroMemory(out, size); ZeroMemory(out, size);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p alloc %s\n", out, type); fprintf(stderr, "%p alloc %s\n", out, type);
#endif
return out; return out;
} }
@ -31,9 +30,8 @@ void *uiRealloc(void *p, size_t size, const char *type)
if (out == NULL) if (out == NULL)
abort(); abort();
// TODO zero the extra memory // TODO zero the extra memory
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p realloc %p\n", p, out); fprintf(stderr, "%p realloc %p\n", p, out);
#endif
return out; return out;
} }
@ -42,7 +40,6 @@ void uiFree(void *p)
if (p == NULL) if (p == NULL)
return; return;
free(p); free(p);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", p); fprintf(stderr, "%p free\n", p);
#endif
} }

View File

@ -5,9 +5,8 @@ G_DEFINE_TYPE(uiContainer, uiContainer, GTK_TYPE_CONTAINER)
static void uiContainer_init(uiContainer *c) static void uiContainer_init(uiContainer *c)
{ {
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p alloc uiContainer\n", c); fprintf(stderr, "%p alloc uiContainer\n", c);
#endif
c->children = g_ptr_array_new(); c->children = g_ptr_array_new();
gtk_widget_set_has_window(GTK_WIDGET(c), FALSE); gtk_widget_set_has_window(GTK_WIDGET(c), FALSE);
} }
@ -32,9 +31,8 @@ static void uiContainer_dispose(GObject *obj)
static void uiContainer_finalize(GObject *obj) static void uiContainer_finalize(GObject *obj)
{ {
G_OBJECT_CLASS(uiContainer_parent_class)->finalize(obj); G_OBJECT_CLASS(uiContainer_parent_class)->finalize(obj);
#ifdef uiLogAllocations if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", obj); fprintf(stderr, "%p free\n", obj);
#endif
} }
static void uiContainer_add(GtkContainer *container, GtkWidget *widget) static void uiContainer_add(GtkContainer *container, GtkWidget *widget)

View File

@ -20,8 +20,11 @@
// TODO applicationShouldTerminateAfterLastWindowClosed // TODO applicationShouldTerminateAfterLastWindowClosed
uiInitOptions options;
uiInitError *uiInit(uiInitOptions *o) uiInitError *uiInit(uiInitOptions *o)
{ {
options = *o;
[uiApplication sharedApplication]; [uiApplication sharedApplication];
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy! // don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
// see https://github.com/andlabs/ui/issues/6 // see https://github.com/andlabs/ui/issues/6

View File

@ -5,10 +5,13 @@ struct uiInitError {
GError *err; GError *err;
}; };
uiInitOptions options;
uiInitError *uiInit(uiInitOptions *o) uiInitError *uiInit(uiInitOptions *o)
{ {
uiInitError *err; uiInitError *err;
options = *o;
err = uiNew(uiInitError); err = uiNew(uiInitError);
if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &(err->err)) == FALSE) if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &(err->err)) == FALSE)
return err; return err;

View File

@ -25,6 +25,8 @@ static uiInitError *loadLastError(uiInitError *err, const char *message)
return err; return err;
} }
uiInitOptions options;
uiInitError *uiInit(uiInitOptions *o) uiInitError *uiInit(uiInitOptions *o)
{ {
uiInitError *err; uiInitError *err;
@ -34,6 +36,8 @@ uiInitError *uiInit(uiInitOptions *o)
HCURSOR hDefaultCursor; HCURSOR hDefaultCursor;
NONCLIENTMETRICSW ncm; NONCLIENTMETRICSW ncm;
options = *o;
err = uiNew(uiInitError); err = uiNew(uiInitError);
hInstance = GetModuleHandle(NULL); hInstance = GetModuleHandle(NULL);

View File

@ -1,6 +1,7 @@
// 6 april 2015 // 6 april 2015
#include "ui.h" #include "ui.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
int onClosing(uiWindow *w, void *data) int onClosing(uiWindow *w, void *data)
{ {
@ -111,10 +112,21 @@ static void showSpaced(uiControl *c, void *data)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
uiInitOptions o;
int i;
uiInitError *err; uiInitError *err;
uiControl *getButton, *setButton; uiControl *getButton, *setButton;
err = uiInit(NULL); memset(&o, 0, sizeof (uiInitOptions));
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "leaks") == 0)
o.debugLogAllocations = 1;
else {
fprintf(stderr, "%s: unrecognized option %s\n", argv[0], argv[i]);
return 1;
}
err = uiInit(&o);
if (err != NULL) { if (err != NULL) {
fprintf(stderr, "error initializing ui: %s\n", uiInitErrorMessage(err)); fprintf(stderr, "error initializing ui: %s\n", uiInitErrorMessage(err));
uiInitErrorFree(err); uiInitErrorFree(err);

View File

@ -8,6 +8,15 @@
typedef struct uiInitError uiInitError; typedef struct uiInitError uiInitError;
typedef struct uiInitOptions uiInitOptions; typedef struct uiInitOptions uiInitOptions;
// TODO note that should be initialized to zero
struct uiInitOptions {
// TODO cbSize
// If nonzero, allocations will be logged to stderr.
// See leaks.awk.
int debugLogAllocations;
};
uiInitError *uiInit(uiInitOptions *); uiInitError *uiInit(uiInitOptions *);
const char *uiInitErrorMessage(uiInitError *); const char *uiInitErrorMessage(uiInitError *);
void uiInitErrorFree(uiInitError *); void uiInitErrorFree(uiInitError *);

View File

@ -2,8 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "ui.h" #include "ui.h"
// uncomment the following line to enable memory logging; see leaks.awk extern uiInitOptions options;
#define uiLogAllocations
extern void *uiAlloc(size_t, const char *); extern void *uiAlloc(size_t, const char *);
#define uiNew(T) ((T *) uiAlloc(sizeof (T), #T )) #define uiNew(T) ((T *) uiAlloc(sizeof (T), #T ))

View File

@ -8,25 +8,21 @@
#define toNSString(str) [NSString stringWithUTF8String:(str)] #define toNSString(str) [NSString stringWithUTF8String:(str)]
#define fromNSString(str) [(str) UTF8String] #define fromNSString(str) [(str) UTF8String]
// TODO see if we can override alloc instead
#ifdef uiLogAllocations
#import <stdio.h>
#define uiLogObjCClassAllocations \ #define uiLogObjCClassAllocations \
+ (id)alloc \ + (id)alloc \
{ \ { \
id thing; \ id thing; \
thing = [super alloc]; \ thing = [super alloc]; \
fprintf(stderr, "%p alloc %s\n", thing, [[self className] UTF8String]); \ if (options.debugLogAllocations) \
fprintf(stderr, "%p alloc %s\n", thing, [[self className] UTF8String]); \
return thing; \ return thing; \
} \ } \
- (void)dealloc \ - (void)dealloc \
{ \ { \
[super dealloc]; \ [super dealloc]; \
fprintf(stderr, "%p free\n", self); \ if (options.debugLogAllocations) \
fprintf(stderr, "%p free\n", self); \
} }
#else
#define uiLogObjCClassAllocations
#endif
// util_darwin.m // util_darwin.m
extern void setStandardControlFont(NSControl *); extern void setStandardControlFont(NSControl *);