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 163
 		goto error;
164 164
 	}
165 165
 
166
-	line_buf=malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
166
+	line_buf=pkg_malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
167 167
 	if (line_buf==0){
168 168
 		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
169 169
 		goto error1;
... ...
@@ -193,7 +194,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
193 193
 	
194 194
 	/* check if received needs to be added */
195 195
 	if (check_address(source_ip, msg->via1->host.s, received_dns)!=0){
196
-		received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);
196
+		received_buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
197 197
 		if (received_buf==0){
198 198
 			LOG(L_ERR, "ERROR: forward_request: out of memory\n");
199 199
 			goto error1;
... ...
@@ -451,8 +452,8 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
451 451
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
452 452
 	return 0;
453 453
 error1:
454
-	if (line_buf) free(line_buf);
455
-	if (received_buf) free(received_buf);
454
+	if (line_buf) pkg_free(line_buf);
455
+	if (received_buf) pkg_free(received_buf);
456 456
 error:
457 457
 	if (new_buf) free(new_buf);
458 458
 	if (to) free(to);
... ...
@@ -517,7 +518,8 @@ int forward_reply(struct sip_msg* msg)
517 517
 	new_len=len-via_len;
518 518
 	
519 519
 	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 )*/
520
+	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging 
521
+											(\0 to print it )*/
521 522
 	if (new_buf==0){
522 523
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
523 524
 		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) */
... ...
@@ -286,6 +291,9 @@ static void sig_usr(int signo)
286 286
 			else
287 287
 				printf("statistics dump to %s failed\n", stat_file );
288 288
 #endif
289
+#ifdef PKG_MALLOC
290
+		pkg_status();
291
+#endif
289 292
 		DPrint("INT received, program terminates\n");
290 293
 		DPrint("Thank you for flying ser\n");
291 294
 		exit(0);
... ...
@@ -293,6 +301,9 @@ static void sig_usr(int signo)
293 293
 #ifdef STATS
294 294
 		dump_all_statistic();
295 295
 #endif
296
+#ifdef PKG_MALLOC
297
+		pkg_status();
298
+#endif
296 299
 	}
297 300
 }
298 301
 	
... ...
@@ -512,7 +523,7 @@ int main(int argc, char** argv)
512 512
 
513 513
 #ifdef PKG_MALLOC
514 514
 	/*init mem*/
515
-	mem_block=qm_malloc_init(mem_pool, MEM_POOL_SIZE);
515
+	mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
516 516
 	if (mem_block==0){
517 517
 		LOG(L_CRIT, "could not initialize memory pool\n");
518 518
 		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 54
 	end=start+size;
55 55
 	qm=(struct qm_block*)start;
56 56
 	memset(qm, 0, sizeof(struct qm_block));
57
-	qm->init_size=init_size;
58
-	qm->size=size-init_overhead;
57
+	size-=init_overhead;
58
+	qm->size=size;
59 59
 	qm->real_used=init_overhead;
60
+	qm->max_real_used=qm->real_used;
60 61
 	
61 62
 	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
62 63
 	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 150
 			}
151 151
 			qm->real_used+=f->size;
152 152
 			qm->used+=f->size;
153
+			if (qm->max_real_used<qm->real_used)
154
+				qm->max_real_used=qm->real_used;
153 155
 			return (char*)f+sizeof(struct qm_frag);
154 156
 		}
155 157
 	}
... ...
@@ -197,6 +198,7 @@ void qm_free(struct qm_block* qm, void* p)
197 197
 			f=prev;
198 198
 		}
199 199
 	}
200
+	f->size=size;
200 201
 	FRAG_END(f)->size=f->size;
201 202
 	qm_insert_free(qm, f);
202 203
 }
... ...
@@ -209,10 +211,10 @@ void qm_status(struct qm_block* qm)
209 209
 	int i;
210 210
 
211 211
 	DBG("qm_status (%x):\n", qm);
212
-	DBG(" init_size= %d", qm->init_size);
213
-	DBG(" heap size= %d", qm->size);
212
+	DBG(" heap size= %d\n", qm->size);
214 213
 	DBG(" used= %d, used+overhead=%d, free=%d\n",
215 214
 			qm->used, qm->real_used, qm->size-qm->real_used);
215
+	DBG(" max used (+overhead)= %d\n", qm->max_real_used);
216 216
 	
217 217
 	DBG("dumping all fragments:\n");
218 218
 	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 220
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
221 221
 				(char*)f+sizeof(struct qm_frag), f->size);
222 222
 	DBG("dumping free list:\n");
223
-	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free)
223
+	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
224
+			i++)
224 225
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
225 226
 				(char*)f+sizeof(struct qm_frag), f->size);
226 227
 	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;