/* Heapcraft: C11 checked integer layout, not an allocator. * https://heapcraft.brainer.one/tools/allocator-alignment * Offsets only: no pointers, arena, reservation or ownership. * Assume an aligned block base and a fixed inline header. * The alignment must suit the header and intended payload. * Arithmetic success does not prove capacity or typed access. * All functions return false on invalid input or overflow; * outputs are unchanged on failure. Never use a failed result. * Layout policy: positive header/request, power-of-two alignment. * Helpers accept zero sizes. No allocation policy is implied. * Compile and run (no extra libraries): * cc -std=c11 -Wall -Wextra -Werror -pedantic \ * heapcraft-checked-layout.c -o checked-layout * ./checked-layout */ #include #include #include /* SIZE_MAX */ #include struct layout { size_t footprint; size_t payload_end; size_t span; size_t pre_padding; size_t tail_padding; }; bool checked_add(size_t x, size_t y, size_t *out) { if (out == NULL || y > SIZE_MAX - x) return false; *out = x + y; return true; } bool checked_align_up(size_t x, size_t a, size_t *out) { size_t remainder, padding; if (out == NULL || a == 0 || (a & (a - 1)) != 0) return false; remainder = x % a; padding = remainder == 0 ? 0 : a - remainder; /* Add only the needed padding, not an unconditional a - 1. */ return checked_add(x, padding, out); } bool checked_layout(size_t header, size_t request, size_t a, struct layout *out) { struct layout next; if (out == NULL || header == 0 || request == 0) return false; if (!checked_align_up(header, a, &next.footprint) || !checked_add(next.footprint, request, &next.payload_end) || !checked_align_up(next.payload_end, a, &next.span)) return false; next.pre_padding = next.footprint - header; next.tail_padding = next.span - next.payload_end; *out = next; return true; } /* Tests stay enabled even if you compile with -DNDEBUG. */ static unsigned tests, failures; #define CHECK(label, condition) do { \ ++tests; \ if (!(condition)) { \ ++failures; \ printf("FAIL: %s\n", label); \ } else { \ printf("PASS: %s\n", label); \ } \ } while (0) static bool matches(const struct layout *r, size_t footprint, size_t end, size_t span, size_t pre, size_t tail) { return r->footprint == footprint && r->payload_end == end && r->span == span && r->pre_padding == pre && r->tail_padding == tail; } int main(void) { struct layout r = {7, 8, 9, 10, 11}; size_t value = 123; CHECK("ordinary: H=24 n=13 A=16", checked_layout(24, 13, 16, &r) && matches(&r, 32, 45, 48, 8, 3)); if (failures == 0) printf("H=24 n=13 A=16: footprint=%zu end=%zu span=%zu\n", r.footprint, r.payload_end, r.span); CHECK("exact rounding fit: H=24 n=16 A=16", checked_layout(24, 16, 16, &r) && matches(&r, 32, 48, 48, 8, 0)); CHECK("no padding: H=16 n=16 A=16", checked_layout(16, 16, 16, &r) && matches(&r, 16, 32, 32, 0, 0)); CHECK("alignment 1", checked_layout(3, 5, 1, &r) && matches(&r, 3, 8, 8, 0, 0)); CHECK("zero alignment rejected", !checked_align_up(24, 0, &value) && value == 123); CHECK("non-power-of-two rejected", !checked_align_up(24, 3, &value) && value == 123); CHECK("SIZE_MAX exact addition", checked_add(SIZE_MAX - 1, 1, &value) && value == SIZE_MAX); CHECK("SIZE_MAX exact alignment 1", checked_align_up(SIZE_MAX, 1, &value) && value == SIZE_MAX); CHECK("zero helper input", checked_align_up(0, 16, &value) && value == 0); value = 123; CHECK("SIZE_MAX addition overflow", !checked_add(SIZE_MAX, 1, &value) && value == 123); CHECK("SIZE_MAX rounding overflow", !checked_align_up(SIZE_MAX, 2, &value) && value == 123); CHECK("near SIZE_MAX already aligned", checked_align_up(SIZE_MAX - 1, 2, &value) && value == SIZE_MAX - 1); r = (struct layout){7, 8, 9, 10, 11}; CHECK("header rounding overflow leaves output unchanged", !checked_layout(SIZE_MAX, 1, 2, &r) && matches(&r, 7, 8, 9, 10, 11)); CHECK("payload addition overflow leaves output unchanged", !checked_layout(1, SIZE_MAX, 1, &r) && matches(&r, 7, 8, 9, 10, 11)); CHECK("span rounding overflow leaves output unchanged", !checked_layout(2, SIZE_MAX - 2, 2, &r) && matches(&r, 7, 8, 9, 10, 11)); CHECK("invalid layout alignment leaves output unchanged", !checked_layout(24, 13, 3, &r) && matches(&r, 7, 8, 9, 10, 11)); CHECK("zero header/request rejected", !checked_layout(0, 13, 16, &r) && !checked_layout(24, 0, 16, &r) && matches(&r, 7, 8, 9, 10, 11)); CHECK("NULL outputs rejected", !checked_add(1, 2, NULL) && !checked_align_up(1, 2, NULL) && !checked_layout(24, 13, 16, NULL)); printf("%u/%u checks passed\n", tests - failures, tests); return failures == 0 ? 0 : 1; }