Allocator alignment calculator
Where does the payload start—and where can the next block begin? Change three sizes to see every byte of a fixed-inline-header layout.
Free, public and calculated in your browser. No account or compiler needed.
Choose the sizes
Positive decimal integers only. Header and request: 1–1,000,000,000 bytes. Alignment: powers of two from 1–65,536. Zero-size allocation policy is outside this model.
H · actual sizeof(Header), in bytes
n · bytes requested by the caller
A · power of two, in bytes
Load an example
Block layout
Offsets from an aligned block base at 0. Ranges include the start and exclude the end.
- Actual header
- [0, 24) · 24 bytes
- Header footprint / payload offset
- 32 bytes
- Pre-payload padding
- [24, 32) · 8 bytes
- Requested payload
- [32, 45) · 13 bytes
- Tail padding
- [45, 48) · 3 bytes
- Total block span
- [0, 48) · 48 bytes
Payload offset 32 and block span 48 are both multiples of 16.
Keep this exact labelled layout in your notes or a C question. The PNG includes byte ranges, inputs and the example URL.
Keep it in your editor · No registration
Checked layout arithmetic in C11
A standalone, reusable integer example: round the header footprint, check its addition to the request, then round the total span. Each addition is checked against your platform’s SIZE_MAX before it happens.
This is not a working allocator or the guest lesson’s reservation implementation. It neither creates nor owns storage, forms pointers, or proves typed-access validity. The browser calculator keeps its bounded JavaScript model; the C file tests real size_t arithmetic in the sandbox or locally.
Run the completed example here
No compiler setup or account. This runs the exact downloadable C11 source with its 18 fixed checks, including H=24, n=13, A=16 and overflow cases. It does not use your current calculator inputs. No changes or lesson harness are added.
Runs use Compiler Explorer’s free GCC 13.2 sandbox, with a 30-second request limit. Availability can vary. Shared guest limits: at least 8 seconds between starts, 20 runs per network per UTC day, and shared capacity limits. This demonstration does not save or complete any lesson.
Match the calculator
Current inputs: checked_layout(24, 13, 16, &result)
Footprint 32 · payload end 45 · span 48
Pre-padding 8 · tail padding 3 bytes.
- Ordinary: H=24, n=13, A=16
Footprint 32 · end 45 · span 48. Padding: 8 + 3. - Exact rounding fit: H=24, n=16, A=16
Footprint 32 · end 48 · span 48. Padding: 8 + 0. - No padding: H=16, n=16, A=16
Footprint 16 · end 32 · span 32. Padding: 0 + 0.
These three cases are in the downloaded tests. “Exact fit” means no tail rounding is needed—not that the block fits an arena. No capacity is supplied.
Compile and run locally
Save the file, open a terminal in its folder, then use a C11 compiler such as GCC or Clang (cc below):
cc -std=c11 \
-Wall -Wextra -Werror \
-pedantic \
heapcraft-checked-layout.c \
-o checked-layout
./checked-layoutExpect 18/18 checks passed and exit status 0. Each failing check prints FAIL and the program exits with status 1. The suite includes invalid alignments and overflow at all three layout stages.
On Windows, use a C11-capable compiler toolchain and run the built executable as .\checked-layout.exe. This file has a test main; keep the helpers and struct, not that main, when reusing it in your project.
Contract and failure handling
checked_layout requires positive header and request sizes and a nonzero power-of-two alignment. The rounding and addition helpers also accept zero sizes. All return false for invalid inputs, null outputs or overflow, leaving the output unchanged. Only read the result after true; on failure, reject the size request rather than using stale offsets.
Assume an already aligned block base and a fixed inline header. Use the actual sizeof(Header), including struct padding; choose alignment appropriate for both header and payload on your implementation. Check remaining capacity separately before forming any pointers. Alignment alone does not settle storage, effective type, aliasing, lifetime or bounds.
Read the complete C11 source and 18 checks
/* 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 <stdbool.h>
#include <stddef.h>
#include <stdint.h> /* SIZE_MAX */
#include <stdio.h>
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;
}
Align the offset. Then align the span.
Let H be the actual header size (including compiler-inserted struct padding), n the requested bytes and A the alignment. Round H up to a multiple of A for the header footprint and payload offset. Add n, then round again for the total block span. At block start 0, the span is also the next aligned block boundary.
For a 24-byte header, 13-byte request and 16-byte alignment, the header occupies [0, 24), pre-payload padding occupies [24, 32), and the payload starts at offset 32. The payload is [32, 45); three tail-padding bytes bring the next boundary to 48. Padding is allocator overhead, not part of the caller’s requested bytes.
What this model assumes
One contiguous arena, an already aligned arena and block base, and a fixed-size inline header at each block start. The chosen alignment must also satisfy the header type’s alignment requirement. Offsets are measured in C bytes, not absolute addresses. Alignment 16 is an example, not a guarantee about your C implementation. The calculator supports power-of-two alignments only.
This is integer layout reasoning, not proof that arbitrary typed access is valid C or that an allocator is production-safe. Storage, effective type, aliasing, lifetime and bounds still matter. An aligned unsigned char array is not automatically storage for arbitrary typed objects. No arena capacity is supplied here: a next boundary is only a candidate, and one at the arena end cannot hold another header.
Supported arithmetic—not C SIZE_MAX simulation
Header and request sizes are each limited to 1,000,000,000, with alignment at most 65,536. Under these bounds all intermediate integers are below 2,000,131,072 and exactly representable in JavaScript. Rounding uses remainders and addition, not JavaScript’s 32-bit bitwise operators. This tool does not emulate size_t, SIZE_MAX, pointer arithmetic or overflow on your C platform. A C allocator must check additions for overflow and remaining arena capacity before forming pointers.
Put alignment under test
Implement aligned reservations in a 64-byte arena and run seven compiled C tests. The free guest exercise is related, but uses an 8-byte policy with no inline headers. It does not give away a full allocator solution.
Try the free lessonRead the full layout walkthrough
Follow the payload-offset and next-block invariant, plus the C storage and overflow caveats.
C allocator alignment: payload and next block →Questions or a calculation issue? heapcraft@brainer.one
