According to http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.together "Memcheck intercepts calls to malloc, calloc, realloc, valloc, memalign, free, new, new[], delete and delete[]" It should be noted Valgrind has support for more calls than listed in documentation, namely: - posix_memalign() - pvalloc() - valloc() But it doesn't have explicit support for C11 aligned allocation function: - void *aligned_alloc (size_t alignment, size_t size); http://man7.org/linux/man-pages/man3/reallocarray.3.html And GNU reallocate array function: - void *reallocarray (void *ptr, size_t nmemb, size_t size); http://man7.org/linux/man-pages/man3/reallocarray.3.html
(In reply to Yann Droneaud from comment #0) > And GNU reallocate array function: > > - void *reallocarray (void *ptr, size_t nmemb, size_t size); > > http://man7.org/linux/man-pages/man3/reallocarray.3.html It's originally an OpenBSD extension, which is also available at least in FreeBSD and NetBSD OpenBSD: https://man.openbsd.org/reallocarray.3 FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=reallocarray NetBSD: http://netbsd.gw.com/cgi-bin/man-cgi?reallocarray
(In reply to Yann Droneaud from comment #0) > But it doesn't have explicit support for C11 aligned allocation function: > > - void *aligned_alloc (size_t alignment, size_t size); > Correct link: http://man7.org/linux/man-pages/man3/aligned_alloc.3.html
More about those functions in glibc documentation: http://www.gnu.org/software/libc/manual/html_node/Summary-of-Malloc.html
I'll take a look at this.
A more authoritative reference https://en.cppreference.com/w/c/memory/aligned_alloc
I'll look at reallocarray another day on FreeBSD (not a high priority for me). For aligned_alloc, I don't think that this will work well. The problem is that aligned_alloc and and memalign alias the same function on Linux GNU libc, i.e., paulf> nm /lib64/libc.so.6 | grep align | grep 0000000000081920 0000000000081920 W aligned_alloc 0000000000081920 t __GI___libc_memalign 0000000000081920 T __libc_memalign 0000000000081920 W memalign 0000000000081920 t __memalign Unless I'm much mistaken Valgrind can't redirect from one function address to two different functions. This limits the usefulness to either a libc with separate implementations of memalign/aligned_alloc or a replacement function. In practice this means that aligned_alloc uses will just show up as memaligns. I'll add a patch for this, but it will be of very limited use. I suggest simply updating the docs.
Created attachment 127060 [details] Patch for algned_alloc support
Finally, I did start writing some regression tests, for instance // example from here https://en.cppreference.com/w/c/memory/aligned_alloc #include <stdlib.h> #include <stdio.h> int main(void) { int *p1 = malloc(10*sizeof *p1); printf("default-aligned addr: %p\n", (void*)p1); free(p1); int *p2 = aligned_alloc(1024, 1024*sizeof *p2); printf("1024-byte aligned addr: %p\n", (void*)p2); if ((size_t)p2 % 1024U) printf("p2 not aligned!\n"); free(p2); }
Even more finally, this patch is worthwhile on FreeBSD.
Currently, in practice I don't see much benefit in adding reallocarray. The glibc implementation just uses realloc: https://code.woboq.org/userspace/glibc/malloc/reallocarray.c.html So does the FreeBSD implementation: https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/reallocarray.c The only difference would be callstacks that are a tiny bit cleaner.
The fix for aligned_alloc is likely to land along with support for FreeBSD. I will close this item then. If you feel that there is a strong need for reallocarray please open a new item and I'll take a look.
This should now be fixed along with the merges for FreeBSD support.