shithub: mc

Download patch

ref: 19ee1c535810ae802d50436a734037fa0dc18bf0
parent: dc7a19d14394fa3bd8e832c1be557478cf390285
author: Ori Bernstein <[email protected]>
date: Sat Jul 21 23:06:36 EDT 2012

Fill in the rest of the slab allocator. Untested.

--- a/alloc.myr
+++ b/alloc.myr
@@ -66,16 +66,63 @@
 		bprev = b
 	;;
 	bprev.next = Zbin
+	-> s
 }
 
-const nextbin = {b, sz
-	-> ((b castto(intptr)) + sz) castto(bin*)
+const bktalloc = {bkt : bucket*
+	var s
+	var b
+
+	/* find a slab */
+	s = bkt.slabs
+	if s == Zslab
+		s = mkslab(bkt)
+		if !s
+			die("No memory left")
+		;;
+		bkt.slabs = s
+	;;
+
+	/* grab the first bin on the slab */
+	b = s.next
+	s.next = b.next
+	s.nfree--
+	if !s.nfree
+		bkt.slabs = s.next
+		s.next = Zslab
+	;;
+
+	-> b castto(byte*)
 }
 
+const bktfree = {bkt : bucket*, m : byte*
+	var s
+	var b
+
+	s = mtrunc(m, Pagesz) castto(slab*)
+	b = m castto(bin*)
+	if s.nfree == 0
+		s.next = bkt.slabs
+		bkt.slabs = s
+	;;
+	s.nfree++
+	b.next = s.freehd
+	s.freehd = b
+}
+
 const delslab = {s : slab*
 
 }
 
+/* bins are variable sizes, so we can't just take a slice */
+const nextbin = {b, sz
+	-> ((b castto(intptr)) + sz) castto(bin*)
+}
+
 const align = {v, align
 	-> (v + align - 1) & ~(align - 1)
+}
+
+const mtrunc = {m, align
+	-> ((m castto(intptr)) & ~(align - 1)) castto(byte*)
 }
--- a/bld.sh
+++ b/bld.sh
@@ -38,8 +38,9 @@
 use die.myr 
 assem syscall-$SYS.s
 build sys-$SYS.myr
-build hello.myr
 build alloc.myr
 build die.myr
+
+build hello.myr
 
 $CC -m32 -o hello sys.o hello.o syscall.o