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

118 lines
3.0 KiB
C

#include "ledger.h"
#include <stdlib.h>
#include <string.h>
static size_t locate(allocation_ledger *ledger, void *address)
{
for (size_t i = 0; i < 128; ++i) {
if (ledger->blocks[i].address == address)
return i;
}
return 128;
}
static void remove_block(allocation_ledger *ledger, size_t index)
{
ledger->bytes -= ledger->blocks[index].bytes;
ledger->live--;
ledger->blocks[index].address = NULL;
ledger->blocks[index].bytes = 0;
}
static void *ledger_resize(void *address, size_t old_size, size_t new_size, void *context)
{
allocation_ledger *ledger = context;
size_t index;
size_t vacant;
void *replacement;
ledger->resize_calls++;
if (new_size == 0 || new_size > 65536) {
ledger->errors++;
return NULL;
}
index = address == NULL ? 128 : locate(ledger, address);
if ((address == NULL && old_size != 0) ||
(address != NULL && (index == 128 || ledger->blocks[index].bytes != old_size))) {
ledger->errors++;
return NULL;
}
if (ledger->fail_next) {
ledger->fail_next = 0;
ledger->failures++;
return NULL;
}
vacant = index;
if (vacant == 128) {
for (size_t i = 0; i < 128; ++i) {
if (ledger->blocks[i].address == NULL) {
vacant = i;
break;
}
}
}
if (vacant == 128) {
ledger->errors++;
return NULL;
}
replacement = malloc(new_size);
if (replacement == NULL) {
ledger->failures++;
return NULL;
}
memset(replacement, 0xD3, new_size);
if (address != NULL) {
size_t preserved = old_size < new_size ? old_size : new_size;
memcpy(replacement, address, preserved);
free(address);
remove_block(ledger, index);
}
ledger->blocks[vacant].address = replacement;
ledger->blocks[vacant].bytes = new_size;
ledger->live++;
ledger->bytes += new_size;
return replacement;
}
static void ledger_release(void *address, size_t bytes, void *context)
{
allocation_ledger *ledger = context;
size_t index;
ledger->release_calls++;
index = address == NULL ? 128 : locate(ledger, address);
if (index == 128 || ledger->blocks[index].bytes != bytes) {
ledger->errors++;
return;
}
free(address);
remove_block(ledger, index);
}
void ledger_init(allocation_ledger *ledger)
{
memset(ledger, 0, sizeof *ledger);
}
fb_allocator ledger_allocator(allocation_ledger *ledger)
{
fb_allocator allocator;
allocator.resize = ledger_resize;
allocator.release = ledger_release;
allocator.context = ledger;
return allocator;
}
void ledger_arm(allocation_ledger *ledger)
{
ledger->fail_next = 1;
}
void ledger_dispose(allocation_ledger *ledger)
{
for (size_t i = 0; i < 128; ++i) {
if (ledger->blocks[i].address != NULL) {
free(ledger->blocks[i].address);
ledger->blocks[i].address = NULL;
}
}
}