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 #define DEBUG_MEMORY_IMPLEMENTATION
39 #include "os/os_memory.h"
40 #include "os/os_memory_debug.h"
41 #include "os/os_thread.h"
43 #include "util/u_debug.h"
44 #include "util/u_debug_stack.h"
45 #include "util/u_double_list.h"
48 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
49 #define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
52 struct debug_memory_header
54 struct list_head head
;
60 #if DEBUG_MEMORY_STACK
61 struct debug_stack_frame backtrace
[DEBUG_MEMORY_STACK
];
68 struct debug_memory_footer
74 static struct list_head list
= { &list
, &list
};
76 pipe_static_mutex(list_mutex
);
78 static unsigned long last_no
= 0;
81 static INLINE
struct debug_memory_header
*
82 header_from_data(void *data
)
85 return (struct debug_memory_header
*)((char *)data
- sizeof(struct debug_memory_header
));
91 data_from_header(struct debug_memory_header
*hdr
)
94 return (void *)((char *)hdr
+ sizeof(struct debug_memory_header
));
99 static INLINE
struct debug_memory_footer
*
100 footer_from_header(struct debug_memory_header
*hdr
)
103 return (struct debug_memory_footer
*)((char *)hdr
+ sizeof(struct debug_memory_header
) + hdr
->size
);
110 debug_malloc(const char *file
, unsigned line
, const char *function
,
113 struct debug_memory_header
*hdr
;
114 struct debug_memory_footer
*ftr
;
116 hdr
= os_malloc(sizeof(*hdr
) + size
+ sizeof(*ftr
));
118 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
119 file
, line
, function
,
120 (long unsigned)size
);
127 hdr
->function
= function
;
129 hdr
->magic
= DEBUG_MEMORY_MAGIC
;
131 #if DEBUG_MEMORY_STACK
132 debug_backtrace_capture(hdr
->backtrace
, 0, DEBUG_MEMORY_STACK
);
135 ftr
= footer_from_header(hdr
);
136 ftr
->magic
= DEBUG_MEMORY_MAGIC
;
138 pipe_mutex_lock(list_mutex
);
139 LIST_ADDTAIL(&hdr
->head
, &list
);
140 pipe_mutex_unlock(list_mutex
);
142 return data_from_header(hdr
);
146 debug_free(const char *file
, unsigned line
, const char *function
,
149 struct debug_memory_header
*hdr
;
150 struct debug_memory_footer
*ftr
;
155 hdr
= header_from_data(ptr
);
156 if(hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
157 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
158 file
, line
, function
,
164 ftr
= footer_from_header(hdr
);
165 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
166 debug_printf("%s:%u:%s: buffer overflow %p\n",
167 hdr
->file
, hdr
->line
, hdr
->function
,
172 pipe_mutex_lock(list_mutex
);
173 LIST_DEL(&hdr
->head
);
174 pipe_mutex_unlock(list_mutex
);
182 debug_calloc(const char *file
, unsigned line
, const char *function
,
183 size_t count
, size_t size
)
185 void *ptr
= debug_malloc( file
, line
, function
, count
* size
);
187 memset( ptr
, 0, count
* size
);
192 debug_realloc(const char *file
, unsigned line
, const char *function
,
193 void *old_ptr
, size_t old_size
, size_t new_size
)
195 struct debug_memory_header
*old_hdr
, *new_hdr
;
196 struct debug_memory_footer
*old_ftr
, *new_ftr
;
200 return debug_malloc( file
, line
, function
, new_size
);
203 debug_free( file
, line
, function
, old_ptr
);
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
,
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
,
225 new_hdr
= os_malloc(sizeof(*new_hdr
) + new_size
+ sizeof(*new_ftr
));
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
);
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
;
239 new_ftr
= footer_from_header(new_hdr
);
240 new_ftr
->magic
= DEBUG_MEMORY_MAGIC
;
242 pipe_mutex_lock(list_mutex
);
243 LIST_REPLACE(&old_hdr
->head
, &new_hdr
->head
);
244 pipe_mutex_unlock(list_mutex
);
247 new_ptr
= data_from_header(new_hdr
);
248 memcpy( new_ptr
, old_ptr
, old_size
< new_size
? old_size
: new_size
);
259 debug_memory_begin(void)
265 debug_memory_end(unsigned long start_no
)
267 size_t total_size
= 0;
268 struct list_head
*entry
;
270 if(start_no
== last_no
)
274 for (; entry
!= &list
; entry
= entry
->prev
) {
275 struct debug_memory_header
*hdr
;
277 struct debug_memory_footer
*ftr
;
279 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
280 ptr
= data_from_header(hdr
);
281 ftr
= footer_from_header(hdr
);
283 if(hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
284 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
285 hdr
->file
, hdr
->line
, hdr
->function
,
290 if((start_no
<= hdr
->no
&& hdr
->no
< last_no
) ||
291 (last_no
< start_no
&& (hdr
->no
< last_no
|| start_no
<= hdr
->no
))) {
292 debug_printf("%s:%u:%s: %lu bytes at %p not freed\n",
293 hdr
->file
, hdr
->line
, hdr
->function
,
294 (unsigned long) hdr
->size
, ptr
);
295 #if DEBUG_MEMORY_STACK
296 debug_backtrace_dump(hdr
->backtrace
, DEBUG_MEMORY_STACK
);
298 total_size
+= hdr
->size
;
301 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
302 debug_printf("%s:%u:%s: buffer overflow %p\n",
303 hdr
->file
, hdr
->line
, hdr
->function
,
310 debug_printf("Total of %lu KB of system memory apparently leaked\n",
311 (unsigned long) (total_size
+ 1023)/1024);
314 debug_printf("No memory leaks detected.\n");