Browse code

- fixed new malloc

Andrei Pelinescu-Onciul authored on 23/11/2001 11:38:33
Showing 5 changed files
... ...
@@ -28,7 +28,7 @@ NAME=ser
28 28
 # DEBUG compiles in some extra debugging code
29 29
 # OLD_PARSER uses the old and stable parser (from ser 8.3.2)
30 30
 # DNS_IP_HACK faster ip address resolver for ip strings (e.g "127.0.0.1")
31
-DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK #-DNO_DEBUG#-DSTATS -DNO_DEBUG 
31
+DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK -DPKG_MALLOC#-DSTATS -DNO_DEBUG 
32 32
 #-DNO_LOG
33 33
 
34 34
 PROFILE=  # -pg #set this if you want profiling
... ...
@@ -21,6 +21,7 @@
21 21
 #include "globals.h"
22 22
 #include "data_lump.h"
23 23
 #include "ut.h"
24
+#include "mem.h"
24 25
 
25 26
 #ifdef DEBUG_DMALLOC
26 27
 #include <dmalloc.h>
... ...
@@ -163,7 +164,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
163 164
 		goto error;
164 165
 	}
165 166
 
166
-	line_buf=malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
167
+	line_buf=pkg_malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
167 168
 	if (line_buf==0){
168 169
 		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
169 170
 		goto error1;
... ...
@@ -193,7 +194,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
193 194
 	
194 195
 	/* check if received needs to be added */
195 196
 	if (check_address(source_ip, msg->via1->host.s, received_dns)!=0){
196
-		received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);
197
+		received_buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
197 198
 		if (received_buf==0){
198 199
 			LOG(L_ERR, "ERROR: forward_request: out of memory\n");
199 200
 			goto error1;
... ...
@@ -451,8 +452,8 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
451 452
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
452 453
 	return 0;
453 454
 error1:
454
-	if (line_buf) free(line_buf);
455
-	if (received_buf) free(received_buf);
455
+	if (line_buf) pkg_free(line_buf);
456
+	if (received_buf) pkg_free(received_buf);
456 457
 error:
457 458
 	if (new_buf) free(new_buf);
458 459
 	if (to) free(to);
... ...
@@ -517,7 +518,8 @@ int forward_reply(struct sip_msg* msg)
517 518
 	new_len=len-via_len;
518 519
 	
519 520
 	DBG(" old size: %d, new size: %d\n", len, new_len);
520
-	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging (\0 to print it )*/
521
+	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging 
522
+											(\0 to print it )*/
521 523
 	if (new_buf==0){
522 524
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
523 525
 		goto error;
... ...
@@ -158,6 +158,11 @@ int process_no = 0;
158 158
 /* cfg parsing */
159 159
 int cfg_errors=0;
160 160
 
161
+#ifdef PKG_MALLOC
162
+char mem_pool[PKG_MEM_POOL_SIZE];
163
+struct qm_block* mem_block;
164
+#endif
165
+
161 166
 
162 167
 #define MAX_FD 32 /* maximum number of inherited open file descriptors,
163 168
 		    (normally it shouldn't  be bigger  than 3) */
... ...
@@ -285,6 +290,9 @@ static void sig_usr(int signo)
285 290
 				printf("statistic dumped to %s\n", stat_file );
286 291
 			else
287 292
 				printf("statistics dump to %s failed\n", stat_file );
293
+#endif
294
+#ifdef PKG_MALLOC
295
+		pkg_status();
288 296
 #endif
289 297
 		DPrint("INT received, program terminates\n");
290 298
 		DPrint("Thank you for flying ser\n");
... ...
@@ -292,6 +300,9 @@ static void sig_usr(int signo)
292 300
 	} else if (signo==SIGUSR1) { /* statistic */
293 301
 #ifdef STATS
294 302
 		dump_all_statistic();
303
+#endif
304
+#ifdef PKG_MALLOC
305
+		pkg_status();
295 306
 #endif
296 307
 	}
297 308
 }
... ...
@@ -512,7 +523,7 @@ int main(int argc, char** argv)
512 523
 
513 524
 #ifdef PKG_MALLOC
514 525
 	/*init mem*/
515
-	mem_block=qm_malloc_init(mem_pool, MEM_POOL_SIZE);
526
+	mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
516 527
 	if (mem_block==0){
517 528
 		LOG(L_CRIT, "could not initialize memory pool\n");
518 529
 		goto error;
... ...
@@ -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");
... ...
@@ -22,10 +22,10 @@ struct qm_frag_end{
22 22
 
23 23
 
24 24
 struct qm_block{
25
-	unsigned int init_size;
26 25
 	unsigned int size; /* total size */
27 26
 	unsigned int used; /* alloc'ed size*/
28 27
 	unsigned int real_used; /* used+malloc overhead*/
28
+	unsigned int max_real_used;
29 29
 	
30 30
 	struct qm_frag* first_frag;
31 31
 	struct qm_frag_end* last_frag_end;