Commit 70aadeca authored by jan.koester's avatar jan.koester
Browse files

malloc: 16-byte block header on 32-bit targets too (8-aligned pointers on ppc)

The header was 4+4+unsigned long: 12 bytes on ppc, so every block sat at
16n+12 and malloc()/calloc()/realloc() returned pointers only 4-aligned,
below ppc32's alignof(double) and max_align_t (8). GObject aborted in
g_object_new() (g_object_is_aligned) on the nas-itx build; anything
storing doubles or 64-bit atomics in heap memory was affected. A uint64_t
in the union pads it to 16 bytes everywhere (static_assert'ed).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent f285accf
Loading
Loading
Loading
Loading
+6 −0
Changes for src/linux/alloc.cpp: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -111,8 +111,14 @@ namespace sys {
                union {
                    unsigned long size;        /* valid while allocated: real usable byte size, for size_of() */
                    block_header* next_free;   /* valid while free (pooled blocks only): next node on this size class's free list */
                    /* keeps the header at 16 bytes on 32-bit targets too: with a
                     * 4-byte long it was 12, every block 16n+12 and malloc()'s
                     * pointers only 4-aligned -- below ppc's alignof(double) and
                     * max_align_t of 8 (GObject's g_object_is_aligned() aborted) */
                    uint64_t _pad;
                };
            };
            static_assert(sizeof(block_header) == 16, "malloc() alignment relies on a 16-byte block header");

            static constexpr unsigned NUM_CLASSES = 13;
            static constexpr unsigned long SLAB_SIZE = 256 * 1024;