...
+ free(mem);
Shouldn't this be an munmap() ?
free() is being used to free memory allocated by memalign().
Oh, I missed that detail.
I'm afraid memalign+free that might not be what you want: there is no guarantee that what you are getting hasn't been used before and is not already filled with other pages?
I'm not sure I've been using memalign() to allocate aligned memory and the using madvise().
I'll switch up to mmap() for this test. I'll send v2.