| 1 |
/** |
| 2 |
* @file mem.c |
| 3 |
* @brief メモリ関係 |
| 4 |
* @author BananaJinn |
| 5 |
* @version $Id: mem.c,v 1.3 2007/05/01 12:41:54 bananajinn Exp $ |
| 6 |
* 円盤複写屋 |
| 7 |
* Copyright (C) 2004-2007 BananaJinn<banana@mxh.mesh.ne.jp>. |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#if !defined(MACOSX) |
| 11 |
# include <malloc.h> |
| 12 |
#endif |
| 13 |
#include "mem.h" |
| 14 |
|
| 15 |
#if defined(DEBUGMEM) |
| 16 |
struct _MEMDEBUG { |
| 17 |
void *ptr; |
| 18 |
size_t size; |
| 19 |
char fname[16]; |
| 20 |
long line; |
| 21 |
} *g_MemArray = NULL; |
| 22 |
int g_NumMemArray = 0; |
| 23 |
int g_MemDebug = 0; |
| 24 |
|
| 25 |
# if defined(WIN32) |
| 26 |
# include <windows.h> |
| 27 |
# define PRINT_MEMDEBUG Trace |
| 28 |
# else |
| 29 |
# define PRINT_MEMDEBUG printf |
| 30 |
# endif |
| 31 |
# define MEMDUMP_MAX 48 |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined(DEBUGMEM) && defined(WIN32) |
| 35 |
static void Trace(const char *format, ...) |
| 36 |
{ |
| 37 |
va_list args; |
| 38 |
char buf[512]; |
| 39 |
|
| 40 |
va_start(args, format); |
| 41 |
_vsnprintf(buf, sizeof(buf), format, args); |
| 42 |
va_end(args); |
| 43 |
OutputDebugString(buf); |
| 44 |
} |
| 45 |
#endif /* DEBUGMEM */ |
| 46 |
|
| 47 |
|
| 48 |
#if defined(DEBUGMEM) |
| 49 |
static void MemDebugStore(struct _MEMDEBUG *md, |
| 50 |
void *ptr, size_t size, const char *fname, long line) |
| 51 |
{ |
| 52 |
md->ptr = ptr; |
| 53 |
md->size = size; |
| 54 |
strncpy(md->fname, fname, sizeof(md->fname)); |
| 55 |
md->fname[sizeof(md->fname)-1] = '\0'; |
| 56 |
md->line = line; |
| 57 |
} |
| 58 |
#endif /* DEBUGMEM */ |
| 59 |
|
| 60 |
|
| 61 |
#if defined(DEBUGMEM) |
| 62 |
static void MemDebug(const char *fname, long line, |
| 63 |
void *old_ptr, void *new_ptr, size_t new_size) |
| 64 |
{ |
| 65 |
int index; |
| 66 |
char *wp; |
| 67 |
|
| 68 |
if(!g_MemDebug){ |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
#if defined(WIN32) |
| 73 |
wp = strrchr(fname, '\\'); |
| 74 |
#else |
| 75 |
wp = strrchr(fname, '/'); |
| 76 |
#endif |
| 77 |
if(wp != NULL){ |
| 78 |
fname = wp+1; |
| 79 |
} |
| 80 |
|
| 81 |
if(old_ptr == NULL){ |
| 82 |
#if defined(DUMP_MEMDEBUG) |
| 83 |
PRINT_MEMDEBUG("Add %p(%d bytes) by %s(%ld)\n", new_ptr, new_size, |
| 84 |
fname, line); |
| 85 |
#endif |
| 86 |
for(index=0; index<g_NumMemArray; index++){ |
| 87 |
if(g_MemArray[index].ptr == NULL){ |
| 88 |
MemDebugStore(&g_MemArray[index], |
| 89 |
new_ptr, new_size, fname, line); |
| 90 |
return; |
| 91 |
} |
| 92 |
} |
| 93 |
g_MemArray = realloc(g_MemArray, |
| 94 |
sizeof(struct _MEMDEBUG)*(g_NumMemArray+1)); |
| 95 |
if(g_MemArray != NULL){ |
| 96 |
MemDebugStore(&g_MemArray[g_NumMemArray], |
| 97 |
new_ptr, new_size, fname, line); |
| 98 |
g_NumMemArray++; |
| 99 |
} |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
for(index=0; index<g_NumMemArray; index++){ |
| 104 |
if(g_MemArray[index].ptr == old_ptr){ |
| 105 |
if(new_ptr==NULL){ |
| 106 |
#if defined(DUMP_MEMDEBUG) |
| 107 |
PRINT_MEMDEBUG("Delete %p(%d bytes) by %s(%ld)\n", |
| 108 |
old_ptr, g_MemArray[index].size, |
| 109 |
fname, line); |
| 110 |
#endif |
| 111 |
MemDebugStore(&g_MemArray[index], NULL, 0, "", 0); |
| 112 |
return; |
| 113 |
} |
| 114 |
else{ |
| 115 |
#if defined(DUMP_MEMDEBUG) |
| 116 |
PRINT_MEMDEBUG("Resize %p(%d bytes) => %p(%d bytes) by %s(%ld)\n", |
| 117 |
old_ptr, g_MemArray[index].size, |
| 118 |
new_ptr, new_size, |
| 119 |
fname, line); |
| 120 |
#endif |
| 121 |
MemDebugStore(&g_MemArray[index], new_ptr, new_size, |
| 122 |
fname, line); |
| 123 |
return; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
PRINT_MEMDEBUG("ERROR! %p is invalid address. %s(%ld)\n", |
| 128 |
old_ptr, fname, line); |
| 129 |
return; |
| 130 |
} |
| 131 |
#endif /* DEBUGMEM */ |
| 132 |
|
| 133 |
#if defined(DEBUGMEM) |
| 134 |
static void MemDump(void *ptr, size_t size) |
| 135 |
{ |
| 136 |
size_t index, index16; |
| 137 |
char ascii[16+1]; |
| 138 |
unsigned char *bytep = (unsigned char *)ptr; |
| 139 |
|
| 140 |
for(index=0; index<size && index<MEMDUMP_MAX; index++){ |
| 141 |
index16 = index & 15; |
| 142 |
if((bytep[index] >= 0x20) && (bytep[index] < 0x7f)){ |
| 143 |
ascii[index16] = bytep[index]; |
| 144 |
} |
| 145 |
else{ |
| 146 |
ascii[index16] = '.'; |
| 147 |
} |
| 148 |
if(index16 == 0){ |
| 149 |
PRINT_MEMDEBUG("0x%08lX ", bytep+index); |
| 150 |
} |
| 151 |
else if(index16 == 8){ |
| 152 |
PRINT_MEMDEBUG(" "); |
| 153 |
} |
| 154 |
PRINT_MEMDEBUG("%02X ", bytep[index]); |
| 155 |
if(index16 == 15){ |
| 156 |
ascii[16] = '\0'; |
| 157 |
PRINT_MEMDEBUG("%s\n", ascii); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
index16 = index & 15; |
| 162 |
if(index16 != 0){ |
| 163 |
ascii[index16] = '\0'; |
| 164 |
PRINT_MEMDEBUG("%*s %s\n", |
| 165 |
3*index16+((index16 >= 8) ? 1:0), |
| 166 |
"", ascii); |
| 167 |
} |
| 168 |
} |
| 169 |
#endif /* DEBUGMEM */ |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* @brief メモリデバッグ開始 |
| 174 |
* @note DEBUGMEMがデファインされていない場合は何もしない |
| 175 |
*/ |
| 176 |
void MemDebugStart() |
| 177 |
{ |
| 178 |
#if defined(DEBUGMEM) |
| 179 |
if(g_MemDebug){ |
| 180 |
MemDebugEnd(); |
| 181 |
} |
| 182 |
g_MemDebug = 1; |
| 183 |
#endif /* DEBUGMEM */ |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* @brief メモリデバッグ終了 |
| 189 |
* @note DEBUGMEMがデファインされていない場合は何もしない |
| 190 |
*/ |
| 191 |
void MemDebugEnd() |
| 192 |
{ |
| 193 |
#if defined(DEBUGMEM) |
| 194 |
if(g_MemArray){ |
| 195 |
free(g_MemArray); |
| 196 |
g_MemArray = NULL; |
| 197 |
} |
| 198 |
g_NumMemArray = 0; |
| 199 |
g_MemDebug = 0; |
| 200 |
#endif /* DEBUGMEM */ |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @brief メモリリークを表示 |
| 205 |
* @note DEBUGMEMがデファインされていない場合は何もしない |
| 206 |
*/ |
| 207 |
void MemDumpLeaks() |
| 208 |
{ |
| 209 |
#if defined(DEBUGMEM) |
| 210 |
int index; |
| 211 |
int leakcnt=0; |
| 212 |
|
| 213 |
for(index=0; index<g_NumMemArray; index++){ |
| 214 |
if(g_MemArray[index].ptr != NULL){ |
| 215 |
PRINT_MEMDEBUG("Detect memory leaks! %p(%d bytes) by %s(%ld)\n", |
| 216 |
g_MemArray[index].ptr, g_MemArray[index].size, |
| 217 |
g_MemArray[index].fname, g_MemArray[index].line); |
| 218 |
MemDump(g_MemArray[index].ptr, g_MemArray[index].size); |
| 219 |
leakcnt++; |
| 220 |
} |
| 221 |
} |
| 222 |
if(leakcnt == 0){ |
| 223 |
PRINT_MEMDEBUG("No memory leaks!\n"); |
| 224 |
} |
| 225 |
#endif /* DEBUGMEM */ |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
#if defined(DEBUGMEM) |
| 230 |
/** |
| 231 |
* @brief メモリ確保 |
| 232 |
* @param[in] fname 呼び出し元ソースファイル名 |
| 233 |
* @param[in] line 呼び出し元ソースファイル行番号 |
| 234 |
* @param[in] size メモリサイズ(バイト数) |
| 235 |
* @return 確保したメモリの先頭アドレスを返す。エラーの場合はNULLを返す。 |
| 236 |
*/ |
| 237 |
void *MemNewF(const char *fname, long line, size_t size) |
| 238 |
{ |
| 239 |
void *ptr = malloc(size); |
| 240 |
MemDebug(fname, line, NULL, ptr, size); |
| 241 |
return ptr; |
| 242 |
} |
| 243 |
#endif /* DEBUGMEM */ |
| 244 |
|
| 245 |
#if defined(DEBUGMEM) |
| 246 |
/** |
| 247 |
* @brief メモリサイズ拡大 |
| 248 |
* @param[in] fname 呼び出し元ソースファイル名 |
| 249 |
* @param[in] line 呼び出し元ソースファイル行番号 |
| 250 |
* @param[in] ptr 拡大するメモリの先頭アドレス |
| 251 |
* @param[in] size 新しいメモリサイズ(バイト数) |
| 252 |
* @return 拡大されたメモリの先頭アドレスを返す。エラーの場合はNULLを返す。 |
| 253 |
*/ |
| 254 |
void *MemResizeF(const char *fname, long line, void *ptr, size_t new_size) |
| 255 |
{ |
| 256 |
void *new_ptr; |
| 257 |
|
| 258 |
new_ptr = realloc(ptr, new_size); |
| 259 |
MemDebug(fname, line, ptr, new_ptr, new_size); |
| 260 |
|
| 261 |
return new_ptr; |
| 262 |
} |
| 263 |
#endif /* DEBUGMEM */ |
| 264 |
|
| 265 |
|
| 266 |
#if defined(DEBUGMEM) |
| 267 |
/** |
| 268 |
* @brief 確保されたメモリの解放 |
| 269 |
* @param[in] fname 呼び出し元ソースファイル名 |
| 270 |
* @param[in] line 呼び出し元ソースファイル行番号 |
| 271 |
* @param[in] ptr 解放するメモリの先頭アドレス |
| 272 |
* @return NULLを返す。 |
| 273 |
*/ |
| 274 |
void *MemFreeF(const char *fname, long line, void *ptr) |
| 275 |
{ |
| 276 |
MemDebug(fname, line, ptr, NULL, 0); |
| 277 |
free(ptr); |
| 278 |
|
| 279 |
return NULL; |
| 280 |
} |
| 281 |
#endif /* DEBUGMEM */ |
| 282 |
|