npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
allocators.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ALLOCATORS_HH_
2 #define NPSTAT_ALLOCATORS_HH_
3 
4 /*!
5 // \file allocators.hh
6 //
7 // \brief Utilities related to memory management
8 //
9 // Author: I. Volobouev
10 //
11 // October 2009
12 */
13 
14 #include <cassert>
15 
16 namespace npstat {
17  /*
18  // Avoiding false positive g++11 free-nonheap-object diagnostic.
19  // Note that the corresponding pragma does not work if it is
20  // simply inserted around delete [] inside "destroyBuffer" function.
21  // Looks like g++ bug to me.
22  */
23  namespace Private {
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
26  template <typename T>
27  void reallyDestroyBuffer(T* thisBuffer) {delete [] thisBuffer;}
28 #pragma GCC diagnostic pop
29 
30  template <typename T>
31  void dontDestroyBuffer(T* /* thisBuffer */) {}
32  }
33 
34  /**
35  // Function for allocating memory buffers if their size
36  // exceeds the size of the buffer available on the stack
37  */
38  template <typename T>
39  inline T* makeBuffer(unsigned sizeNeeded, T* stackBuffer,
40  unsigned sizeofStackBuffer)
41  {
42  if (sizeNeeded > sizeofStackBuffer || stackBuffer == 0)
43  return new T[sizeNeeded];
44  else
45  return stackBuffer;
46  }
47 
48  /** Function for freeing memory buffers allocated by "makeBuffer" */
49  template <typename T>
50  inline void destroyBuffer(T* thisBuffer, const T* stackBuffer)
51  {
52  void (*fcn_ptr)(T*) = thisBuffer == stackBuffer ?
53  &Private::dontDestroyBuffer<T> : &Private::reallyDestroyBuffer<T>;
54  fcn_ptr(thisBuffer);
55  }
56 
57  /** Copy a buffer (with possible type conversion on the fly) */
58  template <typename T1, typename T2>
59  inline void copyBuffer(T1* dest, const T2* source, const unsigned long len)
60  {
61  if (len)
62  {
63  assert(dest);
64  assert(source);
65  for (unsigned long i=0; i<len; ++i)
66  *dest++ = static_cast<T1>(*source++);
67  }
68  }
69 
70  /**
71  // Copy a buffer (with possible type conversion on the fly)
72  // transposing it in the process (treating as a square matrix)
73  */
74  template <typename T1, typename T2>
75  inline void transposeBuffer(T1* dest, const T2* source,
76  const unsigned long dim)
77  {
78  if (dim)
79  {
80  assert(dest);
81  assert(source);
82  for (unsigned long i=0; i<dim; ++i)
83  {
84  for (unsigned long j=0; j<dim; ++j)
85  dest[j*dim] = static_cast<T1>(*source++);
86  ++dest;
87  }
88  }
89  }
90 
91  /**
92  // Copy a buffer (with possible type conversion on the fly)
93  // transposing it in the process (treating as an M x N matrix)
94  */
95  template <typename T1, typename T2>
96  inline void transposeBuffer(T1* dest, const T2* source,
97  const unsigned long M,
98  const unsigned long N)
99  {
100  if (M && N)
101  {
102  assert(dest);
103  assert(source);
104  for (unsigned long i=0; i<M; ++i)
105  {
106  for (unsigned long j=0; j<N; ++j)
107  dest[j*M] = static_cast<T1>(*source++);
108  ++dest;
109  }
110  }
111  }
112 
113  /**
114  // Clear a buffer (set all elements to the value produced by the
115  // default constructor)
116  */
117  template <typename T>
118  inline void clearBuffer(T* buf, const unsigned long len)
119  {
120  if (len)
121  {
122  assert(buf);
123  const T zero = T();
124  for (unsigned long i=0; i<len; ++i)
125  *buf++ = zero;
126  }
127  }
128 }
129 
130 #endif // NPSTAT_ALLOCATORS_HH_
Definition: AbsArrayProjector.hh:14
void clearBuffer(T *buf, const unsigned long len)
Definition: allocators.hh:118
void destroyBuffer(T *thisBuffer, const T *stackBuffer)
Definition: allocators.hh:50
void transposeBuffer(T1 *dest, const T2 *source, const unsigned long dim)
Definition: allocators.hh:75
void copyBuffer(T1 *dest, const T2 *source, const unsigned long len)
Definition: allocators.hh:59
T * makeBuffer(unsigned sizeNeeded, T *stackBuffer, unsigned sizeofStackBuffer)
Definition: allocators.hh:39