3 const int mark_bits_granularity = sizeof(cell) * 8;
4 const int mark_bits_mask = sizeof(cell) * 8 - 1;
6 template <typename Block> struct mark_bits {
13 void clear_mark_bits() { memset(marked, 0, bits_size * sizeof(cell)); }
15 void clear_forwarding() { memset(forwarding, 0, bits_size * sizeof(cell)); }
17 mark_bits(cell size, cell start)
20 bits_size(size / data_alignment / mark_bits_granularity),
21 marked(new cell[bits_size]),
22 forwarding(new cell[bits_size]) {
34 cell block_line(const Block* address) {
35 return (((cell) address - start) / data_alignment);
38 Block* line_block(cell line) {
39 return (Block*)(line * data_alignment + start);
42 std::pair<cell, cell> bitmap_deref(const Block* address) {
43 cell line_number = block_line(address);
44 cell word_index = (line_number / mark_bits_granularity);
45 cell word_shift = (line_number & mark_bits_mask);
46 return std::make_pair(word_index, word_shift);
49 bool bitmap_elt(cell* bits, const Block* address) {
50 std::pair<cell, cell> position = bitmap_deref(address);
51 return (bits[position.first] & ((cell) 1 << position.second)) != 0;
54 Block* next_block_after(const Block* block) {
55 return (Block*)((cell) block + block->size());
58 void set_bitmap_range(cell* bits, const Block* address) {
59 std::pair<cell, cell> start = bitmap_deref(address);
60 std::pair<cell, cell> end = bitmap_deref(next_block_after(address));
62 cell start_mask = ((cell) 1 << start.second) - 1;
63 cell end_mask = ((cell) 1 << end.second) - 1;
65 if (start.first == end.first)
66 bits[start.first] |= start_mask ^ end_mask;
68 FACTOR_ASSERT(start.first < bits_size);
69 bits[start.first] |= ~start_mask;
71 for (cell index = start.first + 1; index < end.first; index++)
72 bits[index] = (cell) - 1;
75 FACTOR_ASSERT(end.first < bits_size);
76 bits[end.first] |= end_mask;
81 bool marked_p(const Block* address) { return bitmap_elt(marked, address); }
83 void set_marked_p(const Block* address) { set_bitmap_range(marked, address); }
85 /* The eventual destination of a block after compaction is just the number
86 of marked blocks before it. Live blocks must be marked on entry. */
87 void compute_forwarding() {
89 for (cell index = 0; index < bits_size; index++) {
90 forwarding[index] = accum;
91 accum += popcount(marked[index]);
95 /* We have the popcount for every mark_bits_granularity entries; look
96 up and compute the rest */
97 Block* forward_block(const Block* original) {
98 FACTOR_ASSERT(marked_p(original));
99 std::pair<cell, cell> position = bitmap_deref(original);
100 cell offset = (cell) original & (data_alignment - 1);
102 cell approx_popcount = forwarding[position.first];
103 cell mask = ((cell) 1 << position.second) - 1;
105 cell new_line_number =
106 approx_popcount + popcount(marked[position.first] & mask);
107 Block* new_block = (Block*)((char*)line_block(new_line_number) + offset);
108 FACTOR_ASSERT(new_block <= original);
112 Block* next_unmarked_block_after(const Block* original) {
113 std::pair<cell, cell> position = bitmap_deref(original);
114 cell bit_index = position.second;
116 for (cell index = position.first; index < bits_size; index++) {
117 cell mask = ((fixnum) marked[index] >> bit_index);
119 /* Found an unmarked block on this page. Stop, it's hammer time */
120 cell clear_bit = rightmost_clear_bit(mask);
121 return line_block(index * mark_bits_granularity + bit_index +
124 /* No unmarked blocks on this page. Keep looking */
129 /* No unmarked blocks were found */
130 return (Block*)(this->start + this->size);
133 Block* next_marked_block_after(const Block* original) {
134 std::pair<cell, cell> position = bitmap_deref(original);
135 cell bit_index = position.second;
137 for (cell index = position.first; index < bits_size; index++) {
138 cell mask = (marked[index] >> bit_index);
140 /* Found an marked block on this page. Stop, it's hammer time */
141 cell set_bit = rightmost_set_bit(mask);
142 return line_block(index * mark_bits_granularity + bit_index + set_bit);
144 /* No marked blocks on this page. Keep looking */
149 /* No marked blocks were found */
150 return (Block*)(this->start + this->size);
153 cell unmarked_block_size(Block* original) {
154 Block* next_marked = next_marked_block_after(original);
155 return ((char*)next_marked - (char*)original);