Merge commit 'origin/master' into gallium-map-range
[mesa.git] / src / gallium / auxiliary / util / u_debug_memory.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Memory debugging.
31 *
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35 #include "pipe/p_config.h"
36
37 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
38 #include <windows.h>
39 #include <winddi.h>
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
41 #include <wdm.h>
42 #else
43 #include <stdio.h>
44 #include <stdlib.h>
45 #endif
46
47 #include "util/u_debug.h"
48 #include "util/u_debug_stack.h"
49 #include "util/u_double_list.h"
50
51
52 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
53 #define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
54
55
56 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
57 #define real_malloc(_size) EngAllocMem(0, _size, 'D3AG')
58 #define real_free(_ptr) EngFreeMem(_ptr)
59 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
60 #define real_malloc(_size) ExAllocatePool(0, _size)
61 #define real_free(_ptr) ExFreePool(_ptr)
62 #else
63 #define real_malloc(_size) malloc(_size)
64 #define real_free(_ptr) free(_ptr)
65 #endif
66
67
68 struct debug_memory_header
69 {
70 struct list_head head;
71
72 unsigned long no;
73 const char *file;
74 unsigned line;
75 const char *function;
76 #if DEBUG_MEMORY_STACK
77 struct debug_stack_frame backtrace[DEBUG_MEMORY_STACK];
78 #endif
79 size_t size;
80
81 unsigned magic;
82 };
83
84 struct debug_memory_footer
85 {
86 unsigned magic;
87 };
88
89
90 static struct list_head list = { &list, &list };
91
92 static unsigned long last_no = 0;
93
94
95 static INLINE struct debug_memory_header *
96 header_from_data(void *data)
97 {
98 if(data)
99 return (struct debug_memory_header *)((char *)data - sizeof(struct debug_memory_header));
100 else
101 return NULL;
102 }
103
104 static INLINE void *
105 data_from_header(struct debug_memory_header *hdr)
106 {
107 if(hdr)
108 return (void *)((char *)hdr + sizeof(struct debug_memory_header));
109 else
110 return NULL;
111 }
112
113 static INLINE struct debug_memory_footer *
114 footer_from_header(struct debug_memory_header *hdr)
115 {
116 if(hdr)
117 return (struct debug_memory_footer *)((char *)hdr + sizeof(struct debug_memory_header) + hdr->size);
118 else
119 return NULL;
120 }
121
122
123 void *
124 debug_malloc(const char *file, unsigned line, const char *function,
125 size_t size)
126 {
127 struct debug_memory_header *hdr;
128 struct debug_memory_footer *ftr;
129
130 hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr));
131 if(!hdr) {
132 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
133 file, line, function,
134 (long unsigned)size);
135 return NULL;
136 }
137
138 hdr->no = last_no++;
139 hdr->file = file;
140 hdr->line = line;
141 hdr->function = function;
142 hdr->size = size;
143 hdr->magic = DEBUG_MEMORY_MAGIC;
144
145 #if DEBUG_MEMORY_STACK
146 debug_backtrace_capture(hdr->backtrace, 0, DEBUG_MEMORY_STACK);
147 #endif
148
149 ftr = footer_from_header(hdr);
150 ftr->magic = DEBUG_MEMORY_MAGIC;
151
152 LIST_ADDTAIL(&hdr->head, &list);
153
154 return data_from_header(hdr);
155 }
156
157 void
158 debug_free(const char *file, unsigned line, const char *function,
159 void *ptr)
160 {
161 struct debug_memory_header *hdr;
162 struct debug_memory_footer *ftr;
163
164 if(!ptr)
165 return;
166
167 hdr = header_from_data(ptr);
168 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
169 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
170 file, line, function,
171 ptr);
172 debug_assert(0);
173 return;
174 }
175
176 ftr = footer_from_header(hdr);
177 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
178 debug_printf("%s:%u:%s: buffer overflow %p\n",
179 hdr->file, hdr->line, hdr->function,
180 ptr);
181 debug_assert(0);
182 }
183
184 LIST_DEL(&hdr->head);
185 hdr->magic = 0;
186 ftr->magic = 0;
187
188 real_free(hdr);
189 }
190
191 void *
192 debug_calloc(const char *file, unsigned line, const char *function,
193 size_t count, size_t size )
194 {
195 void *ptr = debug_malloc( file, line, function, count * size );
196 if( ptr )
197 memset( ptr, 0, count * size );
198 return ptr;
199 }
200
201 void *
202 debug_realloc(const char *file, unsigned line, const char *function,
203 void *old_ptr, size_t old_size, size_t new_size )
204 {
205 struct debug_memory_header *old_hdr, *new_hdr;
206 struct debug_memory_footer *old_ftr, *new_ftr;
207 void *new_ptr;
208
209 if(!old_ptr)
210 return debug_malloc( file, line, function, new_size );
211
212 if(!new_size) {
213 debug_free( file, line, function, old_ptr );
214 return NULL;
215 }
216
217 old_hdr = header_from_data(old_ptr);
218 if(old_hdr->magic != DEBUG_MEMORY_MAGIC) {
219 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
220 file, line, function,
221 old_ptr);
222 debug_assert(0);
223 return NULL;
224 }
225
226 old_ftr = footer_from_header(old_hdr);
227 if(old_ftr->magic != DEBUG_MEMORY_MAGIC) {
228 debug_printf("%s:%u:%s: buffer overflow %p\n",
229 old_hdr->file, old_hdr->line, old_hdr->function,
230 old_ptr);
231 debug_assert(0);
232 }
233
234 /* alloc new */
235 new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
236 if(!new_hdr) {
237 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
238 file, line, function,
239 (long unsigned)new_size);
240 return NULL;
241 }
242 new_hdr->no = old_hdr->no;
243 new_hdr->file = old_hdr->file;
244 new_hdr->line = old_hdr->line;
245 new_hdr->function = old_hdr->function;
246 new_hdr->size = new_size;
247 new_hdr->magic = DEBUG_MEMORY_MAGIC;
248
249 new_ftr = footer_from_header(new_hdr);
250 new_ftr->magic = DEBUG_MEMORY_MAGIC;
251
252 LIST_REPLACE(&old_hdr->head, &new_hdr->head);
253
254 /* copy data */
255 new_ptr = data_from_header(new_hdr);
256 memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size );
257
258 /* free old */
259 old_hdr->magic = 0;
260 old_ftr->magic = 0;
261 real_free(old_hdr);
262
263 return new_ptr;
264 }
265
266 unsigned long
267 debug_memory_begin(void)
268 {
269 return last_no;
270 }
271
272 void
273 debug_memory_end(unsigned long start_no)
274 {
275 size_t total_size = 0;
276 struct list_head *entry;
277
278 if(start_no == last_no)
279 return;
280
281 entry = list.prev;
282 for (; entry != &list; entry = entry->prev) {
283 struct debug_memory_header *hdr;
284 void *ptr;
285 struct debug_memory_footer *ftr;
286
287 hdr = LIST_ENTRY(struct debug_memory_header, entry, head);
288 ptr = data_from_header(hdr);
289 ftr = footer_from_header(hdr);
290
291 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
292 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
293 hdr->file, hdr->line, hdr->function,
294 ptr);
295 debug_assert(0);
296 }
297
298 if((start_no <= hdr->no && hdr->no < last_no) ||
299 (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) {
300 debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
301 hdr->file, hdr->line, hdr->function,
302 hdr->size, ptr);
303 #if DEBUG_MEMORY_STACK
304 debug_backtrace_dump(hdr->backtrace, DEBUG_MEMORY_STACK);
305 #endif
306 total_size += hdr->size;
307 }
308
309 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
310 debug_printf("%s:%u:%s: buffer overflow %p\n",
311 hdr->file, hdr->line, hdr->function,
312 ptr);
313 debug_assert(0);
314 }
315 }
316
317 if(total_size) {
318 debug_printf("Total of %u KB of system memory apparently leaked\n",
319 (total_size + 1023)/1024);
320 }
321 else {
322 debug_printf("No memory leaks detected.\n");
323 }
324 }