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 * Set to 1 to enable checking of freed blocks of memory.
53 * Basically, don't really deallocate freed memory; keep it in the list
54 * but mark it as freed and do extra checking in debug_memory_check().
55 * This can detect some cases of use-after-free. But note that since we
56 * never really free anything this will use a lot of memory.
58 #define DEBUG_FREED_MEMORY 0
59 #define DEBUG_FREED_BYTE 0x33
62 struct debug_memory_header
64 struct list_head head
;
70 #if DEBUG_MEMORY_STACK
71 struct debug_stack_frame backtrace
[DEBUG_MEMORY_STACK
];
74 #if DEBUG_FREED_MEMORY
75 boolean freed
; /**< Is this a freed block? */
81 struct debug_memory_footer
87 static struct list_head list
= { &list
, &list
};
89 pipe_static_mutex(list_mutex
);
91 static unsigned long last_no
= 0;
94 static INLINE
struct debug_memory_header
*
95 header_from_data(void *data
)
98 return (struct debug_memory_header
*)((char *)data
- sizeof(struct debug_memory_header
));
104 data_from_header(struct debug_memory_header
*hdr
)
107 return (void *)((char *)hdr
+ sizeof(struct debug_memory_header
));
112 static INLINE
struct debug_memory_footer
*
113 footer_from_header(struct debug_memory_header
*hdr
)
116 return (struct debug_memory_footer
*)((char *)hdr
+ sizeof(struct debug_memory_header
) + hdr
->size
);
123 debug_malloc(const char *file
, unsigned line
, const char *function
,
126 struct debug_memory_header
*hdr
;
127 struct debug_memory_footer
*ftr
;
129 hdr
= os_malloc(sizeof(*hdr
) + size
+ sizeof(*ftr
));
131 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
132 file
, line
, function
,
133 (long unsigned)size
);
140 hdr
->function
= function
;
142 hdr
->magic
= DEBUG_MEMORY_MAGIC
;
143 #if DEBUG_FREED_MEMORY
147 #if DEBUG_MEMORY_STACK
148 debug_backtrace_capture(hdr
->backtrace
, 0, DEBUG_MEMORY_STACK
);
151 ftr
= footer_from_header(hdr
);
152 ftr
->magic
= DEBUG_MEMORY_MAGIC
;
154 pipe_mutex_lock(list_mutex
);
155 LIST_ADDTAIL(&hdr
->head
, &list
);
156 pipe_mutex_unlock(list_mutex
);
158 return data_from_header(hdr
);
162 debug_free(const char *file
, unsigned line
, const char *function
,
165 struct debug_memory_header
*hdr
;
166 struct debug_memory_footer
*ftr
;
171 hdr
= header_from_data(ptr
);
172 if(hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
173 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
174 file
, line
, function
,
180 ftr
= footer_from_header(hdr
);
181 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
182 debug_printf("%s:%u:%s: buffer overflow %p\n",
183 hdr
->file
, hdr
->line
, hdr
->function
,
188 #if DEBUG_FREED_MEMORY
189 /* Check for double-free */
191 /* Mark the block as freed but don't really free it */
193 /* Save file/line where freed */
196 /* set freed memory to special value */
197 memset(ptr
, DEBUG_FREED_BYTE
, hdr
->size
);
199 pipe_mutex_lock(list_mutex
);
200 LIST_DEL(&hdr
->head
);
201 pipe_mutex_unlock(list_mutex
);
210 debug_calloc(const char *file
, unsigned line
, const char *function
,
211 size_t count
, size_t size
)
213 void *ptr
= debug_malloc( file
, line
, function
, count
* size
);
215 memset( ptr
, 0, count
* size
);
220 debug_realloc(const char *file
, unsigned line
, const char *function
,
221 void *old_ptr
, size_t old_size
, size_t new_size
)
223 struct debug_memory_header
*old_hdr
, *new_hdr
;
224 struct debug_memory_footer
*old_ftr
, *new_ftr
;
228 return debug_malloc( file
, line
, function
, new_size
);
231 debug_free( file
, line
, function
, old_ptr
);
235 old_hdr
= header_from_data(old_ptr
);
236 if(old_hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
237 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
238 file
, line
, function
,
244 old_ftr
= footer_from_header(old_hdr
);
245 if(old_ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
246 debug_printf("%s:%u:%s: buffer overflow %p\n",
247 old_hdr
->file
, old_hdr
->line
, old_hdr
->function
,
253 new_hdr
= os_malloc(sizeof(*new_hdr
) + new_size
+ sizeof(*new_ftr
));
255 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
256 file
, line
, function
,
257 (long unsigned)new_size
);
260 new_hdr
->no
= old_hdr
->no
;
261 new_hdr
->file
= old_hdr
->file
;
262 new_hdr
->line
= old_hdr
->line
;
263 new_hdr
->function
= old_hdr
->function
;
264 new_hdr
->size
= new_size
;
265 new_hdr
->magic
= DEBUG_MEMORY_MAGIC
;
266 #if DEBUG_FREED_MEMORY
267 new_hdr
->freed
= FALSE
;
270 new_ftr
= footer_from_header(new_hdr
);
271 new_ftr
->magic
= DEBUG_MEMORY_MAGIC
;
273 pipe_mutex_lock(list_mutex
);
274 LIST_REPLACE(&old_hdr
->head
, &new_hdr
->head
);
275 pipe_mutex_unlock(list_mutex
);
278 new_ptr
= data_from_header(new_hdr
);
279 memcpy( new_ptr
, old_ptr
, old_size
< new_size
? old_size
: new_size
);
290 debug_memory_begin(void)
296 debug_memory_end(unsigned long start_no
)
298 size_t total_size
= 0;
299 struct list_head
*entry
;
301 if(start_no
== last_no
)
305 for (; entry
!= &list
; entry
= entry
->prev
) {
306 struct debug_memory_header
*hdr
;
308 struct debug_memory_footer
*ftr
;
310 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
311 ptr
= data_from_header(hdr
);
312 ftr
= footer_from_header(hdr
);
314 if(hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
315 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
316 hdr
->file
, hdr
->line
, hdr
->function
,
321 if((start_no
<= hdr
->no
&& hdr
->no
< last_no
) ||
322 (last_no
< start_no
&& (hdr
->no
< last_no
|| start_no
<= hdr
->no
))) {
323 debug_printf("%s:%u:%s: %lu bytes at %p not freed\n",
324 hdr
->file
, hdr
->line
, hdr
->function
,
325 (unsigned long) hdr
->size
, ptr
);
326 #if DEBUG_MEMORY_STACK
327 debug_backtrace_dump(hdr
->backtrace
, DEBUG_MEMORY_STACK
);
329 total_size
+= hdr
->size
;
332 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
333 debug_printf("%s:%u:%s: buffer overflow %p\n",
334 hdr
->file
, hdr
->line
, hdr
->function
,
341 debug_printf("Total of %lu KB of system memory apparently leaked\n",
342 (unsigned long) (total_size
+ 1023)/1024);
345 debug_printf("No memory leaks detected.\n");
351 * We can periodically call this from elsewhere to do a basic sanity
352 * check of the heap memory we've allocated.
355 debug_memory_check(void)
357 struct list_head
*entry
;
360 for (; entry
!= &list
; entry
= entry
->prev
) {
361 struct debug_memory_header
*hdr
;
362 struct debug_memory_footer
*ftr
;
365 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
366 ftr
= footer_from_header(hdr
);
367 ptr
= (const char *) data_from_header(hdr
);
369 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
370 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
371 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
375 if (ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
376 debug_printf("%s:%u:%s: buffer overflow %p\n",
377 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
381 #if DEBUG_FREED_MEMORY
382 /* If this block is marked as freed, check that it hasn't been touched */
385 for (i
= 0; i
< hdr
->size
; i
++) {
386 if (ptr
[i
] != DEBUG_FREED_BYTE
) {
387 debug_printf("Memory error: byte %d of block at %p of size %d is 0x%x\n",
388 i
, ptr
, hdr
->size
, ptr
[i
]);
389 debug_printf("Block was freed at %s:%d\n", hdr
->file
, hdr
->line
);
391 assert(ptr
[i
] == DEBUG_FREED_BYTE
);