Browse Source

- fixed new malloc

Andrei Pelinescu-Onciul 24 năm trước cách đây
mục cha
commit
02690ea631
5 tập tin đã thay đổi với 31 bổ sung15 xóa
  1. 1 1
      Makefile
  2. 7 5
      forward.c
  3. 12 1
      main.c
  4. 10 7
      q_malloc.c
  5. 1 1
      q_malloc.h

+ 1 - 1
Makefile

@@ -28,7 +28,7 @@ NAME=ser
 # DEBUG compiles in some extra debugging code
 # OLD_PARSER uses the old and stable parser (from ser 8.3.2)
 # DNS_IP_HACK faster ip address resolver for ip strings (e.g "127.0.0.1")
-DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK #-DNO_DEBUG#-DSTATS -DNO_DEBUG 
+DEFS=-DNOCR -DMACROEATER -DDNS_IP_HACK -DPKG_MALLOC#-DSTATS -DNO_DEBUG 
 #-DNO_LOG
 
 PROFILE=  # -pg #set this if you want profiling

+ 7 - 5
forward.c

@@ -21,6 +21,7 @@
 #include "globals.h"
 #include "data_lump.h"
 #include "ut.h"
+#include "mem.h"
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
@@ -163,7 +164,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
 		goto error;
 	}
 
-	line_buf=malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
+	line_buf=pkg_malloc(sizeof(char)*MAX_VIA_LINE_SIZE);
 	if (line_buf==0){
 		LOG(L_ERR, "ERROR: forward_request: out of memory\n");
 		goto error1;
@@ -193,7 +194,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
 	
 	/* check if received needs to be added */
 	if (check_address(source_ip, msg->via1->host.s, received_dns)!=0){
-		received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);
+		received_buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
 		if (received_buf==0){
 			LOG(L_ERR, "ERROR: forward_request: out of memory\n");
 			goto error1;
@@ -451,8 +452,8 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
 	return 0;
 error1:
-	if (line_buf) free(line_buf);
-	if (received_buf) free(received_buf);
+	if (line_buf) pkg_free(line_buf);
+	if (received_buf) pkg_free(received_buf);
 error:
 	if (new_buf) free(new_buf);
 	if (to) free(to);
@@ -517,7 +518,8 @@ int forward_reply(struct sip_msg* msg)
 	new_len=len-via_len;
 	
 	DBG(" old size: %d, new size: %d\n", len, new_len);
-	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging (\0 to print it )*/
+	new_buf=(char*)malloc(new_len+1);/* +1 is for debugging 
+											(\0 to print it )*/
 	if (new_buf==0){
 		LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
 		goto error;

+ 12 - 1
main.c

@@ -158,6 +158,11 @@ int process_no = 0;
 /* cfg parsing */
 int cfg_errors=0;
 
+#ifdef PKG_MALLOC
+char mem_pool[PKG_MEM_POOL_SIZE];
+struct qm_block* mem_block;
+#endif
+
 
 #define MAX_FD 32 /* maximum number of inherited open file descriptors,
 		    (normally it shouldn't  be bigger  than 3) */
@@ -285,6 +290,9 @@ static void sig_usr(int signo)
 				printf("statistic dumped to %s\n", stat_file );
 			else
 				printf("statistics dump to %s failed\n", stat_file );
+#endif
+#ifdef PKG_MALLOC
+		pkg_status();
 #endif
 		DPrint("INT received, program terminates\n");
 		DPrint("Thank you for flying ser\n");
@@ -292,6 +300,9 @@ static void sig_usr(int signo)
 	} else if (signo==SIGUSR1) { /* statistic */
 #ifdef STATS
 		dump_all_statistic();
+#endif
+#ifdef PKG_MALLOC
+		pkg_status();
 #endif
 	}
 }
@@ -512,7 +523,7 @@ int main(int argc, char** argv)
 
 #ifdef PKG_MALLOC
 	/*init mem*/
-	mem_block=qm_malloc_init(mem_pool, MEM_POOL_SIZE);
+	mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
 	if (mem_block==0){
 		LOG(L_CRIT, "could not initialize memory pool\n");
 		goto error;

+ 10 - 7
q_malloc.c

@@ -33,9 +33,7 @@ struct qm_block* qm_malloc_init(char* address, unsigned int size)
 	char* end;
 	struct qm_block* qm;
 	unsigned int init_overhead;
-	unsigned int init_size;
 	
-	init_size=size;
 	/* make address and size multiple of 8*/
 	start=(char*)( ((unsigned int)address%8)?((unsigned int)address+8)/8*8:
 			(unsigned int)address);
@@ -54,9 +52,10 @@ struct qm_block* qm_malloc_init(char* address, unsigned int size)
 	end=start+size;
 	qm=(struct qm_block*)start;
 	memset(qm, 0, sizeof(struct qm_block));
-	qm->init_size=init_size;
-	qm->size=size-init_overhead;
+	size-=init_overhead;
+	qm->size=size;
 	qm->real_used=init_overhead;
+	qm->max_real_used=qm->real_used;
 	
 	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
 	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)
 			}
 			qm->real_used+=f->size;
 			qm->used+=f->size;
+			if (qm->max_real_used<qm->real_used)
+				qm->max_real_used=qm->real_used;
 			return (char*)f+sizeof(struct qm_frag);
 		}
 	}
@@ -197,6 +198,7 @@ void qm_free(struct qm_block* qm, void* p)
 			f=prev;
 		}
 	}
+	f->size=size;
 	FRAG_END(f)->size=f->size;
 	qm_insert_free(qm, f);
 }
@@ -209,10 +211,10 @@ void qm_status(struct qm_block* qm)
 	int i;
 
 	DBG("qm_status (%x):\n", qm);
-	DBG(" init_size= %d", qm->init_size);
-	DBG(" heap size= %d", qm->size);
+	DBG(" heap size= %d\n", qm->size);
 	DBG(" used= %d, used+overhead=%d, free=%d\n",
 			qm->used, qm->real_used, qm->size-qm->real_used);
+	DBG(" max used (+overhead)= %d\n", qm->max_real_used);
 	
 	DBG("dumping all fragments:\n");
 	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)
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
 				(char*)f+sizeof(struct qm_frag), f->size);
 	DBG("dumping free list:\n");
-	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free)
+	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
+			i++)
 		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
 				(char*)f+sizeof(struct qm_frag), f->size);
 	DBG("-----------------------------\n");

+ 1 - 1
q_malloc.h

@@ -22,10 +22,10 @@ struct qm_frag_end{
 
 
 struct qm_block{
-	unsigned int init_size;
 	unsigned int size; /* total size */
 	unsigned int used; /* alloc'ed size*/
 	unsigned int real_used; /* used+malloc overhead*/
+	unsigned int max_real_used;
 	
 	struct qm_frag* first_frag;
 	struct qm_frag_end* last_frag_end;