Browse code

performance and bug fixes

Jiri Kuthan authored on 07/01/2002 04:39:49
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,355 +0,0 @@
1
-/* $Id$
2
- *
3
- */
4
-
5
-#define q_malloc
6
-#ifdef q_malloc
7
-
8
-#include "q_malloc.h"
9
-#include "dprint.h"
10
-
11
-
12
-/*usefull macros*/
13
-#define FRAG_END(f)  \
14
-			((struct qm_frag_end*)((char*)(f)+sizeof(struct qm_frag)+ \
15
-								   (f)->size))
16
-
17
-#define FRAG_NEXT(f) \
18
-			((struct qm_frag*)((char*)(f)+sizeof(struct qm_frag)+(f)->size+ \
19
-							   sizeof(struct qm_frag_end)))
20
-			
21
-#define FRAG_PREV(f) \
22
-		( (struct qm_frag*) ( ((char*)(f)-sizeof(struct qm_frag_end))- \
23
-		((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))->size- \
24
-			sizeof(struct qm_frag) ) )
25
-
26
-#define PREV_FRAG_END(f) \
27
-		((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))
28
-
29
-#ifdef DBG_QM_MALLOC
30
-#define ST_CHECK_PATTERN   0xf0f0f0f0
31
-#define END_CHECK_PATTERN1 0xc0c0c0c0
32
-#define END_CHECK_PATTERN2 0xabcdefed
33
-
34
-
35
-static  void qm_debug_frag(struct qm_block* qm, struct qm_frag* f)
36
-{
37
-	if (f->check!=ST_CHECK_PATTERN){
38
-		LOG(L_CRIT, "BUG: qm_*: fragm. %x beginning overwritten(%x)!\n",
39
-				f, f->check);
40
-		qm_status(qm);
41
-		abort();
42
-	};
43
-	if ((FRAG_END(f)->check1!=END_CHECK_PATTERN1)||
44
-		(FRAG_END(f)->check2!=END_CHECK_PATTERN2)){
45
-		LOG(L_CRIT, "BUG: qm_*: fragm. %x end overwritten(%x, %x)!\n",
46
-				f, FRAG_END(f)->check1, FRAG_END(f)->check2);
47
-		qm_status(qm);
48
-		abort();
49
-	}
50
-	if ((f>qm->first_frag)&&
51
-			((PREV_FRAG_END(f)->check1!=END_CHECK_PATTERN1) ||
52
-				(PREV_FRAG_END(f)->check2!=END_CHECK_PATTERN2) ) ){
53
-		LOG(L_CRIT, "BUG: qm_*: prev. fragm. tail overwritten(%x, %x)[%x]!\n",
54
-				PREV_FRAG_END(f)->check1, PREV_FRAG_END(f)->check2, f);
55
-		qm_status(qm);
56
-		abort();
57
-	}
58
-}
59
-#endif
60
-
61
-
62
-
63
-/* init malloc and return a qm_block*/
64
-struct qm_block* qm_malloc_init(char* address, unsigned int size)
65
-{
66
-	char* start;
67
-	char* end;
68
-	struct qm_block* qm;
69
-	unsigned int init_overhead;
70
-	
71
-	/* make address and size multiple of 8*/
72
-	start=(char*)( ((unsigned int)address%8)?((unsigned int)address+8)/8*8:
73
-			(unsigned int)address);
74
-	if (size<start-address) return 0;
75
-	size-=(start-address);
76
-	if (size <8) return 0;
77
-	size=(size%8)?(size-8)/8*8:size;
78
-	
79
-	init_overhead=sizeof(struct qm_block)+sizeof(struct qm_frag)+
80
-		sizeof(struct qm_frag_end);
81
-	if (size < init_overhead)
82
-	{
83
-		/* not enough mem to create our control structures !!!*/
84
-		return 0;
85
-	}
86
-	end=start+size;
87
-	qm=(struct qm_block*)start;
88
-	memset(qm, 0, sizeof(struct qm_block));
89
-	size-=init_overhead;
90
-	qm->size=size;
91
-	qm->real_used=init_overhead;
92
-	qm->max_real_used=qm->real_used;
93
-	
94
-	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
95
-	qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
96
-	/* init initial fragment*/
97
-	qm->first_frag->size=size;
98
-	qm->first_frag->u.nxt_free=&(qm->free_lst);
99
-	qm->last_frag_end->size=size;
100
-	qm->last_frag_end->prev_free=&(qm->free_lst);
101
-#ifdef DBG_QM_MALLOC
102
-	qm->first_frag->check=ST_CHECK_PATTERN;
103
-	qm->last_frag_end->check1=END_CHECK_PATTERN1;
104
-	qm->last_frag_end->check2=END_CHECK_PATTERN2;
105
-#endif
106
-	/* init free_lst* */
107
-	qm->free_lst.u.nxt_free=qm->first_frag;
108
-	qm->free_lst_end.prev_free=qm->first_frag;
109
-	qm->free_lst.size=0;
110
-	qm->free_lst_end.size=0;
111
-	
112
-	
113
-	return qm;
114
-}
115
-
116
-
117
-static inline void qm_insert_free(struct qm_block* qm, struct qm_frag* frag)
118
-{
119
-	struct qm_frag* f;
120
-	struct qm_frag* prev;
121
-
122
-	for(f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
123
-		if (frag->size < f->size) break;
124
-	}
125
-	/*insert it here*/
126
-	prev=FRAG_END(f)->prev_free;
127
-	prev->u.nxt_free=frag;
128
-	FRAG_END(frag)->prev_free=prev;
129
-	frag->u.nxt_free=f;
130
-	FRAG_END(f)->prev_free=frag;
131
-}
132
-
133
-
134
-
135
-static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
136
-{
137
-	struct qm_frag *prev;
138
-	struct qm_frag *next;
139
-	
140
-	struct qm_frag_end *end;
141
-
142
-	prev=FRAG_END(frag)->prev_free;
143
-	next=frag->u.nxt_free;
144
-	prev->u.nxt_free=next;
145
-	FRAG_END(next)->prev_free=prev;
146
-	
147
-}
148
-
149
-
150
-
151
-#ifdef DBG_QM_MALLOC
152
-void* qm_malloc(struct qm_block* qm, unsigned int size, char* file, char* func,
153
-					unsigned int line)
154
-#else
155
-void* qm_malloc(struct qm_block* qm, unsigned int size)
156
-#endif
157
-{
158
-	struct qm_frag* f;
159
-	struct qm_frag_end* end;
160
-	struct qm_frag* n;
161
-	unsigned int rest;
162
-	unsigned int overhead;
163
-	
164
-#ifdef DBG_QM_MALLOC
165
-	DBG("qm_malloc(%x, %d) called from %s: %s(%d)\n", qm, size, file, func,
166
-			line);
167
-#endif
168
-	/*size must be a multiple of 8*/
169
-	size=(size%8)?(size+8)/8*8:size;
170
-	if (size>(qm->size-qm->real_used)) return 0;
171
-	if (qm->free_lst.u.nxt_free==&(qm->free_lst)) return 0;
172
-	/*search for a suitable free frag*/
173
-	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
174
-		if (f->size>=size){
175
-			/* we found it!*/
176
-			/*detach it from the free list*/
177
-#ifdef DBG_QM_MALLOC
178
-			qm_debug_frag(qm, f);
179
-#endif
180
-			qm_detach_free(qm, f);
181
-			/*mark it as "busy"*/
182
-			f->u.is_free=0;
183
-			
184
-			/*see if we'll use full frag, or we'll split it in 2*/
185
-			rest=f->size-size;
186
-			overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
187
-			if (rest>overhead){
188
-				f->size=size;
189
-				/*split the fragment*/
190
-				end=FRAG_END(f);
191
-				end->size=size;
192
-				n=(struct qm_frag*)((char*)end+sizeof(struct qm_frag_end));
193
-				n->size=rest-overhead;
194
-				FRAG_END(n)->size=n->size;
195
-				qm->real_used+=overhead;
196
-#ifdef DBG_QM_MALLOC
197
-				end->check1=END_CHECK_PATTERN1;
198
-				end->check2=END_CHECK_PATTERN2;
199
-				/* frag created by malloc, mark it*/
200
-				n->file=file;
201
-				n->func="frag. from qm_malloc";
202
-				n->line=line;
203
-				n->check=ST_CHECK_PATTERN;
204
-/*				FRAG_END(n)->check1=END_CHECK_PATTERN1;
205
-				FRAG_END(n)->check2=END_CHECK_PATTERN2; */
206
-#endif
207
-				/* reinsert n in free list*/
208
-				qm_insert_free(qm, n);
209
-			}else{
210
-				/* we cannot split this fragment any more => alloc all of it*/
211
-			}
212
-			qm->real_used+=f->size;
213
-			qm->used+=f->size;
214
-			if (qm->max_real_used<qm->real_used)
215
-				qm->max_real_used=qm->real_used;
216
-#ifdef DBG_QM_MALLOC
217
-			f->file=file;
218
-			f->func=func;
219
-			f->line=line;
220
-			f->check=ST_CHECK_PATTERN;
221
-		/*  FRAG_END(f)->check1=END_CHECK_PATTERN1;
222
-			FRAG_END(f)->check2=END_CHECK_PATTERN2;*/
223
-	DBG("qm_malloc(%x, %d) returns address %x\n", qm, size,
224
-			(char*)f+sizeof(struct qm_frag) );
225
-#endif
226
-			return (char*)f+sizeof(struct qm_frag);
227
-		}
228
-	}
229
-	return 0;
230
-}
231
-
232
-
233
-
234
-#ifdef DBG_QM_MALLOC
235
-void qm_free(struct qm_block* qm, void* p, char* file, char* func, 
236
-				unsigned int line)
237
-#else
238
-void qm_free(struct qm_block* qm, void* p)
239
-#endif
240
-{
241
-	struct qm_frag* f;
242
-	struct qm_frag* prev;
243
-	struct qm_frag* next;
244
-	struct qm_frag_end *end;
245
-	unsigned int overhead;
246
-	unsigned int size;
247
-
248
-#ifdef DBG_QM_MALLOC
249
-	DBG("qm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
250
-	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
251
-		LOG(L_CRIT, "BUG: qm_free: bad pointer %x (out of memory block!) - "
252
-				"aborting\n", p);
253
-		abort();
254
-	}
255
-#endif
256
-	if (p==0) {
257
-		DBG("WARNING:qm_free: free(0) called\n");
258
-		return;
259
-	}
260
-	prev=next=0;
261
-	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
262
-#ifdef DBG_QM_MALLOC
263
-	qm_debug_frag(qm, f);
264
-	if (f->u.is_free){
265
-		LOG(L_CRIT, "BUG: qm_free: freeing already freed pointer,"
266
-				" first free: %s: %s(%d) - aborting\n",
267
-				f->file, f->func, f->line);
268
-		abort();
269
-	}
270
-	DBG("qm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
271
-			f->line);
272
-#endif
273
-	overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
274
-	next=FRAG_NEXT(f);
275
-	size=f->size;
276
-	qm->used-=size;
277
-	qm->real_used-=size;
278
-#ifdef DBG_QM_MALLOC
279
-	qm_debug_frag(qm, f);
280
-#endif
281
-	if (((char*)next < (char*)qm->last_frag_end) &&( next->u.is_free)){
282
-		/* join */
283
-		qm_detach_free(qm, next);
284
-		size+=next->size+overhead;
285
-		qm->real_used-=overhead;
286
-	}
287
-	
288
-	if (f > qm->first_frag){
289
-		prev=FRAG_PREV(f);
290
-		/*	(struct qm_frag*)((char*)f - (struct qm_frag_end*)((char*)f-
291
-								sizeof(struct qm_frag_end))->size);*/
292
-#ifdef DBG_QM_MALLOC
293
-		qm_debug_frag(qm, f);
294
-#endif
295
-		if (prev->u.is_free){
296
-			/*join*/
297
-			qm_detach_free(qm, prev);
298
-			size+=prev->size+overhead;
299
-			qm->real_used-=overhead;
300
-			f=prev;
301
-		}
302
-	}
303
-	f->size=size;
304
-	FRAG_END(f)->size=f->size;
305
-#ifdef DBG_QM_MALLOC
306
-	f->file=file;
307
-	f->func=func;
308
-	f->line=line;
309
-#endif
310
-	qm_insert_free(qm, f);
311
-}
312
-
313
-
314
-
315
-void qm_status(struct qm_block* qm)
316
-{
317
-	struct qm_frag* f;
318
-	int i;
319
-
320
-	LOG(L_INFO, "qm_status (%x):\n", qm);
321
-	LOG(L_INFO, " heap size= %d\n", qm->size);
322
-	LOG(L_INFO, " used= %d, used+overhead=%d, free=%d\n",
323
-			qm->used, qm->real_used, qm->size-qm->real_used);
324
-	LOG(L_INFO, " max used (+overhead)= %d\n", qm->max_real_used);
325
-	
326
-	LOG(L_INFO, "dumping all fragments:\n");
327
-	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
328
-			,i++){
329
-		LOG(L_INFO, "    %3d. %c  address=%x  size=%d\n", i, 
330
-				(f->u.is_free)?'a':'N',
331
-				(char*)f+sizeof(struct qm_frag), f->size);
332
-#ifdef DBG_QM_MALLOC
333
-		LOG(L_INFO, "            %s from %s: %s(%d)\n",
334
-				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
335
-		LOG(L_INFO, "        start check=%x, end check= %x, %x\n",
336
-				f->check, FRAG_END(f)->check1, FRAG_END(f)->check2);
337
-#endif
338
-	}
339
-	DBG("dumping free list:\n");
340
-	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
341
-			i++){
342
-		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
343
-				(char*)f+sizeof(struct qm_frag), f->size);
344
-#ifdef DBG_QM_MALLOC
345
-		DBG("            %s from %s: %s(%d)\n", 
346
-				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
347
-#endif
348
-	}
349
-	LOG(L_INFO, "-----------------------------\n");
350
-}
351
-
352
-
353
-
354
-
355
-#endif
Browse code

