shithub: gefs

Download patch

ref: f3b230905d394348aae96da2d3140a629902422f
parent: 0170f073563a6fc46d36f4edeff0f9faa0dde7b1
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Dec 16 10:31:53 EST 2021

tree: remove silly inline/ptr distinction from values

we're packing/unpacking them anyways, just do it when we
use them; it's less code and less confusing.

--- a/check.c
+++ b/check.c
@@ -27,9 +27,10 @@
 {
 	Kvp x, y;
 	Msg mx, my;
-	int i, r;
+	int i, r, fill;
 	Blk *c;
 	int fail;
+	Bptr bp;
 
 	fail = 0;
 	if(h < 0){
@@ -64,16 +65,17 @@
 			fail++;
 		}
 		if(b->type == Tpivot){
-			if(isfree(x.bp.addr)){
-				fprint(fd, "freed block in use: %llx\n", x.bp.addr);
+			bp = getptr(&x, &fill);
+			if(isfree(bp.addr)){
+				fprint(fd, "freed block in use: %llx\n", bp.addr);
 				fail++;
 			}
-			if((c = getblk(x.bp, 0)) == nil){
+			if((c = getblk(bp, 0)) == nil){
 				fprint(fd, "corrupt block: %r\n");
 				fail++;
 				continue;
 			}
-			if(blkfill(c) != x.fill){
+			if(blkfill(c) != fill){
 				fprint(fd, "mismatched block fill\n");
 				fail++;
 			}
@@ -98,7 +100,8 @@
 	}
 	if(b->type == Tpivot){
 		getval(b, b->nval-1, &y);
-		if((c = getblk(x.bp, 0)) == nil){
+		bp = getptr(&x, &fill);
+		if((c = getblk(bp, 0)) == nil){
 			fprint(fd, "corrupt block: %r\n");
 			fail++;
 		}
--- a/dat.h
+++ b/dat.h
@@ -48,6 +48,7 @@
 	Keymax	= 128,			/* key data limit */
 	Inlmax	= 256,			/* inline data limit */
 	Ptrsz	= 24,			/* off, hash, gen */
+	Pptrsz	= 26,			/* off, hash, gen, fill */
 	Fillsz	= 2,			/* block fill count */
 	Offksz	= 17,			/* type, qid, off */
 	Snapsz	= 9,			/* tag, snapid */
@@ -386,19 +387,8 @@
 };
 
 struct Val {
-	int type;
-	union {
-		/* block pointer */
-		struct {
-			Bptr	bp;
-			ushort	fill;
-		};
-		/* inline values */
-		struct {
-			short	nv;
-			char	*v;
-		};
-	};
+	short	nv;
+	char	*v;
 };
 
 struct Kvp {
--- a/dump.c
+++ b/dump.c
@@ -44,7 +44,7 @@
 }
 
 static int
-showval(Fmt *fmt, Kvp *v, int op)
+showval(Fmt *fmt, Kvp *v, int op, int flg)
 {
 	char *p;
 	Bptr bp;
@@ -52,6 +52,11 @@
 	int n, wop, ht;
 
 	n = 0;
+	if(flg){
+		assert(v->nv == Ptrsz+2);
+		n = fmtprint(fmt, "(%B,%d)", unpackbp(v->v), GBIT16(v->v+Ptrsz));
+		return n;
+	}
 	switch(v->k[0]){
 	case Kdat:	/* qid[8] off[8] => ptr[16]:	pointer to data page */
 		switch(op){
@@ -61,10 +66,7 @@
 			break;
 		case Onop:
 		case Oinsert:
-			bp.addr = GBIT64(v->v+0);
-			bp.hash = GBIT64(v->v+8);
-			bp.gen = GBIT64(v->v+16);
-			n = fmtprint(fmt, "ptr:%B", bp);
+			n = fmtprint(fmt, "ptr:%B", unpackbp(v->v));
 			break;
 		}
 	case Kent:	/* pqid[8] name[n] => dir[n]:	serialized Dir */
@@ -141,8 +143,9 @@
 	[Owstat]	"Owstat",
 	};
 	Msg *m;
-	int n;
+	int f, n;
 
+	f = (fmt->flags & FmtSharp) != 0;
 	m = va_arg(fmt->args, Msg*);
 	if(m == nil)
 		return fmtprint(fmt, "Msg{nil}");
@@ -149,7 +152,7 @@
 	n = fmtprint(fmt, "Msg(%s, ", opname[m->op]);
 	n += showkey(fmt, m);
 	n += fmtprint(fmt, ") => (");
-	n += showval(fmt, m, m->op);
+	n += showval(fmt, m, m->op, f);
 	n += fmtprint(fmt, ")");
 	return n;
 }
@@ -158,8 +161,9 @@
 Pconv(Fmt *fmt)
 {
 	Kvp *kv;
-	int n;
+	int f, n;
 
+	f = (fmt->flags & FmtSharp) != 0;
 	kv = va_arg(fmt->args, Kvp*);
 	if(kv == nil)
 		return fmtprint(fmt, "Kvp{nil}");
@@ -166,10 +170,7 @@
 	n = fmtprint(fmt, "Kvp(");
 	n += showkey(fmt, kv);
 	n += fmtprint(fmt, ") => (");
-	if(kv->type == Vinl)
-		n += showval(fmt, kv, Onop);
-	else
-		n += fmtprint(fmt, "(%B,%ud))", kv->bp, kv->fill);
+	n += showval(fmt, kv, Onop, f);
 	n += fmtprint(fmt, ")");
 	return n;
 }
@@ -215,6 +216,7 @@
 {
 	Blk *c;
 	int i;
+	Bptr bp;
 	Kvp kv;
 	Msg m;
 
@@ -233,13 +235,16 @@
 	}
 	for(i = 0; i < b->nval; i++){
 		getval(b, i, &kv);
-		fprint(fd, "%.*s[%03d]|%P\n", 4*indent, spc, i, &kv);
 		if(b->type == Tpivot){
-			if((c = getblk(kv.bp, 0)) == nil)
+			fprint(fd, "%.*s[%03d]|%#P\n", 4*indent, spc, i, &kv);
+			bp = unpackbp(kv.v);
+			if((c = getblk(bp, 0)) == nil)
 				sysfatal("failed load: %r");
 			if(recurse)
 				rshowblk(fd, c, indent + 1, 1);
 			putblk(c);
+		}else{
+			fprint(fd, "%.*s[%03d]|%P\n", 4*indent, spc, i, &kv);
 		}
 	}
 }
--- a/fns.h
+++ b/fns.h
@@ -57,8 +57,9 @@
 int	keycmp(Key *, Key *);
 
 /* for dumping */
-void	getval(Blk *, int, Kvp *);
-void	getmsg(Blk *, int, Msg *);
+void	getval(Blk*, int, Kvp*);
+void	getmsg(Blk*, int, Msg*);
+Bptr	getptr(Kvp*, int*);
 
 void	initshow(void);
 void	showblk(int, Blk*, char*, int);
--- a/pack.c
+++ b/pack.c
@@ -161,7 +161,6 @@
 		werrstr("stat too big: %.*s...", 32, d->name);
 		return -1;
 	}
-	kv->type = Vinl;
 	kv->k = k;
 	kv->nk = ek - k;
 	kv->v = v;
@@ -195,6 +194,7 @@
 	vlong atime, mtime;
 	int err;
 
+	memset(d, 0, sizeof(Dir));
 	err = 0;
 	k = kv->k + 9;
 	ek = kv->k + kv->nk;
--- a/tree.c
+++ b/tree.c
@@ -23,14 +23,8 @@
 	memcpy(buf+ src->nk, src->v, src->nv);
 	dst->k = buf;
 	dst->nk = src->nk;
-	dst->type = src->type;
-	if(src->type == Vinl){
-		dst->v = buf+src->nk;
-		dst->nv = src->nv;
-	}else{
-		dst->bp = src->bp;
-		dst->fill = src->fill;
-	}
+	dst->v = buf+src->nk;
+	dst->nv = src->nv;
 }
 
 void
@@ -66,10 +60,7 @@
 int
 valsz(Kvp *kv)
 {
-	if(kv->type == Vref)
-		return 2 + 2+kv->nk + Ptrsz + Fillsz;
-	else
-		return 2 + 2+kv->nk + 2+kv->nv;
+	return 2 + 2+kv->nk + 2+kv->nv;
 }
 
 void
@@ -79,21 +70,21 @@
 
 	assert(i >= 0 && i < b->nval);
 	o = GBIT16(b->data + 2*i);
-	if(b->type == Tpivot){
-		kv->type = Vref;
-		kv->nk = GBIT16(b->data + o);
-		kv->k = b->data + o + 2;
-		kv->bp = unpackbp(kv->k + kv->nk);
-		kv->fill = GBIT16(kv->k + kv->nk + Ptrsz);
-	}else{
-		kv->type = Vinl;
-		kv->nk = GBIT16(b->data + o);
-		kv->k = b->data + o + 2;
-		kv->nv = GBIT16(kv->k + kv->nk);
-		kv->v = kv->k + kv->nk + 2;
-	}
+	kv->nk = GBIT16(b->data + o);
+	kv->k = b->data + o + 2;
+	kv->nv = GBIT16(kv->k + kv->nk);
+	kv->v = kv->k + kv->nk + 2;
 }
 
+Bptr
+getptr(Kvp *kv, int *fill)
+{
+	assert(kv->nv == Ptrsz || kv->nv == Ptrsz+2);
+	if(fill != nil)
+		*fill = GBIT16(kv->v + Ptrsz);
+	return unpackbp(kv->v);
+}
+
 void
 setval(Blk *b, int i, Kvp *kv)
 {
@@ -104,7 +95,7 @@
 	spc = (b->type == Tleaf) ? Leafspc : Pivspc;
 	p = b->data + 2*i;
 	nk = 2 + kv->nk;
-	nv = (kv->type == Vref) ? Ptrsz+Fillsz : 2 + kv->nv;
+	nv = 2 + kv->nv;
 	memmove(p + 2, p, 2*(b->nval - i));
 	b->nval++;
 	b->valsz += nk + nv;
@@ -119,22 +110,29 @@
 	assert(2*b->nval + b->valsz <= spc);
 	assert(2*b->nval <= o);
 	p = b->data + o;
-	if(b->type == Tpivot){
-		PBIT16(b->data + 2*i, o);
-		PBIT16(p +  0, kv->nk);
-		memcpy(p +  2, kv->k, kv->nk);
-		p = packbp(p + kv->nk + 2, &kv->bp);
-		PBIT16(p, kv->fill);
-	} else {
-		PBIT16(b->data + 2*i, o);
-		PBIT16(p +  0, kv->nk);
-		memcpy(p +  2, kv->k, kv->nk);
-		PBIT16(p + kv->nk + 2, kv->nv);
-		memcpy(p + kv->nk + 4, kv->v, kv->nv);
-	}
+	PBIT16(b->data + 2*i, o);
+	PBIT16(p +  0, kv->nk);
+	memcpy(p +  2, kv->k, kv->nk);
+	PBIT16(p + kv->nk + 2, kv->nv);
+	memcpy(p + kv->nk + 4, kv->v, kv->nv);
 }
 
 void
+setptr(Blk *b, int i, Key *k, Bptr bp, int fill)
+{
+	char *p, buf[Ptrsz+2];
+	Kvp kv;
+
+	kv.k = k->k;
+	kv.nk = k->nk;
+	kv.v = buf;
+	kv.nv = sizeof(buf);
+	p = packbp(buf, &bp);
+	PBIT16(p, fill);
+	setval(b, i, &kv);
+}
+
+void
 setmsg(Blk *b, int i, Msg *m)
 {
 	char *p;
@@ -170,7 +168,6 @@
 	assert(i >= 0 && i < b->nbuf);
 	o = GBIT16(b->data + Pivspc + 2*i);
 	p = b->data + Pivspc + o;
-	m->type = Vinl;
 	m->op = *p;
 	m->nk = GBIT16(p + 1);
 	m->k = p + 3;
@@ -296,10 +293,7 @@
 			if(keycmp(&kv, &m) > 0)
 				kv.Key = m.Key;
 		}
-		kv.type = Vref;
-		kv.bp = pp->nl->bp;
-		kv.fill = blkfill(pp->nl);
-		setval(n, i++, &kv);
+		setptr(n, i++, &kv, pp->nl->bp, blkfill(pp->nl));
 		if(nbytes != nil)
 			*nbytes += valsz(&kv);
 	}
