[LEDE-DEV] [PATCH] libubox: Fix calloc_a() to return mem aligned pointers

Felix Fietkau nbd at nbd.name
Fri Feb 24 00:39:56 PST 2017


On 2017-02-24 03:37, Ted Hess wrote:
> Yousong -
> 
> As a side note to your side note - If you examine the actual
> mechanics of the allocation, the memory block is indeed size aligned to
> (4*sizeof(size_t)), but the actual pointer returned is offset of
> (2*sizeof(size_t)) within the block. As in CHUNK_TO_MEM...
I think that for calloc_a, using 2*sizeof(size_t) is probably overkill;
it would be 64 bit on 32 bit architectures and 128 bit on 64 bit
architectures.

Using sizeof(size_t) should be enough:

On AArch64, misalignment could only happen if something uses 128-bit
types natively. I'm not aware of any code doing that.
On ARMv5 there's a set of 64-bit load/store ops which can't handle
unaligned access, but that has been causing so many issues (including in
kernel code) that I've patched the toolchain to change the minimum
architecture level of it to ARMv6 (where unaligned load/store is
explicitly allowed).
On MIPS and PPC I'm not aware of any similar issues with 64 bit load/store.
On x86 we wouldn't even need to do any alignment, it's handled
efficiently in the CPU.

- Felix



More information about the Lede-dev mailing list