Просмотр исходного кода

- added fm_realloc (now only vm_ misses realloc)
- cosmetic fixes in ip_addr (print_ip changed)

Andrei Pelinescu-Onciul 22 лет назад
Родитель
Сommit
9c01c86051
13 измененных файлов с 248 добавлено и 54 удалено
  1. 4 2
      Makefile.defs
  2. 1 1
      TODO
  3. 11 6
      ip_addr.c
  4. 1 1
      ip_addr.h
  5. 178 27
      mem/f_malloc.c
  6. 7 0
      mem/f_malloc.h
  7. 15 0
      mem/mem.h
  8. 1 1
      mem/q_malloc.c
  9. 14 0
      mem/shm_mem.h
  10. 2 3
      route.c
  11. 2 2
      route_struct.c
  12. 8 7
      tcp_main.c
  13. 4 4
      tcp_read.c

+ 4 - 2
Makefile.defs

@@ -40,7 +40,7 @@ export makefile_defs
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL =   12
-EXTRAVERSION = dev-t06
+EXTRAVERSION = dev-t07
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
@@ -272,7 +272,9 @@ DEFS+= $(extra_defs) \
 	 -DUSE_IPV6 \
 	 -DUSE_TCP \
 	 -DDISABLE_NAGLE \
-	 -DDBG_QM_MALLOC \
+	 -DF_MALLOC \
+	# -DDBG_F_MALLOC \
+	# -DDBG_QM_MALLOC \
 	 #-DF_MALLOC \
 	 #-DNO_DEBUG \
 	 #-DNO_LOG

+ 1 - 1
TODO

@@ -3,7 +3,7 @@ $Id$
 ( - todo, x - done)
 
 release:
-- fix kill(0, SIGTERM) on startup error (will kill also the launching shell
+x fix kill(0, SIGTERM) on startup error (will kill also the launching shell
  if non-interactive)
 - fix modules destroy (some modules will try to free uninitialized resources
    if modules_init was not called first)

+ 11 - 6
ip_addr.c

@@ -93,24 +93,29 @@ error:
 
 
 
-void print_ip(struct ip_addr* ip)
+void print_ip(char* p, struct ip_addr* ip, char *s)
 {
 	switch(ip->af){
 		case AF_INET:
-			DBG("%d.%d.%d.%d",	ip->u.addr[0],
+			DBG("%s%d.%d.%d.%d%s", (p)?p:"",
+								ip->u.addr[0],
 								ip->u.addr[1],
 								ip->u.addr[2],
-								ip->u.addr[3]);
+								ip->u.addr[3],
+								(s)?s:""
+								);
 			break;
 		case AF_INET6:
-			DBG("%x:%x:%x:%x:%x:%x:%x:%x",	htons(ip->u.addr16[0]),
+			DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"",
+											htons(ip->u.addr16[0]),
 											htons(ip->u.addr16[1]),
 											htons(ip->u.addr16[2]),
 											htons(ip->u.addr16[3]),
 											htons(ip->u.addr16[4]),
 											htons(ip->u.addr16[5]),
 											htons(ip->u.addr16[6]),
-											htons(ip->u.addr16[7])
+											htons(ip->u.addr16[7]),
+											(s)?s:""
 				);
 			break;
 		default:
@@ -153,5 +158,5 @@ void print_net(struct net* net)
 		LOG(L_WARN, "ERROR: print net: null pointer\n");
 		return;
 	}
-	print_ip(&net->ip); DBG("/"); print_ip(&net->mask);
+	print_ip("", &net->ip, "/"); print_ip("", &net->mask, "");
 }

+ 1 - 1
ip_addr.h

@@ -157,7 +157,7 @@ struct dest_info{
 struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask);
 struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen);
 
-void print_ip(struct ip_addr* ip);
+void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
 void stdout_print_ip(struct ip_addr* ip);
 void print_net(struct net* net);
 

+ 178 - 27
mem/f_malloc.c

@@ -24,11 +24,18 @@
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+/*
+ * History:
+ * --------
+ *              created by andrei
+ *  2003-07-06  added fm_realloc (andrei)
+ */
 
 
 #if !defined(q_malloc) && !(defined VQ_MALLOC)  && (defined F_MALLOC)
 
 #include <string.h>
+#include <stdlib.h>
 
 #include "f_malloc.h"
 #include "../dprint.h"
@@ -107,7 +114,42 @@ static inline void fm_insert_free(struct fm_block* qm, struct fm_frag* frag)
 
 
 
