[PATCH v5] malloc: Optimize small memory clearing for calloc

Florian Weimer fweimer@redhat.com
Mon Dec 2 12:05:29 GMT 2024


* Adhemerval Zanella Netto:

>> +static __always_inline void *
>> +clear_memory (void *mem, unsigned long clearsize)
>> +{
>> +  /* Unroll clear memory size up to 9 * INTERNAL_SIZE_T bytes.  We know
>> +     that contents have an odd number of INTERNAL_SIZE_T-sized words;
>> +     minimally 3 words.  */
>> +  INTERNAL_SIZE_T *d = (INTERNAL_SIZE_T *) mem;
>
> I think this strictly UB and it might generate some issues on architecture 
> with strict alignment requirement (like sparc and some riscv chips). I
> think we will need to use either some struct helper with __attribute__((packed))
> or memcpy to avoid it.

I think everything is properly aligned?  The “overlapping” comment is a
bit misleading, it's about multiple stores to the same locations, not
partially overlapping stores.

Thanks,
Florian



More information about the Libc-alpha mailing list