From 4e03a117da423dca1772dd556aad66ebf73644ff Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 9 Apr 2015 22:38:11 -0400 Subject: [PATCH] Changed the allocation logging from a compile-time setting to the first initialization option. --- alloc_darwin.m | 15 ++++++--------- alloc_unix.c | 15 ++++++--------- alloc_windows.c | 15 ++++++--------- container_unix.c | 10 ++++------ init_darwin.m | 3 +++ init_unix.c | 3 +++ init_windows.c | 4 ++++ test.c | 14 +++++++++++++- ui.h | 9 +++++++++ uipriv.h | 3 +-- uipriv_darwin.h | 12 ++++-------- 11 files changed, 59 insertions(+), 44 deletions(-) diff --git a/alloc_darwin.m b/alloc_darwin.m index df29566c..83a948e2 100644 --- a/alloc_darwin.m +++ b/alloc_darwin.m @@ -11,9 +11,8 @@ void *uiAlloc(size_t size, const char *type) out = malloc(size); NSCAssert(out != NULL, @"out of memory in uiAlloc()"); memset(out, 0, size); -#ifdef uiLogAllocations - fprintf(stderr, "%p alloc %s\n", out, type); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p alloc %s\n", out, type); return out; } @@ -26,9 +25,8 @@ void *uiRealloc(void *p, size_t size, const char *type) out = realloc(p, size); NSCAssert(out != NULL, @"out of memory in uiRealloc()"); // TODO zero the extra memory -#ifdef uiLogAllocations - fprintf(stderr, "%p realloc %p\n", p, out); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p realloc %p\n", p, out); return out; } @@ -37,7 +35,6 @@ void uiFree(void *p) if (p == NULL) return; free(p); -#ifdef uiLogAllocations - fprintf(stderr, "%p free\n", p); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p free\n", p); } diff --git a/alloc_unix.c b/alloc_unix.c index 238cf39e..33482b25 100644 --- a/alloc_unix.c +++ b/alloc_unix.c @@ -7,9 +7,8 @@ void *uiAlloc(size_t size, const char *type) void *out; out = g_malloc0(size); -#ifdef uiLogAllocations - fprintf(stderr, "%p alloc %s\n", out, type); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p alloc %s\n", out, type); return out; } @@ -21,16 +20,14 @@ void *uiRealloc(void *p, size_t size, const char *type) return uiAlloc(size, type); // TODO fill with 0s out = g_realloc(p, size); -#ifdef uiLogAllocations - fprintf(stderr, "%p realloc %p\n", p, out); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p realloc %p\n", p, out); return out; } void uiFree(void *p) { g_free(p); -#ifdef uiLogAllocations - fprintf(stderr, "%p free\n", p); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p free\n", p); } diff --git a/alloc_windows.c b/alloc_windows.c index 55ebea70..9c7900f3 100644 --- a/alloc_windows.c +++ b/alloc_windows.c @@ -15,9 +15,8 @@ void *uiAlloc(size_t size, const char *type) if (out == NULL) abort(); // TODO figure this part out ZeroMemory(out, size); -#ifdef uiLogAllocations - fprintf(stderr, "%p alloc %s\n", out, type); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p alloc %s\n", out, type); return out; } @@ -31,9 +30,8 @@ void *uiRealloc(void *p, size_t size, const char *type) if (out == NULL) abort(); // TODO zero the extra memory -#ifdef uiLogAllocations - fprintf(stderr, "%p realloc %p\n", p, out); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p realloc %p\n", p, out); return out; } @@ -42,7 +40,6 @@ void uiFree(void *p) if (p == NULL) return; free(p); -#ifdef uiLogAllocations - fprintf(stderr, "%p free\n", p); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p free\n", p); } diff --git a/container_unix.c b/container_unix.c index 279e4184..5ee421cd 100644 --- a/container_unix.c +++ b/container_unix.c @@ -5,9 +5,8 @@ G_DEFINE_TYPE(uiContainer, uiContainer, GTK_TYPE_CONTAINER) static void uiContainer_init(uiContainer *c) { -#ifdef uiLogAllocations - fprintf(stderr, "%p alloc uiContainer\n", c); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p alloc uiContainer\n", c); c->children = g_ptr_array_new(); 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) { G_OBJECT_CLASS(uiContainer_parent_class)->finalize(obj); -#ifdef uiLogAllocations - fprintf(stderr, "%p free\n", obj); -#endif + if (options.debugLogAllocations) + fprintf(stderr, "%p free\n", obj); } static void uiContainer_add(GtkContainer *container, GtkWidget *widget) diff --git a/init_darwin.m b/init_darwin.m index b251b479..4a3d2db1 100644 --- a/init_darwin.m +++ b/init_darwin.m @@ -20,8 +20,11 @@ // TODO applicationShouldTerminateAfterLastWindowClosed +uiInitOptions options; + uiInitError *uiInit(uiInitOptions *o) { + options = *o; [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! // see https://github.com/andlabs/ui/issues/6 diff --git a/init_unix.c b/init_unix.c index 67954c0c..60abc919 100644 --- a/init_unix.c +++ b/init_unix.c @@ -5,10 +5,13 @@ struct uiInitError { GError *err; }; +uiInitOptions options; + uiInitError *uiInit(uiInitOptions *o) { uiInitError *err; + options = *o; err = uiNew(uiInitError); if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &(err->err)) == FALSE) return err; diff --git a/init_windows.c b/init_windows.c index 8a0a9ade..172f48a1 100644 --- a/init_windows.c +++ b/init_windows.c @@ -25,6 +25,8 @@ static uiInitError *loadLastError(uiInitError *err, const char *message) return err; } +uiInitOptions options; + uiInitError *uiInit(uiInitOptions *o) { uiInitError *err; @@ -34,6 +36,8 @@ uiInitError *uiInit(uiInitOptions *o) HCURSOR hDefaultCursor; NONCLIENTMETRICSW ncm; + options = *o; + err = uiNew(uiInitError); hInstance = GetModuleHandle(NULL); diff --git a/test.c b/test.c index e63ed893..991b62b8 100644 --- a/test.c +++ b/test.c @@ -1,6 +1,7 @@ // 6 april 2015 #include "ui.h" #include +#include int onClosing(uiWindow *w, void *data) { @@ -111,10 +112,21 @@ static void showSpaced(uiControl *c, void *data) int main(int argc, char *argv[]) { + uiInitOptions o; + int i; uiInitError *err; 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) { fprintf(stderr, "error initializing ui: %s\n", uiInitErrorMessage(err)); uiInitErrorFree(err); diff --git a/ui.h b/ui.h index 9bf8601a..7013a360 100644 --- a/ui.h +++ b/ui.h @@ -8,6 +8,15 @@ typedef struct uiInitError uiInitError; 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 *); const char *uiInitErrorMessage(uiInitError *); void uiInitErrorFree(uiInitError *); diff --git a/uipriv.h b/uipriv.h index c42cbb0e..f028590f 100644 --- a/uipriv.h +++ b/uipriv.h @@ -2,8 +2,7 @@ #include #include "ui.h" -// uncomment the following line to enable memory logging; see leaks.awk -#define uiLogAllocations +extern uiInitOptions options; extern void *uiAlloc(size_t, const char *); #define uiNew(T) ((T *) uiAlloc(sizeof (T), #T )) diff --git a/uipriv_darwin.h b/uipriv_darwin.h index 0aa348d0..9cb85f9b 100644 --- a/uipriv_darwin.h +++ b/uipriv_darwin.h @@ -8,25 +8,21 @@ #define toNSString(str) [NSString stringWithUTF8String:(str)] #define fromNSString(str) [(str) UTF8String] -// TODO see if we can override alloc instead -#ifdef uiLogAllocations -#import #define uiLogObjCClassAllocations \ + (id)alloc \ { \ id thing; \ 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; \ } \ - (void)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 extern void setStandardControlFont(NSControl *);