shithub: mc

Download patch

ref: dc7a19d14394fa3bd8e832c1be557478cf390285
parent: 54366e8a221b4fc485eae0b376fee39ec897d1ca
author: Ori Bernstein <[email protected]>
date: Sat Jul 21 22:46:05 EDT 2012

Initialize slabs in the allocator.

--- a/alloc.myr
+++ b/alloc.myr
@@ -30,8 +30,8 @@
 	nfree	: size  /* the number of free nodes */
 ;;
 
-type bin = struct
-	next	: bin*
+type bin = struct /* NB: must be smaller than sizeof(slab) */
+	next	: bin* /* the next bin in the free list */
 ;;
 
 const allocinit = {b : bucket*, sz
@@ -42,9 +42,13 @@
 	b.ncache = 0
 }
 
-const mkslab = {b : bucket*
+const mkslab = {bkt : bucket*
 	var p
 	var s
+	var b
+	var i
+	var bprev
+	var off /* offset of bin head */
 
 	p = mmap(Zbyte, Pagesz, Mprotrw, Mpriv | Manon, -1, 0)
 	if p == Mapbad
@@ -52,7 +56,20 @@
 	;;
 
 	s = p castto(slab*)
-	s.nfree = b.nper
+	s.nfree = bkt.nper
+	/* skip past the slab header */
+	off = align(sizeof(slab), 16)
+	b = nextbin(s castto(bin*), off)
+	for i = 0; i < bkt.nper; i++
+		b = nextbin(b, bkt.sz)
+		bprev.next = b
+		bprev = b
+	;;
+	bprev.next = Zbin
+}
+
+const nextbin = {b, sz
+	-> ((b castto(intptr)) + sz) castto(bin*)
 }
 
 const delslab = {s : slab*
--- a/types.myr
+++ b/types.myr
@@ -1,4 +1,5 @@
 pkg std =
-	type size = uint32	/* spans entire address space */
-	type off = uint32	/* file offsets */
+	type size	= uint32 /* spans entire address space */
+	type off	= uint32 /* file offsets */
+	type intptr	= uint32 /* can hold any pointer losslessly */
 ;;