- fixed free hash table bug - added even more debug support to qm_malloc (-DDBG_QM_MALLOC)

Andrei Pelinescu-Onciul authored on 11/12/2001 07:45:30
Showing 1 changed files
... ...
@@ -23,8 +23,41 @@
23 23
 		((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))->size- \
24 24
 			sizeof(struct qm_frag) ) )
25 25
 
26
+#define PREV_FRAG_END(f) \
27
+		((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))
28
+
29
+#ifdef DBG_QM_MALLOC
30
+#define ST_CHECK_PATTERN   0xf0f0f0f0
31
+#define END_CHECK_PATTERN1 0xc0c0c0c0
32
+#define END_CHECK_PATTERN2 0xabcdefed
26 33
 
27 34
 
35
+static  void qm_debug_frag(struct qm_block* qm, struct qm_frag* f)
36
+{
37
+	if (f->check!=ST_CHECK_PATTERN){
38
+		LOG(L_CRIT, "BUG: qm_*: fragm. %x beginning overwritten(%x)!\n",
39
+				f, f->check);
40
+		qm_status(qm);
41
+		abort();
42
+	};
43
+	if ((FRAG_END(f)->check1!=END_CHECK_PATTERN1)||
44
+		(FRAG_END(f)->check2!=END_CHECK_PATTERN2)){
45
+		LOG(L_CRIT, "BUG: qm_*: fragm. %x end overwritten(%x, %x)!\n",
46
+				f, FRAG_END(f)->check1, FRAG_END(f)->check2);
47
+		qm_status(qm);
48
+		abort();
49
+	}
50
+	if ((f>qm->first_frag)&&
51
+			((PREV_FRAG_END(f)->check1!=END_CHECK_PATTERN1) ||
52
+				(PREV_FRAG_END(f)->check2!=END_CHECK_PATTERN2) ) ){
53
+		LOG(L_CRIT, "BUG: qm_*: prev. fragm. tail overwritten(%x, %x)[%x]!\n",
54
+				PREV_FRAG_END(f)->check1, PREV_FRAG_END(f)->check2, f);
55
+		qm_status(qm);
56
+		abort();
57
+	}
58
+}
59
+#endif
60
+
28 61
 
