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