ref: 3431e61137396389bafa1dcbc040fc3bd9de85f1
parent: 6072ac40fe755bf6deddfbcec42135cd01af2eac
author: 9ferno <[email protected]>
date: Fri Jan 6 12:38:50 EST 2023
rename qalloc to allot and qfree to release
--- a/extents.c
+++ b/extents.c
@@ -735,12 +735,11 @@
return oldstart;
}
-/* allocate n blocks and return that block number */
-u64
-qalloc(Extents *es, u64 n)
+/* allocate n blocks and return a status. start has a block number if allocated */
+s8
+allot(Extents *es, u64 n, u64 *start)
{
Extent *e, *euse;
- u64 start;
char msg[64];
s64 dir;
@@ -748,8 +747,7 @@
es->panic("qalloc: es == nil");
if(es->n == 0)
es->panic("qalloc entering es->n == 0\n");
- start = 0;
- USED(start);
+
qlock(&es->lck);
if(es->n == 0)
rsleep(&es->isempty);
@@ -790,17 +788,22 @@
qunlock(&es->lck);
(*es->flush)();
qlock(&es->lck);
- }else
- rsleep(&es->isempty);
+ }else{
+ if(es->linger)
+ rsleep(&es->isempty);
+ else
+ return 0;
+ }
goto again;
}
else if(e->len == n)
- start = pluck(es, e);
- else /* found something bigger */
- start = slice(es, e, n);
+ *start = pluck(es, e);
+ else{ /* found something bigger */
+ *start = slice(es, e, n);
+ }
if(es->log && es->loglevel == Everbose){
- es->log(es->logfd, "qalloc()'ed start %llud len %llud blocks:\n", start, n);
+ es->log(es->logfd, "qalloc()'ed start %llud len %llud blocks:\n", *start, n);
}else if(es->log && es->loglevel == Edebug){
snprint(msg, 64, "qalloc() %llud blocks:\n", n);
showextentslists(es->logfd, msg, es);
@@ -810,8 +813,8 @@
qunlock(&es->lck);
/* 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;
+ es->log(es->logfd, "%s-%llud %llud\n", es->name, *start, n);
+ return 1;
}
/*
@@ -823,7 +826,7 @@
free n blocks allocated at block number
*/
void
-qfree(Extents *es, u64 start, u64 len)
+release(Extents *es, u64 start, u64 len)
{
char msg[64];
@@ -955,7 +958,7 @@
p = ep;
p++; /* to skip over the new line */
- qfree(es, start, nblocks);
+ release(es, start, nblocks);
}
return es->n;
}
@@ -1063,7 +1066,7 @@
}
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))
+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))
{
es->isempty.l = &es->lck;
if(name != nil)
@@ -1071,6 +1074,7 @@
es->loglevel = loglevel;
es->logfd = logfd;
es->flush = flush;
+ es->linger = linger;
es->log = log;
es->panic = panic;
es->malloc = malloc;
@@ -1104,7 +1108,7 @@
return nil;
for(e=lowest(es); e!=nil && e->high != nil; e=e->high){
start = e->start+e->len;
- qfree(inv, start, e->high->start-start);
+ release(inv, start, e->high->start-start);
}
return inv;
}
--- a/extents.h
+++ b/extents.h
@@ -50,13 +50,14 @@
*/
struct Extents {
char name[32];
- void (*flush)(void); /* flush when nothing is available */
+ void (*flush)(void);/* flush when nothing is available */
+ u8 linger; /* wait until something becomes available */
u8 loglevel; /* Everbose or Edebug */
u32 logfd;
void (*log)(int fd, char *fmt, ...);
void (*panic)(char *fmt, ...);
- void *(*malloc)(u32 sz);
+ void *(*malloc)(u32 sz); /* as 9p servers use a different malloc */
/* private and derived */
Extent *lowest; /* find the first block number in a jiffy */
@@ -78,12 +79,13 @@
struct Extent *prev, *next;
};
-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 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 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);
+/* 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);
Extents *holes(Extents *es, Extents *inv);
void showblocknos(int fd, Extents *es);
--- a/testextents.c
+++ b/testextents.c
@@ -67,17 +67,17 @@
bp = Bfdopen(0, OREAD);
Blethal(bp, nil);
- initextents(&es, "testextents", 0, 1, nil, nil, panic, emalloc);
+ initextents(&es, "testextents", 0, 0, 1, nil, nil, panic, emalloc);
while((line = Brdstr(bp, '\n', 1)) != nil) {
act = line[0];
if(act == '-'){
len = strtoull(line+1, nil, 10);
- qalloc(&es, len);
+ allot(&es, len);
}else{
bno = strtoull(line, &p, 10);
p++; /* for the space */
len = strtoull(p, nil, 10);
- qfree(&es, bno, len);
+ release(&es, bno, len);
}
free(line);
}