29 62
 
30 63
 /* init malloc and return a qm_block*/
... ...
@@ -65,6 +98,11 @@ struct qm_block* qm_malloc_init(char* address, unsigned int size)
65 98
 	qm->first_frag->u.nxt_free=&(qm->free_lst);
66 99
 	qm->last_frag_end->size=size;
67 100
 	qm->last_frag_end->prev_free=&(qm->free_lst);
101
+#ifdef DBG_QM_MALLOC
102
+	qm->first_frag->check=ST_CHECK_PATTERN;
103
+	qm->last_frag_end->check1=END_CHECK_PATTERN1;
104
+	qm->last_frag_end->check2=END_CHECK_PATTERN2;
105
+#endif
68 106
 	/* init free_lst* */
69 107
 	qm->free_lst.u.nxt_free=qm->first_frag;
70 108
 	qm->free_lst_end.prev_free=qm->first_frag;
... ...
@@ -136,6 +174,9 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
136 174
 		if (f->size>=size){
137 175
 			/* we found it!*/
138 176
 			/*detach it from the free list*/
177
+#ifdef DBG_QM_MALLOC
178
+			qm_debug_frag(qm, f);
179
+#endif
139 180
 			qm_detach_free(qm, f);
140 181
 			/*mark it as "busy"*/
141 182
 			f->u.is_free=0;
... ...
@@ -153,10 +194,15 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
153 194
 				FRAG_END(n)->size=n->size;
154 195
 				qm->real_used+=overhead;
155 196
 #ifdef DBG_QM_MALLOC
197
+				end->check1=END_CHECK_PATTERN1;
198
+				end->check2=END_CHECK_PATTERN2;
156 199
 				/* frag created by malloc, mark it*/
157 200
 				n->file=file;
158 201
 				n->func="frag. from qm_malloc";
159 202
 				n->line=line;
203
+				n->check=ST_CHECK_PATTERN;
204
+/*				FRAG_END(n)->check1=END_CHECK_PATTERN1;
205
+				FRAG_END(n)->check2=END_CHECK_PATTERN2; */
160 206
 #endif
161 207
 				/* reinsert n in free list*/
162 208
 				qm_insert_free(qm, n);
... ...
@@ -171,7 +217,11 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
171 217
 			f->file=file;
172 218
 			f->func=func;
173 219
 			f->line=line;
174
-	DBG("qm_malloc(%x, %d) returns address %x\n", qm, size,(char*)f+sizeof(struct qm_frag) );
220
+			f->check=ST_CHECK_PATTERN;
221
+		/*  FRAG_END(f)->check1=END_CHECK_PATTERN1;
222
+			FRAG_END(f)->check2=END_CHECK_PATTERN2;*/
223
+	DBG("qm_malloc(%x, %d) returns address %x\n", qm, size,
224
+			(char*)f+sizeof(struct qm_frag) );
175 225
 #endif
176 226
 			return (char*)f+sizeof(struct qm_frag);
177 227
 		}
