ref: 21d4f2bce79e207b5c46d31ad1e8b7824a2b464a
parent: 3f34c5ebfa25b7f6494c0097c6ea0e889745a82f
author: Ori Bernstein <[email protected]>
date: Fri Jul 20 14:49:34 EDT 2012
Start work on memory allocator.
--- /dev/null
+++ b/alloc.myr
@@ -1,0 +1,64 @@
+use "sys.use"
+
+pkg std =
+ type size = uint
+
+ generic alloc : ( -> @a*)
+ generic free : (v:@a* -> void)
+
+ const bytealloc : (sz:size -> byte*)
+ const bytefree : (ptr:byte* -> void)
+;;
+
+/* null pointers */
+const Zbyte = 0 castto(byte*)
+const Zslab = 0 castto(slab*)
+const Zbin = 0 castto(bin*)
+const Pagesz = 4096 /* on the systems this supports, anyways... */
+
+type bucket = struct
+ sz : size /* aligned size */
+ nper : size /* max number of elements per slab */
+ slabs : slab* /* partially filled or free slabs */
+ cache : slab* /* cache of empty slabs, to prevent thrashing */
+ ncache : size /* size of cache */
+;;
+
+type slab = struct
+ freehd : bin*
+ next : slab*
+ nfree : int
+;;
+
+type bin = struct
+ next : bin*
+;;
+
+const allocinit = {b : bucket*, sz
+ b.sz = align(sz, 16)
+ b.nper = (Pagesz - sizeof(slab))/b.sz
+ b.slabs = Zslab
+ b.cache = Zslab
+ b.ncache = 0
+}
+
+const mkslab = {b : bucket*
+ var p
+ var s
+
+ p = mmap(Zbyte, Mprotrw, Mpriv | Manon, -1, 0)
+ if p == Mapbad
+ die("Unable to mmap")
+ ;;
+
+ s = p castto(slab*)
+ s.nfree = b.nper
+}
+
+const delslab = {s : slab*
+
+}
+
+const align = {v, align
+ -> (v + align - 1) & ~(align - 1)
+}
--- a/bld.sh
+++ b/bld.sh
@@ -32,5 +32,6 @@
use sys.myr
build sys.myr
build hello.myr
+build alloc.myr
$CC -m32 -o hello sys.o hello.o syscall.o
--- a/hello.myr
+++ b/hello.myr
@@ -1,5 +1,5 @@
use "sys.use"
const main = {
- sys.write(1, "Hello world\n")
+ std.write(1, "Hello world\n")
}
--- a/sys.myr
+++ b/sys.myr
@@ -1,6 +1,8 @@
-pkg sys =
+pkg std =
type scno = int
type fdopt = int
+ type mprot = int
+ type mopt = int
type statbuf = struct
dev : uint
ino : uint
@@ -22,6 +24,7 @@
_unused2: uint
;;
+ /* open options */
const Rdonly : fdopt = 0x0
const Wronly : fdopt = 0x1
const Rdwr : fdopt = 0x2
@@ -31,6 +34,25 @@
const Ndelay : fdopt = 0x800
const Trunc : fdopt = 0x200
+ /* mmap protection */
+ const Mprotexec : mprot = 0x1
+ const Mprotrd : mprot = 0x2
+ const Mprotwr : mprot = 0x4
+ const Mprotrw : mprot = 0x6
+ const Mprotnone : mprot = 0x0
+
+ /* mmap options */
+ const Mshared : mopt = 0x1
+ const Mpriv : mopt = 0x2
+ const Mfixed : mopt = 0x10
+ const Mfile : mopt = 0x0
+ const Manon : mopt = 0x20
+ const M32bit : mopt = 0x40
+
+ /* return value for a failed mapping */
+ const Mapbad : byte* = -1 castto(byte*)
+
+ /* syscalls */
const Sysexit : scno = 1
const Sysread : scno = 3
const Syswrite : scno = 4
@@ -57,7 +79,7 @@
const lseek : (fd:int, off:uint, whence:int -> int)
const fstat : (fd:int, sb:statbuf* -> int)
const munmap : (addr:byte*, len:uint -> int)
- const mmap : (add:byte*, len:uint, prot:uint, flags:uint, fd:int, off:uint)
+ const mmap : (addr:byte*, len:uint, prot:uint, flags:uint, fd:int, off:uint)
;;
const exit = {status; -> syscall(Sysexit, 1);}
--- a/syscall.s
+++ b/syscall.s
@@ -1,5 +1,5 @@
-.globl sys$syscall
-sys$syscall:
+.globl std$syscall
+std$syscall:
pushl %ebp
movl %esp,%ebp
movl 12(%ebp),%eax #count