ref: b538318ab3f7b3c31fadd1a917767c3e3451755a
parent: 0d00f3f90ad95b4f03617ece28db893ef0b99204
author: Noam Preil <noam@pixelhero.dev>
date: Tue Sep 24 05:45:07 EDT 2024
cache: remove free list
--- a/cache.c
+++ b/cache.c
@@ -16,8 +16,7 @@
static char *data;
static bucket_t *buckets, *bucketend;
-static u32int *freelist;
-static u32int freelen;
+static u32int freebufs;
static Lock freelock;
enum {
@@ -40,8 +39,8 @@
// when selecting the portion of the fibonacci hash to use, so it's
// not strictly Fibonacci hashing that requires power-of-two bucket sizes
// so much as our high-performance implementation thereof).
- REQUIREDENTRIES = CACHESIZE / ENTRYSIZE,
- ENTRIES = REQUIREDENTRIES * TARGETDIVISOR,
+ REQENTRIES = CACHESIZE / ENTRYSIZE,
+ ENTRIES = REQENTRIES * TARGETDIVISOR,
BUCKETS = ENTRIES / BUCKETSIZE,
// CACHEBITS needs to be computed by hand because no logarithms in
// macros. 2^(CACHEBITS) = BUCKETS.
@@ -64,7 +63,6 @@
if(data == (void*)-1)
sysfatal("failed to allocate cache space of %llux", (usize)ENTRYSIZE*ENTRIES);
buckets = sbrk(64 + 64*BUCKETS);
- freelist = sbrk(sizeof(u32int) * ENTRIES);
displacement = ((usize)buckets & 0b111111);
if(displacement != 0){
displacement = 64 - displacement;
@@ -73,10 +71,7 @@
// alignment
assert(((usize)buckets&0b111111) == 0);
bucketend = buckets + BUCKETS;
- freelen = ENTRIES / TARGETDIVISOR;
- for(u32int i = 0; i < freelen; i += 1){
- freelist[i] = i;
- }
+ freebufs = REQENTRIES;
}
// For now: random eviction.
@@ -110,11 +105,11 @@
{
u32int result;
lock(&freelock);
- if(freelen == 0){
+ if(freebufs == 0){
result = evict();
} else {
- freelen -= 1;
- result = freelist[freelen];
+ result = REQENTRIES - freebufs;
+ freebufs -= 1;
}
unlock(&freelock);
return result;