summary refs log tree commit diff
path: root/src/libstd/arena.rs
blob: fed358d93881d46c080786c6a9a59e333060662d (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
46
47
48
49
50
51
52
53
54
55
56
// Dynamic arenas.

export arena, arena_with_size;

import list;
import list::{list, cons, nil};

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

fn chunk(size: uint) -> @chunk {
    let mut v = ~[];
    vec::reserve(v, size);
    @{ data: v, mut fill: 0u }
}

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

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

impl arena for arena {
    fn alloc_grow(n_bytes: uint, align: uint) -> *() {
        // Allocate a new chunk.
        let mut head = list::head(self.chunks);
        let chunk_size = vec::capacity(head.data);
        let new_min_chunk_size = uint::max(n_bytes, chunk_size);
        head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u));
        self.chunks = @cons(head, self.chunks);

        ret self.alloc(n_bytes, align);
    }

    #[inline(always)]
    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 end = start + n_bytes;
        if end > vec::capacity(head.data) {
            ret self.alloc_grow(n_bytes, align);
        }

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