Q083FreeFirmware
Add decimal lists from a fixed pool
Question
Add two least-significant-digit-first decimal lists from a bounded SRAM node pool into a separate fixed destination pool without partial output. Use the supplied span helpers on a flat-address C platform where uintptr_t preserves byte addresses. Nonnull arguments identify live, correctly aligned objects of their declared types and capacities. UINT8_MAX marks an empty list; two empty lists sum to the single zero node. The complete destination span and both metadata objects must be mutually disjoint and disjoint from the active source pool. A zero-capacity destination may be null. Validate arguments and source lists before deciding whether capacity is sufficient. The source pool must be nonnull and src_count must be 1 through 32 even when both heads are UINT8_MAX.
Implementation scaffold
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct { uint8_t digit; uint8_t next; } digit_node_t;
typedef enum { ADD_OK, ADD_BAD_ARG, ADD_BAD_LIST, ADD_NO_SPACE } add_status_t;
static bool add_span(const void *pointer, size_t bytes,
uintptr_t *first, uintptr_t *last) {
const uintptr_t begin = (uintptr_t)pointer;
if (bytes > UINTPTR_MAX - begin) return false;
*first = begin;
*last = begin + bytes;
return true;
}
static bool add_overlap(uintptr_t a_first, uintptr_t a_last,
uintptr_t b_first, uintptr_t b_last) {
return a_first < b_last && b_first < a_last;
}
static bool validate_digit_list(const digit_node_t *src, size_t src_count,
uint8_t head, uint32_t *visited) {
// TODO: validate one list and return its visited-node mask.
}
add_status_t add_decimal_lists(const digit_node_t *src, size_t src_count,
uint8_t a_head, uint8_t b_head, digit_node_t *dst, size_t dst_cap,
uint8_t *dst_head, size_t *dst_used) {
// TODO: validate spans and both walks, stage the carry pass, then commit.
}
Trace one case
LSF decimal list A=[9,9] (99); B=[1]destination list=[0,0,1] (100)The carry propagates through both existing digits and allocates one final standard (canonical) digit after the sizing pass confirms capacity.
Requirements
- Reject bad pointers, overlap, bad digits or links, cycles, and any source node shared by both lists.
- Use no heap or recursion and at most two 32-bit visited masks for source validation.
- Run a sizing carry pass before writing and return no-space without changing the destination or output metadata.
- Write a canonical consecutive result, propagating final carry and representing zero with one zero node.
