summary refs log tree commit diff
path: root/src/libstd/arena.rs
blob: 3c316ecf5ccdcbedb8c39dc6d35f9f0e90b1bce5 (plain)
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
41
42
43
44
45
// Dynamic arenas.

export arena, arena_with_size;

import list;

type chunk = {data: [u8], mut fill: uint};
type arena = {mut chunks: list::list<@chunk>};

fn chunk(size: uint) -> @chunk {
    @{ data: vec::from_elem(size, 0u8), mut fill: 0u }
}

fn arena_with_size(initial_size: uint) -> arena {
    ret {mut chunks: list::cons(chunk(initial_size), @list::nil)};
}

fn arena() -> arena {
    arena_with_size(32u)
}

impl arena for arena {
    fn alloc(n_bytes: uint, align: uint) -> *() {
        let alignm1 = align - 1u;
        let mut head = list::head(self.chunks);

        let mut start = head.fill;
        start = (start + alignm1) & !alignm1;
        let mut end = start + n_bytes;

        if end > vec::len(head.data) {
            // Allocate a new chunk.
            let new_min_chunk_size = uint::max(n_bytes, vec::len(head.data));
            head = chunk(uint::next_power_of_two(new_min_chunk_size));
            self.chunks = list::cons(head, @self.chunks);
            start = 0u;
            end = n_bytes;
        }

        let p = ptr::offset(ptr::addr_of(head.fill), start);
        head.fill = end;
        unsafe { ret unsafe::reinterpret_cast(p); }
    }
}