One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
298 lines
7.7 KiB
C++
298 lines
7.7 KiB
C++
#include "counter.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
|
|
namespace resource {
|
|
Counter::Object::Object(unsigned object_id, int initial)
|
|
: id(object_id), refs(1), value(initial), finalized(false) {}
|
|
|
|
Counter::Counter() : slots_{}, arena_{}, finalized_{}, next_id_(1) {
|
|
slots_.fill(nullptr);
|
|
}
|
|
|
|
Counter::~Counter() = default;
|
|
|
|
Result Counter::success() {
|
|
return {Code::ok, false, 0};
|
|
}
|
|
|
|
Result Counter::success(int value) {
|
|
return {Code::ok, true, value};
|
|
}
|
|
|
|
Result Counter::failure(Code code) {
|
|
return {code, false, 0};
|
|
}
|
|
|
|
bool Counter::valid_slot(std::size_t slot) {
|
|
return slot < slot_count;
|
|
}
|
|
|
|
bool Counter::valid_value(int value) {
|
|
return value >= value_min && value <= value_max;
|
|
}
|
|
|
|
Counter::Object* Counter::allocate(int value) {
|
|
if (next_id_ > maximum_commands) {
|
|
return nullptr;
|
|
}
|
|
auto object = std::make_unique<Object>(next_id_, value);
|
|
Object* pointer = object.get();
|
|
arena_.push_back(std::move(object));
|
|
++next_id_;
|
|
return pointer;
|
|
}
|
|
|
|
void Counter::acquire(Object* object) {
|
|
if (object == nullptr || object->finalized) {
|
|
throw std::logic_error("retain of finalized object");
|
|
}
|
|
const unsigned previous = object->refs.fetch_add(1, std::memory_order_relaxed);
|
|
if (previous == 0 || previous >= slot_count) {
|
|
throw std::logic_error("invalid owner count before retain");
|
|
}
|
|
}
|
|
|
|
void Counter::drop(Object* object) {
|
|
if (object == nullptr || object->finalized) {
|
|
throw std::logic_error("release of finalized object");
|
|
}
|
|
const unsigned previous = object->refs.fetch_sub(1, std::memory_order_acq_rel);
|
|
if (previous == 0 || previous > slot_count) {
|
|
throw std::logic_error("invalid owner count before release");
|
|
}
|
|
if (previous == 1) {
|
|
object->finalized = true;
|
|
finalized_.push_back(object->id);
|
|
}
|
|
}
|
|
|
|
Result Counter::create(std::size_t slot, int value) {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
if (!valid_value(value)) {
|
|
return failure(Code::range);
|
|
}
|
|
if (slots_[slot] != nullptr) {
|
|
return failure(Code::occupied);
|
|
}
|
|
Object* object = allocate(value);
|
|
if (object == nullptr) {
|
|
return failure(Code::limit);
|
|
}
|
|
slots_[slot] = object;
|
|
return success();
|
|
}
|
|
|
|
Result Counter::retain(std::size_t source, std::size_t destination) {
|
|
if (!valid_slot(source) || !valid_slot(destination)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[source];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
if (slots_[destination] != nullptr) {
|
|
return failure(Code::occupied);
|
|
}
|
|
acquire(object);
|
|
slots_[destination] = object;
|
|
return success();
|
|
}
|
|
|
|
Result Counter::release(std::size_t slot) {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
slots_[slot] = nullptr;
|
|
drop(object);
|
|
return success();
|
|
}
|
|
|
|
Result Counter::move(std::size_t source, std::size_t destination) {
|
|
if (!valid_slot(source) || !valid_slot(destination)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[source];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
if (source == destination) {
|
|
return success();
|
|
}
|
|
Object* displaced = slots_[destination];
|
|
slots_[destination] = object;
|
|
slots_[source] = nullptr;
|
|
if (displaced != nullptr) {
|
|
drop(displaced);
|
|
}
|
|
return success();
|
|
}
|
|
|
|
Result Counter::swap(std::size_t left, std::size_t right) {
|
|
if (!valid_slot(left) || !valid_slot(right)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
std::swap(slots_[left], slots_[right]);
|
|
return success();
|
|
}
|
|
|
|
Result Counter::set(std::size_t slot, int value) {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
if (!valid_value(value)) {
|
|
return failure(Code::range);
|
|
}
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
object->value = value;
|
|
return success();
|
|
}
|
|
|
|
Result Counter::add(std::size_t slot, int delta) {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
const long long sum = static_cast<long long>(object->value) + delta;
|
|
if (sum < value_min || sum > value_max) {
|
|
return failure(Code::range);
|
|
}
|
|
object->value = static_cast<int>(sum);
|
|
return success();
|
|
}
|
|
|
|
Result Counter::read(std::size_t slot) const {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
return success(object->value);
|
|
}
|
|
|
|
Result Counter::count(std::size_t slot) const {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
return success(static_cast<int>(object->refs.load(std::memory_order_acquire)));
|
|
}
|
|
|
|
Result Counter::clone(std::size_t source, std::size_t destination) {
|
|
if (!valid_slot(source) || !valid_slot(destination)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
Object* object = slots_[source];
|
|
if (object == nullptr) {
|
|
return failure(Code::empty);
|
|
}
|
|
if (slots_[destination] != nullptr) {
|
|
return failure(Code::occupied);
|
|
}
|
|
Object* copy = allocate(object->value);
|
|
if (copy == nullptr) {
|
|
return failure(Code::limit);
|
|
}
|
|
slots_[destination] = copy;
|
|
return success();
|
|
}
|
|
|
|
Result Counter::replace(std::size_t slot, int value) {
|
|
if (!valid_slot(slot)) {
|
|
return failure(Code::bad_argument);
|
|
}
|
|
if (!valid_value(value)) {
|
|
return failure(Code::range);
|
|
}
|
|
Object* replacement = allocate(value);
|
|
if (replacement == nullptr) {
|
|
return failure(Code::limit);
|
|
}
|
|
Object* previous = slots_[slot];
|
|
slots_[slot] = replacement;
|
|
if (previous != nullptr) {
|
|
drop(previous);
|
|
}
|
|
return success();
|
|
}
|
|
|
|
Result Counter::clear() {
|
|
for (std::size_t slot = 0; slot < slot_count; ++slot) {
|
|
if (slots_[slot] != nullptr) {
|
|
Object* object = slots_[slot];
|
|
slots_[slot] = nullptr;
|
|
drop(object);
|
|
}
|
|
}
|
|
return success();
|
|
}
|
|
|
|
Result Counter::snapshot_result() const {
|
|
return success();
|
|
}
|
|
|
|
Snapshot Counter::snapshot() const {
|
|
Snapshot result{};
|
|
result.present.fill(false);
|
|
result.active = 0;
|
|
result.finalized = finalized_;
|
|
for (std::size_t slot = 0; slot < slot_count; ++slot) {
|
|
Object* object = slots_[slot];
|
|
if (object == nullptr) {
|
|
continue;
|
|
}
|
|
result.present[slot] = true;
|
|
result.slots[slot] = {object->id, object->value,
|
|
object->refs.load(std::memory_order_acquire)};
|
|
}
|
|
for (const auto& object : arena_) {
|
|
if (!object->finalized) {
|
|
++result.active;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
unsigned Counter::created_count() const {
|
|
return next_id_ - 1;
|
|
}
|
|
|
|
bool Counter::invariant() const {
|
|
for (const auto& object : arena_) {
|
|
unsigned holders = 0;
|
|
for (const Object* slot : slots_) {
|
|
if (slot == object.get()) {
|
|
++holders;
|
|
}
|
|
}
|
|
unsigned refs = object->refs.load(std::memory_order_acquire);
|
|
if (object->finalized) {
|
|
if (refs != 0 || holders != 0) {
|
|
return false;
|
|
}
|
|
} else if (refs != holders || holders == 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|