code: mafs

Download patch

ref: b055ce05f9e89d524b11ec1dbc40f0d5b93707b4
parent: dc27391f182ca42b6937671953bdeb639c787d8b
author: 9ferno <gophone2015@gmail.com>
date: Wed Oct 26 13:02:44 EDT 2022

using extents in Rawblocksize units instead of bytes

--- a/extents.c
+++ b/extents.c
@@ -179,7 +179,7 @@
 	return e;
 }
 
-int
+s64
 belongs(Extent *e, u64 start, u64 len)
 {
 	if(e == nil)
@@ -202,7 +202,7 @@
 Extent *
 doadd(Extents *es, u64 start, u64 len)
 {
-	int dir;
+	s64 dir;
 	Extent *e;
 
 	if(es == nil)
@@ -221,7 +221,7 @@
 	e = es->lru;
 	dir = belongs(es->lru, start, len);
 	if(chatty9p > 7){
-		print(" 	belongs(e %llud %llud start %llud .. %llud) %d\n",
+		print(" 	belongs(e %llud %llud start %llud .. %llud) %llud\n",
 			 e->start, e->start+e->len-1, start, start+len-1, dir);
 	}
 	if(dir == 0)
@@ -307,6 +307,7 @@
 	if(fhigh == nil){
 		dlow->high = nil;
 		es->lru = dlow;
+		es->n--;
 	}
 
 	/* nil e f => nil f */
@@ -313,6 +314,7 @@
 	if(dlow == nil){
 		fhigh->low = nil;
 		es->lru = es->head = fhigh;
+		es->n--;
 	}
 
 	if(dlow != nil && fhigh != nil){
@@ -319,6 +321,7 @@
 		dlow->high = fhigh;
 		fhigh->low = dlow;
 		es->lru = fhigh;
+		es->n--;
 	}
 	start = e->start;
 	free(e);
@@ -356,7 +359,7 @@
 		snprint(msg, 64, "balloc() %llud blocks:\n", n);
 		showextents(2, msg, es);
 	}*/
-	/* TODO get rid of this call to the lowest by using Extent *head */
+
 	for(e = lowest(es); e != nil && e->len < n; e = e->high)
 		;
 	if(e == nil){
--- a/iobuf.c
+++ b/iobuf.c
@@ -10,17 +10,20 @@
 	and write queue
  */
 Extents memunits = {0};
-u8 *memunitpool = 0;
+u8 *memunitpool = nil;
+u8 *memunitstart = nil;
 
+/* using nunits + 1 for alignment */
 void
 initmemunitpool(u64 nunits)
 {
-	memunitpool = sbrk(nunits * Rawblocksize);
+	memunitstart = sbrk((nunits+1) * Rawblocksize);
+	memunitpool = memunitstart+Rawblocksize- ((u64)memunitstart%Rawblocksize);
 	initextents(&memunits);
 	if(chatty9p > 4)
 		dprint("initmemunitpool: memunitpool %p nunits*Rawblocksize %p\n",
 				memunitpool, nunits*Rawblocksize);
-	bfree(&memunits, (u64)memunitpool, nunits*Rawblocksize);
+	bfree(&memunits, 0, nunits);
 }
 
 u8 *
@@ -28,14 +31,11 @@
 {
 	u64 m;
 
-	m = balloc(&memunits, Rawblocksize);
-	if(m == 0)
-		panic("allocmemunit: no memory available");
-
+	m = balloc(&memunits, 1);
 	if(chatty9p > 4)
 		dprint("allocmemunit: memunitpool %p m %p\n",
 				memunitpool, m);
-	return (u8*)m;
+	return memunitpool+(m*Rawblocksize);
 }
 
 void
@@ -43,9 +43,11 @@
 {
 	if(m == 0)
 		panic("freememunit: m == 0\n");
-	bfree(&memunits, (u64)m, Rawblocksize);
+	if((m-memunitpool)%Rawblocksize)
+		panic("freememunit: (m-memunitpool)%%Rawblocksize %llud\n",
+				(u64)(m-memunitpool)%Rawblocksize);
+	bfree(&memunits, (m-memunitpool)/Rawblocksize, 1);
 }
-
 
 /*
    add an Iobuf to the collisions lru linked list