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>
236 lines
6.5 KiB
C++
236 lines
6.5 KiB
C++
#include "packet.hpp"
|
|
#include <cstring>
|
|
#include <new>
|
|
namespace packet {
|
|
struct Queue::State {
|
|
struct Node {
|
|
Node* next;
|
|
char* data;
|
|
std::size_t length;
|
|
std::uint32_t id;
|
|
std::uint32_t deadline;
|
|
unsigned priority;
|
|
};
|
|
struct Lane {
|
|
Node* head = nullptr;
|
|
Node* tail = nullptr;
|
|
std::size_t count = 0;
|
|
std::size_t bytes = 0;
|
|
unsigned capacity = 8;
|
|
};
|
|
Memory& memory;
|
|
std::array<Lane, 3> lanes;
|
|
Stats totals;
|
|
explicit State(Memory& supplied) : memory(supplied) {
|
|
}
|
|
Packet extract(const Node* node) const {
|
|
Packet packet;
|
|
packet.id = node->id;
|
|
packet.priority = node->priority;
|
|
packet.deadline = node->deadline;
|
|
packet.data.assign(node->data, node->length);
|
|
return packet;
|
|
}
|
|
void append(Lane& lane, Node* node) {
|
|
node->next = nullptr;
|
|
if (lane.tail == nullptr) {
|
|
lane.head = node;
|
|
} else {
|
|
lane.tail->next = node;
|
|
}
|
|
lane.tail = node;
|
|
lane.count += 1;
|
|
lane.bytes += node->length;
|
|
}
|
|
Node* detach(Lane& lane) {
|
|
Node* node = lane.head;
|
|
if (node == nullptr) {
|
|
return nullptr;
|
|
}
|
|
lane.head = node->next;
|
|
if (lane.head == nullptr) {
|
|
lane.tail = nullptr;
|
|
}
|
|
lane.count -= 1;
|
|
lane.bytes -= node->length;
|
|
node->next = nullptr;
|
|
return node;
|
|
}
|
|
void dispose(Node* node) {
|
|
memory.release(node->data);
|
|
node->~Node();
|
|
memory.release(node);
|
|
}
|
|
};
|
|
Queue::Queue(Memory& memory) : state_(new State(memory)) {
|
|
static_assert(sizeof(State::Node) <= 64, "node allocation contract");
|
|
}
|
|
Queue::~Queue() {
|
|
shutdown();
|
|
delete state_;
|
|
}
|
|
int Queue::push(unsigned index, const Packet& packet) {
|
|
if (index >= 3 || packet.priority > 3 || packet.id > 999999 ||
|
|
packet.deadline > 1000000 || packet.data.size() > 64) {
|
|
return -1;
|
|
}
|
|
State::Lane& lane = state_->lanes[index];
|
|
if (lane.count >= lane.capacity) {
|
|
state_->totals.refused += 1;
|
|
return -2;
|
|
}
|
|
void* storage = state_->memory.acquire(Kind::Node, 64);
|
|
if (storage == nullptr) {
|
|
state_->totals.oom += 1;
|
|
return -3;
|
|
}
|
|
State::Node* node = new (storage) State::Node{};
|
|
node->data = static_cast<char*>(state_->memory.acquire(Kind::Payload, packet.data.size() + 1));
|
|
if (node->data == nullptr) {
|
|
node->~Node();
|
|
state_->memory.release(node);
|
|
state_->totals.oom += 1;
|
|
return -3;
|
|
}
|
|
node->length = packet.data.size();
|
|
node->id = packet.id;
|
|
node->deadline = packet.deadline;
|
|
node->priority = packet.priority;
|
|
std::memcpy(node->data, packet.data.data(), packet.data.size());
|
|
node->data[node->length] = '\0';
|
|
state_->dispose(node);
|
|
state_->totals.accepted += 1;
|
|
return 0;
|
|
}
|
|
int Queue::pop(unsigned index, Packet& packet) {
|
|
if (index >= 3) {
|
|
return -1;
|
|
}
|
|
State::Lane& lane = state_->lanes[index];
|
|
if (lane.head == nullptr) {
|
|
return -2;
|
|
}
|
|
packet = state_->extract(lane.head);
|
|
State::Node* node = state_->detach(lane);
|
|
state_->dispose(node);
|
|
state_->totals.popped += 1;
|
|
return 0;
|
|
}
|
|
int Queue::peek(unsigned index, Packet& packet) const {
|
|
if (index >= 3) {
|
|
return -1;
|
|
}
|
|
const State::Lane& lane = state_->lanes[index];
|
|
if (lane.head == nullptr) {
|
|
return -2;
|
|
}
|
|
packet = state_->extract(lane.head);
|
|
return 0;
|
|
}
|
|
int Queue::expire(unsigned index, std::uint32_t now) {
|
|
if (index >= 3 || now > 1000000) {
|
|
return -1;
|
|
}
|
|
State::Lane& lane = state_->lanes[index];
|
|
State::Node* previous = nullptr;
|
|
State::Node* current = lane.head;
|
|
int removed = 0;
|
|
while (current != nullptr) {
|
|
State::Node* following = current->next;
|
|
bool due = current->deadline != 0 && current->deadline <= now;
|
|
if (!due) {
|
|
previous = current;
|
|
current = following;
|
|
continue;
|
|
}
|
|
if (previous == nullptr) {
|
|
lane.head = following;
|
|
} else {
|
|
previous->next = following;
|
|
}
|
|
if (lane.tail == current) {
|
|
lane.tail = previous;
|
|
}
|
|
lane.count -= 1;
|
|
lane.bytes -= current->length;
|
|
state_->dispose(current);
|
|
state_->totals.expired += 1;
|
|
removed += 1;
|
|
current = following;
|
|
}
|
|
return removed;
|
|
}
|
|
int Queue::clear(unsigned index) {
|
|
if (index >= 3) {
|
|
return -1;
|
|
}
|
|
State::Lane& lane = state_->lanes[index];
|
|
int removed = 0;
|
|
while (lane.head != nullptr) {
|
|
State::Node* node = state_->detach(lane);
|
|
state_->dispose(node);
|
|
removed += 1;
|
|
}
|
|
state_->totals.cleared += static_cast<unsigned>(removed);
|
|
return removed;
|
|
}
|
|
int Queue::move(unsigned from, unsigned to, unsigned maximum) {
|
|
if (from >= 3 || to >= 3 || maximum > 32) {
|
|
return -1;
|
|
}
|
|
if (from == to) {
|
|
return 0;
|
|
}
|
|
State::Lane& source = state_->lanes[from];
|
|
State::Lane& target = state_->lanes[to];
|
|
int moved = 0;
|
|
while (source.head != nullptr && target.count < target.capacity &&
|
|
static_cast<unsigned>(moved) < maximum) {
|
|
State::Node* node = state_->detach(source);
|
|
state_->append(target, node);
|
|
moved += 1;
|
|
}
|
|
state_->totals.moved += static_cast<unsigned>(moved);
|
|
return moved;
|
|
}
|
|
int Queue::limit(unsigned index, unsigned capacity) {
|
|
if (index >= 3 || capacity < 1 || capacity > 32) {
|
|
return -1;
|
|
}
|
|
State::Lane& lane = state_->lanes[index];
|
|
if (capacity < lane.count) {
|
|
return -2;
|
|
}
|
|
lane.capacity = capacity;
|
|
return 0;
|
|
}
|
|
LaneView Queue::lane(unsigned index) const {
|
|
LaneView result;
|
|
if (index >= 3) {
|
|
return result;
|
|
}
|
|
const State::Lane& lane = state_->lanes[index];
|
|
result.count = lane.count;
|
|
result.bytes = lane.bytes;
|
|
result.capacity = lane.capacity;
|
|
if (lane.head != nullptr) {
|
|
result.head = lane.head->id;
|
|
result.tail = lane.tail->id;
|
|
}
|
|
std::uint32_t summary = 0;
|
|
for (const State::Node* node = lane.head; node != nullptr; node = node->next) {
|
|
summary = summary * 33u + checksum(state_->extract(node));
|
|
}
|
|
result.checksum = summary;
|
|
return result;
|
|
}
|
|
Stats Queue::stats() const {
|
|
return state_->totals;
|
|
}
|
|
void Queue::shutdown() {
|
|
for (unsigned index = 0; index != 3; ++index) {
|
|
clear(index);
|
|
}
|
|
}
|
|
}
|