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 "pipe/p_debug.h"
48 #include "util/u_double_list.h"
51 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
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)
61 #define real_malloc(_size) malloc(_size)
62 #define real_free(_ptr) free(_ptr)
66 struct debug_memory_header
68 struct list_head head
;
78 struct debug_memory_footer
84 static struct list_head list
= { &list
, &list
};
86 static unsigned long last_no
= 0;
89 static INLINE
struct debug_memory_header
*
90 header_from_data(void *data
)
93 return (struct debug_memory_header
*)((char *)data
- sizeof(struct debug_memory_header
));
99 data_from_header(struct debug_memory_header
*hdr
)
102 return (void *)((char *)hdr
+ sizeof(struct debug_memory_header
));
107 static INLINE
struct debug_memory_footer
*
108 footer_from_header(struct debug_memory_header
*hdr
)
111 return (struct debug_memory_footer
*)((char *)hdr
+ sizeof(struct debug_memory_header
) + hdr
->size
);
118 debug_malloc(const char *file
, unsigned line
, const char *function
,
121 struct debug_memory_header
*hdr
;
122 struct debug_memory_footer
*ftr
;
124 hdr
= real_malloc(sizeof(*hdr
) + size
+ sizeof(*ftr
));
131 hdr
->function
= function
;
133 hdr
->magic
= DEBUG_MEMORY_MAGIC
;
135 ftr
= footer_from_header(hdr
);
136 ftr
->magic
= DEBUG_MEMORY_MAGIC
;
138 LIST_ADDTAIL(&hdr
->head
, &list
);
140 return data_from_header(hdr
);
144 debug_free(const char *file
, unsigned line
, const char *function
,
147 struct debug_memory_header
*hdr
;
148 struct debug_memory_footer
*ftr
;
153 hdr
= header_from_data(ptr
);
154 if(hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
155 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
156 file
, line
, function
,
162 ftr
= footer_from_header(hdr
);
163 if(ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
164 debug_printf("%s:%u:%s: buffer overflow %p\n",
165 hdr
->file
, hdr
->line
, hdr
->function
,
170 LIST_DEL(&hdr
->head
);
178 debug_calloc(const char *file
, unsigned line
, const char *function
,
179 size_t count
, size_t size
)
181 void *ptr
= debug_malloc( file
, line
, function
, count
* size
);
183 memset( ptr
, 0, count
* size
);
188 debug_realloc(const char *file
, unsigned line
, const char *function
,
189 void *old_ptr
, size_t old_size
, size_t new_size
)
191 struct debug_memory_header
*old_hdr
, *new_hdr
;
192 struct debug_memory_footer
*old_ftr
, *new_ftr
;
196 return debug_malloc( file
, line
, function
, new_size
);
199 debug_free( file
, line
, function
, old_ptr
);
203 old_hdr
= header_from_data(old_ptr
);
204 if(old_hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
205 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
206 file
, line
, function
,
212 old_ftr
= footer_from_header(old_hdr
);
213 if(old_ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
214 debug_printf("%s:%u:%s: buffer overflow %p\n",
215 old_hdr
->file
, old_hdr
->line
, old_hdr
->function
,
221 new_hdr
= real_malloc(sizeof(*new_hdr
) + new_size
+ sizeof(*new_ftr
));
224 new_hdr
->no
= old_hdr
->no
;
225 new_hdr
->file
= old_hdr
->file
;
226 new_hdr
->line
= old_hdr
->line
;
227 new_hdr
->function
= old_hdr
->function
;
228 new_hdr
->size
= new_size
;
229 new_hdr
->magic
= DEBUG_MEMORY_MAGIC
;
231 new_ftr
= footer_from_header(new_hdr
);
232 new_ftr
->magic
= DEBUG_MEMORY_MAGIC
;
234 LIST_REPLACE(&old_hdr
->head
, &new_hdr
->head
);
237 new_ptr
= data_from_header(new_hdr
);
238 memcpy( new_ptr
, old_ptr
, old_size
< new_size
? old_size
: new_size
);
249 debug_memory_begin(void)
255 debug_memory_end(unsigned long start_no
)
257 size_t total_size
= 0;
258 struct list_head
*entry
;
261 for (; entry
!= &list
; entry
= entry
->prev
) {
262 struct debug_memory_header
*hdr
;
264 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
265 ptr
= data_from_header(hdr
);
266 if((start_no
<= hdr
->no
&& hdr
->no
< last_no
) ||
267 (last_no
< start_no
&& (hdr
->no
< last_no
|| start_no
<= hdr
->no
))) {
268 debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
269 hdr
->file
, hdr
->line
, hdr
->function
,
271 total_size
+= hdr
->size
;
275 debug_printf("Total of %u KB of system memory apparently leaked\n",
276 (total_size
+ 1023)/1024);