commit 1b514694b7db293fee61cd36828ddf1e41a4f358
parent 9009cda6c00522e802a5266d1544f58183fd7c2e
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date: Fri, 18 Nov 2022 16:59:02 +0100
os9: Implement freeb()
We add a pool to reuse pages.
Diffstat:
3 files changed, 38 insertions(+), 12 deletions(-)
diff --git a/include/os9/os9.h b/include/os9/os9.h
@@ -257,6 +257,7 @@ extern void unlocktask(Task *);
extern void *alloc(size_t);
extern void *allocb(int);
extern void freeb(void *);
+extern void ialloc(void);
/* sys.c */
extern void isys(void);
diff --git a/src/os9/alloc.c b/src/os9/alloc.c
@@ -6,35 +6,44 @@
union bucket {
long long ll;
uintptr_t up;
+ union bucket *next;
};
+struct {
+ union bucket *list;
+ mutex_t m;
+} bufpool;
void
freeb(void *bp)
{
+ union bucket *p;
+
if (!bp)
return;
- /* TODO */
+
+ lock(&bufpool.m);
+ p = bp;
+ p->next = bufpool.list;
+ bufpool.list = p;
+ unlock(&bufpool.m);
}
void *
allocb(int n)
{
- void *addr = NULL;
- static unsigned long npages;
- static mutex_t m;
+ union bucket *bp = NULL;
- lock(&m);
- if (n >= NR_BUFFERS - npages) {
+ lock(&bufpool.m);
+ if (!bufpool.list) {
errno = ENOMEM;
- goto end;
+ } else {
+ bp = bufpool.list;
+ bufpool.list = bp->next;
}
- addr= buffertab[npages];
- npages += n;
-end:
- unlock(&m);
+ unlock(&bufpool.m);
- return addr;
+ return bp;
}
void *
@@ -74,3 +83,18 @@ end:
return bp;
}
+
+void
+ialloc(void)
+{
+ int i;
+ union bucket *bp, *prev;
+
+ prev = NULL;
+ for (i = 0; i < NR_BUFFERS; i++) {
+ bp = (union bucket *) buffertab[i];
+ bp->next = prev;
+ prev = bp;
+ }
+ bufpool.list = bp;
+}
diff --git a/src/os9/sys.c b/src/os9/sys.c
@@ -114,6 +114,7 @@ info(void)
void
isys(void)
{
+ ialloc();
iproc();
idev();
iconf();