... ...
@@ -210,6 +260,7 @@ void qm_free(struct qm_block* qm, void* p)
210 260
 	prev=next=0;
211 261
 	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
212 262
 #ifdef DBG_QM_MALLOC
263
+	qm_debug_frag(qm, f);
213 264
 	if (f->u.is_free){
214 265
 		LOG(L_CRIT, "BUG: qm_free: freeing already freed pointer,"
215 266
 				" first free: %s: %s(%d) - aborting\n",
... ...
@@ -224,6 +275,9 @@ void qm_free(struct qm_block* qm, void* p)
224 275
 	size=f->size;
225 276
 	qm->used-=size;
226 277
 	qm->real_used-=size;
278
+#ifdef DBG_QM_MALLOC
279
+	qm_debug_frag(qm, f);
280
+#endif
227 281
 	if (((char*)next < (char*)qm->last_frag_end) &&( next->u.is_free)){
228 282
 		/* join */
229 283
 		qm_detach_free(qm, next);
... ...
@@ -235,6 +289,9 @@ void qm_free(struct qm_block* qm, void* p)
235 289
 		prev=FRAG_PREV(f);
236 290
 		/*	(struct qm_frag*)((char*)f - (struct qm_frag_end*)((char*)f-
237 291
 								sizeof(struct qm_frag_end))->size);*/
292
+#ifdef DBG_QM_MALLOC
293
+		qm_debug_frag(qm, f);
294
+#endif
238 295
 		if (prev->u.is_free){
239 296
 			/*join*/
240 297
 			qm_detach_free(qm, prev);
... ...
@@ -275,6 +332,8 @@ void qm_status(struct qm_block* qm)
275 332
 #ifdef DBG_QM_MALLOC
276 333
 		LOG(L_INFO, "            %s from %s: %s(%d)\n",
277 334
 				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
335
+		LOG(L_INFO, "        start check=%x, end check= %x, %x\n",
336
+				f->check, FRAG_END(f)->check1, FRAG_END(f)->check2);
278 337
 #endif
279 338
 	}
280 339
 	DBG("dumping free list:\n");
Browse code

- fixed macros ("(",")", do{}while(0)). - small Makefile changes

Andrei Pelinescu-Onciul authored on 08/12/2001 14:20:09
Showing 1 changed files
... ...
@@ -11,15 +11,16 @@
11 11
 
12 12
 /*usefull macros*/
13 13
 #define FRAG_END(f)  \
14
-			((struct qm_frag_end*)((char*)f+sizeof(struct qm_frag)+f->size))
14
+			((struct qm_frag_end*)((char*)(f)+sizeof(struct qm_frag)+ \
15
+								   (f)->size))
15 16
 
16 17
 #define FRAG_NEXT(f) \
17
-			((struct qm_frag*)((char*)f+sizeof(struct qm_frag)+f->size+ \
18
+			((struct qm_frag*)((char*)(f)+sizeof(struct qm_frag)+(f)->size+ \
18 19
 							   sizeof(struct qm_frag_end)))
19 20
 			
20 21
 #define FRAG_PREV(f) \
21
-		( (struct qm_frag*) ( ((char*)f-sizeof(struct qm_frag_end))- \
22
-		((struct qm_frag_end*)((char*)f-sizeof(struct qm_frag_end)))->size- \
22
+		( (struct qm_frag*) ( ((char*)(f)-sizeof(struct qm_frag_end))- \
23
+		((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))->size- \
23 24
 			sizeof(struct qm_frag) ) )
24 25
 
25 26
 
Browse code

- changed some DBGs into LOG(L_CRIT), used only when DBG_QM_MALLOC is defined

Andrei Pelinescu-Onciul authored on 06/12/2001 23:58:35
Showing 1 changed files
... ...
@@ -197,7 +197,7 @@ void qm_free(struct qm_block* qm, void* p)
197 197
 #ifdef DBG_QM_MALLOC
198 198
 	DBG("qm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
199 199
 	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
200
-		DBG("qm_free: ERROR!!!: bad pointer %x (out of memory block!) - "
200
+		LOG(L_CRIT, "BUG: qm_free: bad pointer %x (out of memory block!) - "
201 201
 				"aborting\n", p);
202 202
 		abort();
203 203
 	}
... ...
@@ -210,8 +210,9 @@ void qm_free(struct qm_block* qm, void* p)
210 210
 	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
211 211
 #ifdef DBG_QM_MALLOC
212 212
 	if (f->u.is_free){
213
-		DBG("qm_free: ERROR!!!: freeing already freed pointer, first free:"
214
-				" %s: %s(%d) - aborting\n", f->file, f->func, f->line);
213
+		LOG(L_CRIT, "BUG: qm_free: freeing already freed pointer,"
214
+				" first free: %s: %s(%d) - aborting\n",
215
+				f->file, f->func, f->line);
215 216
 		abort();
216 217
 	}
217 218
 	DBG("qm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
Browse code

- changed malloc status to LOG(L_INFO) - fixed a malloc(0) in tm/sip_msg.c/*cloner (new_uri)

Andrei Pelinescu-Onciul authored on 06/12/2001 23:43:46
Showing 1 changed files
... ...
@@ -258,19 +258,20 @@ void qm_status(struct qm_block* qm)
258 258
 	struct qm_frag* f;
259 259
 	int i;
260 260
 
261
-	DBG("qm_status (%x):\n", qm);
262
-	DBG(" heap size= %d\n", qm->size);
263
-	DBG(" used= %d, used+overhead=%d, free=%d\n",
261
+	LOG(L_INFO, "qm_status (%x):\n", qm);
262
+	LOG(L_INFO, " heap size= %d\n", qm->size);
263
+	LOG(L_INFO, " used= %d, used+overhead=%d, free=%d\n",
264 264
 			qm->used, qm->real_used, qm->size-qm->real_used);
265
-	DBG(" max used (+overhead)= %d\n", qm->max_real_used);
265
+	LOG(L_INFO, " max used (+overhead)= %d\n", qm->max_real_used);
266 266
 	
267
-	DBG("dumping all fragments:\n");
267
+	LOG(L_INFO, "dumping all fragments:\n");
268 268
 	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
269 269
 			,i++){
270
-		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
270
+		LOG(L_INFO, "    %3d. %c  address=%x  size=%d\n", i, 
271
+				(f->u.is_free)?'a':'N',
271 272
 				(char*)f+sizeof(struct qm_frag), f->size);
272 273
 #ifdef DBG_QM_MALLOC
273
-		DBG("            %s from %s: %s(%d)\n",
274
+		LOG(L_INFO, "            %s from %s: %s(%d)\n",
274 275
 				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
275 276
 #endif
276 277
 	}
... ...
@@ -284,7 +285,7 @@ void qm_status(struct qm_block* qm)
284 285
 				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
285 286
 #endif
286 287
 	}
287
-	DBG("-----------------------------\n");
288
+	LOG(L_INFO, "-----------------------------\n");
288 289
 }
289 290
 
290 291
 
Browse code

the famous bug fixed!

Bogdan-Andrei Iancu authored on 05/12/2001 21:09:01
Showing 1 changed files
... ...
@@ -170,6 +170,7 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
170 170
 			f->file=file;
171 171
 			f->func=func;
172 172
 			f->line=line;
173
+	DBG("qm_malloc(%x, %d) returns address %x\n", qm, size,(char*)f+sizeof(struct qm_frag) );
173 174
 #endif
174 175
 			return (char*)f+sizeof(struct qm_frag);
175 176
 		}
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 1 changed files
... ...
@@ -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 122
 	unsigned int rest;
118 123
 	unsigned int overhead;
119 124
 	
125
+#ifdef DBG_QM_MALLOC
126
+	DBG("qm_malloc(%x, %d) called from %s: %s(%d)\n", qm, size, file, func,
127
+			line);
128
+#endif
120 129
 	/*size must be a multiple of 8*/
121 130
 	size=(size%8)?(size+8)/8*8:size;
122 131
 	if (size>(qm->size-qm->real_used)) return 0;
... ...
@@ -142,6 +151,12 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
142 151
 				n->size=rest-overhead;
143 152
 				FRAG_END(n)->size=n->size;
144 153
 				qm->real_used+=overhead;
154
+#ifdef DBG_QM_MALLOC
155
+				/* frag created by malloc, mark it*/
156
+				n->file=file;
157
+				n->func="frag. from qm_malloc";
158
+				n->line=line;
159
+#endif
145 160
 				/* reinsert n in free list*/
146 161
 				qm_insert_free(qm, n);
147 162
 			}else{
... ...
@@ -151,6 +166,11 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
151 166
 			qm->used+=f->size;
152 167
 			if (qm->max_real_used<qm->real_used)
153 168
 				qm->max_real_used=qm->real_used;
169
+#ifdef DBG_QM_MALLOC
170
+			f->file=file;
171
+			f->func=func;
172
+			f->line=line;
173
+#endif
154 174
 			return (char*)f+sizeof(struct qm_frag);
155 175
 		}
156 176
 	}
... ...
@@ -159,7 +179,12 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
159 179
 
160 180
 
161 181
 
182
+#ifdef DBG_QM_MALLOC
183
+void qm_free(struct qm_block* qm, void* p, char* file, char* func, 
184
+				unsigned int line)
185
+#else
162 186
 void qm_free(struct qm_block* qm, void* p)
187
+#endif
163 188
 {
164 189
 	struct qm_frag* f;
165 190
 	struct qm_frag* prev;
... ...
@@ -168,12 +193,29 @@ void qm_free(struct qm_block* qm, void* p)
168 193
 	unsigned int overhead;
169 194
 	unsigned int size;
170 195
 
196
+#ifdef DBG_QM_MALLOC
197
+	DBG("qm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
198
+	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
199
+		DBG("qm_free: ERROR!!!: bad pointer %x (out of memory block!) - "
200
+				"aborting\n", p);
201
+		abort();
202
+	}
203
+#endif
171 204
 	if (p==0) {
172 205
 		DBG("WARNING:qm_free: free(0) called\n");
173 206
 		return;
174 207
 	}
175 208
 	prev=next=0;
176 209
 	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
210
+#ifdef DBG_QM_MALLOC
211
+	if (f->u.is_free){
212
+		DBG("qm_free: ERROR!!!: freeing already freed pointer, first free:"
213
+				" %s: %s(%d) - aborting\n", f->file, f->func, f->line);
214
+		abort();
215
+	}
216
+	DBG("qm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
217
+			f->line);
218
+#endif
177 219
 	overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
178 220
 	next=FRAG_NEXT(f);
179 221
 	size=f->size;
... ...
@@ -200,6 +242,11 @@ void qm_free(struct qm_block* qm, void* p)
200 242
 	}
201 243
 	f->size=size;
202 244
 	FRAG_END(f)->size=f->size;
245
+#ifdef DBG_QM_MALLOC
246
+	f->file=file;
247
+	f->func=func;
248
+	f->line=line;
249
+#endif
203 250
 	qm_insert_free(qm, f);
204 251
 }
205 252
 
... ...
@@ -218,14 +265,24 @@ void qm_status(struct qm_block* qm)
218 265
 	
219 266
 	DBG("dumping all fragments:\n");
220 267
 	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
221
-			,i++)
268
+			,i++){
222 269
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
223 270
 				(char*)f+sizeof(struct qm_frag), f->size);
271
+#ifdef DBG_QM_MALLOC
272
+		DBG("            %s from %s: %s(%d)\n",
273
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
274
+#endif
275
+	}
224 276
 	DBG("dumping free list:\n");
225 277
 	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
226
-			i++)
278
+			i++){
227 279
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
228 280
 				(char*)f+sizeof(struct qm_frag), f->size);
281
+#ifdef DBG_QM_MALLOC
282
+		DBG("            %s from %s: %s(%d)\n", 
283
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
284
+#endif
285
+	}
229 286
 	DBG("-----------------------------\n");
230 287
 }
231 288
 
Browse code

- fixed new malloc

Andrei Pelinescu-Onciul authored on 23/11/2001 11:38:33
Showing 1 changed files
... ...
@@ -33,9 +33,7 @@ struct qm_block* qm_malloc_init(char* address, unsigned int size)
33 33
 	char* end;
34 34
 	struct qm_block* qm;
35 35
 	unsigned int init_overhead;
36
-	unsigned int init_size;
37 36
 	
38
-	init_size=size;
39 37
 	/* make address and size multiple of 8*/
40 38
 	start=(char*)( ((unsigned int)address%8)?((unsigned int)address+8)/8*8:
41 39
 			(unsigned int)address);
... ...
@@ -54,9 +52,10 @@ struct qm_block* qm_malloc_init(char* address, unsigned int size)
54 52
 	end=start+size;
55 53
 	qm=(struct qm_block*)start;
56 54
 	memset(qm, 0, sizeof(struct qm_block));
57
-	qm->init_size=init_size;
58
-	qm->size=size-init_overhead;
55
+	size-=init_overhead;
56
+	qm->size=size;
59 57
 	qm->real_used=init_overhead;
58
+	qm->max_real_used=qm->real_used;
60 59
 	
61 60
 	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
62 61
 	qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
... ...
@@ -150,6 +149,8 @@ void* qm_malloc(struct qm_block* qm, unsigned int size)
150 149
 			}
151 150
 			qm->real_used+=f->size;
152 151
 			qm->used+=f->size;
152
+			if (qm->max_real_used<qm->real_used)
153
+				qm->max_real_used=qm->real_used;
153 154
 			return (char*)f+sizeof(struct qm_frag);
154 155
 		}
