Is there a linux equivalent of _aligned_realloc?
I want to use realloc so I don't have to memcpy the data every time I resize it. Am I stuck with mmap? I only used mmap once is there a recommended way of implementing memory that will be resized a few times? I'm assuming I can't mix mmap with aligned_alloc and I would have to do memcpy for the first resize? (or always use mmap)
The realloc below doesn't always align. I tested under (64bit) linux using gcc and clang
#include<cstdlib>
#include<cstdio>
#define ALIGNSIZE 64
int main()
{
for(int i = 0; i<10; i++)
{
void *p = aligned_alloc(ALIGNSIZE, 4096);
void *p2 = realloc(p, 4096+2048); //This doesn't always align
//void *p3 = aligned_alloc(ALIGNSIZE, 4096/24); //Doesn't need this line to make it unaligned.
if (((long)p & (ALIGNSIZE-1)) != 0 || ((long)p2 & (ALIGNSIZE-1)) != 0)
printf("%d %d %d\n", i, ((long)p & (ALIGNSIZE-1)) != 0, ((long)p2 & (ALIGNSIZE-1)) != 0);
}
}
malloc
,realloc
etc. are guaranteed to return an address that is suitable as a pointer to any data type on the current platform. This means on a 64-bit system it is at least aligned to an address suitable for a 64-bit value, i.e. a multiple of 8 bytes. – Tankard