amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

100 lines
2.7 KiB
C

#include "model_internal.h"
#include <string.h>
int fm_process_check(const struct fm_model *model, int process)
{
if (process < 0 || process >= FM_PROCESS_CAP) {
return FM_RANGE;
}
if (!model->processes[process].alive) {
return FM_STATE;
}
return FM_OK;
}
int fm_stream_check(const struct fm_model *model, int process, int stream)
{
int result;
if (process < 0 || process >= FM_PROCESS_CAP ||
stream < 0 || stream >= FM_STREAM_CAP) {
return FM_RANGE;
}
result = fm_process_check(model, process);
if (result != FM_OK) {
return result;
}
if (model->processes[process].streams[stream].description < 0) {
return FM_CLOSED;
}
return FM_OK;
}
void fm_stream_clear(struct fm_stream *stream)
{
stream->description = -1;
stream->length = 0;
memset(stream->pending, 0, sizeof stream->pending);
}
void fm_process_clear(struct fm_process *process)
{
int index;
process->used = 0;
process->alive = 0;
process->code = 0;
for (index = 0; index < FM_STREAM_CAP; ++index) {
fm_stream_clear(&process->streams[index]);
}
}
int fm_store_append(struct fm_model *model, int description,
const unsigned char *bytes, size_t length)
{
struct fm_description *file;
size_t end;
if (description < 0 || description >= FM_DESCRIPTION_CAP) {
return FM_RANGE;
}
file = &model->descriptions[description];
if (length == 0) {
return FM_OK;
}
if (file->offset > model->capacity || length > model->capacity - file->offset) {
return FM_NOSPACE;
}
end = file->offset + length;
if (file->offset > file->length) {
memset(file->bytes + file->length, 0, file->offset - file->length);
}
memcpy(file->bytes + file->offset, bytes, length);
file->offset = end;
if (end > file->length) {
file->length = end;
}
file->commits += 1;
return FM_OK;
}
int fm_init(struct fm_model *model, size_t capacity)
{
int process;
int description;
if (capacity > FM_FILE_CAP) {
return FM_RANGE;
}
memset(model, 0, sizeof *model);
model->capacity = capacity;
for (process = 0; process < FM_PROCESS_CAP; ++process) {
fm_process_clear(&model->processes[process]);
}
for (description = 0; description < FM_DESCRIPTION_CAP; ++description) {
model->descriptions[description].offset = 0;
model->descriptions[description].length = 0;
model->descriptions[description].references = 0;
model->descriptions[description].commits = 0;
}
model->processes[0].used = 1;
model->processes[0].alive = 1;
return FM_OK;
}