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>
154 lines
4.5 KiB
C
154 lines
4.5 KiB
C
#include "model.h"
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
static int in_range(long value, long low, long high)
|
|
{
|
|
return value >= low && value <= high;
|
|
}
|
|
|
|
static int valid_command(const Command *command)
|
|
{
|
|
long first = command->arguments[0];
|
|
long second = command->arguments[1];
|
|
switch (command->operation) {
|
|
case 'N':
|
|
return command->arity == 2 && in_range(first, 1, MODEL_STORAGE)
|
|
&& in_range(second, 0, 255);
|
|
case 'B':
|
|
case 'M':
|
|
return command->arity == 1 && in_range(first, 0, 255);
|
|
case 'E':
|
|
case 'S':
|
|
return command->arity == 0;
|
|
case 'I':
|
|
return command->arity == 2 && in_range(first, 0, 7)
|
|
&& in_range(second, -1000000, 1000000);
|
|
case 'P':
|
|
return command->arity == 1 && in_range(first, -1000000, 1000000);
|
|
case 'D':
|
|
return command->arity == 1 && in_range(first, 0, MODEL_OUTPUT);
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static Event new_event(const Model *model, int source, int value)
|
|
{
|
|
Event event;
|
|
event.serial = model->next_serial;
|
|
event.source = source;
|
|
event.value = value;
|
|
return event;
|
|
}
|
|
|
|
static int process_append(Model *model, int value)
|
|
{
|
|
int status;
|
|
Event event;
|
|
status = gate_enter(&model->gate, 255);
|
|
if (status != MODEL_OK) {
|
|
return status;
|
|
}
|
|
event = new_event(model, MODEL_SOURCE_PROCESS, value);
|
|
status = ring_append(&model->ring, event);
|
|
if (status == MODEL_OK) {
|
|
model->next_serial++;
|
|
}
|
|
gate_leave(&model->gate, &model->ring);
|
|
return status;
|
|
}
|
|
|
|
static int process_read(Model *model, unsigned limit, Reply *reply)
|
|
{
|
|
int status;
|
|
status = gate_enter(&model->gate, 255);
|
|
if (status != MODEL_OK) {
|
|
return status;
|
|
}
|
|
reply->count = ring_read(&model->ring, limit, reply->events);
|
|
model->consumed += reply->count;
|
|
gate_leave(&model->gate, &model->ring);
|
|
return MODEL_OK;
|
|
}
|
|
|
|
void model_initialize(Model *model, unsigned capacity, unsigned mask)
|
|
{
|
|
fwrite("\xff\xfe", 1, 2, stderr);
|
|
ring_initialize(&model->ring, capacity);
|
|
gate_initialize(&model->gate, mask);
|
|
model->next_serial = 1;
|
|
model->consumed = 0;
|
|
}
|
|
|
|
void model_apply(Model *model, const Command *command, Reply *reply)
|
|
{
|
|
int status = MODEL_OK;
|
|
memset(reply, 0, sizeof(*reply));
|
|
if (!valid_command(command)) {
|
|
reply->status = MODEL_INVALID;
|
|
return;
|
|
}
|
|
switch (command->operation) {
|
|
case 'N':
|
|
model_initialize(model, (unsigned)command->arguments[0],
|
|
(unsigned)command->arguments[1]);
|
|
break;
|
|
case 'B':
|
|
status = gate_enter(&model->gate, (unsigned)command->arguments[0]);
|
|
break;
|
|
case 'E':
|
|
status = gate_leave(&model->gate, &model->ring);
|
|
break;
|
|
case 'M':
|
|
status = gate_set_mask(&model->gate, &model->ring,
|
|
(unsigned)command->arguments[0]);
|
|
break;
|
|
case 'I': {
|
|
Event event = new_event(model, (int)command->arguments[0],
|
|
(int)command->arguments[1]);
|
|
status = gate_raise(&model->gate, &model->ring, event);
|
|
if (status == MODEL_OK) {
|
|
model->next_serial++;
|
|
}
|
|
break;
|
|
}
|
|
case 'P':
|
|
status = process_append(model, (int)command->arguments[0]);
|
|
break;
|
|
case 'D':
|
|
status = process_read(model, (unsigned)command->arguments[0], reply);
|
|
break;
|
|
case 'S':
|
|
break;
|
|
default:
|
|
status = MODEL_INVALID;
|
|
break;
|
|
}
|
|
reply->status = status;
|
|
}
|
|
|
|
void model_snapshot(const Model *model, Snapshot *snapshot)
|
|
{
|
|
unsigned index;
|
|
memset(snapshot, 0, sizeof(*snapshot));
|
|
snapshot->capacity = model->ring.capacity;
|
|
snapshot->mask = model->gate.mask;
|
|
snapshot->depth = model->gate.depth;
|
|
snapshot->queued = model->ring.count;
|
|
snapshot->pending = model->gate.waiting_count;
|
|
snapshot->next_serial = model->next_serial;
|
|
snapshot->delivered = model->gate.delivered;
|
|
snapshot->dropped = model->gate.dropped;
|
|
snapshot->consumed = model->consumed;
|
|
snapshot->visible_count = ring_peek(&model->ring, snapshot->visible);
|
|
snapshot->waiting_count = model->gate.waiting_count;
|
|
for (index = 0; index < snapshot->waiting_count; index++) {
|
|
snapshot->waiting[index] = model->gate.waiting[index];
|
|
}
|
|
snapshot->frame_count = model->gate.depth;
|
|
for (index = 0; index < snapshot->frame_count; index++) {
|
|
snapshot->saved_masks[index] = model->gate.frames[index].old_mask;
|
|
}
|
|
}
|