1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 * @author José Fonseca <jrfonseca@tungstengraphics.com>
35 #include "pipe/p_config.h"
37 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
47 #include "util/u_debug.h"
48 #include "util/u_debug_stack.h"
49 #include "util/u_double_list.h"
52 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
53 #define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
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)
63 #define real_malloc(_size) malloc(_size)
64 #define real_free(_ptr) free(_ptr)
68 struct debug_memory_header
70 struct list_head head
;
76 #if DEBUG_MEMORY_STACK
77 struct debug_stack_frame backtrace
[DEBUG_MEMORY_STACK
];
84 struct debug_memory_footer
90 static struct list_head list
= { &list
, &list
};
92 static unsigned long last_no
= 0;
95 static INLINE
struct debug_memory_header
*
96 header_from_data(void *data
)
99 return (struct debug_memory_header
*)((char *)data
- sizeof(struct debug_memory_header
));
105 data_from_header(struct debug_memory_header
*hdr
)
108 return (void *)((char *)hdr
+ sizeof(struct debug_memory_header
));
113 static INLINE
struct debug_memory_footer
*
114 footer_from_header(struct debug_memory_header
*hdr
)
117 return (struct debug_memory_footer
*)((char *)hdr
+ sizeof(struct debug_memory_header
) + hdr
->size
);
124 debug_malloc(const char *file
, unsigned line
, const char *function
,
127 struct debug_memory_header
*hdr
;
128 struct debug_memory_footer
*ftr
;
130 hdr
= real_malloc(sizeof(*hdr
) + size
+ sizeof(*ftr
));
132 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
133 file
, line
, function
,
134 (long unsigned)size
);
141 hdr
->function
= function
;
143 hdr
->magic
= DEBUG_MEMORY_MAGIC
;
145 #if DEBUG_MEMORY_STACK
146 debug_backtrace_capture(hdr
->backtrace
, 0, DEBUG_MEMORY_STACK
);
149 ftr
= footer_from_header(hdr
);
150 ftr
->magic
= DEBUG_MEMORY_MAGIC
;
152 LIST_ADDTAIL(&hdr
->head
, &list
);
154 return data_from_header(hdr
);
158 debug_free(const char *file
, unsigned line
, const char *function
,
161 struct debug_memory_header
*hdr
;
162 struct debug_memory_footer
*ftr
;
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
,
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
,
184 LIST_DEL(&hdr
->head
);
192 debug_calloc(const char *file
, unsigned line
, const char *function
,
193 size_t count
, size_t size
)
195 void *ptr
= debug_malloc( file
, line
, function
, count
* size
);
197 memset( ptr
, 0, count
* size
);
202 debug_realloc(const char *file
, unsigned line
, const char *function
,
203 void *old_ptr
, size_t old_size
, size_t new_size
)
205 struct debug_memory_header
*old_hdr
, *new_hdr
;
206 struct debug_memory_footer
*old_ftr
, *new_ftr
;
210 return debug_malloc( file
, line
, function
, new_size
);
213 debug_free( file
, line
, function
, old_ptr
);
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
,
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
,
235 new_hdr
= real_malloc(sizeof(*new_hdr
) + new_size
+ sizeof(*new_ftr
));
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
);
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
;
249 new_ftr
= footer_from_header(new_hdr
);
250 new_ftr
->magic
= DEBUG_MEMORY_MAGIC
;
252 LIST_REPLACE(&old_hdr
->head
, &new_hdr
->head
);
255 new_ptr
= data_from_header(new_hdr
);
256 memcpy( new_ptr
, old_ptr
, old_size
< new_size
? old_size
: new_size
);
267 debug_memory_begin(void)
273 debug_memory_end(unsigned long start_no
)
275 size_t total_size
= 0;
276 struct list_head
*entry
;
278 if(start_no
== last_no
)
282 for (; entry
!= &list
; entry
= entry
->prev
) {
283 struct debug_memory_header
*hdr
;
285 struct debug_memory_footer
*ftr
;
287 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
288 ptr
= data_from_header(hdr
);
289 ftr
= footer_from_header(hdr
);
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
,
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
,
303 #if DEBUG_MEMORY_STACK
304 debug_backtrace_dump(hdr
->backtrace
, DEBUG_MEMORY_STACK
);
306 total_size
+= hdr
->size
;
309 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
310 debug_printf("%s:%u:%s: buffer overflow %p\n",
311 hdr
->file
, hdr
->line
, hdr
->function
,
318 debug_printf("Total of %u KB of system memory apparently leaked\n",
319 (total_size
+ 1023)/1024);
322 debug_printf("No memory leaks detected.\n");