-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuddy_buffer.rs
More file actions
40 lines (31 loc) · 1.1 KB
/
Copy pathbuddy_buffer.rs
File metadata and controls
40 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! Buddy arena: variable-size allocations from a shared region.
use std::num::NonZeroUsize;
use arena_alligator::{BuddyArena, BuddyGeometry};
use bytes::BufMut;
fn nz(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).unwrap()
}
fn main() {
let geo = BuddyGeometry::exact(nz(1024 * 1024), nz(256)).unwrap();
let arena = BuddyArena::builder(geo).build().unwrap();
let mut small = arena.allocate(nz(100)).unwrap();
small.put_slice(b"small payload");
println!("requested 100 B, got {} B capacity", small.capacity());
let small_bytes = small.freeze();
let mut large = arena.allocate(nz(50_000)).unwrap();
large.put_bytes(0xCD, 50_000);
println!("requested 50000 B, got {} B capacity", large.capacity());
let large_bytes = large.freeze();
let m = arena.metrics();
println!(
"splits: {}, largest_free_block: {} B",
m.splits, m.largest_free_block
);
drop(small_bytes);
drop(large_bytes);
let m = arena.metrics();
println!(
"after drop: coalesces: {}, largest_free_block: {} B",
m.coalesces, m.largest_free_block
);
}