utils: Rename address_range to address_range32 to allow implementation of address_range16 and address_range64

This commit is contained in:
kd-11 2025-05-26 02:49:20 +03:00 committed by kd-11
parent 79bcb7790c
commit 4f7c82ba8a
51 changed files with 368 additions and 368 deletions

View file

@ -37,7 +37,7 @@ namespace utils
/**
* Address Range utility class
*/
class address_range
class address_range32
{
public:
u32 start = umax; // First address in range
@ -60,13 +60,13 @@ namespace utils
return (start1 >= start2 && end1 <= end2);
}
constexpr address_range(u32 _start, u32 _end) : start(_start), end(_end) {}
constexpr address_range32(u32 _start, u32 _end) : start(_start), end(_end) {}
public:
// Constructors
constexpr address_range() = default;
constexpr address_range32() = default;
static constexpr address_range start_length(u32 _start, u32 _length)
static constexpr address_range32 start_length(u32 _start, u32 _length)
{
if (!_length)
{
@ -76,7 +76,7 @@ namespace utils
return {_start, _start + (_length - 1)};
}
static constexpr address_range start_end(u32 _start, u32 _end)
static constexpr address_range32 start_end(u32 _start, u32 _end)
{
return {_start, _end};
}
@ -105,7 +105,7 @@ namespace utils
}
// Overlapping checks
bool overlaps(const address_range &other) const
bool overlaps(const address_range32 &other) const
{
AUDIT(valid() && other.valid());
return range_overlaps(start, end, other.start, other.end);
@ -117,7 +117,7 @@ namespace utils
return address_overlaps(addr, start, end);
}
bool inside(const address_range &other) const
bool inside(const address_range32 &other) const
{
AUDIT(valid() && other.valid());
return range_inside_range(start, end, other.start, other.end);
@ -126,7 +126,7 @@ namespace utils
inline bool inside(const address_range_vector &vec) const;
inline bool overlaps(const address_range_vector &vec) const;
bool touches(const address_range &other) const
bool touches(const address_range32 &other) const
{
AUDIT(valid() && other.valid());
// returns true if there is overlap, or if sections are side-by-side
@ -134,7 +134,7 @@ namespace utils
}
// Utilities
s32 signed_distance(const address_range &other) const
s32 signed_distance(const address_range32 &other) const
{
if (touches(other))
{
@ -152,7 +152,7 @@ namespace utils
return -static_cast<s32>(start - other.end - 1);
}
u32 distance(const address_range &other) const
u32 distance(const address_range32 &other) const
{
if (touches(other))
{
@ -170,7 +170,7 @@ namespace utils
return (start - other.end - 1);
}
address_range get_min_max(const address_range &other) const
address_range32 get_min_max(const address_range32 &other) const
{
return {
std::min(valid() ? start : umax, other.valid() ? other.start : umax),
@ -178,7 +178,7 @@ namespace utils
};
}
void set_min_max(const address_range &other)
void set_min_max(const address_range32 &other)
{
*this = get_min_max(other);
}
@ -188,7 +188,7 @@ namespace utils
return (valid() && is_page_aligned(start) && is_page_aligned(length()));
}
address_range to_page_range() const
address_range32 to_page_range() const
{
AUDIT(valid());
return { page_start(start), page_end(end) };
@ -202,7 +202,7 @@ namespace utils
AUDIT(is_page_range());
}
address_range get_intersect(const address_range &clamp) const
address_range32 get_intersect(const address_range32 &clamp) const
{
if (!valid() || !clamp.valid())
{
@ -212,7 +212,7 @@ namespace utils
return { std::max(start, clamp.start), std::min(end, clamp.end) };
}
void intersect(const address_range &clamp)
void intersect(const address_range32 &clamp)
{
if (!clamp.valid())
{
@ -238,7 +238,7 @@ namespace utils
}
// Comparison Operators
bool operator ==(const address_range& other) const
bool operator ==(const address_range32& other) const
{
return (start == other.start && end == other.end);
}
@ -252,21 +252,21 @@ namespace utils
}
};
static inline address_range page_for(u32 addr)
static inline address_range32 page_for(u32 addr)
{
return address_range::start_end(page_start(addr), page_end(addr));
return address_range32::start_end(page_start(addr), page_end(addr));
}
/**
* Address Range Vector utility class
*
* Collection of address_range objects. Allows for merging and removing ranges from the set.
* Collection of address_range32 objects. Allows for merging and removing ranges from the set.
*/
class address_range_vector
{
public:
using vector_type = std::vector<address_range>;
using vector_type = std::vector<address_range32>;
using iterator = vector_type::iterator;
using const_iterator = vector_type::const_iterator;
using size_type = vector_type::size_type;
@ -280,8 +280,8 @@ namespace utils
inline void clear() { data.clear(); }
inline size_type size() const { return data.size(); }
inline bool empty() const { return data.empty(); }
inline address_range& operator[](size_type n) { return data[n]; }
inline const address_range& operator[](size_type n) const { return data[n]; }
inline address_range32& operator[](size_type n) { return data[n]; }
inline const address_range32& operator[](size_type n) const { return data[n]; }
inline iterator begin() { return data.begin(); }
inline const_iterator begin() const { return data.begin(); }
inline iterator end() { return data.end(); }
@ -289,7 +289,7 @@ namespace utils
// Search for ranges that touch new_range. If found, merge instead of adding new_range.
// When adding a new range, re-use invalid ranges whenever possible
void merge(const address_range &new_range)
void merge(const address_range32 &new_range)
{
// Note the case where we have
// AAAA BBBB
@ -301,8 +301,8 @@ namespace utils
return;
}
address_range *found = nullptr;
address_range *invalid = nullptr;
address_range32 *found = nullptr;
address_range32 *invalid = nullptr;
for (auto &existing : data)
{
@ -349,20 +349,20 @@ namespace utils
void merge(const address_range_vector &other)
{
for (const address_range &new_range : other)
for (const address_range32 &new_range : other)
{
merge(new_range);
}
}
// Exclude a given range from data
void exclude(const address_range &exclusion)
void exclude(const address_range32 &exclusion)
{
// Note the case where we have
// AAAAAAA
// EEE
// where data={A} and exclusion=E.
// In this case, we need to reduce A to the head (before E starts), and then create a new address_range B for the tail (after E ends), i.e.
// In this case, we need to reduce A to the head (before E starts), and then create a new address_range32 B for the tail (after E ends), i.e.
// AA BB
// EEE
@ -371,13 +371,13 @@ namespace utils
return;
}
address_range *invalid = nullptr; // try to re-use an invalid range instead of calling push_back
address_range32 *invalid = nullptr; // try to re-use an invalid range instead of calling push_back
// We use index access because we might have to push_back within the loop, which could invalidate the iterators
size_type _size = data.size();
for (size_type n = 0; n < _size; ++n)
{
address_range &existing = data[n];
address_range32 &existing = data[n];
if (!existing.valid())
{
@ -430,7 +430,7 @@ namespace utils
else
{
// IMPORTANT: adding to data invalidates "existing". This must be done last!
data.push_back(address_range::start_end(exclusion.next_address(), tail_end));
data.push_back(address_range32::start_end(exclusion.next_address(), tail_end));
}
}
}
@ -440,7 +440,7 @@ namespace utils
void exclude(const address_range_vector &other)
{
for (const address_range &exclusion : other)
for (const address_range32 &exclusion : other)
{
exclude(exclusion);
}
@ -478,25 +478,25 @@ namespace utils
}
// Test for overlap with a given range
bool overlaps(const address_range &range) const
bool overlaps(const address_range32 &range) const
{
return std::any_of(data.cbegin(), data.cend(), [&range](const address_range& cur)
return std::any_of(data.cbegin(), data.cend(), [&range](const address_range32& cur)
{
return cur.valid() && cur.overlaps(range);
});
}
// Test for overlap with a given address_range vector
// Test for overlap with a given address_range32 vector
bool overlaps(const address_range_vector &other) const
{
for (const address_range &rng1 : data)
for (const address_range32 &rng1 : data)
{
if (!rng1.valid())
{
continue;
}
for (const address_range &rng2 : other.data)
for (const address_range32 &rng2 : other.data)
{
if (!rng2.valid())
{
@ -513,18 +513,18 @@ namespace utils
}
// Test if a given range is fully contained inside this vector
bool contains(const address_range &range) const
bool contains(const address_range32 &range) const
{
return std::any_of(this->begin(), this->end(), [&range](const address_range& cur)
return std::any_of(this->begin(), this->end(), [&range](const address_range32& cur)
{
return cur.valid() && cur.inside(range);
});
}
// Test if all ranges in this vector are full contained inside a specific range
bool inside(const address_range &range) const
bool inside(const address_range32 &range) const
{
return std::all_of(this->begin(), this->end(), [&range](const address_range& cur)
return std::all_of(this->begin(), this->end(), [&range](const address_range32& cur)
{
return !cur.valid() || cur.inside(range);
});
@ -547,12 +547,12 @@ namespace utils
// These declarations must be done after address_range_vector has been defined
bool address_range::inside(const address_range_vector &vec) const
bool address_range32::inside(const address_range_vector &vec) const
{
return vec.contains(*this);
}
bool address_range::overlaps(const address_range_vector &vec) const
bool address_range32::overlaps(const address_range_vector &vec) const
{
return vec.overlaps(*this);
}
@ -565,9 +565,9 @@ namespace std
static_assert(sizeof(usz) >= 2 * sizeof(u32), "usz must be at least twice the size of u32");
template <>
struct hash<utils::address_range>
struct hash<utils::address_range32>
{
usz operator()(const utils::address_range& k) const
usz operator()(const utils::address_range32& k) const
{
// we can guarantee a unique hash since our type is 64 bits and usz as well
return (usz{ k.start } << 32) | usz{ k.end };