ref: 81bef8daa90ad1c1589f3ebee10b030ad248b4c7
parent: 7a6f9b61aa7161b2240764b9d6335f9c487c6858
author: Ori Bernstein <[email protected]>
date: Thu Oct 17 06:57:30 EDT 2013
Allow 'empty slices' for slalloc/slfree This allows you to initialize slices to empty, and then free them safely. I find this is useful for building up slices incrementally, where you start with an empty slice and grow it as you go.
--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -90,6 +90,9 @@
/* allocates a slice of 'len' elements. */
generic slalloc = {len
+ if len == 0
+ -> [][:]
+ ;;
-> (bytealloc(len*sizeof(@a)) castto(@a#))[0:len]
}
@@ -100,6 +103,9 @@
/* Frees a slice */
generic slfree = {sl
+ if sl.len == 0
+ ->
+ ;;
bytefree(sl castto(byte#), sl.len * sizeof(@a))
}