Skip to content

Commit

Permalink
Use std::uintptr_t internally in MemorySection class to reduce the nu…
Browse files Browse the repository at this point in the history
…mber of casts
  • Loading branch information
danielkrupinski committed Dec 14, 2023
1 parent a86cb20 commit 7c35978
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Source/Utils/MemorySection.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@
struct MemorySection {
MemorySection() = default;

explicit MemorySection(std::span<const std::byte> section)
: section{ section }
explicit MemorySection(std::span<const std::byte> section) noexcept
: base{reinterpret_cast<std::uintptr_t>(section.data())}
, size{section.size()}
{
}

[[nodiscard]] std::span<const std::byte> raw() const noexcept
{
return section;
return {reinterpret_cast<const std::byte*>(base), size};
}

[[nodiscard]] bool contains(std::uintptr_t address, std::size_t size) const noexcept
[[nodiscard]] bool contains(std::uintptr_t address, std::size_t objectSize) const noexcept
{
return address >= std::uintptr_t(section.data()) && section.size() >= size && (address - std::uintptr_t(section.data())) <= section.size() - size;
return address >= base && size >= objectSize && (address - base) <= size - objectSize;
}

[[nodiscard]] bool contains(std::uintptr_t address) const noexcept
{
return address >= std::uintptr_t(section.data()) && address - std::uintptr_t(section.data()) < section.size();
return address >= base && address - base < size;
}

private:
std::span<const std::byte> section{};
std::uintptr_t base{0};
std::size_t size{0};
};

0 comments on commit 7c35978

Please sign in to comment.