質問編集履歴
1
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -27,15 +27,15 @@
|
|
27
27
|
#include <string.h>
|
28
28
|
|
29
29
|
//必要な関数のプロトタイプ宣言
|
30
|
-
void *dbg_malloc(size_t size,const char *file,int line);
|
30
|
+
void *dbg_malloc(size_t size,const char *file,int line);
|
31
31
|
|
32
|
-
void *dbg_calloc(size_t number,size_t size,const char *file,int line);
|
32
|
+
void *dbg_calloc(size_t number,size_t size,const char *file,int line);
|
33
33
|
|
34
|
-
void *dbg_realloc(void *ptr,size_t size,const char *file,int line);
|
34
|
+
void *dbg_realloc(void *ptr,size_t size,const char *file,int line); り
|
35
35
|
|
36
|
-
char *dbg_strdup(const char *str,const char *file,int line);
|
36
|
+
char *dbg_strdup(const char *str,const char *file,int line);
|
37
37
|
|
38
|
-
void dbg_free(void *ptr,const char *file,int line);
|
38
|
+
void dbg_free(void *ptr,const char *file,int line);
|
39
39
|
|
40
40
|
//メモリ管理情報の出力用の3つの関数を宣言
|
41
41
|
void *dbg_print_alloc_count(FILE *fp);
|
@@ -43,9 +43,6 @@
|
|
43
43
|
void *dbg_print_all_alloc_block(FILE *fp);
|
44
44
|
|
45
45
|
#ifndef DBG_MALLOC_NOCHANGE
|
46
|
-
//DBG_MALLOC_NOCHANGEの定義/未定義を切り替えることで、
|
47
|
-
//#defineの有効,無効を切り替える
|
48
|
-
|
49
46
|
#define malloc(s) dbg_malloc((s),__FILE__,__LINE__)
|
50
47
|
#define calloc(n,s) dbg_calloc((n),(s),__FILE__,__LINE__)
|
51
48
|
#define realloc(p,s) dbg_realloc((p),(s),__FILE__,__LINE__)
|
@@ -53,7 +50,7 @@
|
|
53
50
|
#define free(p) dbg_free((p),__FILE__,__LINE__)
|
54
51
|
#endif
|
55
52
|
|
56
|
-
#endif
|
53
|
+
#endif
|
57
54
|
|
58
55
|
|
59
56
|
メモリ管理用ライブラリ(dbg_malloc_p.h)
|
@@ -67,119 +64,46 @@
|
|
67
64
|
//struct memory_block構造体のメンバにtime_t型を利用しているためです
|
68
65
|
|
69
66
|
#include "dbg_malloc.h"
|
70
|
-
//#define DBG_MALLOC_NOCHANGEの定義を行ってから"dbg_malloc.h"を
|
71
|
-
//インクルードをしている。この定義により,
|
72
|
-
//dbg_malloc()内部のmalloc()やfree()の#define が無効になる。
|
73
|
-
|
74
|
-
//ライブラリ本体で,、元のmalloc()とfree()を呼び出す必要があるため,
|
75
|
-
//malloc()とfree()がdbg_malloc()とdbg_free()に置き換わらないように,
|
76
|
-
//#define が無効にしている。
|
77
|
-
|
78
|
-
|
79
|
-
//これ以降必要な構造体struct memory_count,struct memory_block,
|
80
|
-
//struct free_countを定義している
|
81
67
|
//ソース中でdbg_malloc()を利用している場所ごとに作成される構造体
|
82
68
|
struct memory_count{
|
83
|
-
const char *file;
|
69
|
+
const char *file;
|
84
|
-
int line;
|
70
|
+
int line;
|
85
|
-
int number;
|
71
|
+
int number;
|
86
|
-
// プログラム終了時にはこの値が0になっていないときは
|
87
|
-
// メモリの解放忘れがあtることを示している
|
88
|
-
|
89
|
-
unsigned int alloc_count
|
72
|
+
unsigned int alloc_count
|
90
|
-
// 対応するdbg_malloc()が呼ばれた回数を表す
|
91
|
-
|
92
|
-
unsigned int free_count;
|
73
|
+
unsigned int free_count;
|
93
|
-
// 対応するdbg_free()が呼ばれた回数を表す
|
94
|
-
|
95
|
-
int size;
|
74
|
+
int size;
|
96
|
-
// プログラム終了時にはこの値が0になっている
|
97
|
-
|
98
|
-
unsigned int alloc_size;
|
75
|
+
unsigned int alloc_size;
|
99
|
-
// プログラム終了時にはalloc_sizeとfree_sizeの値が
|
100
|
-
// 等しくなっているはず。
|
101
|
-
|
102
|
-
unsigned int free_size;
|
76
|
+
unsigned int free_size;
|
103
|
-
// プログラム終了時にはalloc_sizeとfree_sizeの値が
|
104
|
-
// 等しくなっているはず。
|
105
|
-
|
106
77
|
struct memory_block *block_list;
|
107
|
-
// 現在獲得している領域のリストポインタ
|
108
|
-
// struct memory_block構造体のリンクリストへのポインタ
|
109
|
-
|
110
78
|
struct free_count *free_list;
|
111
|
-
// 過去に解放した領域のリストポインタ
|
112
|
-
// struct memory_free構造体のリンクリストへのポインタ
|
113
|
-
|
114
79
|
struct memory_count *prev;
|
115
|
-
// 前のstruct memory_countへのポインタ
|
116
|
-
// struct memory_count構造体のリンクリストを作るためのポインタ
|
117
|
-
|
118
80
|
struct memory_count *next;
|
119
|
-
// 次のstruct memory_countへのポインタ
|
120
|
-
// struct memory_count構造体のリンクリストを作るためのポインタ
|
121
81
|
};
|
122
82
|
|
123
83
|
//メモリ管理用ブロック
|
124
|
-
//1回のdbg_malloc()呼び出しごとに1個作成され,
|
125
|
-
//対応するmemory_countのblock_listメンバに接続される
|
126
84
|
struct memory_block{
|
127
|
-
int id;
|
85
|
+
int id;
|
128
|
-
int size;
|
86
|
+
int size;
|
129
|
-
time_t t;
|
87
|
+
time_t t;
|
130
|
-
|
131
88
|
struct memory_count *mcp;
|
132
|
-
// バックポインタ,自分がぶら下がっているstruct memory_count構造体への
|
133
|
-
// ポインタ,「のれん状の付け根」へのポインタ
|
134
|
-
// dbg_free()の呼び出しの時に,対応するstruct memory_block構造体の検索後,
|
135
|
-
// 自分がぶら下がっているstruct memory_count構造体の付け根となる
|
136
|
-
// struct memory_count構造体を逆引きするのに使わられる
|
137
|
-
|
138
|
-
struct memory_block *prev;
|
89
|
+
struct memory_block *prev;
|
139
|
-
// struct memory_count構造体からのblock_listのリンク
|
140
|
-
// struct memory_count構造体からのリンクリストのためのprevポインタ
|
141
|
-
// struct memory_block構造体を削除する際には,リンクリストの途中から
|
142
|
-
// 削除するので,prevポインタを持たせる必要がある
|
143
|
-
|
144
90
|
struct memory_block *next;
|
145
|
-
// struct memory_countのblock_listのリンク
|
146
|
-
// struct memory_count構造体からのリンクリストのためのnextポインタ
|
147
|
-
|
148
91
|
struct memory_block *hash_prev;
|
149
|
-
// ハッシュ用のリンク
|
150
|
-
// struct memory_block構造体の ハッシュ用のポインタ
|
151
|
-
// dbg_free()によってstruct memory_block構造体を削除する際には,リンクリストの途中から
|
152
|
-
// 削除するので,prevポインタを持たせる必要がある
|
153
|
-
|
154
92
|
struct memory_block *hash_next;
|
155
|
-
|
93
|
+
|
156
|
-
// struct memory_block構造体の ハッシュ用のポインタ
|
157
|
-
|
158
94
|
void *data;
|
159
|
-
// malloc()によって確保されたメモリ領域へのポインタ
|
160
|
-
// dbg_malloc()呼びだし時には,必要なメモリ領域と管理領域
|
161
|
-
// (struct memory_block構造体用の領域)はは別々に獲得して,
|
162
|
-
// struct memory_block構造体のdataメンバが実際のメモリ領域を指すことになる
|
163
95
|
};
|
164
96
|
|
165
97
|
//free()管理用のブロック
|
166
|
-
//どのmalloc()がどのfree()で解放されたかの統計情報を管理する。
|
167
|
-
//対応するmemory_countのfree listメンバに接続される
|
168
98
|
struct free_count{
|
169
|
-
const char *file;
|
99
|
+
const char *file;
|
170
|
-
|
171
|
-
int line;
|
100
|
+
int line;
|
172
|
-
|
173
|
-
unsigned int count;
|
101
|
+
unsigned int count;
|
174
|
-
// dbg_free()が呼ばれた回数
|
175
|
-
|
176
|
-
unsigned int size;
|
102
|
+
unsigned int size;
|
177
|
-
struct free_count *next;
|
103
|
+
struct free_count *next;
|
178
104
|
};
|
179
105
|
|
180
106
|
//malloc()統計情報となる変数の型宣言
|
181
|
-
//関数と変数名はグローバルになってしまうため,衝突を防止する目的で,
|
182
|
-
//dbg_という接頭語を付けている
|
183
107
|
extern unsigned int dbg_malloc_count; //malloc()の呼び出し回数
|
184
108
|
extern unsigned int dbg_free_count; //free()の呼び出し回数
|
185
109
|
extern unsigned int dbg_malloc_number; //現在のメモリ取得量
|
@@ -188,17 +112,7 @@
|
|
188
112
|
extern struct memory_count *dbg_memory_count_head; //リストの先端
|
189
113
|
extern struct memory_count *dbg_memory_count_tail; //リストの終端
|
190
114
|
|
191
|
-
|
192
|
-
//struct memory_block検索用のハッシュ
|
193
|
-
//アドレスはたぶん4バイトアラインメントされているので
|
194
|
-
//下2桁は必ず4の倍数になってしまう。
|
195
|
-
//ハッシュ,ハッシュ関数には注意すること
|
196
|
-
|
197
|
-
//dbg_free()が呼び出し時に,引数で与えられたメモリ領域に
|
198
|
-
//対応するstruct memory_block構造体を検索するために利用される
|
199
|
-
//
|
200
|
-
//
|
201
|
-
#define DBG_HASH_NUM 997
|
115
|
+
#define DBG_HASH_NUM 997
|
202
116
|
#define DBG_HASH_FUNC(p)\
|
203
117
|
((\
|
204
118
|
(((unsigned long int)(p) >>4) & 0xf)+\
|
@@ -207,16 +121,7 @@
|
|
207
121
|
|
208
122
|
// ハッシュ用配列
|
209
123
|
extern struct memory_block *dbg_malloc_block_hash[DBG_HASH_NUM];
|
210
|
-
//型宣言されているdbg_malloc_block_hash[]には,struct memory_block構造体の
|
211
|
-
//リンクリスト構造体へのポインタを格納する
|
212
|
-
//struct memory_block構造体は,struct memory_count構造体にのれん状に
|
213
|
-
//ぶら下がっているリンクリストは別に,ハッシュ用の独立したリンクリストを
|
214
|
-
//構築する。
|
215
124
|
|
216
|
-
//ハッシュの個数文(この場合は997個)のリンクリストが存在する。
|
217
|
-
//それらの先頭へのポインタを配列dbg_malloc_block_hash[]で管理している
|
218
|
-
//
|
219
|
-
|
220
125
|
// 初期化用関数のプロトタイプ宣言
|
221
126
|
void dbg_init();
|
222
127
|
|