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 759feeb7a1
commit 4e03a117da
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);
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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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)

View File

@ -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

View File

@ -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;

View File

@ -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);

14
test.c
View File

@ -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);

9
ui.h
View File

@ -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 *);

View File

@ -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 ))

View File

@ -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 *);