#ifndef LargeRange_h
#define LargeRange_h
#include "BAssert.h"
#include "Range.h"
namespace bmalloc {
class LargeRange : public Range {
public:
LargeRange()
: Range()
, m_startPhysicalSize(0)
, m_totalPhysicalSize(0)
{
}
LargeRange(const Range& other, size_t startPhysicalSize, size_t totalPhysicalSize)
: Range(other)
, m_startPhysicalSize(startPhysicalSize)
, m_totalPhysicalSize(totalPhysicalSize)
{
BASSERT(this->size() >= this->totalPhysicalSize());
BASSERT(this->totalPhysicalSize() >= this->startPhysicalSize());
}
LargeRange(void* begin, size_t size, size_t startPhysicalSize, size_t totalPhysicalSize)
: Range(begin, size)
, m_startPhysicalSize(startPhysicalSize)
, m_totalPhysicalSize(totalPhysicalSize)
{
BASSERT(this->size() >= this->totalPhysicalSize());
BASSERT(this->totalPhysicalSize() >= this->startPhysicalSize());
}
size_t startPhysicalSize() const { return m_startPhysicalSize; }
void setStartPhysicalSize(size_t startPhysicalSize) { m_startPhysicalSize = startPhysicalSize; }
size_t totalPhysicalSize() const { return m_totalPhysicalSize; }
void setTotalPhysicalSize(size_t totalPhysicalSize) { m_totalPhysicalSize = totalPhysicalSize; }
std::pair<LargeRange, LargeRange> split(size_t) const;
void setEligible(bool eligible) { m_isEligible = eligible; }
bool isEligibile() const { return m_isEligible; }
bool operator<(const void* other) const { return begin() < other; }
bool operator<(const LargeRange& other) const { return begin() < other.begin(); }
private:
size_t m_startPhysicalSize;
size_t m_totalPhysicalSize;
bool m_isEligible { true };
};
inline bool canMerge(const LargeRange& a, const LargeRange& b)
{
if (!a.isEligibile() || !b.isEligibile()) {
return false;
}
if (a.end() == b.begin())
return true;
if (b.end() == a.begin())
return true;
return false;
}
inline LargeRange merge(const LargeRange& a, const LargeRange& b)
{
const LargeRange& left = std::min(a, b);
if (left.size() == left.startPhysicalSize()) {
return LargeRange(
left.begin(),
a.size() + b.size(),
a.startPhysicalSize() + b.startPhysicalSize(),
a.totalPhysicalSize() + b.totalPhysicalSize());
}
return LargeRange(
left.begin(),
a.size() + b.size(),
left.startPhysicalSize(),
a.totalPhysicalSize() + b.totalPhysicalSize());
}
inline std::pair<LargeRange, LargeRange> LargeRange::split(size_t leftSize) const
{
BASSERT(leftSize <= this->size());
size_t rightSize = this->size() - leftSize;
if (leftSize <= startPhysicalSize()) {
BASSERT(totalPhysicalSize() >= leftSize);
LargeRange left(begin(), leftSize, leftSize, leftSize);
LargeRange right(left.end(), rightSize, startPhysicalSize() - leftSize, totalPhysicalSize() - leftSize);
return std::make_pair(left, right);
}
double ratio = static_cast<double>(leftSize) / static_cast<double>(this->size());
size_t leftTotalPhysicalSize = static_cast<size_t>(ratio * totalPhysicalSize());
BASSERT(leftTotalPhysicalSize <= leftSize);
leftTotalPhysicalSize = std::max(startPhysicalSize(), leftTotalPhysicalSize);
size_t rightTotalPhysicalSize = totalPhysicalSize() - leftTotalPhysicalSize;
if (rightTotalPhysicalSize > rightSize) { leftTotalPhysicalSize += rightTotalPhysicalSize - rightSize;
BASSERT(leftTotalPhysicalSize <= leftSize);
rightTotalPhysicalSize = rightSize;
}
LargeRange left(begin(), leftSize, startPhysicalSize(), leftTotalPhysicalSize);
LargeRange right(left.end(), rightSize, 0, rightTotalPhysicalSize);
return std::make_pair(left, right);
}
}
#endif // LargeRange_h