31#ifndef ETL_BUFFER_DESCRIPTORS_INCLUDED
32#define ETL_BUFFER_DESCRIPTORS_INCLUDED
50 template <
typename TBuffer,
size_t BUFFER_SIZE_,
size_t N_BUFFERS_,
typename TFlag =
bool>
51 class buffer_descriptors
55 struct descriptor_item;
59 typedef TBuffer value_type;
60 typedef value_type* pointer;
61 typedef size_t size_type;
62 typedef TFlag flag_type;
64 static ETL_CONSTANT size_type N_BUFFERS = N_BUFFERS_;
65 static ETL_CONSTANT size_type BUFFER_SIZE = BUFFER_SIZE_;
74 friend class buffer_descriptors;
76 static ETL_CONSTANT size_type MAX_SIZE = buffer_descriptors::BUFFER_SIZE;
80 : pdesc_item(ETL_NULLPTR)
85 descriptor(
const descriptor& other)
86 : pdesc_item(other.pdesc_item)
91 descriptor& operator=(
const descriptor& other)
93 pdesc_item = other.pdesc_item;
100 assert(pdesc_item != ETL_NULLPTR);
102 return pdesc_item->pbuffer;
107 ETL_NODISCARD ETL_CONSTEXPR size_type max_size()
const
114 bool is_allocated()
const
116 return bool(pdesc_item->in_use);
121 bool is_released()
const
123 return bool(!pdesc_item->in_use);
128 bool is_valid()
const
130 return pdesc_item != ETL_NULLPTR;
136 pdesc_item->in_use =
false;
142 descriptor(descriptor_item* pdesc_item_)
143 : pdesc_item(pdesc_item_)
150 pdesc_item->in_use =
true;
154 descriptor_item* pdesc_item;
172 notification(descriptor desc_,
size_t count_)
180 descriptor get_descriptor()
const
187 size_t get_count()
const
199 typedef etl::delegate<void(notification)> callback_type;
202 buffer_descriptors(TBuffer* pbuffers_, callback_type callback_ = callback_type())
203 : callback(callback_)
205 for (
size_t i = 0UL; i < N_BUFFERS; ++i)
207 descriptor_items[i].pbuffer = pbuffers_ + (i * BUFFER_SIZE);
208 descriptor_items[i].in_use =
false;
213 void set_callback(
const callback_type& callback_)
215 callback = callback_;
221 for (
size_t i = 0UL; i < N_BUFFERS; ++i)
223 descriptor_items[i].in_use =
false;
231 bool is_valid()
const
233 return callback.is_valid();
237 void notify(notification n)
240 if (callback.is_valid())
248 descriptor allocate()
250 descriptor desc(&descriptor_items[next]);
252 if (desc.is_released())
268 descriptor allocate(value_type fill_)
270 descriptor desc = allocate();
274 etl::fill_n(desc.data(), BUFFER_SIZE, fill_);
283 struct descriptor_item
286 volatile flag_type in_use;
289 callback_type callback;
290 etl::array<descriptor_item, N_BUFFERS> descriptor_items;
291 etl::cyclic_value<uint_least8_t, 0U, N_BUFFERS - 1> next;
294 template <
typename TBuffer,
size_t BUFFER_SIZE_,
size_t N_BUFFERS_,
typename TFlag>
295 ETL_CONSTANT
typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
296 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::N_BUFFERS;
298 template <
typename TBuffer,
size_t BUFFER_SIZE_,
size_t N_BUFFERS_,
typename TFlag>
299 ETL_CONSTANT
typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
300 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::BUFFER_SIZE;
302 template <
typename TBuffer,
size_t BUFFER_SIZE_,
size_t N_BUFFERS_,
typename TFlag>
303 ETL_CONSTANT
typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
304 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::descriptor::MAX_SIZE;
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470