#include "hotplug.hpp" namespace hotplug { CpuSet::CpuSet() : bits_(0) { } CpuSet::CpuSet(std::uint32_t bits) : bits_(bits & 65535U) { } bool CpuSet::valid_count(long long count) { return count >= 1 && count <= max_cpus; } bool CpuSet::valid_mask(long long mask, int count) { if (!valid_count(count)) { return false; } if (mask < 0) { return false; } const auto limit = std::uint32_t(1) << count; return static_cast(mask) < limit; } bool CpuSet::contains(int cpu) const { if (cpu < 0 || cpu >= max_cpus) { return false; } return (bits_ & (std::uint32_t(1) << cpu)) != 0; } bool CpuSet::insert(int cpu) { if (cpu < 0 || cpu >= max_cpus) { return false; } const auto bit = std::uint32_t(1) << cpu; const bool changed = (bits_ & bit) == 0; bits_ |= bit; return changed; } bool CpuSet::erase(int cpu) { if (cpu < 0 || cpu >= max_cpus) { return false; } const auto bit = std::uint32_t(1) << cpu; const bool changed = (bits_ & bit) != 0; bits_ &= ~bit; return changed; } bool CpuSet::empty() const { return bits_ == 0; } int CpuSet::count() const { auto remaining = bits_; int result = 0; while (remaining != 0) { remaining &= remaining - 1; ++result; } return result; } std::uint32_t CpuSet::bits() const { return bits_; } std::vector CpuSet::ascending() const { std::vector result; result.reserve(count()); for (int cpu = 0; cpu < max_cpus; ++cpu) { if (contains(cpu)) { result.push_back(cpu); } } return result; } std::vector CpuSet::descending() const { auto result = ascending(); for (std::size_t left = 0, right = result.size(); left < right; ++left) { --right; if (left >= right) { break; } const int saved = result[left]; result[left] = result[right]; result[right] = saved; } return result; } CpuSet CpuSet::difference(const CpuSet &other) const { return CpuSet(bits_ & ~other.bits_); } CpuSet CpuSet::intersect(const CpuSet &other) const { return CpuSet(bits_ & other.bits_); } bool CpuSet::subset_of(const CpuSet &other) const { return (bits_ & ~other.bits_) == 0; } bool CpuSet::operator==(const CpuSet &other) const { return bits_ == other.bits_; } }