Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:17

0001 #ifndef NPSTAT_ALLOCATORS_HH_
0002 #define NPSTAT_ALLOCATORS_HH_
0003 
0004 /*!
0005 // \file allocators.h
0006 //
0007 // \brief Utilities related to memory management
0008 //
0009 // Author: I. Volobouev
0010 //
0011 // October 2009
0012 */
0013 
0014 #include <cassert>
0015 
0016 namespace npstat {
0017   /**
0018     // Function for allocating memory buffers if their size
0019     // exceeds the size of the buffer available on the stack
0020     */
0021   template <typename T>
0022   inline T* makeBuffer(unsigned sizeNeeded, T* stackBuffer, unsigned sizeofStackBuffer) {
0023     if (sizeNeeded > sizeofStackBuffer || stackBuffer == nullptr)
0024       return new T[sizeNeeded];
0025     else
0026       return stackBuffer;
0027   }
0028 
0029   /** Function for freeing memory buffers allocated by "makeBuffer" */
0030   template <typename T>
0031   inline void destroyBuffer(T* thisBuffer, const T* stackBuffer) {
0032     if (thisBuffer != stackBuffer)
0033       delete[] thisBuffer;
0034   }
0035 
0036   /** Copy a buffer (with possible type conversion on the fly) */
0037   template <typename T1, typename T2>
0038   inline void copyBuffer(T1* dest, const T2* source, const unsigned long len) {
0039     if (len) {
0040       assert(dest);
0041       assert(source);
0042       for (unsigned long i = 0; i < len; ++i)
0043         *dest++ = static_cast<T1>(*source++);
0044     }
0045   }
0046 
0047   /**
0048     // Copy a buffer (with possible type conversion on the fly)
0049     // transposing it in the process (treating as a square matrix)
0050     */
0051   template <typename T1, typename T2>
0052   inline void transposeBuffer(T1* dest, const T2* source, const unsigned long dim) {
0053     if (dim) {
0054       assert(dest);
0055       assert(source);
0056       for (unsigned long i = 0; i < dim; ++i) {
0057         for (unsigned long j = 0; j < dim; ++j)
0058           dest[j * dim] = static_cast<T1>(*source++);
0059         ++dest;
0060       }
0061     }
0062   }
0063 
0064   /**
0065     // Copy a buffer (with possible type conversion on the fly)
0066     // transposing it in the process (treating as an M x N matrix)
0067     */
0068   template <typename T1, typename T2>
0069   inline void transposeBuffer(T1* dest, const T2* source, const unsigned long M, const unsigned long N) {
0070     if (M && N) {
0071       assert(dest);
0072       assert(source);
0073       for (unsigned long i = 0; i < M; ++i) {
0074         for (unsigned long j = 0; j < N; ++j)
0075           dest[j * M] = static_cast<T1>(*source++);
0076         ++dest;
0077       }
0078     }
0079   }
0080 
0081   /** 
0082     // Clear a buffer (set all elements to the value produced by the
0083     // default constructor)
0084     */
0085   template <typename T>
0086   inline void clearBuffer(T* buf, const unsigned long len) {
0087     if (len) {
0088       assert(buf);
0089       const T zero = T();
0090       for (unsigned long i = 0; i < len; ++i)
0091         *buf++ = zero;
0092     }
0093   }
0094 }  // namespace npstat
0095 
0096 #endif  // NPSTAT_ALLOCATORS_HH_