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

210 lines
5.4 KiB
C

#include "ticket.h"
#include <string.h>
static int owner_valid(uint32_t owner)
{
return owner >= 1 && owner <= TICKET_MAX_OWNER;
}
static size_t occupied(const TicketBook *book)
{
size_t n = 0;
for (size_t i = 0; i < TICKET_SLOTS; ++i) {
if (book->entries[i].used) {
++n;
}
}
return n;
}
static int find_owner(const TicketBook *book, uint32_t owner)
{
for (size_t i = 0; i < TICKET_SLOTS; ++i) {
if (book->entries[i].used && book->entries[i].grant.owner == owner) {
return (int)i;
}
}
return -1;
}
static int unused_slot(const TicketBook *book)
{
for (size_t i = 0; i < TICKET_SLOTS; ++i) {
if (!book->entries[i].used) {
return (int)i;
}
}
return -1;
}
static uint64_t interval_sum(uint64_t first, uint64_t count)
{
uint64_t pair = first + first + count - 1;
if ((count & 1) == 0) {
return (count / 2) * pair;
}
return count * (pair / 2);
}
int ticket_reset(TicketBook *book, uint64_t first, uint64_t limit)
{
if (book == NULL || first == 0 || first > limit || limit > TICKET_MAX_ID) {
return -1;
}
uint64_t epoch = book->epoch + 1;
memset(book->entries, 0, sizeof(book->entries));
book->open = 1;
book->epoch = epoch;
book->first = first;
book->limit = limit;
book->next = first;
book->allocated = 0;
book->committed = 0;
book->canceled = 0;
book->committed_sum = 0;
return 0;
}
int ticket_reserve(TicketBook *book, uint32_t owner, uint32_t count, TicketGrant *out)
{
if (book == NULL || out == NULL || !owner_valid(owner) || count == 0 || count > TICKET_MAX_COUNT) {
return -1;
}
if (!book->open) {
return -2;
}
if (find_owner(book, owner) >= 0) {
return -4;
}
int slot = unused_slot(book);
if (slot < 0) {
return -5;
}
uint64_t cursor = book->first + book->allocated;
if (cursor > book->limit || count > book->limit - cursor + 1) {
return -3;
}
TicketGrant value;
value.epoch = book->epoch;
value.owner = owner;
value.first = cursor;
value.last = cursor + count - 1;
value.count = count;
value.sum = interval_sum(cursor, count);
size_t used = occupied(book);
size_t position = 0;
while (position < used && book->entries[position].grant.owner < owner) {
++position;
}
memmove(&book->entries[position + 1], &book->entries[position],
(used - position) * sizeof(book->entries[0]));
book->entries[position].used = 1;
book->entries[position].grant = value;
book->next = cursor + count;
book->allocated += count;
*out = value;
return 0;
}
static int lookup(TicketBook *book, uint32_t owner, TicketGrant *out)
{
if (book == NULL || out == NULL || !owner_valid(owner)) {
return -1;
}
if (!book->open) {
return -2;
}
int slot = find_owner(book, owner);
if (slot < 0) {
return -6;
}
return slot;
}
int ticket_peek(TicketBook *book, uint32_t owner, TicketGrant *out)
{
int slot = lookup(book, owner, out);
if (slot < 0) {
return slot;
}
*out = book->entries[slot].grant;
return 0;
}
int ticket_commit(TicketBook *book, uint32_t owner, TicketGrant *out)
{
int slot = lookup(book, owner, out);
if (slot < 0) {
return slot;
}
TicketGrant value = book->entries[slot].grant;
book->committed += value.count;
book->committed_sum += value.sum;
size_t used = occupied(book);
memmove(&book->entries[slot], &book->entries[slot + 1],
(used - (size_t)slot - 1) * sizeof(book->entries[0]));
book->entries[used - 1].used = 0;
*out = value;
return 0;
}
int ticket_cancel(TicketBook *book, uint32_t owner, TicketGrant *out)
{
int slot = lookup(book, owner, out);
if (slot < 0) {
return slot;
}
TicketGrant value = book->entries[slot].grant;
book->canceled += value.count;
size_t used = occupied(book);
memmove(&book->entries[slot], &book->entries[slot + 1],
(used - (size_t)slot - 1) * sizeof(book->entries[0]));
book->entries[used - 1].used = 0;
*out = value;
return 0;
}
int ticket_stats(TicketBook *book, TicketStats *out)
{
if (book == NULL || out == NULL) {
return -1;
}
TicketStats value = {0};
if (book->open) {
value.open = 1;
value.epoch = book->epoch;
value.first = book->first;
value.limit = book->limit;
value.next = book->next;
value.allocated = book->allocated;
value.committed = book->committed;
value.canceled = book->canceled;
value.committed_sum = book->committed_sum;
value.pending_owners = occupied(book);
for (size_t i = 0; i < TICKET_SLOTS; ++i) {
if (book->entries[i].used) {
value.pending_units += book->entries[i].grant.count;
}
}
}
*out = value;
return 0;
}
int ticket_list(TicketBook *book, TicketGrant *out, size_t capacity, size_t *written)
{
if (book == NULL || written == NULL) {
return -1;
}
size_t count = book->open ? occupied(book) : 0;
if (capacity < count || (count != 0 && out == NULL)) {
return -1;
}
size_t n = count;
for (size_t i = 0; i < n; ++i) {
out[i] = book->entries[i].grant;
}
*written = n;
return 0;
}