YARP
Yet Another Robot Platform
FixedSizeBuffersManager-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/Log.h>
10 #include <yarp/os/LogStream.h>
11 
12 #include <stdexcept>
13 
14 
15 template <typename T>
17  key(0), dataPtr(nullptr), numOfElements(0)
18 {
19 }
20 
21 
22 template <typename T>
24 {
25 }
26 
27 
28 template <typename T>
30 {
31  return dataPtr;
32 }
33 
34 
35 template <typename T>
37 {
38  return numOfElements;
39 }
40 
41 
42 template <typename T>
44 {
45  if (index < numOfElements) {
46  return dataPtr[index];
47  } else {
48  throw std::out_of_range("yarp::dev::impl::Buffer::getValue(index): index is out of range");
49  }
50 }
51 
52 
53 template <typename T>
54 void yarp::dev::impl::Buffer<T>::setValue(uint32_t index, T value)
55 {
56  if (index < numOfElements) {
57  dataPtr[index] = value;
58  } else {
59  throw std::out_of_range("yarp::dev::impl::Buffer::setValue(index, value): index is out of range");
60  }
61 }
62 
63 
64 template <typename T>
66 {
67  return dataPtr[index];
68 }
69 
70 
71 template <typename T>
72 yarp::dev::impl::FixedSizeBuffersManager<T>::FixedSizeBuffersManager(uint32_t zizeOfBuffers, std::size_t initialNumOfBuffers)
73 {
74  m_numElem = zizeOfBuffers;
75  m_buffers.resize(0);
76 
77  for (size_t i = 0; i < initialNumOfBuffers; i++) {
78  T* buff = new T[m_numElem];
79  m_buffers.push_back(buff);
80  }
81  m_usedBuffers.resize(initialNumOfBuffers, false);
82  m_firstFreeBuff = 0;
83 }
84 
85 
86 template <typename T>
88 {
89  m_mutex.lock();
90  //get first free buffer
92  uint32_t i;
93  T* dataPtr;
94  bool needNewBuff = true;
95  if (false == m_usedBuffers[m_firstFreeBuff]) {
96  //you are lucky
97  i = m_firstFreeBuff;
98  needNewBuff = false;
99  } else {
100  for (std::size_t p = 0; p < m_buffers.size(); p++) {
101  if (false == m_usedBuffers[p]) {
102  i = p;
103  needNewBuff = false;
104  break;
105  }
106  }
107  }
108 
109  //if all buffers are used, I create new one and return it
110  if (needNewBuff)
111  {
112  dataPtr = new T[m_numElem];
113  if (nullptr == dataPtr) {
114  //I should not never be here because, if no more memory is available, I should be in "catch" branch
115  yError() << "FixedSizeBuffersManager::getBuffer() no more memory!!";
116  }
117  m_buffers.push_back(dataPtr);
118  m_usedBuffers.push_back(true);
119  // yError() << "I need to create a new buffer. Now size is " << m_buffers.size() << "pointer is " << dataPtr;
120  i = m_buffers.size() - 1;
121  }
122  else
123  {
124  //use the first free buffer
125  dataPtr = m_buffers[i];
126  m_usedBuffers[i] = true;
127  }
128  buffer.key = i;
129  buffer.dataPtr = dataPtr;
130  buffer.numOfElements = m_numElem;
131  //yInfo() << "getBuffer: key=" << buffer.key << " ptr=" << buffer.dataPtr;
132  m_mutex.unlock();
133  return buffer;
134 }
135 
136 
137 // template <typename T>
138 // void yarp::dev::impl::FixedSizeBuffersManager<T>::releaseBuffer(T* datapointer)
139 // {
140 // m_mutex.lock();
141 // std::size_t i;
142 // for(i=0; i< m_buffers.size(); i++) {
143 // if(m_buffers[i] == datapointer) {
144 // m_usedBuffers[i] = false;
145 // break;
146 // }
147 // }
148 // if(i>=m_buffers.size()) {
149 // yError() << "FixedSizeBuffersManager::releaseBuffer(T* datapointer) error in deallocation!!";
150 // }
151 // m_mutex.unlock();
152 // }
153 
154 
155 template <typename T>
157 {
158  m_mutex.lock();
159 
160  if (buffer.key >= m_buffers.size()) {
161  yError() << "FixedSizeBuffersManager::releaseBuffer((Buffer<T> &buffer) error in deallocation!!";
162  }
163 
164  m_usedBuffers[buffer.key] = false;
165  m_firstFreeBuff = buffer.key;
166  //yInfo() << "ReleaseBuffer: key=" << buffer.key << " ptr=" << buffer.dataPtr;
167  m_mutex.unlock();
168 }
169 
170 
171 template <typename T>
173 {
174  m_mutex.lock();
175  for (std::size_t i = 0; i < m_buffers.size(); i++) {
176  yDebug() << "buff[" << i << "]: addr = " << m_buffers[i] << "; it is used?" << m_usedBuffers[i];
177  }
178 
179  m_mutex.unlock();
180 }
181 
182 
183 template <typename T>
185 {
186  for (size_t i = 0; i < m_buffers.size(); i++) {
187  delete[] m_buffers[i];
188  }
189 }
190 
191 
192 template <typename T>
194 {
195  return m_numElem;
196 }
LogStream.h
yarp::dev::impl::FixedSizeBuffersManager::FixedSizeBuffersManager
FixedSizeBuffersManager(uint32_t sizeOfBuffers, std::size_t initialNumOfBuffers=3)
Constructor.
Definition: FixedSizeBuffersManager-inl.h:72
yarp::dev::impl::Buffer::~Buffer
~Buffer()
Destructor.Note that the memory will not deallocated.
Definition: FixedSizeBuffersManager-inl.h:23
yarp::dev::impl::Buffer::getData
T * getData()
Return the data pointer.
Definition: FixedSizeBuffersManager-inl.h:29
yarp::dev::impl::FixedSizeBuffersManager::getBuffer
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
Definition: FixedSizeBuffersManager-inl.h:87
yarp::dev::impl::Buffer::getValue
T getValue(uint32_t index)
Return the value contained in the buffer at index index.
Definition: FixedSizeBuffersManager-inl.h:43
yError
#define yError(...)
Definition: Log.h:282
yarp::dev::impl::Buffer::operator[]
T & operator[](uint32_t index)
Access specified element.
Definition: FixedSizeBuffersManager-inl.h:65
Log.h
yarp::dev::impl::Buffer::getSize
uint32_t getSize()
Return the number of element of buffer.
Definition: FixedSizeBuffersManager-inl.h:36
yarp::dev::impl::FixedSizeBuffersManager::getBufferSize
std::size_t getBufferSize()
Get the number of elements of a buffer.
Definition: FixedSizeBuffersManager-inl.h:193
yarp::dev::impl::Buffer
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
Definition: FixedSizeBuffersManager.h:27
buffer
Definition: V4L_camera.h:75
yarp::dev::impl::FixedSizeBuffersManager::printBuffers
void printBuffers()
Prints info of buffers.
Definition: FixedSizeBuffersManager-inl.h:172
yarp::dev::impl::FixedSizeBuffersManager::releaseBuffer
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
Definition: FixedSizeBuffersManager-inl.h:156
yarp::dev::impl::FixedSizeBuffersManager::~FixedSizeBuffersManager
~FixedSizeBuffersManager()
Destructor.
Definition: FixedSizeBuffersManager-inl.h:184
yarp::dev::impl::Buffer::Buffer
Buffer()
Constructor.
Definition: FixedSizeBuffersManager-inl.h:16
yDebug
#define yDebug(...)
Definition: Log.h:237
yarp::dev::impl::Buffer::setValue
void setValue(uint32_t index, T value)
Set the value @value in the buffer at index index.
Definition: FixedSizeBuffersManager-inl.h:54