code: libextents

Download patch

ref: 214285f15454d3fb306938ee5cec5af79be9c78e
parent: 3431e61137396389bafa1dcbc040fc3bd9de85f1
author: 9ferno <gophone2015@gmail.com>
date: Tue Jan 10 20:42:10 EST 2023

more fixes

--- a/extents.c
+++ b/extents.c
@@ -6,7 +6,7 @@
 void	showextent(int fd, char *pre, Extent *e);
 static int extentsfd = -1;
 
-Extent *
+static Extent *
 emalloc(void* (*malloc)(u32 sz))
 {
 	return (Extent *) malloc(sizeof(Extent));
@@ -737,7 +737,7 @@
 
 /* allocate n blocks and return a status. start has a block number if allocated */
 s8
-allot(Extents *es, u64 n, u64 *start)
+ualloc(Extents *es, u64 n, u64 *start)
 {
 	Extent *e, *euse;
 	char msg[64];
@@ -745,12 +745,10 @@
 
 	if(es == nil)
 		es->panic("qalloc: es == nil");
-	if(es->n == 0)
-		es->panic("qalloc entering es->n == 0\n");
 
 	qlock(&es->lck);
 	if(es->n == 0)
-		rsleep(&es->isempty);
+		goto Empty;
 	if(es->loglevel == Everbose){
 		es->log(es->logfd, "qalloc() %llud blocks:\n", n);
 	}else if(es->loglevel == Edebug){
@@ -784,6 +782,7 @@
 	if(e == nil){
 		snprint(msg, 64, "qalloc() %llud %s: waiting\n", n, es->name);
 		showextents(2, msg, es);
+Empty:
 		if(es->flush){
 			qunlock(&es->lck);
 			(*es->flush)();
@@ -791,8 +790,10 @@
 		}else{
 			if(es->linger)
 				rsleep(&es->isempty);
-			else
+			else{
+				qunlock(&es->lck);
 				return 0;
+			}
 		}
 		goto again;
 	}
@@ -826,7 +827,7 @@
  free n blocks allocated at block number
  */
 void
-release(Extents *es, u64 start, u64 len)
+ufree(Extents *es, u64 start, u64 len)
 {
 	char msg[64];
 
@@ -847,7 +848,7 @@
 	}
 //	if(es->n == 1) the sleeper could just be waiting for a different len block
 		rwakeup(&es->isempty);
-	if(es->n == 0)
+	if(es->n == 0) /* should never happen */
 		es->panic("qfree exiting es->n == 0\n");
 	qunlock(&es->lck);
 }
@@ -904,6 +905,7 @@
 	s32 ret;
 
 	used = 0;
+	ret = 0;
 	qlock(&es->lck);
 	for(e = lowest(es); e != nil; e = e->high){
 		used += snprint(buf+used, nbuf-used,
@@ -958,7 +960,7 @@
 
 		p = ep;
 		p++; /* to skip over the new line */
-		release(es, start, nblocks);
+		ufree(es, start, nblocks);
 	}
 	return es->n;
 }
@@ -1066,7 +1068,7 @@
 }
 
 void
-initextents(Extents *es, char *name, u8 linger, u8 loglevel, u32 logfd, void (*flush)(void), void (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz))
+initextents(Extents *es, char *name, u8 linger, u8 loglevel, u32 logfd, void (*flush)(void), int (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz))
 {
 	es->isempty.l = &es->lck;
 	if(name != nil)
@@ -1108,7 +1110,7 @@
 		return nil;
 	for(e=lowest(es); e!=nil && e->high != nil; e=e->high){
 		start = e->start+e->len;
-		release(inv, start, e->high->start-start);
+		ufree(inv, start, e->high->start-start);
 	}
 	return inv;
 }
--- a/extents.h
+++ b/extents.h
@@ -5,11 +5,13 @@
 
  Extents are used to manage contiguous space (such as memory space and disk space).
 	The space is is split into units of the same size (quantum).
+	The scaling of the addresses (start) by the unit is the responsibility
+	of the client.
 
  All space is split into a sequence of units. Each such sequence of units
 	is called an Extent. The data structure Extents is used to contain this
-	information. Space is added to the Extents using bfree() and allocated
-	using balloc(). When freed, adjacent extents are coalesced to create a
+	information. Space is added to the Extents using ufree() and allocated
+	using ualloc(). When freed, adjacent extents are coalesced to create a
 	large extent, if they are continuous.
 
  Debugging
@@ -55,7 +57,7 @@
 
 	u8 loglevel;	/* Everbose or Edebug */
 	u32 logfd;
-	void (*log)(int fd, char *fmt, ...);
+	int (*log)(int fd, char *fmt, ...);
 	void (*panic)(char *fmt, ...);
 	void *(*malloc)(u32 sz);	/* as 9p servers use a different malloc */
 
@@ -79,13 +81,13 @@
 	struct Extent *prev, *next;
 };
 
-void	initextents(Extents *es, char *name, u8 linger, u8 loglevel, u32 logfd, void (*flush)(void), void (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz));
+void	initextents(Extents *es, char *name, u8 linger, u8 loglevel, u32 logfd, void (*flush)(void), int (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz));
 void	freeextents(Extents *es);
 
 /* allocate n quanta */
-s8	 allot(Extents *es, u64 n, u64 *start);
-void release(Extents *es, u64 start, u64 len);/* free len units starting from start */
-u64	 navailable(Extents *es);
+s8	 ualloc(Extents *es, u64 n, u64 *start);
+void ufree(Extents *es, u64 start, u64 len);/* free len units starting from start */
+u64	 nfrees(Extents *es);
 Extents *holes(Extents *es, Extents *inv);
 
 void	showblocknos(int fd, Extents *es);
--- a/testextents.c
+++ b/testextents.c
@@ -72,12 +72,12 @@
 		act = line[0];
 		if(act == '-'){
 			len = strtoull(line+1, nil, 10);
-			allot(&es, len);
+			ualloc(&es, len);
 		}else{
 			bno = strtoull(line, &p, 10);
 			p++;	/* for the space */
 			len = strtoull(p, nil, 10);
-			release(&es, bno, len);
+			ufree(&es, bno, len);
 		}
 		free(line);
 	}