malloc_count icon indicating copy to clipboard operation
malloc_count copied to clipboard

Support for `aligned_alloc`

Open ByteHamster opened this issue 3 years ago • 1 comments

Without this change, calls to aligned_alloc are not counted. Also, passing a pointer obtained from aligned_alloc to free crashes the application (double free or corruption).

ByteHamster avatar Apr 04 '22 09:04 ByteHamster

I'd like to confirm that this PR works.

However, this warning should definitely be fixed:

malloc_count/malloc_count.c:368:38: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
  368 |     extra_alignment = *(size_t*)(ptr + sizeof(size_t)) = 0;

Some background as to why this is important at all. We were trying to use malloc_count in the scope of AVX-512 computations and found that this simple program crashes:

#include <immintrin.h>

int main(int argc, char** argv) {
    auto* array = new __m512i[100];
    delete[] array;
    return 0;
}

It crashes because for allocating the wide integer array, GCC uses aligned_alloc rather than malloc, but it uses free for the delete[] statement as usual.

pdinklag avatar May 10 '22 12:05 pdinklag