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

Yousong Zhou yszhou4tech at gmail.com
Thu Feb 23 17:07:31 PST 2017


On 24 February 2017 at 08:30, Ted Hess <thess at kitschensync.net> wrote:
>
>
> -----Original Message----- From: Yousong Zhou
> Sent: Thursday, February 23, 2017 7:15 PM
> To: Ted Hess
> Cc: lede-dev
> Subject: Re: [LEDE-DEV] [PATCH] libubox: Fix calloc_a() to return mem
> aligned pointers
>
>
> On 24 February 2017 at 05:20, Ted Hess <thess at kitschensync.net> wrote:
>>
>> The current implementation of calloc_a() returns packed pointers for the
>> extra
>> arguments. These packed, unaligned, pointers are OK for a lot of
>> architectures,
>> but not all. This patch will aligned the pointers returned in a manner
>> congruent
>> with malloc(). I do not believe the extra padding overhead is all the
>> burdensome
>> considering the overhead of separate malloc/calloc/free call to accomplish
>> the
>> same thing.
>>
>>
>> Signed-off-by: Ted Hess <thess at kitschensync.net>
>> ---
>>  utils.c | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/utils.c b/utils.c
>> index 5d9d5aa..314f716 100644
>> --- a/utils.c
>> +++ b/utils.c
>> @@ -27,6 +27,9 @@
>>                 _addr; \
>>                 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg,
>> size_t) : 0)
>>
>> +#define C_PTR_ALIGN    (2*sizeof(size_t))
>> +#define C_PTR_MASK     (-C_PTR_ALIGN)
>> +
>
>
> sizeof(long) should be used for C_PTR_ALIGN, though I cannot find the
> quote at the moment...
>
>                yousong
>
> I picked the expression from malloc in the musl sources. No hard
> preferences, but it does do proper alignment for 64-bit systems and other
> sensitive data-types AFAICT.
>
> /ted
>

Okay, according to the c99, size_t is supposed be able to express
sizeof(char[LONG_MAX]).  So I guess your code is safer than
sizeof(long) actually ;)

But as a side note, at the moment, musl malloc uses (4*sizeof(size_t))
as SIZE_ALIGN, uClibc uses (2 * (sizeof(size_t))) as MALLOC_ALIGNMENT

                yousong



More information about the Lede-dev mailing list