1273 lines
24 KiB
JSON

{
"kind": "ring",
"seed": 20261949,
"events": [
[
"Z"
],
[
"A",
1,
219
],
[
"Q",
1
],
[
"F",
1
],
[
"A",
2,
133
],
[
"P",
0
],
[
"R",
0
],
[
"C",
0
],
[
"Z"
],
[
"A",
1,
101
],
[
"Q",
1
],
[
"A",
2,
202
],
[
"Q",
2
],
[
"A",
3,
303
],
[
"Q",
3
],
[
"A",
4,
404
],
[
"Q",
4
],
[
"P",
0
],
[
"P",
1
],
[
"Q",
1
],
[
"Q",
2
],
[
"K",
4
],
[
"P",
2
],
[
"P",
3
],
[
"P",
4
],
[
"Z"
],
[
"A",
1,
65535
],
[
"Q",
1
],
[
"Q",
1
],
[
"Q",
1
],
[
"Q",
1
],
[
"Q",
1
],
[
"F",
1
],
[
"K",
1
],
[
"P",
7
],
[
"R",
7
],
[
"C",
7
],
[
"K",
1
],
[
"K",
1
],
[
"K",
1
],
[
"Z"
],
[
"C",
9
],
[
"K",
6
],
[
"P",
7
],
[
"R",
8
],
[
"Q",
1
],
[
"F",
8
],
[
"K",
1
],
[
"Q",
2
],
[
"R",
0
],
[
"A",
8,
22677
],
[
"F",
2
],
[
"K",
10
],
[
"K",
9
],
[
"P",
1
],
[
"A",
8,
10503
],
[
"P",
9
],
[
"Q",
2
],
[
"C",
0
],
[
"R",
6
],
[
"P",
7
],
[
"F",
8
],
[
"A",
4,
25386
],
[
"Q",
4
],
[
"R",
4
],
[
"P",
0
],
[
"Q",
6
],
[
"P",
4
],
[
"A",
1,
30366
],
[
"C",
9
],
[
"K",
9
],
[
"A",
6,
11522
],
[
"K",
6
],
[
"P",
0
],
[
"Q",
4
],
[
"R",
9
],
[
"A",
2,
11828
],
[
"A",
8,
58271
],
[
"Q",
5
],
[
"P",
0
],
[
"P",
4
],
[
"A",
1,
33276
],
[
"A",
7,
8060
],
[
"F",
7
],
[
"Q",
7
],
[
"R",
1
],
[
"Z"
],
[
"C",
5
],
[
"R",
0
],
[
"A",
1,
15843
],
[
"P",
9
],
[
"F",
9
],
[
"R",
4
],
[
"Q",
10
],
[
"R",
3
],
[
"F",
5
],
[
"K",
7
],
[
"Q",
8
],
[
"A",
2,
61065
],
[
"Q",
1
],
[
"Q",
6
],
[
"R",
2
],
[
"K",
4
],
[
"P",
1
],
[
"P",
2
],
[
"Q",
1
],
[
"A",
3,
24490
],
[
"Q",
5
],
[
"A",
9,
13095
],
[
"K",
3
],
[
"F",
5
],
[
"A",
1,
30323
],
[
"A",
10,
26534
],
[
"C",
4
],
[
"Q",
4
],
[
"Q",
10
],
[
"P",
8
],
[
"F",
3
],
[
"C",
5
],
[
"F",
4
],
[
"Q",
5
],
[
"F",
8
],
[
"K",
6
],
[
"A",
7,
6833
],
[
"Q",
1
],
[
"C",
4
],
[
"Q",
7
],
[
"A",
5,
13077
],
[
"F",
6
],
[
"Q",
6
],
[
"A",
5,
37687
],
[
"Q",
4
],
[
"Z"
],
[
"Q",
7
],
[
"A",
7,
9657
],
[
"A",
2,
47774
],
[
"R",
7
],
[
"A",
7,
57326
],
[
"A",
3,
5182
],
[
"A",
10,
27935
],
[
"F",
8
],
[
"K",
2
],
[
"Q",
4
],
[
"A",
10,
42213
],
[
"P",
2
],
[
"K",
3
],
[
"Q",
4
],
[
"A",
7,
17648
],
[
"A",
2,
24946
],
[
"Q",
6
],
[
"A",
7,
32606
],
[
"F",
4
],
[
"P",
1
],
[
"F",
2
],
[
"F",
3
],
[
"P",
8
],
[
"Q",
10
],
[
"P",
1
],
[
"C",
9
],
[
"K",
9
],
[
"P",
5
],
[
"Q",
6
],
[
"A",
4,
23297
],
[
"A",
5,
19661
],
[
"Q",
1
],
[
"Q",
5
],
[
"A",
10,
6034
],
[
"F",
6
],
[
"A",
4,
7908
],
[
"Q",
4
],
[
"R",
4
],
[
"Q",
7
],
[
"A",
7,
7261
],
[
"Q",
9
],
[
"K",
8
],
[
"C",
8
],
[
"K",
7
],
[
"K",
6
],
[
"Z"
],
[
"R",
8
],
[
"C",
7
],
[
"A",
2,
1995
],
[
"Q",
7
],
[
"R",
8
],
[
"P",
7
],
[
"A",
2,
12433
],
[
"K",
5
],
[
"P",
1
],
[
"A",
1,
22899
],
[
"C",
8
],
[
"P",
5
],
[
"K",
1
],
[
"K",
9
],
[
"Q",
9
],
[
"C",
5
],
[
"F",
7
],
[
"P",
9
],
[
"P",
7
],
[
"R",
8
],
[
"K",
7
],
[
"P",
4
],
[
"P",
5
],
[
"Q",
6
],
[
"A",
3,
55060
],
[
"Q",
10
],
[
"Q",
10
],
[
"Q",
10
],
[
"A",
10,
33515
],
[
"K",
3
],
[
"P",
4
],
[
"Q",
5
],
[
"C",
1
],
[
"F",
9
],
[
"P",
5
],
[
"C",
1
],
[
"Q",
10
],
[
"R",
0
],
[
"Q",
10
],
[
"P",
3
],
[
"P",
3
],
[
"C",
0
],
[
"P",
4
],
[
"Q",
5
],
[
"P",
0
],
[
"Z"
],
[
"A",
5,
50740
],
[
"A",
1,
50862
],
[
"A",
5,
7226
],
[
"Q",
2
],
[
"A",
4,
64980
],
[
"R",
0
],
[
"P",
8
],
[
"K",
8
],
[
"A",
8,
11149
],
[
"P",
1
],
[
"P",
1
],
[
"Q",
9
],
[
"R",
6
],
[
"K",
2
],
[
"A",
1,
45414
],
[
"A",
1,
63531
],
[
"C",
6
],
[
"R",
5
],
[
"A",
8,
55182
],
[
"K",
3
],
[
"A",
6,
9437
],
[
"Q",
3
],
[
"P",
9
],
[
"K",
1
],
[
"P",
6
],
[
"P",
8
],
[
"P",
6
],
[
"R",
7
],
[
"P",
0
],
[
"C",
0
],
[
"Q",
4
],
[
"A",
4,
32384
],
[
"A",
7,
65060
],
[
"R",
0
],
[
"C",
1
],
[
"C",
4
],
[
"Q",
8
],
[
"K",
3
],
[
"R",
3
],
[
"A",
7,
27624
],
[
"A",
6,
12019
],
[
"A",
5,
4577
],
[
"A",
1,
48149
],
[
"P",
3
],
[
"Q",
5
],
[
"Z"
],
[
"Q",
5
],
[
"Q",
10
],
[
"P",
8
],
[
"F",
2
],
[
"Q",
9
],
[
"F",
9
],
[
"K",
2
],
[
"P",
5
],
[
"A",
1,
21724
],
[
"A",
8,
49328
],
[
"F",
8
],
[
"C",
5
],
[
"P",
9
],
[
"P",
7
],
[
"R",
7
],
[
"A",
6,
6904
],
[
"R",
3
],
[
"K",
6
],
[
"Q",
5
],
[
"A",
9,
62752
],
[
"P",
9
],
[
"A",
7,
53293
],
[
"K",
8
],
[
"A",
4,
49811
],
[
"R",
4
],
[
"Z"
],
[
"A",
0,
0
],
[
"A",
1,
65536
]
],
"readonly": {
"command.h": "#ifndef COMMAND_H\n#define COMMAND_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define COMMAND_ARGUMENTS 5u\n#define INPUT_CAPACITY 256u\ntypedef struct {\n char code;\n unsigned count;\n uint32_t argument[COMMAND_ARGUMENTS];\n unsigned line;\n} command_t;\ntypedef struct {\n FILE *stream;\n unsigned line;\n unsigned commands;\n unsigned rejected;\n unsigned exhausted;\n} input_t;\ntypedef struct {\n int status;\n unsigned value;\n unsigned detail;\n} result_t;\ntypedef struct {\n FILE *stream;\n unsigned operation;\n unsigned fields;\n unsigned failures;\n unsigned used;\n char buffer[512];\n} observation_t;\nvoid input_init(input_t *input, FILE *stream);\nint input_next(input_t *input, command_t *command);\nint command_arity(char code);\nint parse_uint32(const char *start, const char **end, uint32_t *value);\nunsigned byte_pattern(unsigned seed, unsigned index);\nunsigned checksum_bytes(const unsigned char *data, unsigned length);\nint range_valid(unsigned offset, unsigned length, unsigned capacity);\nvoid observation_init(observation_t *observation, FILE *stream);\nvoid observation_begin(observation_t *observation, unsigned operation,\n const result_t *result);\nvoid observation_unsigned(observation_t *observation, unsigned value);\nvoid observation_bytes(observation_t *observation, const unsigned char *bytes,\n unsigned length);\nvoid observation_array(observation_t *observation, const unsigned *values,\n unsigned length);\nvoid observation_end(observation_t *observation);\nint observation_status(const observation_t *observation);\n#endif\n",
"input.c": "#include \"command.h\"\n#include <ctype.h>\n#include <limits.h>\n#include <string.h>\n\nvoid input_init(input_t *input, FILE *stream)\n{\n input->stream = stream;\n input->line = 0u;\n input->commands = 0u;\n input->rejected = 0u;\n input->exhausted = 0u;\n}\n\nint parse_uint32(const char *start, const char **end, uint32_t *value)\n{\n uint32_t accumulated = 0u;\n unsigned digits = 0u;\n const char *cursor = start;\n while (*cursor >= '0' && *cursor <= '9') {\n unsigned digit = (unsigned)(*cursor - '0');\n if (accumulated > (UINT32_MAX - digit) / 10u) {\n return 0;\n }\n accumulated = accumulated * 10u + digit;\n cursor++;\n digits++;\n }\n if (digits == 0u) {\n return 0;\n }\n *value = accumulated;\n *end = cursor;\n return 1;\n}\n\nstatic const char *skip_space(const char *cursor)\n{\n while (*cursor != '\\0' && isspace((unsigned char)*cursor)) {\n cursor++;\n }\n return cursor;\n}\n\nstatic int read_line(input_t *input, char *buffer, unsigned capacity)\n{\n unsigned length = 0u;\n unsigned overflow = 0u;\n unsigned binary = 0u;\n unsigned saw_byte = 0u;\n int byte;\n if (capacity < 2u) {\n return -1;\n }\n for (;;) {\n byte = fgetc(input->stream);\n if (byte == EOF) {\n if (ferror(input->stream)) {\n return -1;\n }\n if (!saw_byte) {\n return 0;\n }\n break;\n }\n saw_byte = 1u;\n if (byte == '\\n') {\n break;\n }\n if (byte == 0) {\n binary = 1u;\n continue;\n }\n if (length + 1u >= capacity) {\n overflow = 1u;\n continue;\n }\n buffer[length] = (char)byte;\n length++;\n }\n input->line++;\n if (length != 0u && buffer[length - 1u] == '\\r') {\n length--;\n }\n buffer[length] = '\\0';\n if (overflow || binary) {\n return -1;\n }\n return 1;\n}\n\nstatic int decode_line(input_t *input, const char *buffer, command_t *command)\n{\n const char *cursor = skip_space(buffer);\n int expected;\n unsigned index;\n if (*cursor == '\\0' || *cursor == '#') {\n return 0;\n }\n command->code = *cursor;\n command->count = 0u;\n command->line = input->line;\n cursor++;\n if (*cursor != '\\0' && !isspace((unsigned char)*cursor)) {\n return -1;\n }\n expected = command_arity(command->code);\n if (expected < 0 || (unsigned)expected > COMMAND_ARGUMENTS) {\n return -1;\n }\n for (index = 0u; index < COMMAND_ARGUMENTS; index++) {\n command->argument[index] = 0u;\n }\n for (index = 0u; index < (unsigned)expected; index++) {\n const char *end;\n uint32_t value;\n cursor = skip_space(cursor);\n if (!parse_uint32(cursor, &end, &value)) {\n return -1;\n }\n if (*end != '\\0' && *end != '#' && !isspace((unsigned char)*end)) {\n return -1;\n }\n command->argument[index] = value;\n command->count++;\n cursor = end;\n }\n cursor = skip_space(cursor);\n if (*cursor != '\\0' && *cursor != '#') {\n return -1;\n }\n return 1;\n}\n\nint input_next(input_t *input, command_t *command)\n{\n char buffer[INPUT_CAPACITY];\n if (input->exhausted) {\n return 0;\n }\n for (;;) {\n int decoded;\n int available = read_line(input, buffer, sizeof(buffer));\n if (available == 0) {\n input->exhausted = 1u;\n return 0;\n }\n if (available < 0) {\n input->rejected++;\n return -1;\n }\n decoded = decode_line(input, buffer, command);\n if (decoded < 0) {\n input->rejected++;\n return -1;\n }\n if (decoded > 0) {\n input->commands++;\n return 1;\n }\n }\n}\n\nunsigned byte_pattern(unsigned seed, unsigned index)\n{\n return (seed + index) & 255u;\n}\n\nunsigned checksum_bytes(const unsigned char *data, unsigned length)\n{\n unsigned index;\n unsigned result = 0u;\n for (index = 0u; index < length; index++) {\n result = (result + (index + 1u) * data[index]) % 65521u;\n }\n return result;\n}\n\nint range_valid(unsigned offset, unsigned length, unsigned capacity)\n{\n if (offset > capacity) {\n return 0;\n }\n if (length > capacity - offset) {\n return 0;\n }\n return length != 0u;\n}\n",
"observation.c": "#include \"command.h\"\n#include <limits.h>\n\nstatic int observation_flush(observation_t *observation)\n{\n size_t offset = 0u;\n if (observation->failures != 0u) {\n observation->used = 0u;\n return 0;\n }\n while (offset < observation->used) {\n size_t remaining = (size_t)observation->used - offset;\n size_t written = fwrite(observation->buffer + offset, 1u,\n remaining, observation->stream);\n if (written == 0u) {\n observation->failures++;\n observation->used = 0u;\n return 0;\n }\n offset += written;\n }\n observation->used = 0u;\n return 1;\n}\n\nstatic int observation_character(observation_t *observation, char byte)\n{\n if (observation->failures != 0u) {\n return 0;\n }\n if (observation->used == sizeof(observation->buffer)) {\n if (!observation_flush(observation)) {\n return 0;\n }\n }\n observation->buffer[observation->used] = byte;\n observation->used++;\n return 1;\n}\n\nstatic int observation_number(observation_t *observation, unsigned value)\n{\n char reversed[sizeof(unsigned) * CHAR_BIT];\n unsigned count = 0u;\n do {\n unsigned digit = value % 10u;\n reversed[count] = (char)('0' + digit);\n count++;\n value /= 10u;\n } while (value != 0u);\n while (count > 0u) {\n count--;\n if (!observation_character(observation, reversed[count])) {\n return 0;\n }\n }\n return 1;\n}\n\nstatic void observation_signed(observation_t *observation, int value)\n{\n unsigned magnitude;\n if (value < 0) {\n observation_character(observation, '-');\n magnitude = 0u - (unsigned)value;\n } else {\n magnitude = (unsigned)value;\n }\n observation_number(observation, magnitude);\n}\n\nstatic void observation_separator(observation_t *observation)\n{\n observation_character(observation, ' ');\n}\n\nvoid observation_init(observation_t *observation, FILE *stream)\n{\n observation->stream = stream;\n observation->operation = 0u;\n observation->fields = 0u;\n observation->failures = 0u;\n observation->used = 0u;\n}\n\nvoid observation_begin(observation_t *observation, unsigned operation,\n const result_t *result)\n{\n observation->operation = operation;\n observation->fields = 0u;\n observation_character(observation, 'O');\n observation_separator(observation);\n observation_number(observation, operation);\n observation_separator(observation);\n observation_signed(observation, result->status);\n observation_separator(observation);\n observation_number(observation, result->value);\n observation_separator(observation);\n observation_number(observation, result->detail);\n}\n\nvoid observation_unsigned(observation_t *observation, unsigned value)\n{\n observation_separator(observation);\n observation_number(observation, value);\n observation->fields++;\n}\n\nvoid observation_bytes(observation_t *observation, const unsigned char *bytes,\n unsigned length)\n{\n unsigned index;\n for (index = 0u; index < length; index++) {\n observation_unsigned(observation, (unsigned)bytes[index]);\n }\n}\n\nvoid observation_array(observation_t *observation, const unsigned *values,\n unsigned length)\n{\n unsigned index;\n for (index = 0u; index < length; index++) {\n observation_unsigned(observation, values[index]);\n }\n}\n\nvoid observation_end(observation_t *observation)\n{\n observation_character(observation, '\\n');\n observation_flush(observation);\n}\n\nint observation_status(const observation_t *observation)\n{\n if (observation->failures != 0u) {\n return 1;\n }\n if (observation->used != 0u) {\n return 1;\n }\n return ferror(observation->stream) ? 1 : 0;\n}\n",
"harness.c": "#include \"model.h\"\n#include <stdio.h>\n\nstatic void empty_result(result_t *result)\n{\n result->status = 0;\n result->value = 0u;\n result->detail = 0u;\n}\n\nint main(void)\n{\n state_t state;\n input_t input;\n command_t command;\n observation_t observation;\n unsigned operation = 0u;\n int available;\n input_init(&input, stdin);\n observation_init(&observation, stdout);\n model_init(&state);\n for (;;) {\n result_t result;\n available = input_next(&input, &command);\n if (available == 0) {\n break;\n }\n if (available < 0) {\n fprintf(stderr, \"invalid command on line %u\\n\", input.line);\n return 2;\n }\n operation++;\n empty_result(&result);\n model_apply(&state, &command, &result);\n observation_begin(&observation, operation, &result);\n model_observe(&state, &observation);\n observation_end(&observation);\n }\n if (fflush(stdout) != 0) {\n return 3;\n }\n return observation_status(&observation) ? 3 : 0;\n}\n",
"model.h": "#ifndef MODEL_H\n#define MODEL_H\n#include \"command.h\"\n#define POOL_SLOTS 8u\n#define RING_CAPACITY 4u\n#define CONSUMERS 8u\ntypedef struct {\n unsigned id;\n unsigned value;\n unsigned used;\n unsigned owner;\n unsigned refs;\n} object_t;\ntypedef struct {\n object_t objects[POOL_SLOTS];\n unsigned ring[RING_CAPACITY];\n unsigned handles[CONSUMERS];\n unsigned head;\n unsigned count;\n unsigned allocated;\n unsigned retired;\n unsigned rejected;\n unsigned delivered;\n} state_t;\nint object_find(const state_t *state, unsigned id);\nint object_empty(const state_t *state);\nvoid object_drop(state_t *state, unsigned slot);\nvoid queue_append(state_t *state, unsigned slot);\nvoid queue_remove(state_t *state, unsigned position);\nvoid model_init(state_t *state);\nvoid model_apply(state_t *state, const command_t *command, result_t *result);\nvoid model_observe(const state_t *state, observation_t *observation);\n#endif\n",
"layout.c": "#include \"model.h\"\nint command_arity(char code)\n{\n switch (code) {\n case 'Z': return 0;\n case 'A': return 2;\n case 'Q': return 1;\n case 'F': return 1;\n case 'P': return 1;\n case 'R': return 1;\n case 'C': return 1;\n case 'K': return 1;\n default: return -1;\n }\n}\nvoid model_observe(const state_t *state, observation_t *observation)\n{\n unsigned index;\n observation_unsigned(observation, state->head);\n observation_unsigned(observation, state->count);\n observation_unsigned(observation, state->allocated);\n observation_unsigned(observation, state->retired);\n observation_unsigned(observation, state->rejected);\n observation_unsigned(observation, state->delivered);\n observation_array(observation, state->ring, RING_CAPACITY);\n observation_array(observation, state->handles, CONSUMERS);\n for (index = 0u; index < POOL_SLOTS; index++) {\n const object_t *object = &state->objects[index];\n observation_unsigned(observation, object->id);\n observation_unsigned(observation, object->value);\n observation_unsigned(observation, object->used);\n observation_unsigned(observation, object->owner);\n observation_unsigned(observation, object->refs);\n }\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror -pedantic\nSOURCES = harness.c input.c observation.c layout.c model.c policy.c\nmemory-demo: $(SOURCES) command.h model.h\n\t$(CC) $(CFLAGS) $(SOURCES) -o $@\n.PHONY: clean\nclean:\n\trm -f memory-demo\n"
},
"candidate_files": [
"model.c",
"policy.c"
]
}