As long as you’re ok with having to call a special function to do the freeing, your approach is okay. I would do your #ifdef
s the other way around though: start with the standards-specified options and fall back to platform-specific ones. For example
- If
__STDC_VERSION__ >= 201112L
usealigned_alloc
. - If
_POSIX_VERSION >= 200112L
useposix_memalign
. - If
_MSC_VER
is defined, use the Windows stuff. - …
- If all else fails, just use
malloc
/free
and disable SSE/AVX code.
The problem is harder if you want to be able to pass the allocated pointer to free
; that’s valid on all the standard interfaces, but not on Windows and not necessarily with the legacy memalign
function some unix-like systems have.