SMIL  0.9.1
DBufferPool.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _DBUFFER_POOL_HPP
31 #define _DBUFFER_POOL_HPP
32 
33 #include <limits>
34 #include <stack>
35 
36 #include "Core/include/private/DMemory.hpp"
37 #include "Core/include/private/DTypes.hpp"
38 #include "Core/include/DErrors.h"
39 
40 namespace smil
41 {
42 
43  template <class T>
44  class BufferPool
45  {
46  public:
47  BufferPool(size_t bufSize=0)
48  : bufferSize(bufSize),
49  numberOfBuffers(0),
50  maxNumberOfBuffers(std::numeric_limits<size_t>::max())
51  {}
52  ~BufferPool()
53  {}
54 
55  typedef typename ImDtTypes<T>::lineType bufferType;
56 
57  RES_T initialize(size_t bufSize, SMIL_UNUSED size_t nbr=0)
58  {
59  if (buffers.size()!=0)
60  clear();
61  this->bufferSize = bufSize;
62  return RES_OK;
63  }
64  void clear()
65  {
66  #ifdef USE_OPEN_MP
67  #pragma omp critical
68  #endif // USE_OPEN_MP
69  {
70  while (!availableBuffers.empty())
71  {
72  ImDtTypes<T>::deleteLine(availableBuffers.front());
73  availableBuffers.pop();
74  }
75  }
76  }
77  void setMaxNumberOfBuffers(size_t nbr)
78  {
79  maxNumberOfBuffers = nbr;
80  }
81  size_t getMaxNumberOfBuffers()
82  {
83  return maxNumberOfBuffers;
84  }
85  bufferType getBuffer()
86  {
87  bufferType buf;
88 
89  #ifdef USE_OPEN_MP
90  #pragma omp critical
91  #endif // USE_OPEN_MP
92  {
93  if (availableBuffers.empty())
94  {
95  if (!createBuffer())
96  while (availableBuffers.empty()); // wait
97  }
98 
99  buf = availableBuffers.top();
100  availableBuffers.pop();
101 
102  }
103  return buf;
104  }
105  vector<bufferType> getBuffers(size_t nbr)
106  {
107  vector<bufferType> buffVect;
108 
109  for (int i=0;i<nbr;i++)
110  buffVect.push_back(this->getBuffer());
111 
112  return buffVect;
113  }
114 
115  void releaseBuffer(bufferType &buf)
116  {
117  availableBuffers.push(buf);
118  buf = NULL;
119  }
120  void releaseBuffers(vector<bufferType> &bufs)
121  {
122  for (int i=0;i<bufs.size();i++)
123  availableBuffers.push(bufs[i]);
124  bufs.clear();
125  }
126  void releaseAllBuffers()
127  {
128  while (!availableBuffers.empty())
129  availableBuffers.pop();
130  for (typename vector<bufferType>::iterator it=buffers.begin();it!=buffers.end();it++)
131  availableBuffers.push(*it);
132  }
133  protected:
134  bool createBuffer()
135  {
136  if (buffers.size()>=maxNumberOfBuffers)
137  return false;
138 
139  bufferType buf = ImDtTypes<T>::createLine(this->bufferSize);
140  buffers.push_back(buf);
141  availableBuffers.push(buf);
142 
143  return true;
144  }
145  stack<bufferType> availableBuffers;
146  vector<bufferType> buffers;
147  size_t bufferSize;
148  size_t numberOfBuffers;
149  size_t maxNumberOfBuffers;
150  };
151 
152 } // namespace smil
153 
154 #endif // _DBUFFER_POOL_HPP
155 
Definition: DColorConvert.h:38
Definition: DTypes.hpp:78
Definition: DBufferPool.hpp:44