alloc_utils.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef PLANCK_ALLOC_UTILS_H
00033 #define PLANCK_ALLOC_UTILS_H
00034
00035 #include <cstdlib>
00036 #include "datatypes.h"
00037
00038 template <typename T> class normalAlloc__
00039 {
00040 public:
00041 T *alloc(tsize sz) const { return (sz>0) ? new T[sz] : 0; }
00042 void dealloc (T *ptr) const { delete[] ptr; }
00043 };
00044
00045 template <typename T, int align> class alignAlloc__
00046 {
00047 public:
00048 T *alloc(tsize sz) const
00049 {
00050 using namespace std;
00051 if (sz==0) return 0;
00052 planck_assert((align&(align-1))==0,"alignment must be power of 2");
00053 void *res;
00054
00055
00056 #ifdef __APPLE__
00057 planck_assert(align<=16, "bad alignment requested");
00058 res=malloc(sz*sizeof(T));
00059 planck_assert(res!=0,"error in malloc()");
00060 #else
00061 planck_assert(posix_memalign(&res,align,sz*sizeof(T))==0,
00062 "error in posix_memalign()");
00063 #endif
00064 return static_cast<T *>(res);
00065 }
00066 void dealloc(T *ptr) const
00067 {
00068 using namespace std;
00069 free(ptr);
00070 }
00071 };
00072
00073 #endif