155 156
 	}
... ...
@@ -197,6 +198,7 @@ void qm_free(struct qm_block* qm, void* p)
197 198
 			f=prev;
198 199
 		}
199 200
 	}
201
+	f->size=size;
200 202
 	FRAG_END(f)->size=f->size;
201 203
 	qm_insert_free(qm, f);
202 204
 }
... ...
@@ -209,10 +211,10 @@ void qm_status(struct qm_block* qm)
209 211
 	int i;
210 212
 
211 213
 	DBG("qm_status (%x):\n", qm);
212
-	DBG(" init_size= %d", qm->init_size);
213
-	DBG(" heap size= %d", qm->size);
214
+	DBG(" heap size= %d\n", qm->size);
214 215
 	DBG(" used= %d, used+overhead=%d, free=%d\n",
215 216
 			qm->used, qm->real_used, qm->size-qm->real_used);
217
+	DBG(" max used (+overhead)= %d\n", qm->max_real_used);
216 218
 	
217 219
 	DBG("dumping all fragments:\n");
218 220
 	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
... ...
@@ -220,7 +222,8 @@ void qm_status(struct qm_block* qm)
220 222
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
221 223
 				(char*)f+sizeof(struct qm_frag), f->size);
222 224
 	DBG("dumping free list:\n");
