26 #ifndef INCLUDE_OLA_IO_MEMORYBLOCK_H_
27 #define INCLUDE_OLA_IO_MEMORYBLOCK_H_
51 m_data_end(data + size),
77 unsigned int Capacity()
const {
return m_capacity; }
84 return static_cast<unsigned int>(m_data_end - m_last);
91 unsigned int Size()
const {
92 return static_cast<unsigned int>(m_last - m_first);
100 return m_last == m_first;
107 uint8_t *
Data()
const {
return m_first; }
116 unsigned int Append(
const uint8_t *data,
unsigned int length) {
117 unsigned int bytes_to_write = std::min(
118 length, static_cast<unsigned int>(m_data_end - m_last));
119 memcpy(m_last, data, bytes_to_write);
120 m_last += bytes_to_write;
121 return bytes_to_write;
131 unsigned int Prepend(
const uint8_t *data,
unsigned int length) {
132 unsigned int bytes_to_write = std::min(
133 length, static_cast<unsigned int>(m_first - m_data));
134 memcpy(m_first - bytes_to_write, data + length - bytes_to_write,
136 m_first -= bytes_to_write;
137 return bytes_to_write;
146 unsigned int Copy(uint8_t *data,
unsigned int length)
const {
147 unsigned int bytes_to_read = std::min(
148 length, static_cast<unsigned int>(m_last - m_first));
149 memcpy(data, m_first, bytes_to_read);
150 return bytes_to_read;
159 unsigned int bytes_to_pop = std::min(
160 length, static_cast<unsigned int>(m_last - m_first));
161 m_first += bytes_to_pop;
163 if (m_first == m_last) {
171 uint8_t*
const m_data;
172 uint8_t*
const m_data_end;
173 unsigned int m_capacity;
180 #endif // INCLUDE_OLA_IO_MEMORYBLOCK_H_
unsigned int Size() const
The size of data in this block.
Definition: MemoryBlock.h:91
unsigned int Prepend(const uint8_t *data, unsigned int length)
Prepend data to this block.
Definition: MemoryBlock.h:131
MemoryBlock(uint8_t *data, unsigned int size)
Construct a new MemoryBlock.
Definition: MemoryBlock.h:49
unsigned int PopFront(unsigned int length)
Remove data from the front of the block.
Definition: MemoryBlock.h:158
unsigned int Append(const uint8_t *data, unsigned int length)
Append data to this block.
Definition: MemoryBlock.h:116
uint8_t * Data() const
Provides a pointer to the first byte of valid data in this block.
Definition: MemoryBlock.h:107
unsigned int Capacity() const
The size of the memory region for this block.
Definition: MemoryBlock.h:77
bool Empty() const
Check if the block contains data.
Definition: MemoryBlock.h:99
A MemoryBlock encapsulates a chunk of memory. It's used by the IOQueue and IOStack classes...
Definition: MemoryBlock.h:41
unsigned int Copy(uint8_t *data, unsigned int length) const
Definition: MemoryBlock.h:146
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void SeekBack()
Move the insertation point to the end of the block. This is useful if you want to use the block in pr...
Definition: MemoryBlock.h:68
~MemoryBlock()
Destructor, this frees the memory for the block.
Definition: MemoryBlock.h:60
unsigned int Remaining() const
The free space at the end of the block.
Definition: MemoryBlock.h:83