From: Dale Weiler Date: Sun, 14 Apr 2013 23:51:16 +0000 (+0000) Subject: Added tracing to strdup for allocations, fixed some memleaks, this isn't pretty ... X-Git-Tag: before-library~63 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=ef528d6710d597bad5462f4fbf1c5a6a54cb2a10;p=xonotic%2Fgmqcc.git Added tracing to strdup for allocations, fixed some memleaks, this isn't pretty (trying to track down some weird memory leak issues) --- diff --git a/Makefile b/Makefile index c72b237..41ae2e6 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CYGWIN = $(findstring CYGWIN, $(UNAME)) MINGW = $(findstring MINGW32, $(UNAME)) CC ?= clang -CFLAGS += -Wall -Wextra -Werror -I. -fno-strict-aliasing -fsigned-char $(OPTIONAL) +CFLAGS += -Wall -Wextra -Werror -I. -fno-strict-aliasing -fsigned-char -ffunction-sections -fdata-sections -Wl,-gc-sections $(OPTIONAL) ifneq ($(shell git describe --always 2>/dev/null),) CFLAGS += -DGMQCC_GITINFO="\"$(shell git describe --always)\"" endif diff --git a/ast.c b/ast.c index 919e0a6..b1b7ba8 100644 --- a/ast.c +++ b/ast.c @@ -584,6 +584,7 @@ void ast_member_delete(ast_member *self) * purpose that is not garbage-collected. */ ast_expression_delete((ast_expression*)self); + mem_d(self->name); mem_d(self); } diff --git a/ftepp.c b/ftepp.c index 7337ba3..af3e699 100644 --- a/ftepp.c +++ b/ftepp.c @@ -492,22 +492,28 @@ static bool ftepp_define(ftepp_t *ftepp) break; default: ftepp_error(ftepp, "expected macro name"); - goto cleanup_false; + return false; } (void)ftepp_next(ftepp); if (ftepp->token == '(') { macro->has_params = true; - if (!ftepp_define_params(ftepp, macro)) - goto cleanup_false; + if (!ftepp_define_params(ftepp, macro)) { + ppmacro_delete(macro); + return false; + } } - if (!ftepp_skipspace(ftepp)) - goto cleanup_false; + if (!ftepp_skipspace(ftepp)) { + ppmacro_delete(macro); + return false; + } - if (!ftepp_define_body(ftepp, macro)) - goto cleanup_false; + if (!ftepp_define_body(ftepp, macro)) { + ppmacro_delete(macro); + return false; + } if (ftepp->output_on) vec_push(ftepp->macros, macro); @@ -518,10 +524,6 @@ static bool ftepp_define(ftepp_t *ftepp) for (; l < ftepp_ctx(ftepp).line; ++l) ftepp_out(ftepp, "\n", true); return true; - -cleanup_false: - if (macro) ppmacro_delete(macro); - return false; } /** diff --git a/gmqcc.h b/gmqcc.h index fbddf15..223e0d7 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -300,7 +300,7 @@ void util_meminfo (); bool util_filexists (const char *); bool util_strupper (const char *); bool util_strdigit (const char *); -char *util_strdup (const char *); +char *_util_Estrdup (const char *, const char *, size_t); void util_debug (const char *, const char *, ...); void util_endianswap (void *, size_t, unsigned int); @@ -326,6 +326,8 @@ int util_asprintf (char **ret, const char *fmt, ...); # define mem_r(x, n) util_memory_r((void*)(x), (n), __LINE__, __FILE__) #endif /*! NOTRACK */ +#define util_strdup(X) _util_Estrdup((X), __FILE__, __LINE__) + /* * A flexible vector implementation: all vector pointers contain some * data about themselfs exactly - sizeof(vector_t) behind the pointer diff --git a/intrin.h b/intrin.h index d66cc3c..69490dc 100644 --- a/intrin.h +++ b/intrin.h @@ -44,10 +44,6 @@ ht intrin_intrinsics() { return intrinsics; } -void intrin_intrinsics_destroy() { - util_htdel(intrin_intrinsics()); -} - #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \ do { \ (VALUE) = ast_value_new ( \ @@ -376,6 +372,17 @@ static intrin_t intrinsics[] = { {&intrin_isnan, "__builtin_isnan", "isnan"} }; +void intrin_intrinsics_destroy(parser_t *parser) { + /*size_t i;*/ + (void)parser; + util_htdel(intrin_intrinsics()); +#if 0 + for (i = 0; i < sizeof(intrinsics)/sizeof(intrin_t); i++) + ast_value_delete( (ast_value*) intrinsics[i].intrin(parser)); +#endif +} + + ast_expression *intrin_func(parser_t *parser, const char *name) { static bool init = false; size_t i = 0; diff --git a/parser.c b/parser.c index 3cae34c..db71454 100644 --- a/parser.c +++ b/parser.c @@ -6093,7 +6093,7 @@ void parser_cleanup() util_htdel(parser->aliases); - intrin_intrinsics_destroy(); + intrin_intrinsics_destroy(parser); mem_d(parser); } diff --git a/util.c b/util.c index 023eb5e..2e2aec3 100644 --- a/util.c +++ b/util.c @@ -136,13 +136,13 @@ void util_meminfo() { return; for (info = mem_start; info; info = info->next) { - util_debug("MEM", "lost: % 8u (bytes) at %s:%u\n", + con_out("lost: % 8u (bytes) at %s:%u\n", info->byte, info->file, info->line); } - util_debug("MEM", "Memory information:\n\ + con_out("Memory information:\n\ Total allocations: %llu\n\ Total deallocations: %llu\n\ Total allocated: %llu (bytes)\n\ @@ -159,14 +159,14 @@ void util_meminfo() { * Some string utility functions, because strdup uses malloc, and we want * to track all memory (without replacing malloc). */ -char *util_strdup(const char *s) { +char *_util_Estrdup(const char *s, const char *file, size_t line) { size_t len = 0; char *ptr = NULL; if (!s) return NULL; - if ((len = strlen(s)) && (ptr = (char*)mem_a(len+1))) { + if ((len = strlen(s)) && (ptr = (char*)util_memory_a(len+1, line, file))) { memcpy(ptr, s, len); ptr[len] = '\0'; }