shithub: mc

Download patch

ref: 079536dfac30da7db118b8184ec04f0afdd4d461
parent: 99cfcabc52f10877bbeb3bcdbb1cded0cb3cd9a7
author: Ori Bernstein <[email protected]>
date: Wed Dec 24 17:06:04 EST 2014

Don't directly malloc in alloc.myr

    mmap isn't portable enough.

--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -1,8 +1,8 @@
-use sys
 use "die.use"
 use "extremum.use"
 use "types.use"
 use "units.use"
+use "syswrap.use"
 
 /*
 The allocator implementation here is based on Bonwick's slab allocator.
@@ -45,7 +45,6 @@
 extern const put : (b : byte[:], args : ... -> size)
 
 /* null pointers. only used internally. */
-const Zbyteptr	= 0 castto(byte#)
 const Zslab	= 0 castto(slab#)
 const Zchunk	= 0 castto(chunk#)
 
@@ -224,9 +223,9 @@
 		bkt = &buckets[bktnum(sz)]
 		p = bktalloc(bkt)
 	else
-		p = sys.mmap(Zbyteptr, sz castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)
-		if p == sys.Mapbad
-			die("could not map memory\n")
+		p = getmem(sz)
+		if p == Failmem
+			die("could not get memory\n")
 		;;
 	;;
 	-> p
@@ -245,7 +244,7 @@
 		bkt = &buckets[bktnum(sz)]
 		bktfree(bkt, p)
 	else
-		sys.munmap(p, sz castto(sys.size))
+		freemem(p, sz)
 	;;
 }
 
@@ -275,8 +274,8 @@
 	   cost us memory, and 64 bits of address space means that we're not
 	   going to have issues with running out of address space for a
 	   while. On a 32 bit system this would be a bad idea. */
-	p = sys.mmap(Zbyteptr, Slabsz*2 castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)
-	if p == sys.Mapbad
+	p = getmem(Slabsz*2)
+	if p == Failmem
 		die("Unable to mmap")
 	;;
 
@@ -348,7 +347,7 @@
 		else
 			/* we mapped 2*Slabsz so we could align it,
 			 so we need to unmap the same */
-			sys.munmap(s.head, Slabsz*2 castto(sys.size))
+			freemem(s.head, Slabsz*2)
 		;;
 	;;
 	s.nfree++
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -6,6 +6,8 @@
 	type pid	= sys.pid
 	type fdopt	= sys.fdopt
 
+	const Failmem	: byte#	= -1 castto(byte#)
+
 	const Ordonly  	: fdopt = sys.Ordonly	castto(fdopt)
 	const Owronly  	: fdopt = sys.Owronly	castto(fdopt)
 	const Ordwr    	: fdopt = sys.Ordwr	castto(fdopt)
@@ -35,6 +37,8 @@
 	const execve	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
 	const exit	: (status:int -> void)
 	const waitpid	: (pid:pid, loc:int32#, opt : int64	-> int64)
+	const getmem	: (sz : size -> byte#)
+	const freemem	: (p : byte#, sz : size -> void)
 ;;
 
 /* fd stuff */
@@ -58,3 +62,7 @@
 const execve	= {cmd, args, env;	-> sys.execve(cmd, args, env)}
 const exit	= {status;	sys.exit(status)}
 const waitpid	= {pid, loc, opt;	-> sys.waitpid(pid castto(sys.pid), loc, opt)}
+
+/* memory stuff */
+const getmem	= {sz;		-> sys.mmap(0 castto(byte#), sz castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)}
+const freemem	= {p, sz;	sys.munmap(p, sz castto(sys.size))}