PageRenderTime 42ms CodeModel.GetById 23ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/vm/free_list.hpp

http://github.com/abeaumont/factor
C++ Header | 54 lines | 44 code | 10 blank | 0 comment | 1 complexity | af3a54bec324a97f821e932d96185761 MD5 | raw file
 1namespace factor
 2{
 3
 4static const cell free_list_count = 32;
 5static const cell allocation_page_size = 1024;
 6
 7struct free_heap_block
 8{
 9	cell header;
10
11	bool free_p() const
12	{
13		return (header & 1) == 1;
14	}
15
16	cell size() const
17	{
18		cell size = header & ~7;
19		FACTOR_ASSERT(size > 0);
20		return size;
21	}
22
23	void make_free(cell size)
24	{
25		FACTOR_ASSERT(size > 0);
26		header = size | 1;
27	}
28};
29
30struct block_size_compare {
31	bool operator()(free_heap_block *a, free_heap_block *b) const
32	{
33		return a->size() < b->size();
34	}
35};
36
37typedef std::multiset<free_heap_block *, block_size_compare> large_block_set;
38
39struct free_list {
40	std::vector<free_heap_block *> small_blocks[free_list_count];
41	large_block_set large_blocks;
42	cell free_block_count;
43	cell free_space;
44
45	void clear_free_list();
46	void initial_free_list(cell start, cell end, cell occupied);
47	void add_to_free_list(free_heap_block *block);
48	free_heap_block *find_free_block(cell size);
49	free_heap_block *split_free_block(free_heap_block *block, cell size);
50	bool can_allot_p(cell size);
51	cell largest_free_block();
52};
53
54}