@@ -310,10 +304,7 @@
 			if(keycmp(&kv, &m) > 0)
 				kv.Key = m.Key;
 		}
-		kv.type = Vref;
-		kv.bp = pp->nr->bp;
-		kv.fill = blkfill(pp->nr);
-		setval(n, i++, &kv);
+		setptr(n, i++, &kv, pp->nr->bp, blkfill(pp->nr));
 		if(nbytes != nil)
 			*nbytes += valsz(&kv);
 	}
@@ -916,7 +907,8 @@
 {
 	Blk *l, *m, *r;
 	Kvp kl, kr;
-	int ret;
+	int ret, fill;
+	Bptr bp;
 
 	l = nil;
 	r = nil;
@@ -929,8 +921,9 @@
 	m = refblk(pp->nl);
 	if(idx-1 >= 0){
 		getval(p->b, idx-1, &kl);
-		if(kl.fill + blkfill(m) < Blkspc){
-			if((l = getblk(kl.bp, 0)) == nil)
+		bp = getptr(&kl, &fill);
+		if(fill + blkfill(m) < Blkspc){
+			if((l = getblk(bp, 0)) == nil)
 				goto Out;
 			if(rotmerge(p, pp, idx-1, l, m) == -1)
 				goto Out;
@@ -939,8 +932,9 @@
 	}
 	if(idx+1 < p->b->nval){
 		getval(p->b, idx+1, &kr);
-		if(kr.fill + blkfill(m) < Blkspc){
-			if((r = getblk(kr.bp, 0)) == nil)
+		bp = getptr(&kr, &fill);
+		if(fill + blkfill(m) < Blkspc){
+			if((r = getblk(bp, 0)) == nil)
 				goto Out;
 			if(rotmerge(p, pp, idx, m, r) == -1)
 				goto Out;
@@ -1107,6 +1101,7 @@
 	Path *path;
 	Blk *b, *rb;
 	Kvp sep;
+	Bptr bp;
 
 	sz = 0;
 	qsort(msg, nmsg, sizeof(Msg), msgcmp);
@@ -1143,7 +1138,8 @@
 			break;
 		victim(b, &path[npath]);
 		getval(b, path[npath].idx, &sep);
-		b = getblk(sep.bp, 0);
+		bp = getptr(&sep, nil);
+		b = getblk(bp, 0);
 		if(b == nil)
 			goto Error;
 		npath++;
@@ -1213,6 +1209,7 @@
 	int i, j, ok, same;
 	char *err;
 	Blk **p;
+	Bptr bp;
 	Msg m;
 
 	assert(k != r);
@@ -1224,7 +1221,8 @@
 	for(i = 1; i < h; i++){
 		if(blksearch(p[i-1], k, r, nil) == -1)
 			break;
-		if((p[i] = getblk(r->bp, 0)) == nil)
+		bp = getptr(r, nil);
+		if((p[i] = getblk(bp, 0)) == nil)
 			return Efs;
 	}
 	if(p[h-1] != nil)
@@ -1274,9 +1272,10 @@
 {
 	int i, same;
 	Scanp *p;
+	Bptr bp;
+	Blk *b;
 	Msg m;
 	Kvp v;
-	Blk *b;
 
 	s->done = 0;
 	s->offset = 0;
@@ -1309,7 +1308,8 @@
 			p[i].bi = bufsearch(b, &s->kv, &m, &same);
 			if(p[i].bi == -1 || !same)
 				p[i].bi++;
-			if((b = getblk(v.bp, 0)) == nil)
+			bp = getptr(&v, nil);
+			if((b = getblk(bp, 0)) == nil)
 				return Eio;
 			p[i+1].b = b;
 		}
@@ -1321,9 +1321,10 @@
 char *
 btnext(Scan *s, Kvp *r, int *done)
 {
-	Scanp *p;
 	int i, j, h, ok, start, srcbuf;
+	Scanp *p;
 	Msg m, n;
+	Bptr bp;
 	Kvp kv;
 
 	/* load up the correct blocks for the scan */
@@ -1353,7 +1354,8 @@
 	if(p[start-1].vi < p[start-1].b->nval){
 		for(i = start; i < h; i++){
 			getval(p[i-1].b, p[i-1].vi, &kv);
-			if((p[i].b = getblk(kv.bp, 0)) == nil)
+			bp = getptr(&kv, nil);
+			if((p[i].b = getblk(bp, 0)) == nil)
 				return "error reading block";
 		}