100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
240 lines
6.4 KiB
C++
240 lines
6.4 KiB
C++
#include "pool.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
|
|
namespace resource_map {
|
|
namespace detail {
|
|
bool valid_id(int id)
|
|
{
|
|
return id >= 0 && id < 8;
|
|
}
|
|
bool valid_resource(const Resource& resource)
|
|
{
|
|
if (resource.kind != static_cast<int>(Kind::MEM) &&
|
|
resource.kind != static_cast<int>(Kind::IO)) {
|
|
return false;
|
|
}
|
|
if (resource.start > 65535) {
|
|
return false;
|
|
}
|
|
if (resource.size < 1 || resource.size > 64) {
|
|
return false;
|
|
}
|
|
return static_cast<std::uint64_t>(resource.size) <= 65536 - resource.start;
|
|
}
|
|
bool contains(const Resource& resource, std::uint64_t offset, int count)
|
|
{
|
|
const auto size = static_cast<std::uint64_t>(resource.size);
|
|
if (offset > size) {
|
|
return false;
|
|
}
|
|
return static_cast<std::uint64_t>(count) <= size - offset;
|
|
}
|
|
bool intersects(const Resource& left, const Resource& right)
|
|
{
|
|
const auto left_end = left.start + static_cast<unsigned int>(left.size);
|
|
const auto right_end = right.start + static_cast<unsigned int>(right.size);
|
|
return left.start < right_end && right.start < left_end;
|
|
}
|
|
bool mapping_order(const MappingInfo& left, const MappingInfo& right)
|
|
{
|
|
if (left.start != right.start) {
|
|
return left.start < right.start;
|
|
}
|
|
return left.token < right.token;
|
|
}
|
|
void Pool::clear()
|
|
{
|
|
regions_.clear();
|
|
next_token_ = 1;
|
|
created_ = 0;
|
|
released_ = 0;
|
|
reads_ = 0;
|
|
writes_ = 0;
|
|
fills_ = 0;
|
|
}
|
|
bool Pool::conflicts(const Resource& resource) const
|
|
{
|
|
for (const auto& entry : regions_) {
|
|
if (intersects(resource, entry.second.resource)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
std::uint64_t Pool::create(int owner, unsigned int index, const Resource& resource)
|
|
{
|
|
Region region;
|
|
region.token = next_token_;
|
|
region.owner = owner;
|
|
region.resource_index = index;
|
|
region.resource = resource;
|
|
regions_.emplace(region.token, region);
|
|
++next_token_;
|
|
++created_;
|
|
return region.token;
|
|
}
|
|
void Pool::release(std::uint64_t token)
|
|
{
|
|
const auto entry = regions_.find(token);
|
|
if (entry == regions_.end()) {
|
|
return;
|
|
}
|
|
regions_.erase(entry);
|
|
++released_;
|
|
}
|
|
Region* Pool::find(std::uint64_t token)
|
|
{
|
|
const auto entry = regions_.find(token);
|
|
if (entry == regions_.end()) {
|
|
return nullptr;
|
|
}
|
|
return &entry->second;
|
|
}
|
|
const Region* Pool::find(std::uint64_t token) const
|
|
{
|
|
const auto entry = regions_.find(token);
|
|
if (entry == regions_.end()) {
|
|
return nullptr;
|
|
}
|
|
return &entry->second;
|
|
}
|
|
Code Pool::access_error(const Region& region, std::uint64_t offset, int width) const
|
|
{
|
|
if (width != 1 && width != 2 && width != 4) {
|
|
return Code::ARG;
|
|
}
|
|
if (offset >= static_cast<std::uint64_t>(region.resource.size)) {
|
|
return Code::RANGE;
|
|
}
|
|
return Code::OK;
|
|
}
|
|
ReadResult Pool::read(std::uint64_t token, std::uint64_t offset, int width)
|
|
{
|
|
Region* region = find(token);
|
|
if (region == nullptr) {
|
|
return {Code::STALE, 0};
|
|
}
|
|
const Code code = access_error(*region, offset, width);
|
|
if (code != Code::OK) {
|
|
return {code, 0};
|
|
}
|
|
std::uint64_t value = 0;
|
|
for (int byte = 0; byte < width; ++byte) {
|
|
const auto part = static_cast<std::uint64_t>(region->bytes[offset + byte]);
|
|
value |= part << (8 * byte);
|
|
}
|
|
++region->reads;
|
|
++reads_;
|
|
return {Code::OK, value};
|
|
}
|
|
Code Pool::write(std::uint64_t token, std::uint64_t offset, int width,
|
|
std::uint64_t value)
|
|
{
|
|
Region* region = find(token);
|
|
if (region == nullptr) {
|
|
return Code::STALE;
|
|
}
|
|
const Code code = access_error(*region, offset, width);
|
|
if (code != Code::OK) {
|
|
return code;
|
|
}
|
|
const std::uint64_t limit = (std::uint64_t{1} << (width * 8)) - 1;
|
|
if (value > limit) {
|
|
return Code::VALUE;
|
|
}
|
|
for (int byte = 0; byte < width; ++byte) {
|
|
region->bytes[offset + byte] = static_cast<unsigned char>(value >> (8 * byte));
|
|
}
|
|
++region->writes;
|
|
++writes_;
|
|
return Code::OK;
|
|
}
|
|
Code Pool::fill(std::uint64_t token, std::uint64_t offset, int count, int value)
|
|
{
|
|
Region* region = find(token);
|
|
if (region == nullptr) {
|
|
return Code::STALE;
|
|
}
|
|
if (count < 0 || count > 64 || value < 0 || value > 255) {
|
|
return Code::ARG;
|
|
}
|
|
if (!contains(region->resource, offset, count)) {
|
|
return Code::RANGE;
|
|
}
|
|
for (int byte = 0; byte < count; ++byte) {
|
|
region->bytes[offset + byte] = static_cast<unsigned char>(value);
|
|
}
|
|
++region->fills;
|
|
++fills_;
|
|
return Code::OK;
|
|
}
|
|
BytesResult Pool::dump(std::uint64_t token, std::uint64_t offset, int count) const
|
|
{
|
|
const Region* region = find(token);
|
|
if (region == nullptr) {
|
|
return {Code::STALE, {}};
|
|
}
|
|
if (count < 0 || count > 64) {
|
|
return {Code::ARG, {}};
|
|
}
|
|
if (!contains(region->resource, offset, count)) {
|
|
return {Code::RANGE, {}};
|
|
}
|
|
BytesResult result;
|
|
result.bytes.reserve(static_cast<unsigned int>(count));
|
|
for (int byte = 0; byte < count; ++byte) {
|
|
result.bytes.push_back(region->bytes[offset + byte]);
|
|
}
|
|
return result;
|
|
}
|
|
MappingInfo Pool::describe(const Region& region) const
|
|
{
|
|
MappingInfo info;
|
|
info.token = region.token;
|
|
info.owner = region.owner;
|
|
info.resource_index = region.resource_index;
|
|
info.start = region.resource.start;
|
|
info.size = region.resource.size;
|
|
info.reads = region.reads;
|
|
info.writes = region.writes;
|
|
info.fills = region.fills;
|
|
return info;
|
|
}
|
|
MappingInfo Pool::mapping(std::uint64_t token) const
|
|
{
|
|
const Region* region = find(token);
|
|
if (region == nullptr) {
|
|
MappingInfo result;
|
|
result.code = Code::STALE;
|
|
return result;
|
|
}
|
|
return describe(*region);
|
|
}
|
|
std::vector<MappingInfo> Pool::snapshot() const
|
|
{
|
|
std::vector<MappingInfo> result;
|
|
result.reserve(regions_.size());
|
|
for (const auto& entry : regions_) {
|
|
result.push_back(describe(entry.second));
|
|
}
|
|
std::sort(result.begin(), result.end(), mapping_order);
|
|
return result;
|
|
}
|
|
Statistics Pool::statistics() const
|
|
{
|
|
Statistics result;
|
|
result.created = created_;
|
|
result.released = released_;
|
|
result.read_ops = reads_;
|
|
result.write_ops = writes_;
|
|
result.fill_ops = fills_;
|
|
result.live = regions_.size();
|
|
for (const auto& entry : regions_) {
|
|
result.live_bytes += static_cast<unsigned int>(entry.second.resource.size);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|