質問編集履歴
1
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -56,23 +56,23 @@
|
|
56
56
|
|
57
57
|
//必要な関数のプロトタイプ宣言
|
58
58
|
|
59
|
-
void *dbg_malloc(size_t size,const char *file,int line);
|
59
|
+
void *dbg_malloc(size_t size,const char *file,int line);
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
-
void *dbg_calloc(size_t number,size_t size,const char *file,int line);
|
63
|
+
void *dbg_calloc(size_t number,size_t size,const char *file,int line);
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
-
void *dbg_realloc(void *ptr,size_t size,const char *file,int line);
|
67
|
+
void *dbg_realloc(void *ptr,size_t size,const char *file,int line); り
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
-
char *dbg_strdup(const char *str,const char *file,int line);
|
71
|
+
char *dbg_strdup(const char *str,const char *file,int line);
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
-
void dbg_free(void *ptr,const char *file,int line);
|
75
|
+
void dbg_free(void *ptr,const char *file,int line);
|
76
76
|
|
77
77
|
|
78
78
|
|
@@ -88,12 +88,6 @@
|
|
88
88
|
|
89
89
|
#ifndef DBG_MALLOC_NOCHANGE
|
90
90
|
|
91
|
-
//DBG_MALLOC_NOCHANGEの定義/未定義を切り替えることで、
|
92
|
-
|
93
|
-
//#defineの有効,無効を切り替える
|
94
|
-
|
95
|
-
|
96
|
-
|
97
91
|
#define malloc(s) dbg_malloc((s),__FILE__,__LINE__)
|
98
92
|
|
99
93
|
#define calloc(n,s) dbg_calloc((n),(s),__FILE__,__LINE__)
|
@@ -108,7 +102,7 @@
|
|
108
102
|
|
109
103
|
|
110
104
|
|
111
|
-
#endif
|
105
|
+
#endif
|
112
106
|
|
113
107
|
|
114
108
|
|
@@ -136,221 +130,79 @@
|
|
136
130
|
|
137
131
|
#include "dbg_malloc.h"
|
138
132
|
|
139
|
-
//#define DBG_MALLOC_NOCHANGEの定義を行ってから"dbg_malloc.h"を
|
140
|
-
|
141
|
-
//インクルードをしている。この定義により,
|
142
|
-
|
143
|
-
//dbg_malloc()内部のmalloc()やfree()の#define が無効になる。
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
//ライブラリ本体で,、元のmalloc()とfree()を呼び出す必要があるため,
|
148
|
-
|
149
|
-
//malloc()とfree()がdbg_malloc()とdbg_free()に置き換わらないように,
|
150
|
-
|
151
|
-
//#define が無効にしている。
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
//これ以降必要な構造体struct memory_count,struct memory_block,
|
158
|
-
|
159
|
-
//struct free_countを定義している
|
160
|
-
|
161
133
|
//ソース中でdbg_malloc()を利用している場所ごとに作成される構造体
|
162
134
|
|
163
135
|
struct memory_count{
|
164
136
|
|
165
|
-
const char *file;
|
137
|
+
const char *file;
|
166
|
-
|
138
|
+
|
167
|
-
int line;
|
139
|
+
int line;
|
168
|
-
|
140
|
+
|
169
|
-
int number;
|
141
|
+
int number;
|
170
|
-
|
171
|
-
|
142
|
+
|
172
|
-
|
173
|
-
// メモリの解放忘れがあtることを示している
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
unsigned int alloc_count
|
143
|
+
unsigned int alloc_count
|
178
|
-
|
179
|
-
|
144
|
+
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
unsigned int free_count;
|
145
|
+
unsigned int free_count;
|
184
|
-
|
185
|
-
|
146
|
+
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
int size;
|
147
|
+
int size;
|
190
|
-
|
191
|
-
|
148
|
+
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
unsigned int alloc_size;
|
149
|
+
unsigned int alloc_size;
|
196
|
-
|
197
|
-
|
150
|
+
|
198
|
-
|
199
|
-
// 等しくなっているはず。
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
unsigned int free_size;
|
151
|
+
unsigned int free_size;
|
204
|
-
|
205
|
-
// プログラム終了時にはalloc_sizeとfree_sizeの値が
|
206
|
-
|
207
|
-
// 等しくなっているはず。
|
208
|
-
|
209
|
-
|
210
152
|
|
211
153
|
struct memory_block *block_list;
|
212
154
|
|
213
|
-
// 現在獲得している領域のリストポインタ
|
214
|
-
|
215
|
-
// struct memory_block構造体のリンクリストへのポインタ
|
216
|
-
|
217
|
-
|
218
|
-
|
219
155
|
struct free_count *free_list;
|
220
156
|
|
221
|
-
// 過去に解放した領域のリストポインタ
|
222
|
-
|
223
|
-
// struct memory_free構造体のリンクリストへのポインタ
|
224
|
-
|
225
|
-
|
226
|
-
|
227
157
|
struct memory_count *prev;
|
228
158
|
|
229
|
-
// 前のstruct memory_countへのポインタ
|
230
|
-
|
231
|
-
// struct memory_count構造体のリンクリストを作るためのポインタ
|
232
|
-
|
233
|
-
|
234
|
-
|
235
159
|
struct memory_count *next;
|
236
160
|
|
237
|
-
// 次のstruct memory_countへのポインタ
|
238
|
-
|
239
|
-
// struct memory_count構造体のリンクリストを作るためのポインタ
|
240
|
-
|
241
161
|
};
|
242
162
|
|
243
163
|
|
244
164
|
|
245
165
|
//メモリ管理用ブロック
|
246
166
|
|
247
|
-
//1回のdbg_malloc()呼び出しごとに1個作成され,
|
248
|
-
|
249
|
-
//対応するmemory_countのblock_listメンバに接続される
|
250
|
-
|
251
167
|
struct memory_block{
|
252
168
|
|
253
|
-
int id;
|
169
|
+
int id;
|
254
|
-
|
170
|
+
|
255
|
-
int size;
|
171
|
+
int size;
|
256
|
-
|
172
|
+
|
257
|
-
time_t t;
|
173
|
+
time_t t;
|
258
|
-
|
259
|
-
|
260
174
|
|
261
175
|
struct memory_count *mcp;
|
262
176
|
|
263
|
-
// バックポインタ,自分がぶら下がっているstruct memory_count構造体への
|
264
|
-
|
265
|
-
// ポインタ,「のれん状の付け根」へのポインタ
|
266
|
-
|
267
|
-
// dbg_free()の呼び出しの時に,対応するstruct memory_block構造体の検索後,
|
268
|
-
|
269
|
-
// 自分がぶら下がっているstruct memory_count構造体の付け根となる
|
270
|
-
|
271
|
-
// struct memory_count構造体を逆引きするのに使わられる
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
struct memory_block *prev;
|
177
|
+
struct memory_block *prev;
|
276
|
-
|
277
|
-
// struct memory_count構造体からのblock_listのリンク
|
278
|
-
|
279
|
-
// struct memory_count構造体からのリンクリストのためのprevポインタ
|
280
|
-
|
281
|
-
// struct memory_block構造体を削除する際には,リンクリストの途中から
|
282
|
-
|
283
|
-
// 削除するので,prevポインタを持たせる必要がある
|
284
|
-
|
285
|
-
|
286
178
|
|
287
179
|
struct memory_block *next;
|
288
180
|
|
289
|
-
// struct memory_countのblock_listのリンク
|
290
|
-
|
291
|
-
// struct memory_count構造体からのリンクリストのためのnextポインタ
|
292
|
-
|
293
|
-
|
294
|
-
|
295
181
|
struct memory_block *hash_prev;
|
296
182
|
|
297
|
-
// ハッシュ用のリンク
|
298
|
-
|
299
|
-
// struct memory_block構造体の ハッシュ用のポインタ
|
300
|
-
|
301
|
-
// dbg_free()によってstruct memory_block構造体を削除する際には,リンクリストの途中から
|
302
|
-
|
303
|
-
// 削除するので,prevポインタを持たせる必要がある
|
304
|
-
|
305
|
-
|
306
|
-
|
307
183
|
struct memory_block *hash_next;
|
308
184
|
|
309
|
-
|
185
|
+
|
310
|
-
|
311
|
-
// struct memory_block構造体の ハッシュ用のポインタ
|
312
|
-
|
313
|
-
|
314
186
|
|
315
187
|
void *data;
|
316
188
|
|
317
|
-
// malloc()によって確保されたメモリ領域へのポインタ
|
318
|
-
|
319
|
-
// dbg_malloc()呼びだし時には,必要なメモリ領域と管理領域
|
320
|
-
|
321
|
-
// (struct memory_block構造体用の領域)はは別々に獲得して,
|
322
|
-
|
323
|
-
// struct memory_block構造体のdataメンバが実際のメモリ領域を指すことになる
|
324
|
-
|
325
189
|
};
|
326
190
|
|
327
191
|
|
328
192
|
|
329
193
|
//free()管理用のブロック
|
330
194
|
|
331
|
-
//どのmalloc()がどのfree()で解放されたかの統計情報を管理する。
|
332
|
-
|
333
|
-
//対応するmemory_countのfree listメンバに接続される
|
334
|
-
|
335
195
|
struct free_count{
|
336
196
|
|
337
|
-
const char *file;
|
197
|
+
const char *file;
|
338
|
-
|
339
|
-
|
340
|
-
|
198
|
+
|
341
|
-
int line;
|
199
|
+
int line;
|
342
|
-
|
343
|
-
|
344
|
-
|
200
|
+
|
345
|
-
unsigned int count;
|
201
|
+
unsigned int count;
|
346
|
-
|
347
|
-
|
202
|
+
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
unsigned int size;
|
203
|
+
unsigned int size;
|
352
|
-
|
204
|
+
|
353
|
-
struct free_count *next;
|
205
|
+
struct free_count *next;
|
354
206
|
|
355
207
|
};
|
356
208
|
|
@@ -358,10 +210,6 @@
|
|
358
210
|
|
359
211
|
//malloc()統計情報となる変数の型宣言
|
360
212
|
|
361
|
-
//関数と変数名はグローバルになってしまうため,衝突を防止する目的で,
|
362
|
-
|
363
|
-
//dbg_という接頭語を付けている
|
364
|
-
|
365
213
|
extern unsigned int dbg_malloc_count; //malloc()の呼び出し回数
|
366
214
|
|
367
215
|
extern unsigned int dbg_free_count; //free()の呼び出し回数
|
@@ -378,27 +226,7 @@
|
|
378
226
|
|
379
227
|
|
380
228
|
|
381
|
-
|
382
|
-
|
383
|
-
//struct memory_block検索用のハッシュ
|
384
|
-
|
385
|
-
//アドレスはたぶん4バイトアラインメントされているので
|
386
|
-
|
387
|
-
//下2桁は必ず4の倍数になってしまう。
|
388
|
-
|
389
|
-
//ハッシュ,ハッシュ関数には注意すること
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
//dbg_free()が呼び出し時に,引数で与えられたメモリ領域に
|
394
|
-
|
395
|
-
//対応するstruct memory_block構造体を検索するために利用される
|
396
|
-
|
397
|
-
//
|
398
|
-
|
399
|
-
//
|
400
|
-
|
401
|
-
#define DBG_HASH_NUM 997
|
229
|
+
#define DBG_HASH_NUM 997
|
402
230
|
|
403
231
|
#define DBG_HASH_FUNC(p)\
|
404
232
|
|
@@ -416,24 +244,6 @@
|
|
416
244
|
|
417
245
|
extern struct memory_block *dbg_malloc_block_hash[DBG_HASH_NUM];
|
418
246
|
|
419
|
-
//型宣言されているdbg_malloc_block_hash[]には,struct memory_block構造体の
|
420
|
-
|
421
|
-
//リンクリスト構造体へのポインタを格納する
|
422
|
-
|
423
|
-
//struct memory_block構造体は,struct memory_count構造体にのれん状に
|
424
|
-
|
425
|
-
//ぶら下がっているリンクリストは別に,ハッシュ用の独立したリンクリストを
|
426
|
-
|
427
|
-
//構築する。
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
//ハッシュの個数文(この場合は997個)のリンクリストが存在する。
|
432
|
-
|
433
|
-
//それらの先頭へのポインタを配列dbg_malloc_block_hash[]で管理している
|
434
|
-
|
435
|
-
//
|
436
|
-
|
437
247
|
|
438
248
|
|
439
249
|
// 初期化用関数のプロトタイプ宣言
|