25#ifndef _GEMA_MEMORY_H_
26#define _GEMA_MEMORY_H_
35#define GM_CACHE_LINE_SIZE 64
40#define GM_CACHE_ALIGNED __declspec(align(GM_CACHE_LINE_SIZE))
43#define GmAlignedMalloc(size, align) _aligned_malloc((size), (align))
46#define GmAlignedFree(ptr) _aligned_free(ptr)
50#define GM_CACHE_ALIGNED __attribute__((aligned(GM_CACHE_LINE_SIZE)))
52inline void* GmAlignedMalloc(
size_t size,
size_t align)
55 if(align <
sizeof(
void*))
56 align =
sizeof(
void*);
57 return posix_memalign(&ptr, align, size) ? NULL : ptr;
60#define GmAlignedFree(ptr) ::free(ptr)
67 void* p = GmAlignedMalloc(size, align);
71 throw std::bad_alloc();
77 return (((quintptr)ptr) & (align-1)) == 0;
92 if constexpr(QTypeInfo<T>::isComplex)
94 for(
int i = 0; i < n; i++, ptr++)
99 Q_UNUSED(ptr); Q_UNUSED(n);
111 if constexpr(QTypeInfo<T>::isComplex)
113 for(
int i = 0; i < n; i++, ptr++)
118 Q_UNUSED(ptr); Q_UNUSED(n);
133 if constexpr(QTypeInfo<T>::isComplex)
135 int objectOffset = ceil(
sizeof(T)/(
double)align) * align;
136 for(
int i = 0; i < n; i++)
139 ptr = (T*)(((
char*)ptr) + objectOffset);
144 Q_UNUSED(ptr); Q_UNUSED(align); Q_UNUSED(n);
158 if constexpr(QTypeInfo<T>::isComplex)
160 int objectOffset = ceil(
sizeof(T)/(
double)align) * align;
161 for(
int i = 0; i < n; i++)
164 ptr = (T*)(((
char*)ptr) + objectOffset);
169 Q_UNUSED(ptr); Q_UNUSED(align); Q_UNUSED(n);
183inline void*
GmPmemcpy(
void* dst,
const void* src,
size_t n,
int nt = 0,
184 size_t min = 10 * 1024 * 1024)
188 return memcpy(dst, src, n);
198 size_t ln = bn + n % nt;
200#pragma omp parallel for num_threads(nt)
201 for(
int i = 0; i < nt; i++)
202 memcpy(((
char*)dst) + i * bn, ((
const char*)src) + i * bn, i < nt - 1 ? bn : ln);
206 Q_UNUSED(nt); Q_UNUSED(min);
207 return memcpy(dst, src, n);
213#ifdef ARMA_USE_GEMA_MEMORY_POOL
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
#define GM_CACHE_LINE_SIZE
Cache line size. TODO: Get this from a configuration parameter.
Definition gmMemory.h:35
void GmDestroyAlignedObjectsInMemory(T *ptr, int align, int n)
Calls T destructor for 'n' aligned objects in the memory area pointed by ptr.
Definition gmMemory.h:154
bool GmMemoryIsAligned(void *ptr, int align)
Checks if a pointer is aligned to the given align (which must be a power of two)
Definition gmMemory.h:75
void GmConstructAlignedObjectsInMemory(T *ptr, int align, int n)
Calls the default T constructor for creating 'n' aligned objects in the memory area pointed by ptr.
Definition gmMemory.h:129
bool GmIsCacheAligned(void *ptr)
Checks if a pointer is cache aligned or not.
Definition gmMemory.h:81
void * GmAlignedMallocThrow(size_t size, size_t align)
A version of GmAlignedMalloc that throws a bad_alloc exception on failure.
Definition gmMemory.h:65
void * GmPmemcpy(void *dst, const void *src, size_t n, int nt=0, size_t min=10 *1024 *1024)
Parallel (thread enabled) version of memcpy using OpenMP.
Definition gmMemory.h:183
void GmConstructObjectsInMemory(T *ptr, int n=1)
Calls the default T constructor for creating 'n' consecutive objects in the memory area pointed by pt...
Definition gmMemory.h:90
void GmDestroyObjectsInMemory(T *ptr, int n=1)
Calls T destructor for 'n' consecutive objects in the memory area pointed by ptr.
Definition gmMemory.h:109
Declaration of helper functions for using OpenMP that translate into "empty" statements if compiled w...
int GmOmpAdjustNumThreads(int nt)
Adjusts the given number of threads. If nt <= 0 or if nt > maximum number of omp threads,...
Definition gmOmp.h:53