shithub: riscv

Download patch

ref: 14ac74fce1017b5c23ff4aed6f0289ab82bf71a2
parent: fb96a050f8a9d5f23da3557ba89251ffb73889bf
author: cinap_lenrek <[email protected]>
date: Sun Oct 22 14:03:00 EDT 2023

ndb/dns: embedd name string into DN struct

Avoid some indirection and malloc overhad
and just embedd the name string directy in
the DN struct at the end.

--- a/sys/src/cmd/ndb/dn.c
+++ b/sys/src/cmd/ndb/dn.c
@@ -213,11 +213,11 @@
 {
 	DN **l;
 	DN *dp;
+	int n;
 
 	l = &ht[dnhash(name)];
 	lock(&dnlock);
 	for(dp = *l; dp; dp = dp->next) {
-		assert(dp->magic == DNmagic);
 		if(dp->class == class && cistrcmp(dp->name, name) == 0)
 			goto out;
 		l = &dp->next;
@@ -228,9 +228,9 @@
 		return nil;
 	}
 	dnvars.names++;
-	dp = emalloc(sizeof(*dp));
-	dp->magic = DNmagic;
-	dp->name = estrdup(name);
+	n = strlen(name) + 1;
+	dp = emalloc(sizeof(*dp) + n);
+	memmove(dp->name, name, n);
 	dp->class = class;
 	dp->rr = nil;
 	/* add new DN to tail of the hash list.  *l points to last next ptr. */
@@ -486,7 +486,7 @@
 
 	l = &dp->rr;
 	while ((rp = *l) != nil){
-		assert(rp->magic == RRmagic && rp->cached);
+		assert(rp->cached);
 		if(!rp->db && ((long)(rp->expire - now) <= 0
 		|| (long)(now - (rp->expire - rp->ttl)) > dnvars.oldest))
 			rrdelhead(l); /* rp == *l before; *l == rp->next after */
@@ -585,13 +585,10 @@
 		l = &ht[i];
 		for(dp = *l; dp; dp = *l){
 			if(dp->mark == dnvars.mark){
-				assert(dp->magic == DNmagic);
 				assert(dp->rr == nil);
 				*l = dp->next;
 
-				free(dp->name);
 				memset(dp, 0, sizeof *dp); /* cause trouble */
-				dp->magic = ~DNmagic;
 				free(dp);
 
 				dnvars.names--;
@@ -735,10 +732,10 @@
 	DN *dp;
 	ulong ttl;
 
-	assert(new->magic == RRmagic && !new->cached);
+	assert(!new->cached);
 
 	dp = new->owner;
-	assert(dp != nil && dp->magic == DNmagic);
+	assert(dp != nil);
 	new->auth |= auth;
 	new->next = 0;
 
@@ -760,7 +757,7 @@
 	 */
 	l = &dp->rr;
 	for(rp = *l; rp; rp = *l){
-		assert(rp->magic == RRmagic && rp->cached);
+		assert(rp->cached);
 		if(rp->type == new->type)
 			break;
 		l = &rp->next;
@@ -776,7 +773,7 @@
 	 *  fields (e.g. multiple NS servers).
 	 */
 	while ((rp = *l) != nil){
-		assert(rp->magic == RRmagic && rp->cached);
+		assert(rp->cached);
 		if(rp->type != new->type)
 			break;
 
@@ -864,7 +861,6 @@
 	Null *null;
 	Txt *t, *nt, **l;
 
-	assert(rp->magic == RRmagic);
 	nrp = rralloc(rp->type);
 	switch(rp->type){
 	case Tsoa:
@@ -965,8 +961,6 @@
 {
 	RR *rp, *first, **last;
 
-	assert(dp->magic == DNmagic);
-
 	first = nil;
 	last = &first;
 	lock(&dnlock);
@@ -973,7 +967,7 @@
 
 	/* try for an authoritative db entry */
 	for(rp = dp->rr; rp; rp = rp->next){
-		assert(rp->magic == RRmagic && rp->cached);
+		assert(rp->cached);
 		if(rp->db)
 		if(rp->auth)
 		if(tsame(type, rp->type))
@@ -1742,15 +1736,14 @@
 char*
 estrdup(char *s)
 {
-	int size;
 	char *p;
+	int n;
 
-	size = strlen(s);
-	p = mallocz(size+1, 0);
+	n = strlen(s) + 1;
+	p = mallocz(n, 0);
 	if(p == nil)
 		abort();
-	memmove(p, s, size);
-	p[size] = 0;
+	memmove(p, s, n);
 	setmalloctag(p, getcallerpc(&s));
 	return p;
 }
@@ -1842,13 +1835,10 @@
 
 	while(*l)
 		l = &(*l)->next;
-	n = strlen(name);
-	s = malloc(sizeof(Server)+n+1);
-	if(s == nil)
-		return;
+	n = strlen(name) + 1;
+	s = emalloc(sizeof(*s) + n);
 	s->name = (char*)(s+1);
 	memmove(s->name, name, n);
-	s->name[n] = 0;
 	s->next = nil;
 	*l = s;
 }
@@ -1919,7 +1909,6 @@
 	RR *rp;
 
 	rp = emalloc(sizeof(*rp));
-	rp->magic = RRmagic;
 	rp->pc = getcallerpc(&type);
 	rp->type = type;
 	if (rp->type != type)
@@ -1971,7 +1960,7 @@
 {
 	Txt *t;
 
-	assert(rp->magic == RRmagic && !rp->cached);
+	assert(!rp->cached);
 
 	switch(rp->type){
 	case Tsoa:
@@ -2019,6 +2008,5 @@
 	}
 
 	memset(rp, 0, sizeof *rp);		/* cause trouble */
-	rp->magic = ~RRmagic;
 	free(rp);
 }
--- a/sys/src/cmd/ndb/dnresolve.c
+++ b/sys/src/cmd/ndb/dnresolve.c
@@ -196,7 +196,7 @@
 static void
 queryinit(Query *qp, DN *dp, int type, Request *req)
 {
-	assert(dp && dp->magic == DNmagic);
+	assert(dp != nil);
 
 	memset(qp, 0, sizeof *qp);
 	qp->udpfd = qp->tcpfd = qp->tcpctlfd = -1;
@@ -763,7 +763,6 @@
 	mark = 1UL<<type;
 	arp = nil;
 	for(rp = qp->nsrp; rp; rp = rp->next){
-		assert(rp->magic == RRmagic);
 		if(rp->marker & mark)
 			continue;
 		arp = rrlookup(rp->host, type, NOneg);
--- a/sys/src/cmd/ndb/dns.h
+++ b/sys/src/cmd/ndb/dns.h
@@ -139,9 +139,6 @@
 
 	RRnames=	8,	/* # of referenced names per RR */
 
-	RRmagic=	0xdeadbabe,
-	DNmagic=	0xa110a110,
-
 	/* parallelism: tune; was 32; allow lots */
 	Maxactive=	250,
 
@@ -188,13 +185,12 @@
 struct DN
 {
 	DN	*next;		/* hash collision list */
-	char	*name;		/* owner */
 	RR	*rr;		/* resource records off this name */
 	ulong	ordinal;
 	ushort	class;		/* RR class */
 	uchar	respcode;	/* response code */
 	uchar	mark;		/* for mark and sweep */
-	ulong	magic;
+	char	name[];		/* owner */
 };
 
 /*
@@ -254,7 +250,6 @@
 struct RR
 {
 	RR	*next;
-	ulong	magic;
 	DN	*owner;		/* domain that owns this resource record */
 	uintptr	pc;		/* for tracking memory allocation */
 	ulong	ttl;		/* time to live to be passed on */