CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/JetMETCorrections/InterpolationTables/interface/allocators.h

Go to the documentation of this file.
00001 #ifndef NPSTAT_ALLOCATORS_HH_
00002 #define NPSTAT_ALLOCATORS_HH_
00003 
00014 #include <cassert>
00015 
00016 namespace npstat {
00021     template <typename T>
00022     inline T* makeBuffer(unsigned sizeNeeded, T* stackBuffer,
00023                          unsigned sizeofStackBuffer)
00024     {
00025         if (sizeNeeded > sizeofStackBuffer || stackBuffer == 0)
00026             return new T[sizeNeeded];
00027         else
00028             return stackBuffer;
00029     }
00030 
00032     template <typename T>
00033     inline void destroyBuffer(T* thisBuffer, const T* stackBuffer)
00034     {
00035         if (thisBuffer != stackBuffer)
00036             delete [] thisBuffer;
00037     }
00038 
00040     template <typename T1, typename T2>
00041     inline void copyBuffer(T1* dest, const T2* source, const unsigned long len)
00042     {
00043         if (len)
00044         {
00045             assert(dest);
00046             assert(source);
00047             for (unsigned long i=0; i<len; ++i)
00048                 *dest++ = static_cast<T1>(*source++);
00049         }
00050     }
00051 
00056     template <typename T>
00057     inline void clearBuffer(T* buf, const unsigned long len)
00058     {
00059         if (len)
00060         {
00061             assert(buf);
00062             const T zero = T();
00063             for (unsigned long i=0; i<len; ++i)
00064                 *buf++ = zero;
00065         }
00066     }
00067 }
00068 
00069 #endif // NPSTAT_ALLOCATORS_HH_
00070