c++ - best cross-platform method to get aligned memory -
here code use aligned memory visual studio , gcc
inline void* aligned_malloc(size_t size, size_t align) { void *result; #ifdef _msc_ver result = _aligned_malloc(size, align); #else if(posix_memalign(&result, align, size)) result = 0; #endif return result; } inline void aligned_free(void *ptr) { #ifdef _msc_ver _aligned_free(ptr); #else free(ptr); #endif }
is code fine in general? have seen people use _mm_malloc
, _mm_free
. in cases want aligned memory it's use sse/avx. can use functions in general? make code lot simpler.
lastly, it's easy create own function align memory (see below). why there many different common functions aligned memory (many of work on 1 platform)?
this code 16 byte alignment.
float* array = (float*)malloc(size*sizeof(float)+15); // find aligned position // , use pointer read or write data array float* alignedarray = (float*)(((unsigned long)array + 15) & (~0x0f)); // dellocate memory original "array", not alignedarray free(array); array = alignedarray = 0;
see: http://www.songho.ca/misc/alignment/dataalign.html , how allocate aligned memory using standard library?
edit: in case cares, got idea aligned_malloc() function eigen (eigen/src/core/util/memory.h)
edit: discovered posix_memalign
undefined mingw. however, _mm_malloc
works visual studio 2012, gcc, mingw, , intel c++ compiler seems convenient solution in general. requires using own _mm_free
function, although on implementations can pass pointers _mm_malloc
standard free
/ delete
.
the first function propose indeed work fine.
your "homebrew" function works, has drawback if value aligned, have wasted 15 bytes. may not matter sometimes, os may able provide memory correctly allocated without waste (and if needs aligned 256 or 4096 bytes, risk wasting lot of memory adding "alignment-1" bytes).
Comments
Post a Comment