removed unused code in bots.c
[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
21 #define FILE_NAME_LENGTH  256
22 #define OUTPUT_FILE "leak_info.txt"
23
24 #undef malloc
25 #undef calloc
26 #undef strdup
27 #undef free
28
29 struct MemoryNode {
30 void *address;
31 unsigned int size;
32 char file_name[FILE_NAME_LENGTH];
33 unsigned int line;
34 };
35
36 struct MemoryLeak {
37 struct MemoryNode mem_info;
38 struct MemoryLeak *next;
39 };
40
41 #ifdef HAVE_THREADS
42 static pthread_mutex_t synchronized;
43 #endif
44
45 static struct MemoryLeak *ptr_start = NULL;
46
47 static unsigned int own_allocated_memleaks = 0;
48
49 static void add_mem_info(void * mem_ref, unsigned int size, const char *file, unsigned int line);
50 static void remove_mem_info(void * mem_ref);
51
52 void *xmalloc(unsigned int size, const char *file, unsigned int line) {
53     void *ptr = malloc(size);
54     if (ptr != NULL) {
55         add_mem_info(ptr, size, file, line);
56     }
57     return ptr;
58 }
59
60 void *xcalloc(unsigned int elements, unsigned int size, const char *file, unsigned int line) {
61     void *ptr = calloc(elements, size);
62     if(ptr != NULL) {
63         add_mem_info(ptr, (elements * size), file, line);
64     }
65     return ptr;
66 }
67
68 char *xstrdup(const char *data, const char *file, unsigned int line) {
69     int len = strlen(data)+1;
70     char *ptr = malloc(len);
71     if(ptr != NULL) {
72         strcpy(ptr, data);
73         add_mem_info(ptr, len, file, line);
74     }
75     return ptr;
76 }
77
78 void xfree(void *mem_ref) {
79     remove_mem_info(mem_ref);
80     free(mem_ref);
81 }
82
83
84 static void add_mem_info(void *mem_ref, unsigned int size, const char *file, unsigned int line) {
85     SYNCHRONIZE(synchronized);
86     struct MemoryLeak *mem_leak_info = malloc(sizeof(*mem_leak_info));
87     own_allocated_memleaks++;
88     mem_leak_info->mem_info.address = mem_ref;
89     mem_leak_info->mem_info.size = size;
90     strcpy(mem_leak_info->mem_info.file_name, file);    
91     mem_leak_info->mem_info.line = line;
92     mem_leak_info->next = ptr_start;
93     ptr_start = mem_leak_info;
94     DESYNCHRONIZE(synchronized);
95 }
96
97 static void remove_mem_info(void *mem_ref) {
98     SYNCHRONIZE(synchronized);
99     struct MemoryLeak *leak_info, *next, *prev = NULL;
100     for(leak_info = ptr_start; leak_info; leak_info = next) {
101         next = leak_info->next;
102         if (leak_info->mem_info.address == mem_ref) {
103             if(prev)
104                 prev->next = next;
105             else
106                 ptr_start = next;
107             own_allocated_memleaks--;
108             free(leak_info);
109             break;
110         } else 
111             prev = leak_info;
112     }
113     DESYNCHRONIZE(synchronized);
114 }
115
116 void initMemoryDebug() {
117     THREAD_MUTEX_INIT(synchronized);
118 }
119
120 struct memoryInfoFiles *getMemoryInfoFiles() {
121     SYNCHRONIZE(synchronized);
122     struct MemoryLeak *leak_info;
123     struct memoryInfoFiles *list = NULL, *element;
124     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
125         for(element = list; element; element = element->next) {
126             if(!strcmp(leak_info->mem_info.file_name, element->filename)) {
127                 break;
128             }
129         }
130         if(!element) {
131             element = malloc(sizeof(*element));
132             element->filename = strdup(leak_info->mem_info.file_name);
133             element->allocations = 0;
134             element->allocated = 0;
135             element->next = list;
136             list = element;
137         }
138         element->allocations += 1;
139         element->allocated += leak_info->mem_info.size;
140     }
141     element = malloc(sizeof(*element));
142     element->filename = strdup(__FILE__);
143     element->allocations = own_allocated_memleaks;
144     element->allocated = own_allocated_memleaks * sizeof(struct MemoryLeak);
145     element->next = list;
146     list = element;
147     DESYNCHRONIZE(synchronized);
148     return list;
149 }
150
151 void freeMemoryInfoFiles(struct memoryInfoFiles *files) {
152     struct memoryInfoFiles *next;
153     for(;files;files = next) {
154         next = files->next;
155         free(files->filename);
156         free(files);
157     }
158 }
159
160 struct memoryInfoLines *getMemoryInfoLines(const char *filename) {
161     SYNCHRONIZE(synchronized);
162     struct MemoryLeak *leak_info;
163     struct memoryInfoLines *list = NULL, *element;
164     for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) {
165         if(stricmp(leak_info->mem_info.file_name, filename)) continue;
166         for(element = list; element; element = element->next) {
167             if(element->line == leak_info->mem_info.line && element->allocate == leak_info->mem_info.size) {
168                 break;
169             }
170         }
171         if(!element) {
172             element = malloc(sizeof(*element));
173             element->line = leak_info->mem_info.line;
174             element->allocations = 0;
175             element->allocate = leak_info->mem_info.size;
176             element->next = list;
177             list = element;
178         }
179         element->allocations++;
180     }
181     if(!stricmp(filename, __FILE__)) {
182         element = malloc(sizeof(*element));
183         element->line = 0;
184         element->allocations = own_allocated_memleaks;
185         element->allocate = sizeof(struct MemoryLeak);
186         element->next = list;
187         list = element;
188     }
189     DESYNCHRONIZE(synchronized);
190     return list;
191 }
192
193 void freeMemoryInfoLines(struct memoryInfoLines *lines) {
194     struct memoryInfoLines *next;
195     for(;lines;lines = next) {
196         next = lines->next;
197         free(lines);
198     }
199 }