Merge branch 'development'
[NeonServV5.git] / src / memoryDebug.c
index 442e618e1dfe0dbbf144d387bc82bcf947db1151..d4e828f1aeb127ce84a8c00c41059e49a12518d2 100644 (file)
@@ -1,4 +1,4 @@
-/* memoryDebug.c - NeonServ v5.3
+/* memoryDebug.c - NeonServ v5.6
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -17,6 +17,7 @@
 #include "main.h"
 #include "memoryDebug.h"
 #include "memoryInfo.h"
+#include "tools.h"
 
 #define FILE_NAME_LENGTH  256
 #define OUTPUT_FILE "leak_info.txt"
@@ -44,6 +45,8 @@ static pthread_mutex_t synchronized;
 
 static struct MemoryLeak *ptr_start = NULL;
 
+static unsigned int own_allocated_memleaks = 0;
+
 static void add_mem_info(void * mem_ref, unsigned int size, const char *file, unsigned int line);
 static void remove_mem_info(void * mem_ref);
 
@@ -78,21 +81,27 @@ void xfree(void *mem_ref) {
     free(mem_ref);
 }
 
-
 static void add_mem_info(void *mem_ref, unsigned int size, const char *file, unsigned int line) {
-    SYNCHRONIZE(synchronized);
-    struct MemoryLeak *mem_leak_info = malloc (sizeof(*mem_leak_info));
+    #ifdef HAVE_THREADS
+    pthread_mutex_lock(&synchronized);
+    #endif
+    struct MemoryLeak *mem_leak_info = malloc(sizeof(*mem_leak_info));
+    own_allocated_memleaks++;
     mem_leak_info->mem_info.address = mem_ref;
     mem_leak_info->mem_info.size = size;
     strcpy(mem_leak_info->mem_info.file_name, file);    
     mem_leak_info->mem_info.line = line;
     mem_leak_info->next = ptr_start;
     ptr_start = mem_leak_info;
-    DESYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    pthread_mutex_unlock(&synchronized);
+    #endif
 }
 
 static void remove_mem_info(void *mem_ref) {
-    SYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    pthread_mutex_lock(&synchronized);
+    #endif
     struct MemoryLeak *leak_info, *next, *prev = NULL;
     for(leak_info = ptr_start; leak_info; leak_info = next) {
         next = leak_info->next;
@@ -101,41 +110,15 @@ static void remove_mem_info(void *mem_ref) {
                 prev->next = next;
             else
                 ptr_start = next;
+            own_allocated_memleaks--;
             free(leak_info);
             break;
         } else 
             prev = leak_info;
     }
-    DESYNCHRONIZE(synchronized);
-}
-
-void report_mem_leak() {
-    SYNCHRONIZE(synchronized);
-    struct MemoryLeak *leak_info;
-    FILE *fp_write = fopen(OUTPUT_FILE, "wt");
-    char info[1024];
-
-    if(fp_write != NULL) {
-        sprintf(info, "%s\n", "Memory Leak Summary");
-        fwrite(info, (strlen(info)) , 1, fp_write);
-        sprintf(info, "%s\n", "-----------------------------------");    
-        fwrite(info, (strlen(info)) , 1, fp_write);
-        
-        for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
-            sprintf(info, "address : %p\n", leak_info->mem_info.address);
-            fwrite(info, (strlen(info)) , 1, fp_write);
-            sprintf(info, "size    : %d bytes\n", leak_info->mem_info.size);            
-            fwrite(info, (strlen(info)) , 1, fp_write);
-            sprintf(info, "file    : %s\n", leak_info->mem_info.file_name);
-            fwrite(info, (strlen(info)) , 1, fp_write);
-            sprintf(info, "line    : %d\n", leak_info->mem_info.line);
-            fwrite(info, (strlen(info)) , 1, fp_write);
-            sprintf(info, "%s\n", "-----------------------------------");    
-            fwrite(info, (strlen(info)) , 1, fp_write);
-        }
-    }
-    fclose(fp_write);
-    DESYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    pthread_mutex_unlock(&synchronized);
+    #endif
 }
 
 void initMemoryDebug() {
@@ -143,7 +126,9 @@ void initMemoryDebug() {
 }
 
 struct memoryInfoFiles *getMemoryInfoFiles() {
-    SYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    pthread_mutex_lock(&synchronized);
+    #endif
     struct MemoryLeak *leak_info;
     struct memoryInfoFiles *list = NULL, *element;
     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
@@ -163,8 +148,16 @@ struct memoryInfoFiles *getMemoryInfoFiles() {
         element->allocations += 1;
         element->allocated += leak_info->mem_info.size;
     }
-    DESYNCHRONIZE(synchronized);
-    return element;
+    element = malloc(sizeof(*element));
+    element->filename = strdup(__FILE__);
+    element->allocations = own_allocated_memleaks;
+    element->allocated = own_allocated_memleaks * sizeof(struct MemoryLeak);
+    element->next = list;
+    list = element;
+    #ifdef HAVE_THREADS
+    pthread_mutex_unlock(&synchronized);
+    #endif
+    return list;
 }
 
 void freeMemoryInfoFiles(struct memoryInfoFiles *files) {
@@ -177,7 +170,9 @@ void freeMemoryInfoFiles(struct memoryInfoFiles *files) {
 }
 
 struct memoryInfoLines *getMemoryInfoLines(const char *filename) {
-    SYNCHRONIZE(synchronized);
+    #ifdef HAVE_THREADS
+    pthread_mutex_lock(&synchronized);
+    #endif
     struct MemoryLeak *leak_info;
     struct memoryInfoLines *list = NULL, *element;
     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
@@ -197,8 +192,18 @@ struct memoryInfoLines *getMemoryInfoLines(const char *filename) {
         }
         element->allocations++;
     }
-    DESYNCHRONIZE(synchronized);
-    return element;
+    if(!stricmp(filename, __FILE__)) {
+        element = malloc(sizeof(*element));
+        element->line = 0;
+        element->allocations = own_allocated_memleaks;
+        element->allocate = sizeof(struct MemoryLeak);
+        element->next = list;
+        list = element;
+    }
+    #ifdef HAVE_THREADS
+    pthread_mutex_unlock(&synchronized);
+    #endif
+    return list;
 }
 
 void freeMemoryInfoLines(struct memoryInfoLines *lines) {