code: libextents

Download patch

ref: 6072ac40fe755bf6deddfbcec42135cd01af2eac
parent: fb7898cf13af74d49c56e7f04273b82b281b0515
author: 9ferno <gophone2015@gmail.com>
date: Tue Jan 3 16:17:23 EST 2023

removed Extents.quantum as it is only useful in one instance

--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@
 !tests/test.8
 *.iso
 *.iso.gz
+libextents.*.a
--- a/extents.c
+++ b/extents.c
@@ -808,8 +808,7 @@
 	if(es->n == 0)
 		es->panic("qalloc exiting es->n == 0\n");
 	qunlock(&es->lck);
-	/* uncomment the below line and the other in qfree() for
-		generating test cases of unforeseen behaviour */
+	/* use to generate test cases of unforeseen behaviour */
 	if(es->log && es->loglevel)
 		es->log(es->logfd, "%s-%llud %llud\n", es->name, start, n);
 	return start;
@@ -832,8 +831,7 @@
 		es->panic("qfree: es == nil");
 	if(len <= 0)
 		es->panic("qfree: len <= 0");
-	/* uncomment the below line and the other in qalloc() for
-		generating test cases of unforeseen behaviour */
+	/* use to generate test cases of unforeseen behaviour */
 	if(es->log && es->loglevel)
 		es->log(es->logfd, "%s+%llud %llud\n", es->name, start, len);
 	qlock(&es->lck);
@@ -1065,12 +1063,11 @@
 }
 
 void
-initextents(Extents *es, char *name, u64 quantum, 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 loglevel, u32 logfd, void (*flush)(void), void (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz))
 {
 	es->isempty.l = &es->lck;
 	if(name != nil)
 		strncpy(es->name, name, 32);
-	es->quantum = quantum;
 	es->loglevel = loglevel;
 	es->logfd = logfd;
 	es->flush = flush;
@@ -1077,6 +1074,20 @@
 	es->log = log;
 	es->panic = panic;
 	es->malloc = malloc;
+}
+
+void
+freeextents(Extents *es)
+{
+	Extent *e, *eh;
+
+	qlock(&es->lck);
+	for(e = eh = es->lowest; eh != nil; e = eh){
+		eh = e->high;
+		free(e);
+	}
+
+	free(es);
 }
 
 /*
--- a/extents.h
+++ b/extents.h
@@ -18,6 +18,7 @@
 	By default, only one line is logged for each qalloc and qfree.
  */
 
+#pragma lib "libextents.a"
 enum
 {
 	Nlru  = 32,
@@ -49,8 +50,6 @@
 */
 struct Extents {
 	char name[32];
-	u64 quantum;	/* allocations are a multiple of quantum */
-
 	void (*flush)(void);	/* flush when nothing is available */
 
 	u8 loglevel;	/* Everbose or Edebug */
@@ -59,7 +58,7 @@
 	void (*panic)(char *fmt, ...);
 	void *(*malloc)(u32 sz);
 
-	/* private */
+	/* private and derived */
 	Extent *lowest;	/* find the first block number in a jiffy */
 	QLock lck;
 	u64 n;			/* number of extents */
@@ -79,9 +78,11 @@
 	struct Extent *prev, *next;
 };
 
-void	initextents(Extents *es, char *name, u64 quantum, u8 loglevel, u32 logfd, void (*flush)(void), void (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz));
-u64	 qalloc(Extents *es, u64 len);			/* allocate len*quantum */
-void qfree(Extents *es, u64 start, u64 len);/* free len*quantum starting from start */
+void	initextents(Extents *es, char *name, u8 loglevel, u32 logfd, void (*flush)(void), void (*log)(int fd, char *fmt, ...), void (*panic)(char *fmt, ...), void *(*malloc)(u32 sz));
+void	freeextents(Extents *es);
+
+u64	 qalloc(Extents *es, u64 len);			/* allocate len units */
+void qfree(Extents *es, u64 start, u64 len);/* free len units starting from start */
 u64	 nfrees(Extents *es);
 Extents *holes(Extents *es, Extents *inv);
 
--- a/testextents.c
+++ b/testextents.c
@@ -67,7 +67,7 @@
 	bp = Bfdopen(0, OREAD);
 	Blethal(bp, nil);
 
-	initextents(&es, "testextents", 1, 0, 1, nil, nil, panic, emalloc);
+	initextents(&es, "testextents", 0, 1, nil, nil, panic, emalloc);
 	while((line = Brdstr(bp, '\n', 1)) != nil) {
 		act = line[0];
 		if(act == '-'){