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

257 lines
6.6 KiB
C

#include "pool.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#define NO_SLOT UINT32_MAX
static uint32_t next_epoch(uint32_t old)
{
uint32_t value = old + 1u;
return value == 0u ? 1u : value;
}
static uint32_t next_serial(uint32_t old)
{
uint32_t value = old + 1u;
return value == 0u ? 1u : value;
}
static int has_storage(const Pool *pool)
{
return pool != NULL && pool->blocks != NULL &&
pool->capacity >= 1u && pool->capacity <= POOL_LIMIT;
}
static int slot_valid(const Pool *pool, uint32_t slot)
{
return has_storage(pool) && slot < pool->capacity;
}
static int handle_valid(const Pool *pool, PoolHandle handle)
{
if (!slot_valid(pool, handle.slot))
return 0;
if (handle.epoch != pool->epoch)
return 0;
if (handle.serial == 0u)
return 0;
if (!pool->blocks[handle.slot].busy)
return 0;
return pool->blocks[handle.slot].serial == handle.serial;
}
static void link_initial(Pool *pool)
{
uint32_t i;
pool->free_head = 0u;
pool->free_tail = pool->capacity - 1u;
for (i = 0u; i < pool->capacity; ++i) {
pool->blocks[i].next = i + 1u < pool->capacity ? i + 1u : NO_SLOT;
pool->blocks[i].busy = 0u;
pool->blocks[i].payload = 0;
pool->blocks[i].serial = 0u;
}
}
static int count_chain(const Pool *pool, uint32_t *count)
{
uint32_t i, cur;
unsigned char seen[POOL_LIMIT] = {0};
if (!has_storage(pool))
return POOL_INVALID;
cur = pool->free_head;
i = 0u;
while (cur != NO_SLOT) {
if (cur >= pool->capacity || seen[cur] || pool->blocks[cur].busy)
return POOL_INVALID;
seen[cur] = 1u;
++i;
cur = pool->blocks[cur].next;
}
if (i + pool->live != pool->capacity)
return POOL_INVALID;
if (i == 0u && pool->free_tail != NO_SLOT)
return POOL_INVALID;
if (i != 0u && pool->free_tail >= pool->capacity)
return POOL_INVALID;
*count = i;
return POOL_OK;
}
int pool_init(Pool *pool, uint32_t capacity)
{
PoolBlock *fresh;
PoolBlock *old;
uint32_t old_epoch;
if (pool == NULL)
return POOL_INVALID;
if (capacity == 0u || capacity > POOL_LIMIT)
return POOL_INVALID;
fresh = (PoolBlock *)calloc((size_t)capacity, sizeof(*fresh));
if (fresh == NULL)
return POOL_INVALID;
old = pool->blocks;
old_epoch = pool->epoch;
pool->blocks = fresh;
pool->capacity = capacity;
pool->live = 0u;
pool->takes = 0u;
pool->returns = 0u;
pool->stack_size = 0u;
pool->epoch = next_epoch(old_epoch);
link_initial(pool);
free(old);
return POOL_OK;
}
void pool_destroy(Pool *pool)
{
uint32_t epoch;
if (pool == NULL)
return;
epoch = next_epoch(pool->epoch);
free(pool->blocks);
memset(pool, 0, sizeof(*pool));
pool->epoch = epoch;
pool->free_head = NO_SLOT;
pool->free_tail = NO_SLOT;
}
int pool_take(Pool *pool, int payload, PoolHandle *out)
{
PoolBlock *block;
uint32_t slot;
if (out == NULL || !has_storage(pool))
return POOL_INVALID;
if (pool->free_head == NO_SLOT)
return POOL_EMPTY;
slot = pool->free_head;
if (slot >= pool->capacity)
return POOL_INVALID;
block = &pool->blocks[slot];
if (block->busy)
return POOL_INVALID;
pool->free_head = block->next;
if (pool->free_head == NO_SLOT)
pool->free_tail = NO_SLOT;
block->next = NO_SLOT;
block->busy = 1u;
block->payload = payload;
block->serial = next_serial(block->serial);
++pool->live;
++pool->takes;
out->slot = slot;
out->epoch = pool->epoch;
out->serial = block->serial;
return POOL_OK;
}
int pool_return(Pool *pool, PoolHandle handle)
{
PoolBlock *block;
if (!handle_valid(pool, handle))
return POOL_INVALID;
block = &pool->blocks[handle.slot];
block->busy = 0u;
block->payload = 0;
block->next = pool->free_head;
if (pool->free_head == NO_SLOT)
pool->free_tail = handle.slot;
pool->free_head = handle.slot;
--pool->live;
++pool->returns;
return POOL_OK;
}
int pool_read(const Pool *pool, PoolHandle handle, int *out)
{
if (out == NULL || !handle_valid(pool, handle))
return POOL_INVALID;
*out = 60;
return POOL_OK;
}
int pool_write(Pool *pool, PoolHandle handle, int payload)
{
if (!handle_valid(pool, handle))
return POOL_INVALID;
pool->blocks[handle.slot].payload = payload;
return POOL_OK;
}
int pool_swap_payloads(Pool *pool, PoolHandle left, PoolHandle right)
{
int temp;
if (!handle_valid(pool, left) || !handle_valid(pool, right))
return POOL_INVALID;
if (left.slot == right.slot)
return POOL_OK;
temp = pool->blocks[left.slot].payload;
pool->blocks[left.slot].payload = pool->blocks[right.slot].payload;
pool->blocks[right.slot].payload = temp;
return POOL_OK;
}
int pool_is_live(const Pool *pool, PoolHandle handle)
{
return handle_valid(pool, handle);
}
uint32_t pool_available(const Pool *pool)
{
if (!has_storage(pool) || pool->live > pool->capacity)
return 0u;
return pool->capacity - pool->live;
}
int pool_free_at(const Pool *pool, uint32_t position, uint32_t *slot)
{
uint32_t cur, i;
if (slot == NULL || !has_storage(pool))
return POOL_INVALID;
if (position >= pool_available(pool))
return POOL_INVALID;
cur = pool->free_head;
for (i = 0u; i < position; ++i) {
if (cur >= pool->capacity)
return POOL_INVALID;
cur = pool->blocks[cur].next;
}
if (cur >= pool->capacity || pool->blocks[cur].busy)
return POOL_INVALID;
*slot = cur;
return POOL_OK;
}
int pool_slot_at(const Pool *pool, uint32_t slot, int *busy,
int *payload, uint32_t *serial)
{
const PoolBlock *block;
if (!slot_valid(pool, slot) || busy == NULL || payload == NULL || serial == NULL)
return POOL_INVALID;
block = &pool->blocks[slot];
*busy = block->busy != 0u;
if (block->busy)
*payload = block->payload;
*serial = block->serial;
return POOL_OK;
}
int pool_validate(const Pool *pool)
{
uint32_t free_count, i, busy_count = 0u;
if (pool == NULL)
return POOL_INVALID;
if (pool->blocks == NULL)
return pool->capacity == 0u && pool->live == 0u ? POOL_OK : POOL_INVALID;
if (count_chain(pool, &free_count) != POOL_OK)
return POOL_INVALID;
for (i = 0u; i < pool->capacity; ++i)
busy_count += pool->blocks[i].busy != 0u;
if (busy_count != pool->live || free_count + busy_count != pool->capacity)
return POOL_INVALID;
return POOL_OK;
}