ref: 86ab9603ae87df4052e4b6a92b912683f7d5d7fe
parent: 076fef88db990fb1dd8f0650d018035755d0d45b
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jan 27 17:32:39 EST 2016
Use the zeroedness of the base pointer in alloc. Instead of the special case being .len==0, use .base==(0 castto(void#)). This means that trimmed slices will no longer leak.
--- a/lib/std/alloc.myr
+++ b/lib/std/alloc.myr
@@ -47,6 +47,7 @@
/* null pointers. only used internally. */
const Zslab = 0 castto(slab#)
const Zchunk = 0 castto(chunk#)
+const Zsliceptr = 0 castto(byte#)
const Slabsz = 1*MiB /* 1 meg slabs */
const Cachemax = 16 /* maximum number of slabs in the cache */
@@ -149,7 +150,7 @@
generic slfree = {sl
var head
- if sl.len == 0
+ if sl castto(byte#) == Zsliceptr
-> void
;;
@@ -161,11 +162,16 @@
/* Grows a slice */
generic slgrow = {sl : @a[:], len
- var n
+ var cap
var new
+ var n
/* 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))
+ cap = 0
+ if sl castto(byte#) != Zsliceptr
+ cap = slcap(sl castto(byte#))
+ ;;
+ if cap >= allocsz(len*sizeof(@a))
-> (sl castto(@a#))[:len]
;;