|
| 1 | +// Copyright 2026 Dolphin Emulator Project |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#include "Common/LazyMemoryRegionBitSet.h" |
| 5 | + |
| 6 | +#include <cstring> |
| 7 | + |
| 8 | +#include "Common/MsgHandler.h" |
| 9 | + |
| 10 | +namespace Common |
| 11 | +{ |
| 12 | +LazyMemoryRegionBitSet::LazyMemoryRegionBitSet(size_t bits) : m_bits(bits), m_valid(Create(bits)) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +bool LazyMemoryRegionBitSet::Create(size_t bits) |
| 17 | +{ |
| 18 | + const size_t page_size = MemArena().GetPageSize(); |
| 19 | + if (bits % (page_size * 8) != 0) |
| 20 | + { |
| 21 | + PanicAlertFmt( |
| 22 | + "LazyMemoryRegionBitSet size of {} bits is not aligned with page size of {} bytes", bits, |
| 23 | + page_size); |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + m_pointer = reinterpret_cast<u32*>(m_region.Create(bits / 8)); |
| 28 | + if (!m_pointer) |
| 29 | + { |
| 30 | + PanicAlertFmt("LazyMemoryRegionBitSet failed to create backing region"); |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +void LazyMemoryRegionBitSet::Clear() |
| 38 | +{ |
| 39 | + m_region.Clear(); |
| 40 | +} |
| 41 | + |
| 42 | +void LazyMemoryRegionBitSet::SetBits(size_t bits_start, size_t bits_end) |
| 43 | +{ |
| 44 | + if (bits_start == bits_end) |
| 45 | + return; |
| 46 | + |
| 47 | + RangeCheck(bits_end); |
| 48 | + |
| 49 | + m_region.EnsureMemoryPagesWritable(bits_start / 8, (bits_end - bits_start) / 8); |
| 50 | + |
| 51 | + const u32 extra_first_word = 0xFFFFFFFF << bits_start % 32; |
| 52 | + const u32 extra_last_word = ~(0xFFFFFFFF << bits_end % 32); |
| 53 | + |
| 54 | + if (bits_start / 32 == bits_end / 32) |
| 55 | + { |
| 56 | + m_pointer[bits_start / 32] |= extra_first_word & extra_last_word; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (bits_start % 32 != 0) |
| 61 | + m_pointer[bits_start / 32] |= extra_first_word; |
| 62 | + |
| 63 | + const size_t start_index = (bits_start + 31) / 32; |
| 64 | + const size_t end_index = bits_end / 32; |
| 65 | + std::memset(m_pointer + start_index, 0xFF, (end_index - start_index) * 4); |
| 66 | + |
| 67 | + if (bits_end % 32 != 0) |
| 68 | + m_pointer[bits_end / 32] |= extra_last_word; |
| 69 | +} |
| 70 | + |
| 71 | +void LazyMemoryRegionBitSet::ClearBits(size_t bits_start, size_t bits_end) |
| 72 | +{ |
| 73 | + if (bits_start == bits_end) |
| 74 | + return; |
| 75 | + |
| 76 | + RangeCheck(bits_end); |
| 77 | + |
| 78 | + m_region.EnsureMemoryPagesWritable(bits_start / 8, (bits_end - bits_start) / 8); |
| 79 | + |
| 80 | + const u32 extra_first_word = ~(0xFFFFFFFF << bits_start % 32); |
| 81 | + const u32 extra_last_word = 0xFFFFFFFF << bits_end % 32; |
| 82 | + |
| 83 | + if (bits_start / 32 == bits_end / 32) |
| 84 | + { |
| 85 | + m_pointer[bits_start / 32] &= extra_first_word | extra_last_word; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (bits_start % 32 != 0) |
| 90 | + m_pointer[bits_start / 32] &= extra_first_word; |
| 91 | + |
| 92 | + const size_t start_index = (bits_start + 31) / 32; |
| 93 | + const size_t end_index = bits_end / 32; |
| 94 | + std::memset(m_pointer + start_index, 0x00, (end_index - start_index) * 4); |
| 95 | + |
| 96 | + if (bits_end % 32 != 0) |
| 97 | + m_pointer[bits_end / 32] &= extra_last_word; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace Common |
0 commit comments