Files
sakurac/tests/core/allocators.sakura
T
2026-12-06 13:52:57 -06:00

242 lines
7.3 KiB
Plaintext

# Tests of the memory `core` gives: a block of the heap keeps what it holds
# through resizing; blocks of every class, small, carved from chunks, and
# mapped on their own, hold what's written to them and come back zeroed where
# they're asked to be, and keep what they hold through resizing from each
# class to the next; and threads allocating, resizing, and releasing at once,
# blocks the others made among them, keep what each block holds. The
# allocators of std, an arena and a pool, are tested there.
import std::math;
import std::testing;
import std::thread;
@test def uncleared_memory_keeps_what_it_holds_through_resizing(t: &mut tester): void {
val block := raw_allocate(64) as *mut u64;
for i in 0..8u { block[i] = i * 11; }
val grown := raw_resize(block as *mut u8, 8192) as *mut u64;
var same := true;
for i in 0..8u { same = same && grown[i] == i * 11; }
val shrunk := raw_resize(grown as *mut u8, 16) as *mut u64;
t.expects(same && shrunk[1] == 11);
raw_release(shrunk as *mut u8);
}
# Writes a block's bytes of a seed: each of its first 64, then every 16th, as
# far as it reaches, the same whatever its size, so what it holds is checked
# as far as a block it's resized to reaches. Blocks are aligned to 16 bytes, so
# two that overlap share a byte written.
def stamp(at: *mut u8, size: u64, seed: u64): void {
var i: u64 = 0;
while i < size {
at[i] = ((i * 7 + seed * 13) & 0xFF) as u8;
i += i < 64 ? 1 : 16;
}
}
# Whether a block holds what `stamp` wrote of a seed, as far as the size.
def holds_stamp(at: *u8, size: u64, seed: u64): bool {
var i: u64 = 0;
while i < size {
return false if at[i] != ((i * 7 + seed * 13) & 0xFF) as u8;
i += i < 64 ? 1 : 16;
}
return true;
}
def all_zero(at: *u8, size: u64): bool {
for i in 0..size {
return false if at[i] != 0;
}
return true;
}
def next_random(r: u64): u64 {
return math::wrapping_add(math::wrapping_mul(r, 6364136223846793005), 1442695040888963407);
}
# A size of a number, as likely of each kind: small; carved from chunks; about
# the largest carved, a block's header past it or not; or mapped on its own.
def block_size_from(r: u64): u64 {
val pick := r >> 33;
val kind := pick % 4;
if kind == 0 {
return 1 + pick % 2047;
}
if kind == 1 {
return 2048 + pick % 260000;
}
if kind == 2 {
return 262100 + pick % 64;
}
return 262200 + pick % 400000;
}
@test def blocks_of_every_size_hold_what_is_written_and_come_back_zeroed(t: &mut tester): void {
# About each edge of the classes, and a block's header of 16 bytes before
# the largest carved, 256 KiB.
val sizes: u64[26] = [
1, 15, 16, 17, 255, 256, 257, 1000, 2032, 2033, 2049, 2560, 2561, 4096, 4097, 50000, 65536, 65537,
262127, 262128, 262129, 262144, 300000, 1048576, 1048577, 3000000,
];
var written := true;
var cleared := true;
for k in 0..26u {
val size := sizes[k];
val at := raw_allocate(size);
stamp(at, size, k);
written = written && holds_stamp(at, size, k);
# Every byte written, so a block reused is seen cleared.
for i in 0..size { at[i] = 0xFF; }
raw_release(at);
val again := raw_allocate_zeroed(size);
cleared = cleared && all_zero(again, size);
raw_release(again);
}
t.expects(written);
t.expects(cleared);
}
@test def blocks_keep_what_they_hold_resized_through_every_class(t: &mut tester): void {
var size: u64 = 1;
var at := raw_allocate(size);
stamp(at, size, 5);
var kept := true;
while size < 3000000 {
val grown := size + size / 4 + 1;
at = raw_resize(at, grown);
kept = kept && holds_stamp(at, size, 5);
stamp(at, grown, 5);
size = grown;
}
while size > 1 {
val shrunk := size - size / 4 - 1;
at = raw_resize(at, shrunk);
kept = kept && holds_stamp(at, shrunk, 5);
size = shrunk;
}
raw_release(at);
t.expects(kept);
}
# What a thread churning blocks is given: its seed, where the blocks it's
# given and leaves are, from which, and where it says how many it found
# changed.
struct churn_job {
var seed: u64;
var blocks: *mut u64; # A pointer and size of each block.
var first: u64; # The first of the 32 it's given.
var wrong: *mut u64;
}
# Releases the 32 blocks a thread is given, which another thread made, then
# allocates, resizes, and releases its own at random, 32 at once, checking
# each as it's let go of, and leaves its last for another thread to release,
# marked of 1000 past where each is.
def churn(job: churn_job): void {
var wrong: u64 = 0;
for k in 0..32u {
val slot := (job.first + k) * 2;
val given := job.blocks[slot] as *mut u8;
if !holds_stamp(given, job.blocks[slot + 1], k) {
wrong++;
}
raw_release(given);
}
var at: u64[32];
var size: u64[32];
var seed: u64[32];
for k in 0..32u {
at[k] = 0;
size[k] = 0;
seed[k] = 0;
}
var r := job.seed;
for round in 0..2000u {
r = next_random(r);
val k := (r >> 40) % 32;
if at[k] != 0 {
if !holds_stamp(at[k] as *u8, size[k], seed[k]) {
wrong++;
}
if (r >> 20) % 4 == 0 {
# Resized, it keeps what it held as far as it reaches.
val resized := block_size_from(next_random(r));
val moved := raw_resize(at[k] as *mut u8, resized);
if !holds_stamp(moved, math::min(size[k], resized), seed[k]) {
wrong++;
}
stamp(moved, resized, seed[k]);
at[k] = moved as u64;
size[k] = resized;
continue;
}
raw_release(at[k] as *mut u8);
}
size[k] = block_size_from(r);
seed[k] = round;
val made := (r >> 8) % 2 == 0 ? raw_allocate(size[k]) : raw_allocate_zeroed(size[k]);
stamp(made, size[k], seed[k]);
at[k] = made as u64;
}
for k in 0..32u {
if at[k] == 0 {
size[k] = 100;
at[k] = raw_allocate(100) as u64;
}
else {
if !holds_stamp(at[k] as *u8, size[k], seed[k]) {
wrong++;
}
}
stamp(at[k] as *mut u8, size[k], 1000 + k);
val slot := (job.first + k) * 2;
job.blocks[slot] = at[k];
job.blocks[slot + 1] = size[k];
}
job.wrong[job.first / 32] = wrong;
}
@test def threads_allocating_at_once_keep_what_each_block_holds(t: &mut tester): void {
val threads: u64 = 8;
val blocks := raw_allocate_zeroed(threads * 32 * 16) as *mut u64;
val wrong := raw_allocate_zeroed(threads * 8) as *mut u64;
var r: u64 = 99;
for k in 0..threads * 32 {
r = next_random(r);
val size := block_size_from(r);
val made := raw_allocate(size);
stamp(made, size, k % 32);
blocks[k * 2] = made as u64;
blocks[k * 2 + 1] = size;
}
var running := vector::<thread::handle<void>>::new();
for i in 0..threads {
val job := churn_job{ seed = i * 1000 + 7, blocks = blocks, first = i * 32, wrong = wrong };
running.append(thread::spawn_with(churn, job).expect("A thread starts."));
}
for i in 0..running.length {
running[i].join();
}
# The blocks each left, released by this thread.
var left: u64 = 0;
for k in 0..threads * 32 {
val made := blocks[k * 2] as *mut u8;
if !holds_stamp(made, blocks[k * 2 + 1], 1000 + k % 32) {
left++;
}
raw_release(made);
}
var found: u64 = 0;
for i in 0..threads {
found += wrong[i];
}
t.expects(found == 0);
t.expects(left == 0);
raw_release(blocks as *mut u8);
raw_release(wrong as *mut u8);
}