summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--new/alloc_darwin.m15
-rw-r--r--new/alloc_unix.c15
-rw-r--r--new/alloc_windows.c15
-rw-r--r--new/container_unix.c10
-rw-r--r--new/init_darwin.m3
-rw-r--r--new/init_unix.c3
-rw-r--r--new/init_windows.c4
-rw-r--r--new/test.c14
-rw-r--r--new/ui.h9
-rw-r--r--new/uipriv.h3
-rw-r--r--new/uipriv_darwin.h12
11 files changed, 59 insertions, 44 deletions
diff --git a/new/alloc_darwin.m b/new/alloc_darwin.m
index df29566..83a948e 100644
--- a/new/alloc_darwin.m
+++ b/new/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/new/alloc_unix.c b/new/alloc_unix.c
index 238cf39..33482b2 100644
--- a/new/alloc_unix.c
+++ b/new/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/new/alloc_windows.c b/new/alloc_windows.c
index 55ebea7..9c7900f 100644
--- a/new/alloc_windows.c
+++ b/new/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/new/container_unix.c b/new/container_unix.c
index 279e418..5ee421c 100644
--- a/new/container_unix.c
+++ b/new/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/new/init_darwin.m b/new/init_darwin.m
index b251b47..4a3d2db 100644
--- a/new/init_darwin.m
+++ b/new/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/new/init_unix.c b/new/init_unix.c
index 67954c0..60abc91 100644
--- a/new/init_unix.c
+++ b/new/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/new/init_windows.c b/new/init_windows.c
index 8a0a9ad..172f48a 100644
--- a/new/init_windows.c
+++ b/new/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/new/test.c b/new/test.c
index e63ed89..991b62b 100644
--- a/new/test.c
+++ b/new/test.c
@@ -1,6 +1,7 @@
// 6 april 2015
#include "ui.h"
#include <stdio.h>
+#include <string.h>
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/new/ui.h b/new/ui.h
index 9bf8601..7013a36 100644
--- a/new/ui.h
+++ b/new/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/new/uipriv.h b/new/uipriv.h
index c42cbb0..f028590 100644
--- a/new/uipriv.h
+++ b/new/uipriv.h
@@ -2,8 +2,7 @@
#include <stdlib.h>
#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/new/uipriv_darwin.h b/new/uipriv_darwin.h
index 0aa348d..9cb85f9 100644
--- a/new/uipriv_darwin.h
+++ b/new/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 <stdio.h>
#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 *);