]> gitweb.factorcode.org Git - factor.git/blob - vm/bump_allocator.hpp
VM: debug code which memsets the resetted space
[factor.git] / vm / bump_allocator.hpp
1 namespace factor {
2
3 template <typename Block> struct bump_allocator {
4   /* offset of 'here' and 'end' is hardcoded in compiler backends */
5   cell here;
6   cell start;
7   cell end;
8   cell size;
9
10   bump_allocator(cell size, cell start)
11       : here(start), start(start), end(start + size), size(size) {}
12
13   bool contains_p(Block* block) { return ((cell)block - start) < size; }
14
15   Block* allot(cell size) {
16     cell h = here;
17     here = h + align(size, data_alignment);
18     return (Block*)h;
19   }
20
21   cell occupied_space() { return here - start; }
22
23   cell free_space() { return end - here; }
24
25   cell next_object_after(cell scan) {
26     cell size = ((Block*)scan)->size();
27     if (scan + size < here)
28       return scan + size;
29     else
30       return 0;
31   }
32
33   cell first_object() {
34     if (start != here)
35       return start;
36     else
37       return 0;
38   }
39
40   void flush() {
41     here = start;
42 #ifdef FACTOR_DEBUG
43     /* In case of bugs, there may be bogus references pointing to the
44        memory space after the gc has run. Filling it with a pattern
45        makes accesses to such shadow data fail hard. */
46     memset_cell((void*)start, 0xbaadbaad, size);
47 #endif
48   }
49 };
50
51 }