3 const int mark_bits_granularity = sizeof(cell) * 8;
4 const int mark_bits_mask = sizeof(cell) * 8 - 1;
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(cell address) {
35 return (address - start) / data_alignment;
38 cell line_block(cell line) {
39 return line * data_alignment + start;
42 std::pair<cell, cell> bitmap_deref(const cell 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 cell address) {
50 std::pair<cell, cell> position = bitmap_deref(address);
51 return (bits[position.first] & ((cell)1 << position.second)) != 0;
54 cell next_block_after(const cell block, const cell size) {
58 void set_bitmap_range(cell* bits, const cell address, const cell size) {
59 std::pair<cell, cell> start = bitmap_deref(address);
60 std::pair<cell, cell> end = bitmap_deref(next_block_after(address, size));
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 cell address) { return bitmap_elt(marked, address); }
83 void set_marked_p(const cell address, const cell size) {
84 set_bitmap_range(marked, address, size);
87 // The eventual destination of a block after compaction is just the number
88 // of marked blocks before it. Live blocks must be marked on entry.
89 void compute_forwarding() {
91 for (cell index = 0; index < bits_size; index++) {
92 forwarding[index] = accum;
93 accum += popcount(marked[index]);
97 // We have the popcount for every mark_bits_granularity entries; look
98 // up and compute the rest
99 cell forward_block(const cell original) {
100 FACTOR_ASSERT(marked_p(original));
101 std::pair<cell, cell> position = bitmap_deref(original);
102 cell offset = original & (data_alignment - 1);
104 cell approx_popcount = forwarding[position.first];
105 cell mask = ((cell)1 << position.second) - 1;
107 cell new_line_number =
108 approx_popcount + popcount(marked[position.first] & mask);
109 cell new_block = line_block(new_line_number) + offset;
110 FACTOR_ASSERT(new_block <= original);
114 cell next_unmarked_block_after(const cell original) {
115 std::pair<cell, cell> position = bitmap_deref(original);
116 cell bit_index = position.second;
118 for (cell index = position.first; index < bits_size; index++) {
119 cell mask = ((fixnum)marked[index] >> bit_index);
121 // Found an unmarked block on this page. Stop, it's hammer time
122 cell clear_bit = rightmost_clear_bit(mask);
123 return line_block(index * mark_bits_granularity + bit_index +
126 // No unmarked blocks on this page. Keep looking
131 // No unmarked blocks were found
132 return this->start + this->size;
135 cell next_marked_block_after(const cell original) {
136 std::pair<cell, cell> position = bitmap_deref(original);
137 cell bit_index = position.second;
139 for (cell index = position.first; index < bits_size; index++) {
140 cell mask = (marked[index] >> bit_index);
142 // Found an marked block on this page. Stop, it's hammer time
143 cell set_bit = rightmost_set_bit(mask);
144 return line_block(index * mark_bits_granularity + bit_index + set_bit);
146 // No marked blocks on this page. Keep looking
151 // No marked blocks were found
152 return this->start + this->size;
155 cell unmarked_block_size(cell original) {
156 cell next_marked = next_marked_block_after(original);
157 return next_marked - original;