ref: 4b2ffdef1f8ca76d8a608f4c43f6a0ae13997bf7
parent: b538318ab3f7b3c31fadd1a917767c3e3451755a
author: Noam Preil <noam@pixelhero.dev>
date: Tue Sep 24 06:01:13 EDT 2024
cache: use atomic counter for initial buffer allocation
--- a/cache.c
+++ b/cache.c
@@ -16,8 +16,8 @@
static char *data;
static bucket_t *buckets, *bucketend;
-static u32int freebufs;
-static Lock freelock;
+static long nextbuf = 0;
+static int full = 0;
enum {
BUCKETSIZE = 8,
@@ -71,7 +71,6 @@
// alignment
assert(((usize)buckets&0b111111) == 0);
bucketend = buckets + BUCKETS;
- freebufs = REQENTRIES;
}
// For now: random eviction.
@@ -104,14 +103,13 @@
grabfree(void)
{
u32int result;
- lock(&freelock);
- if(freebufs == 0){
- result = evict();
- } else {
- result = REQENTRIES - freebufs;
- freebufs -= 1;
+ if(full)
+ return evict();
+ result = ainc(&nextbuf);
+ if(result >= REQENTRIES){
+ full = 1;
+ return evict();
}
- unlock(&freelock);
return result;
}