shithub: neoventi

Download patch

ref: 7faf23b902bdc08a5aa221d01d537f6309b79d41
parent: b19e5132e043949c276cd7ee8742eed5994e86b2
author: Noam Preil <noam@pixelhero.dev>
date: Sun Dec 24 21:15:22 EST 2023

cleanup

--- a/disk.c
+++ b/disk.c
@@ -33,16 +33,16 @@
 	return 0;
 }
 
-static VtArena
+static VtArena*
 arenafromindex(u64int aindex)
 {
 	u64int i;
 	for(i = 0; i < numarenas; i += 1){
 		if(strcmp(arenas[i].name, index.amap[aindex].name) == 0)
-			return arenas[i];
+			return &arenas[i];
 	}
 	sysfatal("arena not found");
-	return arenas[0];
+	return nil;
 }
 
 static u64int
@@ -57,7 +57,7 @@
 }
 
 int
-vtreadlookup(u8int *score, VtArena *arena, u64int *addr, u16int *size, u8int *blocks)
+vtreadlookup(u8int *score, VtAddress *addr)
 {
 	u8int *buf;
 	u16int bentries;
@@ -76,55 +76,53 @@
 		sysfatal("index is corrupt: invalid bucket magic: sect %ux, buck %ux", sect.bucketmagic, U32GET(buf + 2));
 	if(!bucketlookup(buf + 6, bentries, score, &entry))
 		sysfatal("entry not found in bucket");
-	*addr = U64GET((buf + 6 + (entry * IEntrySize) + 26));
-	*size = U16GET((buf + 6 + (entry * IEntrySize) + 34));
-	*blocks = buf[6 + (entry*IEntrySize) + 37];
-	aindex = aindexfromaddr(*addr);
-	*arena = arenafromindex(aindex);
-	*addr -= index.amap[aindex].start;
+	addr->offset = U64GET((buf + 6 + (entry * IEntrySize) + 26));
+	addr->size = U16GET((buf + 6 + (entry * IEntrySize) + 34));
+	addr->blocks = buf[6 + (entry*IEntrySize) + 37];
+	aindex = aindexfromaddr(addr->offset);
+	addr->s_arena = arenafromindex(aindex);
+	addr->offset -= index.amap[aindex].start;
 	free(buf);
 	return 1;
 }
 
 static u64int
-arenadirsize(VtArena arena)
+arenadirsize(VtArena *arena)
 {
-	return ((arena.memstats.clumps / (arena.blocksize / 25)) + 1) * arena.blocksize;
+	return ((arena->memstats.clumps / (arena->blocksize / 25)) + 1) * arena->blocksize;
 }
 
 static void
-vtreadarena(VtArena arena, u64int addr, uchar *dbuf, u16int *size)
+vtreadarena(VtArena *arena, u64int addr, uchar *dbuf, u16int *size)
 {
-	u64int end = arena.size - arenadirsize(arena);
-	char *buf = malloc(arena.blocksize);
+	u64int end = arena->size - arenadirsize(arena);
+	char *buf = malloc(arena->blocksize);
 	u16int off, n, m;
 	if(addr + *size > end)
 		*size = end - addr;
-	addr += arena.base;
-	off = addr & (arena.blocksize-1);
+	addr += arena->base;
+	off = addr & (arena->blocksize-1);
 	addr -= off;
 	n = 0;
 	while(n < *size){
-		// Read the next block
-		if(pread(arena.fd, buf, arena.blocksize, addr) != arena.blocksize)
+		if(pread(arena->fd, buf, arena->blocksize, addr) != arena->blocksize)
 			sysfatal("pread failed!");
-		m = arena.blocksize - off;
+		m = arena->blocksize - off;
 		if(m > *size - n)
 			m = *size - n;
 		memmove(&dbuf[n], &buf[off], m);
 		n += m;
 		off = 0;
-		addr += arena.blocksize;
+		addr += arena->blocksize;
 	}
 }
 
 int
-readclump(uchar *dst, VtArena arena, u64int addr, u8int blocks)
+readclump(uchar *dst, VtAddress addr)
 {
-	uchar *buf = malloc(blocks << ABlockLog);
-	u16int size;
-	size = blocks<<ABlockLog;
-	vtreadarena(arena, addr, buf, &size);
+	u16int size = addr.blocks<<ABlockLog;
+	uchar *buf = malloc(size);
+	vtreadarena(addr.s_arena, addr.offset, buf, &size);
 	size = U16GET(buf+7);
 	if(buf[29] == 2){
 		if(unwhack(dst, size, buf+38, U16GET(buf+5)) != size)
--- a/main.c
+++ b/main.c
@@ -29,7 +29,7 @@
 threadmain(int argc, char **argv)
 {
 	parseargs(argc, argv);
-	print("Initializing neoventi build 4... ");
+	print("Initializing neoventi build 5... ");
 	init();
 	validate();
 	print("initialized, launching server.\n");
--- a/neoventi.h
+++ b/neoventi.h
@@ -67,6 +67,13 @@
 } VtArena;
 
 typedef struct {
+	VtArena *s_arena; // shared reference
+	u64int offset;
+	u16int size;
+	u8int blocks;
+} VtAddress;
+
+typedef struct {
 	int		blocklog;		/* log2(blocksize) */
 	int		buckmax;		/* max. entries in a index bucket */
 	u32int		tabbase;		/* base address of index config table on disk */
@@ -120,9 +127,9 @@
 
 int	unwhack(uchar *dst, u16int dsize, uchar *src, u16int ssize);
 void serve(char *addr);
-int readclump(uchar *dst, VtArena arena, u64int addr, u8int blocks);
-/* Looks up a score in the index */
-int vtreadlookup(u8int *score, VtArena *arena, u64int *addr, u16int *size, u8int *blocks);
+/* Looks up the address of a score on disk using the index */
+int vtreadlookup(u8int *score, VtAddress *addr);
+int readclump(uchar *dst, VtAddress addr);
 int Brdu32(Biobufhdr *bio, u32int *u32);
 int stru32int(char *s, u32int *r);
 int stru64int(char *s, u64int *r);
--- a/server.c
+++ b/server.c
@@ -64,10 +64,11 @@
 }
 
 static void
-vtsend(VtConn conn, char *buf, u16int size, u8int tag, int drop)
+vtsend(VtConn conn, char *buf, u16int size, u8int type, int drop)
 {
 	U16PUT(buf, size);
-	buf[2] = tag;
+	buf[2] = type;
+	// +2 because we need to send the u16 size as well!
 	if(write(conn.fd, buf, size+2) != size+2){
 		if(drop)
 			fprint(2, "failed to submit error packet: %r\n");
@@ -79,17 +80,11 @@
 static void
 vtread(VtConn conn, char *buf)
 {
-	u8int *score;
-	VtArena arena;
-	u64int addr;
-	u8int blocks;
-	u16int size;
-	score = (u8int*)buf + 4;
-	if(!vtreadlookup(score, &arena, &addr, &size, &blocks))
+	VtAddress addr;
+	if(!vtreadlookup((u8int*)buf + 4, &addr))
 		sysfatal("todo graceful read errors");
-	// Response: VtRread, msg tag, data
-	readclump((uchar*)buf+4, arena, addr, blocks);
-	vtsend(conn, buf, size+2, VtRread, 0);
+	readclump((uchar*)buf+4, addr);
+	vtsend(conn, buf, addr.size+2, VtRread, 0);
 }
 
 static int