Merge commit 'origin/draw-vbuf-interface'
[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_double_list.h"
49
50
51 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
52
53
54 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
55 #define real_malloc(_size) EngAllocMem(0, _size, 'D3AG')
56 #define real_free(_ptr) EngFreeMem(_ptr)
57 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
58 #define real_malloc(_size) ExAllocatePool(0, _size)
59 #define real_free(_ptr) ExFreePool(_ptr)
60 #else
61 #define real_malloc(_size) malloc(_size)
62 #define real_free(_ptr) free(_ptr)
63 #endif
64
65
66 struct debug_memory_header
67 {
68 struct list_head head;
69
70 unsigned long no;
71 const char *file;
72 unsigned line;
73 const char *function;
74 size_t size;
75 unsigned magic;
76 };
77
78 struct debug_memory_footer
79 {
80 unsigned magic;
81 };
82
83
84 static struct list_head list = { &list, &list };
85
86 static unsigned long last_no = 0;
87
88
89 static INLINE struct debug_memory_header *
90 header_from_data(void *data)
91 {
92 if(data)
93 return (struct debug_memory_header *)((char *)data - sizeof(struct debug_memory_header));
94 else
95 return NULL;
96 }
97
98 static INLINE void *
99 data_from_header(struct debug_memory_header *hdr)
100 {
101 if(hdr)
102 return (void *)((char *)hdr + sizeof(struct debug_memory_header));
103 else
104 return NULL;
105 }
106
107 static INLINE struct debug_memory_footer *
108 footer_from_header(struct debug_memory_header *hdr)
109 {
110 if(hdr)
111 return (struct debug_memory_footer *)((char *)hdr + sizeof(struct debug_memory_header) + hdr->size);
112 else
113 return NULL;
114 }
115
116
117 void *
118 debug_malloc(const char *file, unsigned line, const char *function,
119 size_t size)
120 {
121 struct debug_memory_header *hdr;
122 struct debug_memory_footer *ftr;
123
124 hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr));
125 if(!hdr) {
126 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
127 file, line, function,
128 (long unsigned)size);
129 return NULL;
130 }
131
132 hdr->no = last_no++;
133 hdr->file = file;
134 hdr->line = line;
135 hdr->function = function;
136 hdr->size = size;
137 hdr->magic = DEBUG_MEMORY_MAGIC;
138
139 ftr = footer_from_header(hdr);
140 ftr->magic = DEBUG_MEMORY_MAGIC;
141
142 LIST_ADDTAIL(&hdr->head, &list);
143
144 return data_from_header(hdr);
145 }
146
147 void
148 debug_free(const char *file, unsigned line, const char *function,
149 void *ptr)
150 {
151 struct debug_memory_header *hdr;
152 struct debug_memory_footer *ftr;
153
154 if(!ptr)
155 return;
156
157 hdr = header_from_data(ptr);
158 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
159 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
160 file, line, function,
161 ptr);
162 debug_assert(0);
163 return;
164 }
165
166 ftr = footer_from_header(hdr);
167 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
168 debug_printf("%s:%u:%s: buffer overflow %p\n",
169 hdr->file, hdr->line, hdr->function,
170 ptr);
171 debug_assert(0);
172 }
173
174 LIST_DEL(&hdr->head);
175 hdr->magic = 0;
176 ftr->magic = 0;
177
178 real_free(hdr);
179 }
180
181 void *
182 debug_calloc(const char *file, unsigned line, const char *function,
183 size_t count, size_t size )
184 {
185 void *ptr = debug_malloc( file, line, function, count * size );
186 if( ptr )
187 memset( ptr, 0, count * size );
188 return ptr;
189 }
190
191 void *
192 debug_realloc(const char *file, unsigned line, const char *function,
193 void *old_ptr, size_t old_size, size_t new_size )
194 {
195 struct debug_memory_header *old_hdr, *new_hdr;
196 struct debug_memory_footer *old_ftr, *new_ftr;
197 void *new_ptr;
198
199 if(!old_ptr)
200 return debug_malloc( file, line, function, new_size );
201
202 if(!new_size) {
203 debug_free( file, line, function, old_ptr );
204 return NULL;
205 }
206
207 old_hdr = header_from_data(old_ptr);
208 if(old_hdr->magic != DEBUG_MEMORY_MAGIC) {
209 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
210 file, line, function,
211 old_ptr);
212 debug_assert(0);
213 return NULL;
214 }
215
216 old_ftr = footer_from_header(old_hdr);
217 if(old_ftr->magic != DEBUG_MEMORY_MAGIC) {
218 debug_printf("%s:%u:%s: buffer overflow %p\n",
219 old_hdr->file, old_hdr->line, old_hdr->function,
220 old_ptr);
221 debug_assert(0);
222 }
223
224 /* alloc new */
225 new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
226 if(!new_hdr) {
227 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
228 file, line, function,
229 (long unsigned)new_size);
230 return NULL;
231 }
232 new_hdr->no = old_hdr->no;
233 new_hdr->file = old_hdr->file;
234 new_hdr->line = old_hdr->line;
235 new_hdr->function = old_hdr->function;
236 new_hdr->size = new_size;
237 new_hdr->magic = DEBUG_MEMORY_MAGIC;
238
239 new_ftr = footer_from_header(new_hdr);
240 new_ftr->magic = DEBUG_MEMORY_MAGIC;
241
242 LIST_REPLACE(&old_hdr->head, &new_hdr->head);
243
244 /* copy data */
245 new_ptr = data_from_header(new_hdr);
246 memcpy( new_ptr, old_ptr, old_size < new_size ? old_size : new_size );
247
248 /* free old */
249 old_hdr->magic = 0;
250 old_ftr->magic = 0;
251 real_free(old_hdr);
252
253 return new_ptr;
254 }
255
256 unsigned long
257 debug_memory_begin(void)
258 {
259 return last_no;
260 }
261
262 void
263 debug_memory_end(unsigned long start_no)
264 {
265 size_t total_size = 0;
266 struct list_head *entry;
267
268 if(start_no == last_no)
269 return;
270
271 entry = list.prev;
272 for (; entry != &list; entry = entry->prev) {
273 struct debug_memory_header *hdr;
274 void *ptr;
275 struct debug_memory_footer *ftr;
276
277 hdr = LIST_ENTRY(struct debug_memory_header, entry, head);
278 ptr = data_from_header(hdr);
279 ftr = footer_from_header(hdr);
280
281 if(hdr->magic != DEBUG_MEMORY_MAGIC) {
282 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
283 hdr->file, hdr->line, hdr->function,
284 ptr);
285 debug_assert(0);
286 }
287
288 if((start_no <= hdr->no && hdr->no < last_no) ||
289 (last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) {
290 debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
291 hdr->file, hdr->line, hdr->function,
292 hdr->size, ptr);
293 total_size += hdr->size;
294 }
295
296 if(ftr->magic != DEBUG_MEMORY_MAGIC) {
297 debug_printf("%s:%u:%s: buffer overflow %p\n",
298 hdr->file, hdr->line, hdr->function,
299 ptr);
300 debug_assert(0);
301 }
302 }
303
304 if(total_size) {
305 debug_printf("Total of %u KB of system memory apparently leaked\n",
306 (total_size + 1023)/1024);
307 }
308 else {
309 debug_printf("No memory leaks detected.\n");
310 }
311 }