Browse code

- added DBG_QM_MALLOC, when defined qm_malloc & friends (pkg_, shm_) record and display a lot of debuging info (line no, file name, function name); also some extra checks are done (trying to free a pointer twice, trying to free a pointer allocated by other malloc)

Andrei Pelinescu-Onciul authored on 04/12/2001 18:10:50
Showing 5 changed files
... ...
@@ -33,8 +33,14 @@ NAME=ser
33 33
 # PKG_MALLOC uses a faster malloc (exclusive w/ USE_SHM_MEM)
34 34
 # USE_SHM_MEM all pkg_malloc => shm_malloc (most mallocs use a common sh. mem.
35 35
 #           segment); don't define PKG_MALLOC if you want this!
36
+#  DBG_QM_MALLOC - qm_malloc debug code, will cause pkg_malloc and shm_malloc
37
+#                  to keep and display lot of debuging information: file name,
38
+#                  function, line number of malloc/free call for each block,
39
+#                  extra error checking (trying to free the same pointer
40
+#                  twice, trying to free a pointer alloc'ed with a different
41
+#                  malloc etc.)
36 42
 DEFS=-DTHREAD -DNOCR -DMACROEATER -DDNS_IP_HACK  -DSHM_MEM \
37
-	 -DPKG_MALLOC #-DNO_DEBUG
43
+	 -DPKG_MALLOC -DDBG_QM_MALLOC#-DNO_DEBUG
38 44
 # -DUSE_SHM_MEM
39 45
 #-DNO_DEBUG 
40 46
 #-DPKG_MALLOC
... ...
@@ -15,16 +15,28 @@
15 15
 extern struct qm_block* mem_block;
16 16
 
17 17
 
18
-#define pkg_malloc(s) qm_malloc(mem_block, s)
19
-#define pkg_free(p)   qm_free(mem_block, p)
18
+#ifdef DBG_QM_MALLOC
19
+
20
+#define pkg_malloc(s) qm_malloc(mem_block, (s),__FILE__, __FUNCTION__, \
21
+								__LINE__);
22
+#define pkg_free(p)   qm_free(mem_block, (p), __FILE__,  __FUNCTION__, \
23
+								__LINE__);
24
+
25
+#else
26
+
27
+#define pkg_malloc(s) qm_malloc(mem_block, (s))
28
+#define pkg_free(p)   qm_free(mem_block, (p))
29
+
30
+#endif
31
+
20 32
 #define pkg_status()  qm_status(mem_block)
21 33
 
22 34
 #elif defined(SHM_MEM) && defined(USE_SHM_MEM)
23 35
 
24 36
 #include "shm_mem.h"
25 37
 
26
-#define pkg_malloc(s) shm_malloc(s)
27
-#define pkg_free(p)   shm_free(p)
38
+#define pkg_malloc(s) shm_malloc((s))
39
+#define pkg_free(p)   shm_free((p))
28 40
 #define pkg_status()  shm_status()
29 41
 
30 42
 #else
... ...
@@ -32,10 +44,10 @@ extern struct qm_block* mem_block;
32 32
 #include <stdlib.h>
33 33
 
34 34
 #define pkg_malloc(s) \
