summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-03-20 22:59:21 -0700
committerOri Bernstein <ori@eigenstate.org>2017-03-20 23:09:22 -0700
commitd8959a4a6d41b0a1328d81eef7c29d10ec669f8e (patch)
tree9608dd0d1ebfea400bcb215b5b524ca1c61194bb /lib
parent9c70e61a54066bf48e9c93178e66442b527762d1 (diff)
downloadmc-d8959a4a6d41b0a1328d81eef7c29d10ec669f8e.tar.gz
Align big free sizes.
This means that our big allocation sizes will always be page aligned. This also adds tests.
Diffstat (limited to 'lib')
-rw-r--r--lib/std/bytealloc.myr14
-rw-r--r--lib/std/test/bytealloc.myr52
2 files changed, 60 insertions, 6 deletions
diff --git a/lib/std/bytealloc.myr b/lib/std/bytealloc.myr
index e221ba5..fe22ed0 100644
--- a/lib/std/bytealloc.myr
+++ b/lib/std/bytealloc.myr
@@ -11,19 +11,21 @@ use "slfill"
use "backtrace"
pkg std =
- const startalloctrace : (f : byte[:] -> void)
+ const startalloctrace : (f : byte[:] -> void)
const endalloctrace : (-> void)
+ /* public for testing */
+ pkglocal const zbytealloc : (sz:size -> byte#)
+ const bytealloc : (sz:size -> byte#)
+ const bytefree : (m:byte#, sz:size -> void)
+
/* null pointers. only used internally. */
pkglocal const Zsliceptr = (0 : byte#)
- pkglocal const Align = 16 /* minimum allocation alignment */
+ pkglocal const Align = 16 /* minimum allocation alignment */
pkglocal const align : (m : std.size, align : std.size -> std.size)
pkglocal const allocsz : (sz : std.size -> std.size)
- pkglocal const bytealloc : (sz:size -> byte#)
- pkglocal const zbytealloc : (sz:size -> byte#)
- pkglocal const bytefree : (m:byte#, sz:size -> void)
;;
const Zslab = (0 : slab#)
@@ -193,9 +195,9 @@ const bigalloc = {sz
const bigfree = {p, sz
var minsz, minp, minidx
- minsz = sz
minp = p
minidx = -1
+ minsz = align(sz, Align)
lock(memlck)
for var i = 0; i < cache.len; i++
if cache[i].sz < minsz
diff --git a/lib/std/test/bytealloc.myr b/lib/std/test/bytealloc.myr
new file mode 100644
index 0000000..e6d87d6
--- /dev/null
+++ b/lib/std/test/bytealloc.myr
@@ -0,0 +1,52 @@
+use std
+
+const main = {
+ var a : byte#[1000]
+
+ for sz in [10, 100, 1000, 10000, 10000]
+ std.put("sz: {}\n", sz)
+ for cnt in [1, 10, 100]
+ std.put("cnt: {}\n", cnt)
+ /* alloc forwards, dealloc forwards */
+ for var i = 0; i < cnt; i++
+ a[i] = std.bytealloc(sz)
+ ;;
+ for var i = 0; i < cnt; i++
+ std.bytefree(a[i], sz)
+ ;;
+
+ /* alloc forwards, dealloc backwards */
+ for var i = 0; i < cnt; i++
+ a[i] = std.bytealloc(sz)
+ ;;
+ for var i = cnt; i > 0; i--
+ std.bytefree(a[i - 1], sz)
+ ;;
+
+ /* alloc forwards, dealloc randomly */
+ for var i = 0; i < cnt; i++
+ a[i] = std.bytealloc(sz)
+ ;;
+ shuffle(a[:cnt])
+ for var i = cnt; i > 0; i--
+ std.bytefree(a[i - 1], sz)
+ ;;
+ ;;
+ ;;
+}
+
+const shuffle = {a
+ var t
+ var rng
+ var j
+
+ /* we want determinism for benchmarking */
+ rng = std.mksrng(123)
+ for var i = 0; i < a.len - 1; i++
+ j = std.rngrand(rng, i, a.len)
+ t = a[j]
+ a[j] = a[i]
+ a[i] = t
+ ;;
+}
+