code: libextents

Download patch

ref: 49c690ea8dbcb17be8cd01deffa719a3c659a5d9
parent: f9014997b1759aeeeabac9707bedc12ca809691e
author: 9ferno <gophone2015@gmail.com>
date: Sun Jan 15 10:48:57 EST 2023

binary extents files

--- a/extents.c
+++ b/extents.c
@@ -872,22 +872,8 @@
 s32
 sizeofextents(Extents *es)
 {
-	u64 n, used;
-	s8 tmp[128];
-	Extent *e;
-
-	used = 0;
-	qlock(&es->lck);
-	for(e = lowest(es); e != nil; e = e->high){
-		n = snprint(tmp, 128, "%llud %llud %llud\n",
-						e->start, e->start+e->len-1, e->len);
-		if(n == 128)
-			es->panic("sizeofextents(): increase tmp size");
-		used += n;
-	}
-	// keep it locked?
-	qunlock(&es->lck);
-	return used;
+	/* Do I need a lock? */
+	return es->n**3*sizeof(u64);
 }
 /*
 	write to *buf returns the length written.
@@ -898,27 +884,22 @@
 s32
 saveextents(Extents *es, s8 *buf, u32 nbuf)
 {
-	u64 used;
-	Extent *e;
-	s32 ret;
+	u64 *p, *sp;
 
-	used = 0;
+	sp = p = (u64*)buf;
 	qlock(&es->lck);
+	if(es->n*3*sizeof(u64) < nbuf){
+		qunlock(&es->lck);
+		return -1;
+	}
 	for(e = lowest(es); e != nil; e = e->high){
-		used += snprint(buf+used, nbuf-used,
-						"%llud %llud %llud\n",
-						e->start, e->start+e->len-1, e->len);
-		if(used >= nbuf){
-			es->panic("saveextents(): increase buf size");
-			ret = -1;	/* increase buf size */
-			goto end;
-		}
+		*p++ = e->start;
+		*p++ = e->start+e->len-1;
+		*p++ = e->len;
 	}
-	ret = used;
-	// keep it locked?
 end:
 	qunlock(&es->lck);
-	return ret;
+	return (s32)(p-sp);
 }
 
 /* load the extents from buf of length nbuf */
@@ -927,36 +908,18 @@
 s32
 loadextents(Extents *es, s8 *buf, u32 nbuf)
 {
-	s8 *p, *ep;
-	u64 start, end, nblocks;
+	u64 *p, *ep;
+	u64 start, end, nunits, n;
 
 	p = buf;
 	if(es->lru != nil || es->n != 0){
 		es->panic("extents already loaded.\n"
-			"	TODO make loadextents() be called multiple times");
+			"	Do I need to make loadextents() be called multiple times?");
 	}
-	while(*p != 0 && p-buf < nbuf){
-		start = strtoull(p, &ep, 10);
-		if(p == ep)
-			es->panic("could not read");
-
-		p = ep;
-		p += 1; /* skip over the space */
-		end = strtoull(p, &ep, 10);
-		if(p == ep)
-			es->panic("could not read");
-
-		p = ep;
-		p += 1; /* skip over the space */
-		nblocks = strtoull(p, &ep, 10);
-		if(p == ep)
-			es->panic("could not read");
-		if(end-start+1 != nblocks)
-			es->panic("loadextents does not match up: start %llud end %llud nblocks %llud",
-					start, end, nblocks);
-
-		p = ep;
-		p++; /* to skip over the new line */
+	for(n = nbuf/(3*sizeof(u64)); n>0; n--){
+		start = *p++;
+		end = *p++;
+		nunits = *p++;
 		ufree(es, start, nblocks);
 	}
 	return es->n;