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>
207 lines
5.1 KiB
C
207 lines
5.1 KiB
C
#include "journal.h"
|
|
|
|
#include <string.h>
|
|
|
|
static int room(const Journal *journal)
|
|
{
|
|
if (journal == NULL) {
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
return journal->count < JOURNAL_MAX_OPS ? LAYOUT_OK : LAYOUT_ERANGE;
|
|
}
|
|
|
|
static JournalOp *next_op(Journal *journal)
|
|
{
|
|
JournalOp *op = &journal->ops[journal->count];
|
|
memset(op, 0, sizeof(*op));
|
|
journal->count++;
|
|
return op;
|
|
}
|
|
|
|
void journal_reset(Journal *journal)
|
|
{
|
|
if (journal != NULL) {
|
|
journal->count = 0u;
|
|
}
|
|
}
|
|
|
|
size_t journal_count(const Journal *journal)
|
|
{
|
|
return journal == NULL ? 0u : journal->count;
|
|
}
|
|
|
|
int journal_put(Journal *journal, size_t index, const unsigned char *data, size_t length)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
if (length > LAYOUT_MAX_ROW) {
|
|
return LAYOUT_ERANGE;
|
|
}
|
|
if (length && data == NULL) {
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = JOURNAL_PUT;
|
|
op->first = index;
|
|
op->length = length;
|
|
if (length) {
|
|
memcpy(op->data, data, length);
|
|
}
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
int journal_fill(Journal *journal, size_t index, unsigned char value, size_t length)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
if (length > LAYOUT_MAX_ROW) {
|
|
return LAYOUT_ERANGE;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = JOURNAL_FILL;
|
|
op->first = index;
|
|
op->value = value;
|
|
op->length = length;
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
int journal_copy(Journal *journal, size_t source, size_t destination)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = JOURNAL_COPY;
|
|
op->first = source;
|
|
op->second = destination;
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
int journal_swap(Journal *journal, size_t left, size_t right)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = JOURNAL_SWAP;
|
|
op->first = left;
|
|
op->second = right;
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
int journal_clear(Journal *journal, size_t index)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = JOURNAL_CLEAR;
|
|
op->first = index;
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
static int enqueue_edit(Journal *journal, JournalKind kind, size_t index,
|
|
size_t offset, const unsigned char *data, size_t length)
|
|
{
|
|
JournalOp *op;
|
|
int status = room(journal);
|
|
if (status != LAYOUT_OK) {
|
|
return status;
|
|
}
|
|
if (length > LAYOUT_MAX_ROW) {
|
|
return LAYOUT_ERANGE;
|
|
}
|
|
if (kind != JOURNAL_ERASE && length && data == NULL) {
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
op = next_op(journal);
|
|
op->kind = kind;
|
|
op->first = index;
|
|
op->second = offset;
|
|
op->length = length;
|
|
if (kind != JOURNAL_ERASE && length) {
|
|
memcpy(op->data, data, length);
|
|
}
|
|
return LAYOUT_OK;
|
|
}
|
|
|
|
int journal_patch(Journal *journal, size_t index, size_t offset,
|
|
const unsigned char *data, size_t length)
|
|
{
|
|
return enqueue_edit(journal, JOURNAL_PATCH, index, offset, data, length);
|
|
}
|
|
|
|
int journal_insert(Journal *journal, size_t index, size_t offset,
|
|
const unsigned char *data, size_t length)
|
|
{
|
|
return enqueue_edit(journal, JOURNAL_INSERT, index, offset, data, length);
|
|
}
|
|
|
|
int journal_erase(Journal *journal, size_t index, size_t offset, size_t length)
|
|
{
|
|
return enqueue_edit(journal, JOURNAL_ERASE, index, offset, NULL, length);
|
|
}
|
|
|
|
static int apply(LayoutBank *bank, const JournalOp *op)
|
|
{
|
|
switch (op->kind) {
|
|
case JOURNAL_PUT: {
|
|
size_t effective = op->length;
|
|
return layout_store(bank, op->first, op->data, effective);
|
|
}
|
|
case JOURNAL_FILL:
|
|
return layout_fill(bank, op->first, op->value, op->length);
|
|
case JOURNAL_COPY:
|
|
return layout_copy(bank, op->first, op->second);
|
|
case JOURNAL_SWAP:
|
|
return layout_swap(bank, op->first, op->second);
|
|
case JOURNAL_CLEAR:
|
|
return layout_clear(bank, op->first);
|
|
case JOURNAL_PATCH:
|
|
return layout_patch(bank, op->first, op->second, op->data, op->length);
|
|
case JOURNAL_INSERT:
|
|
return layout_insert(bank, op->first, op->second, op->data, op->length);
|
|
case JOURNAL_ERASE:
|
|
return layout_erase(bank, op->first, op->second, op->length);
|
|
default:
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
}
|
|
|
|
int journal_commit(LayoutBank *bank, Journal *journal)
|
|
{
|
|
LayoutBank staged;
|
|
size_t index;
|
|
int status;
|
|
if (bank == NULL || journal == NULL) {
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
if ((bank->width != 64u && bank->width != 128u) ||
|
|
bank->count == 0u || bank->count > LAYOUT_MAX_SLOTS) {
|
|
return LAYOUT_EINVAL;
|
|
}
|
|
staged = *bank;
|
|
for (index = 0u; index < journal->count; index++) {
|
|
status = apply(&staged, &journal->ops[index]);
|
|
if (status != LAYOUT_OK) {
|
|
journal_reset(journal);
|
|
return status;
|
|
}
|
|
}
|
|
*bank = staged;
|
|
journal_reset(journal);
|
|
return LAYOUT_OK;
|
|
}
|