223
-	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free)
225
+	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
226
+			i++)
224 227
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
225 228
 				(char*)f+sizeof(struct qm_frag), f->size);
226 229
 	DBG("-----------------------------\n");
Browse code

- malloc replacements

Andrei Pelinescu-Onciul authored on 23/11/2001 09:29:00
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,232 @@
1
+/* $Id$
2
+ *
3
+ */
4
+
5
+#define q_malloc
6
+#ifdef q_malloc
7
+
8
+#include "q_malloc.h"
9
+#include "dprint.h"
10
+
11
+
12
+/*usefull macros*/
13
+#define FRAG_END(f)  \
14
+			((struct qm_frag_end*)((char*)f+sizeof(struct qm_frag)+f->size))
15
+
16
+#define FRAG_NEXT(f) \
17
+			((struct qm_frag*)((char*)f+sizeof(struct qm_frag)+f->size+ \
18
+							   sizeof(struct qm_frag_end)))
19
+			
20
+#define FRAG_PREV(f) \
21
+		( (struct qm_frag*) ( ((char*)f-sizeof(struct qm_frag_end))- \
22
+		((struct qm_frag_end*)((char*)f-sizeof(struct qm_frag_end)))->size- \
23
+			sizeof(struct qm_frag) ) )
24
+
25
+
26
+
27
+
28
+
29
+/* init malloc and return a qm_block*/
30
+struct qm_block* qm_malloc_init(char* address, unsigned int size)
31
+{
32
+	char* start;
33
+	char* end;
34
+	struct qm_block* qm;
35
+	unsigned int init_overhead;
36
+	unsigned int init_size;
37
+	
38
+	init_size=size;
39
+	/* make address and size multiple of 8*/
40
+	start=(char*)( ((unsigned int)address%8)?((unsigned int)address+8)/8*8:
41
+			(unsigned int)address);
42
+	if (size<start-address) return 0;
43
+	size-=(start-address);
44
+	if (size <8) return 0;
45
+	size=(size%8)?(size-8)/8*8:size;
46
+	
47
+	init_overhead=sizeof(struct qm_block)+sizeof(struct qm_frag)+
48
+		sizeof(struct qm_frag_end);
49
+	if (size < init_overhead)
50
+	{
51
+		/* not enough mem to create our control structures !!!*/
52
+		return 0;
53
+	}
54
+	end=start+size;
55
+	qm=(struct qm_block*)start;
56
+	memset(qm, 0, sizeof(struct qm_block));
57
+	qm->init_size=init_size;
58
+	qm->size=size-init_overhead;
59
+	qm->real_used=init_overhead;
60
+	
61
+	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
62
+	qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
63
+	/* init initial fragment*/
64
+	qm->first_frag->size=size;
65
+	qm->first_frag->u.nxt_free=&(qm->free_lst);
66
+	qm->last_frag_end->size=size;
67
+	qm->last_frag_end->prev_free=&(qm->free_lst);
68
+	/* init free_lst* */
69
+	qm->free_lst.u.nxt_free=qm->first_frag;
70
+	qm->free_lst_end.prev_free=qm->first_frag;
71
+	qm->free_lst.size=0;
72
+	qm->free_lst_end.size=0;
73
+	
74
+	
75
+	return qm;
76
+}
77
+
78
+
79
+static inline void qm_insert_free(struct qm_block* qm, struct qm_frag* frag)
80
+{
81
+	struct qm_frag* f;
82
+	struct qm_frag* prev;
83
+
84
+	for(f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
85
+		if (frag->size < f->size) break;
86
+	}
87
+	/*insert it here*/
88
+	prev=FRAG_END(f)->prev_free;
89
+	prev->u.nxt_free=frag;
90
+	FRAG_END(frag)->prev_free=prev;
91
+	frag->u.nxt_free=f;
92
+	FRAG_END(f)->prev_free=frag;
93
+}
94
+
95
+
96
+
97
+static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
98
+{
99
+	struct qm_frag *prev;
100
+	struct qm_frag *next;
101
+	
102
+	struct qm_frag_end *end;
103
+
104
+	prev=FRAG_END(frag)->prev_free;
105
+	next=frag->u.nxt_free;
106
+	prev->u.nxt_free=next;
107
+	FRAG_END(next)->prev_free=prev;
108
+	
109
+}
110
+
111
+
112
+
113
+void* qm_malloc(struct qm_block* qm, unsigned int size)
114
+{
115
+	struct qm_frag* f;
116
+	struct qm_frag_end* end;
117
+	struct qm_frag* n;
118
+	unsigned int rest;
119
+	unsigned int overhead;
120
+	
121
+	/*size must be a multiple of 8*/
122
+	size=(size%8)?(size+8)/8*8:size;
123
+	if (size>(qm->size-qm->real_used)) return 0;
124
+	if (qm->free_lst.u.nxt_free==&(qm->free_lst)) return 0;
125
+	/*search for a suitable free frag*/
126
+	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
127
+		if (f->size>=size){
128
+			/* we found it!*/
129
+			/*detach it from the free list*/
130
+			qm_detach_free(qm, f);
131
+			/*mark it as "busy"*/
132
+			f->u.is_free=0;
133
+			
134
+			/*see if we'll use full frag, or we'll split it in 2*/
135
+			rest=f->size-size;
136
+			overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
137
+			if (rest