35
-	(  { void *v; v=malloc(s); \
36
-	   DBG("malloc %x size %d end %x\n", v, s, (unsigned int)v+s);\
35
+	(  { void *v; v=malloc((s)); \
36
+	   DBG("malloc %x size %d end %x\n", v, s, (unsigned int)v+(s));\
37 37
 	   v; } )
38
-#define pkg_free(p)  do{ DBG("free %x\n", p); free(p); }while(0);
38
+#define pkg_free(p)  do{ DBG("free %x\n", (p)); free((p)); }while(0);
39 39
 #define pkg_status()
40 40
 
41 41
 #endif
... ...
@@ -109,7 +109,12 @@ static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
109 109
 
110 110
 
111 111
 
112
+#ifdef DBG_QM_MALLOC
113
+void* qm_malloc(struct qm_block* qm, unsigned int size, char* file, char* func,
114
+					unsigned int line)
115
+#else
112 116
 void* qm_malloc(struct qm_block* qm, unsigned int size)
117
+#endif
113 118
 {
114 119
 	struct qm_frag* f;
115 120
 	struct qm_frag_end* end;
... ...
@@ -117,6 +122,10 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
117 117
 	unsigned int rest;
118 118
 	unsigned int overhead;
119 119
 	
120
+#ifdef DBG_QM_MALLOC
121
+	DBG("qm_malloc(%x, %d) called from %s: %s(%d)\n", qm, size, file, func,
122
+			line);
123
+#endif
120 124
 	/*size must be a multiple of 8*/
121 125
 	size=(size%8)?(size+8)/8*8:size;
122 126
 	if (size>(qm->size-qm->real_used)) return 0;
... ...
@@ -142,6 +151,12 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
142 142
 				n->size=rest-overhead;
143 143
 				FRAG_END(n)->size=n->size;
144 144
 				qm->real_used+=overhead;
145
+#ifdef DBG_QM_MALLOC
146
+				/* frag created by malloc, mark it*/
147
+				n->file=file;
148
+				n->func="frag. from qm_malloc";
149
+				n->line=line;
150
+#endif
145 151
 				/* reinsert n in free list*/
146 152
 				qm_insert_free(qm, n);
147 153
 			}else{
... ...
@@ -151,6 +166,11 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
151 151
 			qm->used+=f->size;
152 152
 			if (qm->max_real_used<qm->real_used)
153 153
 				qm->max_real_used=qm->real_used;
154
+#ifdef DBG_QM_MALLOC
155
+			f->file=file;
156
+			f->func=func;
157
+			f->line=line;
158
+#endif
154 159
 			return (char*)f+sizeof(struct qm_frag);
155 160
 		}
156 161
 	}
... ...
@@ -159,7 +179,12 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
159 159
 
160 160
 
161 161
 
162
+#ifdef DBG_QM_MALLOC
163
+void qm_free(struct qm_block* qm, void* p, char* file, char* func, 
164
+				unsigned int line)
165
+#else
162 166
 void qm_free(struct qm_block* qm, void* p)
167
+#endif
163 168
 {
164 169
 	struct qm_frag* f;
165 170
 	struct qm_frag* prev;
... ...
@@ -168,12 +193,29 @@ void qm_free(struct qm_block* qm, void* p)
168 168
 	unsigned int overhead;
169 169
 	unsigned int size;
170 170
 
171
+#ifdef DBG_QM_MALLOC
172
+	DBG("qm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
173
+	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
174
+		DBG("qm_free: ERROR!!!: bad pointer %x (out of memory block!) - "
175
+				"aborting\n", p);
176
+		abort();
177
+	}
178
+#endif
171 179
 	if (p==0) {
172 180
 		DBG("WARNING:qm_free: free(0) called\n");
173 181
 		return;
174 182
 	}
175 183
 	prev=next=0;
176 184
 	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
185
+#ifdef DBG_QM_MALLOC
186
+	if (f->u.is_free){
187
+		DBG("qm_free: ERROR!!!: freeing already freed pointer, first free:"
188
+				" %s: %s(%d) - aborting\n", f->file, f->func, f->line);
189
+		abort();
190
+	}
191
+	DBG("qm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
192
+			f->line);
193
+#endif
177 194
 	overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
178 195
 	next=FRAG_NEXT(f);
179 196
 	size=f->size;
... ...
@@ -200,6 +242,11 @@ void qm_free(struct qm_block* qm, void* p)
200 200
 	}
201 201
 	f->size=size;
202 202
 	FRAG_END(f)->size=f->size;
203
+#ifdef DBG_QM_MALLOC
204
+	f->file=file;
205
+	f->func=func;
206
+	f->line=line;
207
+#endif
203 208
 	qm_insert_free(qm, f);
204 209
 }
205 210
 
... ...
@@ -218,14 +265,24 @@ void qm_status(struct qm_block* qm)
218 218
 	
219 219
 	DBG("dumping all fragments:\n");
220 220
 	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
