Merge remote-tracking branch 'IoMultiplexer.git/master' into development
[NeonServV5.git] / src / memoryDebug.c
1 /* memoryDebug.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "main.h"
18 #include "memoryDebug.h"
19 #include "memoryInfo.h"
20 #include "tools.h"
21
22 #define FILE_NAME_LENGTH  256
23 #define OUTPUT_FILE "leak_info.txt"
24
25 #undef malloc
26 #undef calloc
27 #undef strdup
28 #undef free
29
30 struct MemoryNode {
31 void *address;
32 unsigned int size;
33 char file_name[FILE_NAME_LENGTH];
34 unsigned int line;
35 };
36
37 struct MemoryLeak {
38 struct MemoryNode mem_info;
39 struct MemoryLeak *next;
40 };
41
42 #ifdef HAVE_THREADS
43 static pthread_mutex_t synchronized;
44 #endif
45
46 static struct MemoryLeak *ptr_start = NULL;
47
48 static unsigned int own_allocated_memleaks = 0;
49
50 static void add_mem_info(void * mem_ref, unsigned int size, const char *file, unsigned int line);
51 static void remove_mem_info(void * mem_ref);
52
53 void *xmalloc(unsigned int size, const char *file, unsigned int line) {
54     void *ptr = malloc(size);
55     if (ptr != NULL) {
56         add_mem_info(ptr, size, file, line);
57     }
58     return ptr;
59 }
60
61 void *xcalloc(unsigned int elements, unsigned int size, const char *file, unsigned int line) {
62     void *ptr = calloc(elements, size);
63     if(ptr != NULL) {
64         add_mem_info(ptr, (elements * size), file, line);
65     }
66     return ptr;
67 }
68
69 char *xstrdup(const char *data, const char *file, unsigned int line) {
70     int len = strlen(data)+1;
71     char *ptr = malloc(len);
72     if(ptr != NULL) {
73         strcpy(ptr, data);
74         add_mem_info(ptr, len, file, line);
75     }
76     return ptr;
77 }
78
79 void xfree(void *mem_ref) {
80     remove_mem_info(mem_ref);
81     free(mem_ref);
82 }
83
84
85 static void add_mem_info(void *mem_ref, unsigned int size, const char *file, unsigned int line) {
86     SYNCHRONIZE(synchronized);
87     struct MemoryLeak *mem_leak_info = malloc(sizeof(*mem_leak_info));
88     own_allocated_memleaks++;
89     mem_leak_info->mem_info.address = mem_ref;
90     mem_leak_info->mem_info.size = size;
91     strcpy(mem_leak_info->mem_info.file_name, file);    
92     mem_leak_info->mem_info.line = line;
93     mem_leak_info->next = ptr_start;
94     ptr_start = mem_leak_info;
95     DESYNCHRONIZE(synchronized);
96 }
97
98 static void remove_mem_info(void *mem_ref) {
99     SYNCHRONIZE(synchronized);
100     struct MemoryLeak *leak_info, *next, *prev = NULL;
101     for(leak_info = ptr_start; leak_info; leak_info = next) {
102         next = leak_info->next;
103         if (leak_info->mem_info.address == mem_ref) {
104             if(prev)
105                 prev->next = next;
106             else
107                 ptr_start = next;
108             own_allocated_memleaks--;
109             free(leak_info);
110             break;
111         } else 
112             prev = leak_info;
113     }
114     DESYNCHRONIZE(synchronized);
115 }
116
117 void initMemoryDebug() {
118     THREAD_MUTEX_INIT(synchronized);
119 }
120
121 struct memoryInfoFiles *getMemoryInfoFiles() {
122     SYNCHRONIZE(synchronized);
123     struct MemoryLeak *leak_info;
124     struct memoryInfoFiles *list = NULL, *element;
125     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
126         for(element = list; element; element = element->next) {
127             if(!strcmp(leak_info->mem_info.file_name, element->filename)) {
128                 break;
129             }
130         }
131         if(!element) {
132             element = malloc(sizeof(*element));
133             element->filename = strdup(leak_info->mem_info.file_name);
134             element->allocations = 0;
135             element->allocated = 0;
136             element->next = list;
137             list = element;
138         }
139         element->allocations += 1;
140         element->allocated += leak_info->mem_info.size;
141     }
142     element = malloc(sizeof(*element));
143     element->filename = strdup(__FILE__);
144     element->allocations = own_allocated_memleaks;
145     element->allocated = own_allocated_memleaks * sizeof(struct MemoryLeak);
146     element->next = list;
147     list = element;
148     DESYNCHRONIZE(synchronized);
149     return list;
150 }
151
152 void freeMemoryInfoFiles(struct memoryInfoFiles *files) {
153     struct memoryInfoFiles *next;
154     for(;files;files = next) {
155         next = files->next;
156         free(files->filename);
157         free(files);
158     }
159 }
160
161 struct memoryInfoLines *getMemoryInfoLines(const char *filename) {
162     SYNCHRONIZE(synchronized);
163     struct MemoryLeak *leak_info;
164     struct memoryInfoLines *list = NULL, *element;
165     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
166         if(stricmp(leak_info->mem_info.file_name, filename)) continue;
167         for(element = list; element; element = element->next) {
168             if(element->line == leak_info->mem_info.line && element->allocate == leak_info->mem_info.size) {
169                 break;
170             }
171         }
172         if(!element) {
173             element = malloc(sizeof(*element));
174             element->line = leak_info->mem_info.line;
175             element->allocations = 0;
176             element->allocate = leak_info->mem_info.size;
177             element->next = list;
178             list = element;
179         }
180         element->allocations++;
181     }
182     if(!stricmp(filename, __FILE__)) {
183         element = malloc(sizeof(*element));
184         element->line = 0;
185         element->allocations = own_allocated_memleaks;
186         element->allocate = sizeof(struct MemoryLeak);
187         element->next = list;
188         list = element;
189     }
190     DESYNCHRONIZE(synchronized);
191     return list;
192 }
193
194 void freeMemoryInfoLines(struct memoryInfoLines *lines) {
195     struct memoryInfoLines *next;
196     for(;lines;lines = next) {
197         next = lines->next;
198         free(lines);
199     }
200 }