PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/bump_allocator.hpp

http://github.com/abeaumont/factor
C++ Header | 54 lines | 44 code | 9 blank | 1 comment | 3 complexity | 31babdb11095c47f8fc9a58fa383870a MD5 | raw file
 1namespace factor
 2{
 3
 4template<typename Block> struct bump_allocator {
 5	/* offset of 'here' and 'end' is hardcoded in compiler backends */
 6	cell here;
 7	cell start;
 8	cell end;
 9	cell size;
10
11	explicit bump_allocator(cell size_, cell start_) :
12		here(start_), start(start_), end(start_ + size_), size(size_) {}
13
14	bool contains_p(Block *block)
15	{
16		return ((cell)block - start) < size;
17	}
18
19	Block *allot(cell size)
20	{
21		cell h = here;
22		here = h + align(size,data_alignment);
23		return (Block *)h;
24	}
25
26	cell occupied_space()
27	{
28		return here - start;
29	}
30
31	cell free_space()
32	{
33		return end - here;
34	}
35
36	cell next_object_after(cell scan)
37	{
38		cell size = ((Block *)scan)->size();
39		if(scan + size < here)
40			return scan + size;
41		else
42			return 0;
43	}
44
45	cell first_object()
46	{
47		if(start != here)
48			return start;
49		else
50			return 0;
51	}
52};
53
54}