221
-			,i++)
221
+			,i++){
222 222
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
223 223
 				(char*)f+sizeof(struct qm_frag), f->size);
224
+#ifdef DBG_QM_MALLOC
225
+		DBG("            %s from %s: %s(%d)\n",
226
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
227
+#endif
228
+	}
224 229
 	DBG("dumping free list:\n");
225 230
 	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
226
-			i++)
231
+			i++){
227 232
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
228 233
 				(char*)f+sizeof(struct qm_frag), f->size);
234
+#ifdef DBG_QM_MALLOC
235
+		DBG("            %s from %s: %s(%d)\n", 
236
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
237
+#endif
238
+	}
229 239
 	DBG("-----------------------------\n");
230 240
 }
231 241
 
... ...
@@ -13,6 +13,11 @@ struct qm_frag{
13 13
 		struct qm_frag* nxt_free;
14 14
 		int is_free;
15 15
 	}u;
16
+#ifdef DBG_QM_MALLOC
17
+	char* file;
18
+	char* func;
19
+	unsigned int line;
20
+#endif
16 21
 };
17 22
 
18 23
 struct qm_frag_end{
... ...
@@ -37,8 +42,21 @@ struct qm_block{
37 37
 
38 38
 
39 39
 struct qm_block* qm_malloc_init(char* address, unsigned int size);
40
+
41
+#ifdef DBG_QM_MALLOC
42
+void* qm_malloc(struct qm_block*, unsigned int size, char* file, char* func, 
43
+					unsigned int line);
44
+#else
40 45
 void* qm_malloc(struct qm_block*, unsigned int size);
46
+#endif
47
+
48
+#ifdef DBG_QM_MALLOC
49
+void  qm_free(struct qm_block*, void* p, char* file, char* func, 
50
+				unsigned int line);
51
+#else
41 52
 void  qm_free(struct qm_block*, void* p);
53
+#endif
54
+
42 55
 void  qm_status(struct qm_block*);
43 56
 
44 57
 
... ...
@@ -80,31 +80,63 @@ again:
80 80
 }
81 81
 
82 82
 
83
-inline static void* shm_malloc(unsigned int size)
84
-{
85
-	void *p;
86
-	
87
-	/*if (shm_lock()==0){*/
88
-		shm_lock();
89
-		p=qm_malloc(shm_block, size);
90
-		shm_unlock();
91
-	/*
92
-	}else{
93
-		p=0;
94
-	}*/
95
-	return p;
96
-}
83
+
84
+#ifdef DBG_QM_MALLOC
85
+#define shm_malloc(size) \
86
+({\
87
+	void *p;\
88
+	\
89
+	/*if (shm_lock()==0){*/\
90
+		shm_lock();\
91
+		p=qm_malloc(shm_block, (size), __FILE__, __FUNCTION__, __LINE__);\
92
+		shm_unlock();\
93
+	/* \
94
+	}else{ \
95
+		p=0;\
96
+	}*/ \
97
+	 p; \
98
+})
99
+
100
+
101
+
102
+#define shm_free(p) \
103
+do { \
104
+		shm_lock(); \
105
+		qm_free(shm_block, (p), __FILE__, __FUNCTION__, __LINE__); \
106
+		shm_unlock(); \
107
+}while(0)
108
+
109
+
110
+#else
111
+
112
+
113
+#define shm_malloc(size) \
114
+({\
115
+	void *p;\
116
+	\
117
+	/*if (shm_lock()==0){*/\
118
+		shm_lock();\
119
+		p=qm_malloc(shm_block, (size));\
120
+		shm_unlock();\
121
+	/* \
122
+	}else{ \
123
+		p=0;\
124
+	}*/ \
125
+	 p; \
126
+})
97 127
 
98 128
 
99 129
 
100 130
 #define shm_free(p) \
101 131
 do { \
102 132
 		shm_lock(); \
103
-		qm_free(shm_block, p); \
133
+		qm_free(shm_block, (p)); \
104 134
 		shm_unlock(); \
105 135
 }while(0)
106 136
 
107 137
 
138
+#endif
139
+
108 140
 
109 141
 #define shm_status() \
110 142
 do { \