/** * \file mat_utils.txx * \author Dhairya Malhotra, dhairya.malhotra@gmail.com * \date 11-5-2013 * \brief This file contains implementation of mem_utils.hpp. */ #include #include #include #include #include #include namespace pvfmm{ namespace mem{ #ifdef __INTEL_OFFLOAD #pragma offload_attribute(push,target(mic)) #endif // For fundamental data types. template T* aligned_malloc_f(size_t size_, size_t alignment){ assert(alignment <= 0x8000); size_t size=size_*sizeof(T); uintptr_t r = (uintptr_t)malloc(size + --alignment + 2); assert(r!=0); uintptr_t o = (uintptr_t)(r + 2 + alignment) & ~(uintptr_t)alignment; if (!r) return NULL; ((uint16_t*)o)[-1] = (uint16_t)(o-r); return (T*)o; //return (T*)fftw_malloc(size); } template void aligned_free_f(T* p_){ void* p=(void*)p_; if (!p) return; free((void*)((uintptr_t)p-((uint16_t*)p)[-1])); //fftw_free(p); } template T* aligned_malloc(size_t size_, size_t alignment){ //void* p=aligned_malloc_f(size_,alignment); //return new(p) T[size_]; T* A=new T[size_]; assert(A!=NULL); return A; } template <> inline double* aligned_malloc(size_t size_, size_t alignment){ return aligned_malloc_f(size_,alignment); } template <> inline float* aligned_malloc(size_t size_, size_t alignment){ return aligned_malloc_f(size_,alignment); } template <> inline char* aligned_malloc(size_t size_, size_t alignment){ return aligned_malloc_f(size_,alignment); } template void aligned_free(T* p_){ delete[] p_; } template <> inline void aligned_free(double* p_){ aligned_free_f(p_); } template <> inline void aligned_free(float* p_){ aligned_free_f(p_); } template <> inline void aligned_free(char* p_){ aligned_free_f(p_); } inline void * memcopy ( void * destination, const void * source, size_t num ){ if(destination==source || num==0) return destination; return memcpy ( destination, source, num ); } #ifdef __INTEL_OFFLOAD #pragma offload_attribute(pop) #endif }//end namespace }//end namespace