shithub: mc

Download patch

ref: a67e53ab83967e47c51b305e684aba6687e2632e
parent: 372a754791164af783fe0949f99bb00629cf1333
author: Ori Bernstein <[email protected]>
date: Tue Jan 21 18:31:24 EST 2014

Add 'zalloc()' function.

--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -33,6 +33,7 @@
 	generic slalloc	: (len : size	-> @a[:])
 	generic slzalloc	: (len : size	-> @a[:])
 	generic slgrow	: (sl : @a[:], len : size	-> @a[:])
+	generic slzgrow	: (sl : @a[:], len : size	-> @a[:])
 	generic slfree	: (sl : @a[:]	-> void)
 
 	const bytealloc	: (sz:size	-> byte#)
@@ -159,7 +160,7 @@
 	var i, n
 	var new
 
-	/* if the slice wouldn't change buckets, we don't need to realloc. */
+	/* if the slice doesn't need a bigger bucket, we don't need to realloc. */
 	if sl.len > 0 && slcap(sl castto(byte#)) >= allocsz(len*sizeof(@a))
 		-> (sl castto(@a#))[:len]
 	;;
@@ -175,6 +176,16 @@
 	-> new
 }
 
+/* Grows a slice, filling new entries with zero bytes */
+generic slzgrow = {sl : @a[:], len
+	var oldsz
+
+	oldsz = sl.len*sizeof(@a)
+	sl = slgrow(sl, len)
+	zfill((sl castto(byte#))[oldsz:len*sizeof(@a)])
+	-> sl
+}
+
 const slcap = {p
 	var phdr
 
@@ -183,14 +194,19 @@
 }
 
 const zbytealloc = {sz
-	var p, sl, i
+	var p
 
 	p = bytealloc(sz)
-	sl = p[0:sz]
+	zfill(p[0:sz])
+	-> p
+}
+
+const zfill = {sl
+	var i
+
 	for i = 0; i < sl.len; i++
 		sl[i] = 0
 	;;
-	-> p
 }
 
 /* Allocates a blob that is 'sz' bytes long. Dies if the allocation fails */