]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Fix memory leaks, more memory tracker stuff as well.
authorDale Weiler <killfieldengine@gmail.com>
Tue, 17 Apr 2012 21:16:11 +0000 (17:16 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Tue, 17 Apr 2012 21:16:11 +0000 (17:16 -0400)
assembler.c
gmqcc.h
main.c
parse.c
util.c

index 9111e2b9d1c8b263034b1631509919f74981c4fd..29375ac0e483db2f3b88ac68b5b199efb2a1068c 100644 (file)
@@ -154,6 +154,13 @@ typedef struct {
     int   offset; /* location in globals */
 } globals;
 VECTOR_MAKE(globals, assembly_constants);
+
+void asm_clear() {
+       size_t i = 0;
+       for (; i < assembly_constants_elements; i++)
+               mem_d(assembly_constants_data[i].name);
+       mem_d(assembly_constants_data);
+}
     
 void asm_parse(FILE *fp) {
     char     *data  = NULL;
@@ -321,8 +328,8 @@ void asm_parse(FILE *fp) {
             printf("%li: Invalid statement, expression, or decleration\n", line);
         
         end:
-        //free(data);
         mem_d(data);
         line ++;
     }
+    asm_clear();
 }
diff --git a/gmqcc.h b/gmqcc.h
index 14a4022d3bc15d05292381f055226129e7275c17..ae2b8f189d22d605ebf2f4ef5cf54a4e68abc1dd 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -158,6 +158,8 @@ int           typedef_add (struct lex_file *file, const char *, const char *);
 //===================================================================
 void *util_memory_a(unsigned int, unsigned int, const char *);
 void  util_memory_d(void       *, unsigned int, const char *);
+void  util_meminfo ();
+
 char *util_strdup  (const char *);
 char *util_strrq   (char *);
 char *util_strrnl  (char *);
@@ -185,10 +187,11 @@ int   util_getline (char **, size_t *, FILE *);
             }                                                            \
             void *temp = mem_a(N##_allocated * sizeof(T));               \
             if  (!temp) {                                                \
-                free(temp);                                              \
+                mem_d(temp);                                             \
                 return -1;                                               \
             }                                                            \
             memcpy(temp, N##_data, (N##_elements * sizeof(T)));          \
+            mem_d(N##_data);                                             \
             N##_data = (T*)temp;                                         \
         }                                                                \
         N##_data[N##_elements] = element;                                \
@@ -211,13 +214,13 @@ int   util_getline (char **, size_t *, FILE *);
  * Each paramater incerements by 3 since vector types hold
  * 3 components (x,y,z).
  */
-#define    OFS_NULL      0
-#define    OFS_RETURN    1
-#define    OFS_PARM0     (OFS_RETURN+3)
-#define    OFS_PARM1     (OFS_PARM0 +3)
-#define    OFS_PARM2     (OFS_PARM1 +3)
-#define    OFS_PARM3     (OFS_PARM2 +3)
-#define    OFS_PARM4     (OFS_PARM3 +3)
+#define OFS_NULL      0
+#define OFS_RETURN    1
+#define OFS_PARM0     (OFS_RETURN+3)
+#define OFS_PARM1     (OFS_PARM0 +3)
+#define OFS_PARM2     (OFS_PARM1 +3)
+#define OFS_PARM3     (OFS_PARM2 +3)
+#define OFS_PARM4     (OFS_PARM3 +3)
 #define OFS_PARM5     (OFS_PARM4 +3)
 #define OFS_PARM6     (OFS_PARM5 +3)
 #define OFS_PARM7     (OFS_PARM6 +3)
diff --git a/main.c b/main.c
index b8117f042806bef33b12f0e8663dd12ca74ad4ae..2a86c32d6e3ee34f9ef0a7ada0452e2bcaad0060 100644 (file)
--- a/main.c
+++ b/main.c
@@ -33,5 +33,6 @@ int main(int argc, char **argv) {
     asm_init ("test.qs", &fp);
     asm_parse(fp);
     asm_close(fp);
+    util_meminfo();
     return 0;
 }
diff --git a/parse.c b/parse.c
index 6423f5daa4b7d14979702843f5541e86e372ee15..8e7f0a65298730ded7e0bafa3848eb4f84c8b6e0 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -291,5 +291,14 @@ int parse_gen(struct lex_file *file) {
     }
     compile_constant_debug();
     lex_reset(file);
+    /* free constants */
+    {
+               size_t i;
+               for (; i < compile_constants_elements; i++) {
+                       mem_d(compile_constants_data[i].name);
+                       mem_d(compile_constants_data[i].string);
+               }
+               mem_d(compile_constants_data);
+       }
     return 1;
 }    
diff --git a/util.c b/util.c
index e431fb2d3aa72c9cd79c4e4b27c38d05a7156840..3f085eae6429623b94a9437f9b33b3b22de8d3a9 100644 (file)
--- a/util.c
+++ b/util.c
 #include <stdarg.h>
 #include <errno.h>
 #include "gmqcc.h"
+
+unsigned long long mem_ab = 0;
+unsigned long long mem_db = 0;
+unsigned long long mem_at = 0;
+unsigned long long mem_dt = 0;
+
 struct memblock_t {
     const char  *file;
     unsigned int line;
@@ -31,14 +36,17 @@ struct memblock_t {
 };
 
 void *util_memory_a(unsigned int byte, unsigned int line, const char *file) {
-    struct memblock_t *data = malloc(sizeof(struct memblock_t) + byte);
+    struct memblock_t *info = malloc(sizeof(struct memblock_t) + byte);
+    void              *data =(void*)((uintptr_t)info+sizeof(struct memblock_t));
     if (!data) return NULL;
-    data->line = line;
-    data->byte = byte;
-    data->file = file;
+    info->line = line;
+    info->byte = byte;
+    info->file = file;
     
-    util_debug("MEM", "allocation: %08u (bytes) at %s:%u\n", byte, file, line);
-    return (void*)((uintptr_t)data+sizeof(struct memblock_t));
+    util_debug("MEM", "allocation: %08u (bytes) address 0x%08X @ %s:%u\n", byte, data, file, line);
+    mem_at++;
+    mem_ab += info->byte;
+    return data;
 }
 
 void util_memory_d(void *ptrn, unsigned int line, const char *file) {
@@ -46,10 +54,23 @@ void util_memory_d(void *ptrn, unsigned int line, const char *file) {
     void              *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t));
     struct memblock_t *info = (struct memblock_t*)data;
     
-    util_debug("MEM", "released:   %08u (bytes) at %s:%u\n", info->byte, file, line);
+    util_debug("MEM", "released:   %08u (bytes) address 0x%08X @ %s:%u\n", info->byte, data, file, line);
+    mem_db += info->byte;
+    mem_dt++;
     free(data);
 }
 
+void util_meminfo() {
+       util_debug("MEM", "Memory information:\n\
+       Total allocations:   %llu\n\
+       Total deallocations: %llu\n\
+       Total allocated:     %llu (bytes)\n\
+       Total deallocated:   %llu (bytes)\n",
+               mem_at, mem_dt,
+               mem_ab, mem_db
+       );
+}
+
 #ifndef mem_d
 #define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
 #endif
@@ -156,7 +177,6 @@ int util_getline(char **lineptr, size_t *n, FILE *stream) {
             
             chr = *n + *lineptr - pos;
             strcpy(tmp,*lineptr);
-            
             if (!(*lineptr = tmp))
                 return -1;