summary refs log tree commit diff
path: root/src/rt/boxed_region.h
blob: bd5312fee99f02379772f3a815b9e760b874ce9d (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
#ifndef BOXED_REGION_H
#define BOXED_REGION_H

#include <stdlib.h>

struct type_desc;
class memory_region;
struct rust_opaque_box;

/* Tracks the data allocated by a particular task in the '@' region.
 * Currently still relies on the standard malloc as a backing allocator, but
 * this could be improved someday if necessary. Every allocation must provide
 * a type descr which describes the payload (what follows the header). */
class boxed_region {
private:
    memory_region *backing_region;
    rust_opaque_box *live_allocs;

    size_t align_to(size_t v, size_t align) {
        size_t alignm1 = align - 1;
        v += alignm1;
        v &= ~alignm1;
        return v;
    }

public:
    boxed_region(memory_region *br)
        : backing_region(br)
        , live_allocs(NULL)
    {}

    rust_opaque_box *first_live_alloc() { return live_allocs; }

    rust_opaque_box *malloc(type_desc *td);
    rust_opaque_box *calloc(type_desc *td);
    void free(rust_opaque_box *box);
};

#endif /* BOXED_REGION_H */