-/* init malloc and return a qm_block*/
+ /* size should be already rounded-up */
+static inline
+#ifdef DBG_F_MALLOC 
+void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,unsigned int size,
+					char* file, char* func, unsigned int line)
+#else
+void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,unsigned int size)
+#endif
+{
+	unsigned int rest;
+	struct fm_frag* n;
+	
+	rest=frag->size-size;
+	if (rest>(FRAG_OVERHEAD+MIN_FRAG_SIZE)){
+		frag->size=size;
+		/*split the fragment*/
+		n=FRAG_NEXT(frag);
+		n->size=rest-FRAG_OVERHEAD;
+#ifdef DBG_F_MALLOC
+		qm->real_used+=FRAG_OVERHEAD;
+		/* frag created by malloc, mark it*/
+		n->file=file;
+		n->func="frag. from fm_malloc";
+		n->line=line;
+		n->check=ST_CHECK_PATTERN;
+#endif
+		/* reinsert n in free list*/
+		fm_insert_free(qm, n);
+	}else{
+		/* we cannot split this fragment any more => alloc all of it*/
+	}
+}
+
+
+
+/* init malloc and return a fm_block*/
 struct fm_block* fm_malloc_init(char* address, unsigned int size)
 {
 	char* start;
@@ -170,12 +212,10 @@ void* fm_malloc(struct fm_block* qm, unsigned int size)
 {
 	struct fm_frag** f;
 	struct fm_frag* frag;
-	struct fm_frag* n;
-	unsigned int rest;
 	int hash;
 	
 #ifdef DBG_F_MALLOC
-	DBG("fm_malloc(%x, %d) called from %s: %s(%d)\n", qm, size, file, func,
+	DBG("fm_malloc(%p, %d) called from %s: %s(%d)\n", qm, size, file, func,
 			line);
 #endif
 	/*size must be a multiple of 8*/
@@ -199,28 +239,12 @@ found:
 	/* detach it from the free list*/
 	frag=*f;
 	*f=frag->u.nxt_free;
+	frag->u.nxt_free=0; /* mark it as 'taken' */
 	
 	/*see if we'll use full frag, or we'll split it in 2*/
-	rest=frag->size-size;
-	if (rest>(FRAG_OVERHEAD+MIN_FRAG_SIZE)){
-		frag->size=size;
-		/*split the fragment*/
-		n=FRAG_NEXT(frag);
-		n->size=rest-FRAG_OVERHEAD;
-#ifdef DBG_F_MALLOC
-		qm->real_used+=FRAG_OVERHEAD;
-		/* frag created by malloc, mark it*/
-		n->file=file;
-		n->func="frag. from qm_malloc";
-		n->line=line;
-		n->check=ST_CHECK_PATTERN;
-#endif
-		/* reinsert n in free list*/
-		fm_insert_free(qm, n);
-	}else{
-		/* we cannot split this fragment any more => alloc all of it*/
-	}
+	
 #ifdef DBG_F_MALLOC
+	fm_split_frag(qm, frag, size, file, func, line);
 	qm->real_used+=frag->size;
 	qm->used+=frag->size;
 
@@ -231,8 +255,10 @@ found:
 	frag->func=func;
 	frag->line=line;
 	frag->check=ST_CHECK_PATTERN;
-	DBG("fm_malloc(%x, %d) returns address %x \n", qm, size,
+	DBG("fm_malloc(%p, %d) returns address %p \n", qm, size,
 		(char*)frag+sizeof(struct fm_frag));
+#else
+	fm_split_frag(qm, frag, size);
 #endif
 	return (char*)frag+sizeof(struct fm_frag);
 }
@@ -250,9 +276,9 @@ void fm_free(struct fm_block* qm, void* p)
 	unsigned int size;
 
 #ifdef DBG_F_MALLOC
-	DBG("fm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
+	DBG("fm_free(%p, %p), called from %s: %s(%d)\n", qm, p, file, func, line);
 	if (p>(void*)qm->last_frag || p<(void*)qm->first_frag){
-		LOG(L_CRIT, "BUG: fm_free: bad pointer %x (out of memory block!) - "
+		LOG(L_CRIT, "BUG: fm_free: bad pointer %p (out of memory block!) - "
 				"aborting\n", p);
 		abort();
 	}
@@ -263,7 +289,7 @@ void fm_free(struct fm_block* qm, void* p)
 	}
 	f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));
 #ifdef DBG_F_MALLOC
-	DBG("fm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
+	DBG("fm_free: freeing block alloc'ed from %s: %s(%ld)\n", f->file, f->func,
 			f->line);
 #endif
 	size=f->size;
@@ -279,6 +305,131 @@ void fm_free(struct fm_block* qm, void* p)
 }
 
 
+#ifdef DBG_F_MALLOC
+void* fm_realloc(struct fm_block* qm, void* p, unsigned int size,
+					char* file, char* func, unsigned int line)
+#else
+void* fm_realloc(struct fm_block* qm, void* p, unsigned int size)
+#endif
+{
+	struct fm_frag *f;
+	struct fm_frag **pf;
+	unsigned int diff;
+	unsigned int orig_size;
+	struct fm_frag *n;
+	void *ptr;
+	
+#ifdef DBG_F_MALLOC
+	DBG("fm_realloc(%p, %p, %d) called from %s: %s(%d)\n", qm, p, size,
+			file, func, line);
+	if (p>(void*)qm->last_frag || p<(void*)qm->first_frag){
+		LOG(L_CRIT, "BUG: fm_free: bad pointer %p (out of memory block!) - "
+				"aborting\n", p);
+		abort();
+	}
+#endif
+	if (size==0) {
+		if (p)
+#ifdef DBG_F_MALLOC
+			fm_free(qm, p, file, func, line);
+#else
+			fm_free(qm, p);
+#endif
+		return 0;
+	}
+	if (p==0)
+#ifdef DBG_F_MALLOC
+		return fm_malloc(qm, size, file, func, line);
+#else
+		return fm_malloc(qm, size);
+#endif
+	f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));
+#ifdef DBG_F_MALLOC
+	DBG("fm_realloc: realloc'ing frag %p alloc'ed from %s: %s(%ld)\n",
+			f, f->file, f->func, f->line);
+#endif
+	size=ROUNDUP(size);
+	orig_size=f->size;
+	if (f->size > size){
+		/* shrink */
+#ifdef DBG_F_MALLOC
+		DBG("fm_realloc: shrinking from %ld to %d\n", f->size, size);
+		fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line);
+		qm->real_used-=(orig_size-f->size);
+		qm->used-=(orig_size-f->size);
+#else
+		fm_split_frag(qm, f, size);
+#endif
+	}else if (f->size<size){
+		/* grow */
+#ifdef DBG_F_MALLOC
+		DBG("fm_realloc: growing from %ld to %d\n", f->size, size);
+#endif
+		diff=size-f->size;
+		n=FRAG_NEXT(f);
+		if (((char*)n < (char*)qm->last_frag) && 
+				(n->u.nxt_free)&&((n->size+FRAG_OVERHEAD)>=diff)){
+			/* join  */
+			/* detach n from the free list */
+			pf=&(qm->free_hash[GET_HASH(n->size)]);
+			/* find it */
+			for(;(*pf)&&(*pf!=n); pf=&((*pf)->u.nxt_free));
+			if (*pf==0){
+				/* not found, bad! */
+				LOG(L_CRIT, "BUG: fm_realloc: could not find %p in free "
+						"list (hash=%ld)\n", n, GET_HASH(n->size));
+				abort();
+			}
+			/* detach */
+			*pf=n->u.nxt_free;
+			/* join */
+			f->size+=n->size+FRAG_OVERHEAD;
+		#ifdef DBG_F_MALLOC
+			qm->real_used-=FRAG_OVERHEAD;
+		#endif
+			/* split it if necessary */
+			if (f->size > size){
+		#ifdef DBG_F_MALLOC
+				fm_split_frag(qm, f, size, file, "fragm. from fm_realloc",
+						line);
+		#else
+				fm_split_frag(qm, f, size);
+		#endif
+			}
+		#ifdef DBG_F_MALLOC
+			qm->real_used+=(f->size-orig_size);
+			qm->used+=(f->size-orig_size);
+		#endif
+		}else{
+			/* could not join => realloc */
+	#ifdef DBG_F_MALLOC
+			ptr=fm_malloc(qm, size, file, func, line);
+	#else
+			ptr=fm_malloc(qm, size);
+	#endif
+			if (ptr)
+				/* copy, need by libssl */
+				memcpy(ptr, p, orig_size);
+	#ifdef DBG_F_MALLOC
+				fm_free(qm, p, file, func, line);
+	#else
+				fm_free(qm, p);
+	#endif
+				p=ptr;
+			}
+	}else{
+		/* do nothing */
+#ifdef DBG_F_MALLOC
+		DBG("fm_realloc: doing nothing, same size: %ld - %d\n", f->size, size);
+#endif
+	}
+#ifdef DBG_F_MALLOC
+	DBG("fm_realloc: returning %p\n", p);
+#endif
+	return p;
+}
+
+
 
 void fm_status(struct fm_block* qm)
 {

+ 7 - 0
mem/f_malloc.h

@@ -118,6 +118,13 @@ void  fm_free(struct fm_block*, void* p, char* file, char* func,
 void  fm_free(struct fm_block*, void* p);
 #endif
 
+#ifdef DBG_F_MALLOC
+void*  fm_realloc(struct fm_block*, void* p, unsigned int size, 
+					char* file, char* func, unsigned int line);
+#else
+void*  fm_realloc(struct fm_block*, void* p, unsigned int size);
+#endif
+
 void  fm_status(struct fm_block*);
 
 

+ 15 - 0
mem/mem.h

@@ -42,6 +42,17 @@
 #include "../config.h"
 #include "../dprint.h"
 
+/* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
+#ifdef F_MALLOC
+	#ifdef DBG_F_MALLOC
+		#ifndef DBG_QM_MALLOC
+			#define DBG_QM_MALLOC
+		#endif
+	#elif defined(DBG_QM_MALLOC)
+		#define DBG_F_MALLOC
+	#endif
+#endif
+
 #ifdef PKG_MALLOC
 #	ifdef VQ_MALLOC
 #		include "vq_malloc.h"
@@ -66,11 +77,14 @@
 				__FUNCTION__, __LINE__)
 #			define pkg_free(p)   vqm_free(mem_block, (p), __FILE__,  \
 				__FUNCTION__, __LINE__)
+#			warn "no proper realloc implementation, use another mem. alloc"
 #		elif defined F_MALLOC
 #			define pkg_malloc(s) fm_malloc(mem_block, (s),__FILE__, \
 				__FUNCTION__, __LINE__)
 #			define pkg_free(p)   fm_free(mem_block, (p), __FILE__,  \
 				__FUNCTION__, __LINE__)
+#			define pkg_realloc(p, s) fm_realloc(mem_block, (p), (s),__FILE__, \
+				__FUNCTION__, __LINE__)
 #		else
 #			define pkg_malloc(s) qm_malloc(mem_block, (s),__FILE__, \
 				__FUNCTION__, __LINE__)
@@ -85,6 +99,7 @@
 #			define pkg_free(p)   vqm_free(mem_block, (p))
 #		elif defined F_MALLOC
 #			define pkg_malloc(s) fm_malloc(mem_block, (s))
+#			define pkg_realloc(p, s) fm_realloc(mem_block, (p), (s))
 #			define pkg_free(p)   fm_free(mem_block, (p))
 #		else
 #			define pkg_malloc(s) qm_malloc(mem_block, (s))

+ 1 - 1
mem/q_malloc.c

@@ -268,7 +268,7 @@ static inline struct qm_frag* qm_find_free(struct qm_block* qm,
 
 
 /* returns 0 on success, -1 on error;
- * new_size < size & rounduped already!*/
+ * new_size < size & rounded-up already!*/
 static inline
 #ifdef DBG_QM_MALLOC
 int split_frag(struct qm_block* qm, struct qm_frag* f, unsigned int new_size,

+ 14 - 0
mem/shm_mem.h

@@ -64,11 +64,13 @@
 #	define MY_FREE vqm_free
 #	define MY_STATUS vqm_status
 #	define  shm_malloc_init vqm_malloc_init
+#	warn "no proper vq_realloc implementantion, try another memory allocator"
 #elif defined F_MALLOC
 #	include "f_malloc.h"
 	extern struct fm_block* shm_block;
 #	define MY_MALLOC fm_malloc
 #	define MY_FREE fm_free
+#	define MY_REALLOC fm_realloc
 #	define MY_STATUS fm_status
 #	define  shm_malloc_init fm_malloc_init
 #else
@@ -96,6 +98,18 @@ void shm_mem_destroy();
 #define shm_lock()    lock_get(mem_lock)
 #define shm_unlock()  lock_release(mem_lock)
 
+/* fix DBG MALLOC stuff */
+
+/* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
+#ifdef F_MALLOC
+	#ifdef DBG_F_MALLOC
+		#ifndef DBG_QM_MALLOC
+			#define DBG_QM_MALLOC
+		#endif
+	#elif defined(DBG_QM_MALLOC)
+		#define DBG_F_MALLOC
+	#endif
+#endif
 
 
 #ifdef DBG_QM_MALLOC

+ 2 - 3
route.c

@@ -370,9 +370,8 @@ inline static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
 			 * !!??!! review: remove this? */
 			he=rev_resolvehost(ip);
 			if (he==0){
-				DBG( "comp_ip: could not rev_resolve ip address: ");
-				print_ip(ip);
-				DBG("\n");
+				print_ip( "comp_ip: could not rev_resolve ip address: ",
+							ip, "\n");
 				ret=0;
 			}else{
 				/*  compare with primary host name */

+ 2 - 2
route_struct.c

@@ -184,7 +184,7 @@ void print_expr(struct expr* exp)
 					print_net((struct net*)exp->r.param);
 					break;
 			case IP_ST:
-					print_ip((struct ip_addr*)exp->r.param);
+					print_ip("", (struct ip_addr*)exp->r.param, "");
 					break;
 			case ACTIONS_ST:
 					print_action((struct action*)exp->r.param);
@@ -328,7 +328,7 @@ void print_action(struct action* a)
 					DBG("%lu",t->p1.number);
 					break;
 			case IP_ST:
-					print_ip((struct ip_addr*)t->p1.data);
+					print_ip("", (struct ip_addr*)t->p1.data, "");
 					break;
 			case EXPR_ST:
 					print_expr((struct expr*)t->p1.data);

+ 8 - 7
tcp_main.c

@@ -147,6 +147,8 @@ struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
 		c->rcv.dst_ip=ba->address;
 		c->rcv.dst_port=ba->port_no;
 	}
+	print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
+	DBG(     "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
 	init_tcp_req(&c->req);
 	c->id=connection_id++;
 	c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
@@ -313,15 +315,15 @@ struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
 	unsigned hash;
 	
 #ifdef EXTRA_DEBUG
-	DBG("tcpconn_find: %d ",id ); print_ip(ip); DBG(" %d\n", port);
+	DBG("tcpconn_find: %d  port %d\n",id, port);
+	print_ip("tcpconn_find: ip ", ip, "\n");
 #endif
 	if (id){
 		hash=tcp_id_hash(id);
 		for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
 #ifdef EXTRA_DEBUG
-			DBG("c=%p, c->id=%d, ip=",c, c->id);
-			print_ip(&c->rcv.src_ip);
-			DBG(" port=%d\n", c->rcv.src_port);
+			DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
+			print_ip("ip=", &c->rcv.src_ip, "\n");
 #endif
 			if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
 		}
@@ -329,9 +331,8 @@ struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
 		hash=tcp_addr_hash(ip, port);
 		for (c=tcpconn_addr_hash[hash]; c; c=c->next){
 #ifdef EXTRA_DEBUG
-			DBG("c=%p, c->id=%d, ip=",c, c->id);
-			print_ip(&c->rcv.src_ip);
-			DBG(" port=%d\n", c->rcv.src_port);
+			DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
+			print_ip("ip=",&c->rcv.src_ip,"\n");
 #endif
 			if ( (c->state!=S_CONN_BAD) && (port==c->rcv.src_port) &&
 					(ip_addr_cmp(ip, &c->rcv.src_ip)) )

+ 4 - 4
tcp_read.c

@@ -450,16 +450,16 @@ again:
 					  "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error,
 					  (int)(req->pos-req->buf), req->buf,
 					  (int)(req->parsed-req->start), req->start);
-			DBG("- received from: port %d, ip -", con->rcv.src_port);
-			print_ip(&con->rcv.src_ip); DBG("-\n");
+			DBG("- received from: port %d\n", con->rcv.src_port);
+			print_ip("- received from: ip ",&con->rcv.src_ip, "\n");
 			resp=CONN_ERROR;
 			goto end_req;
 		}
 		if (req->complete){
 #ifdef EXTRA_DEBUG
 			DBG("tcp_read_req: end of header part\n");
-			DBG("- received from: port %d, ip - ", con->rcv.src_port);
-			print_ip(&con->rcv.src_ip); DBG("-\n");
+			DBG("- received from: port %d\n", con->rcv.src_port);
+			print_ip("- received from: ip ", &con->rcv.src_ip, "\n");
 			DBG("tcp_read_req: headers:\n%.*s.\n",
 					(int)(req->body-req->start), req->start);
 #endif