amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
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>
2026-10-27 01:55:56 +08:00

110 lines
4.0 KiB
C++

#include "packet.hpp"
#include <algorithm>
#include <cstring>
#include <deque>
namespace packet {
struct Queue::State {
struct Entry {
Packet packet;
void* node;
void* payload;
};
Memory& memory;
std::array<std::deque<Entry>,3> queues;
std::array<unsigned,3> capacity{{8,8,8}};
Stats totals;
explicit State(Memory& supplied) : memory(supplied) {}
void release(const Entry& entry) {
memory.release(entry.payload);
memory.release(entry.node);
}
};
Queue::Queue(Memory& memory) : state_(new State(memory)) {}
Queue::~Queue() { shutdown(); delete state_; }
int Queue::push(unsigned lane, const Packet& packet) {
if (lane>=3 || packet.id>999999 || packet.priority>3 || packet.deadline>1000000 || packet.data.size()>64) return -1;
auto& entries=state_->queues[lane];
if (entries.size()==state_->capacity[lane]) { ++state_->totals.refused; return -2; }
void* node=state_->memory.acquire(Kind::Node,64);
if (!node) { ++state_->totals.oom; return -3; }
void* payload=state_->memory.acquire(Kind::Payload,packet.data.size()+1);
if (!payload) { state_->memory.release(node); ++state_->totals.oom; return -3; }
std::memcpy(payload,packet.data.data(),packet.data.size());
static_cast<char*>(payload)[packet.data.size()]=0;
entries.push_back(State::Entry{packet,node,payload});
++state_->totals.accepted;
return 0;
}
int Queue::pop(unsigned lane, Packet& packet) {
if (lane>=3) return -1;
auto& entries=state_->queues[lane];
if (entries.empty()) return -2;
packet=entries.front().packet;
state_->release(entries.front());
entries.pop_front();
++state_->totals.popped;
return 0;
}
int Queue::peek(unsigned lane, Packet& packet) const {
if (lane>=3) return -1;
const auto& entries=state_->queues[lane];
if (entries.empty()) return -2;
packet=entries.front().packet;
return 0;
}
int Queue::expire(unsigned lane, std::uint32_t now) {
if (lane>=3 || now>1000000) return -1;
auto& entries=state_->queues[lane];
std::deque<State::Entry> survivors;
int count=0;
for (const auto& entry:entries) {
if (entry.packet.deadline && entry.packet.deadline<=now) { state_->release(entry); ++count; }
else survivors.push_back(entry);
}
entries.swap(survivors);
state_->totals.expired+=count;
return count;
}
int Queue::clear(unsigned lane) {
if (lane>=3) return -1;
auto& entries=state_->queues[lane];
int count=static_cast<int>(entries.size());
for (const auto& entry:entries) state_->release(entry);
entries.clear();
state_->totals.cleared+=count;
return count;
}
int Queue::move(unsigned from,unsigned to,unsigned maximum) {
if (from>=3 || to>=3 || maximum>32) return -1;
if (from==to) return 0;
auto& source=state_->queues[from];
auto& target=state_->queues[to];
unsigned count=std::min<unsigned>(maximum,std::min<unsigned>(source.size(),state_->capacity[to]-target.size()));
target.insert(target.end(),source.begin(),source.begin()+count);
source.erase(source.begin(),source.begin()+count);
state_->totals.moved+=count;
return static_cast<int>(count);
}
int Queue::limit(unsigned lane,unsigned capacity) {
if (lane>=3 || capacity<1 || capacity>32) return -1;
if (capacity<state_->queues[lane].size()) return -2;
state_->capacity[lane]=capacity;
return 0;
}
LaneView Queue::lane(unsigned lane) const {
LaneView result;
if (lane>=3) return result;
const auto& entries=state_->queues[lane];
result.count=entries.size();
result.capacity=state_->capacity[lane];
if (!entries.empty()) { result.head=entries.front().packet.id; result.tail=entries.back().packet.id; }
for (const auto& entry:entries) {
result.bytes+=entry.packet.data.size();
result.checksum=result.checksum*33u+checksum(entry.packet);
}
return result;
}
Stats Queue::stats() const { return state_->totals; }
void Queue::shutdown() { for (unsigned lane=0;lane<3;++lane) clear(lane); }
}