Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MemoryBlock.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * MemoryBlock.h
17  * A block of memory.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_IO_MEMORYBLOCK_H_
27 #define INCLUDE_OLA_IO_MEMORYBLOCK_H_
28 
29 #include <stdint.h>
30 #include <string.h>
31 #include <algorithm>
32 
33 namespace ola {
34 namespace io {
35 
41 class MemoryBlock {
42  public:
49  MemoryBlock(uint8_t *data, unsigned int size)
50  : m_data(data),
51  m_data_end(data + size),
52  m_capacity(size),
53  m_first(data),
54  m_last(data) {
55  }
56 
61  delete[] m_data;
62  }
63 
68  void SeekBack() {
69  m_first = m_data_end;
70  m_last = m_first;
71  }
72 
77  unsigned int Capacity() const { return m_capacity; }
78 
83  unsigned int Remaining() const {
84  return static_cast<unsigned int>(m_data_end - m_last);
85  }
86 
91  unsigned int Size() const {
92  return static_cast<unsigned int>(m_last - m_first);
93  }
94 
99  bool Empty() const {
100  return m_last == m_first;
101  }
102 
107  uint8_t *Data() const { return m_first; }
108 
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;
122  }
123 
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,
135  bytes_to_write);
136  m_first -= bytes_to_write;
137  return bytes_to_write;
138  }
139 
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;
151  }
152 
158  unsigned int PopFront(unsigned int length) {
159  unsigned int bytes_to_pop = std::min(
160  length, static_cast<unsigned int>(m_last - m_first));
161  m_first += bytes_to_pop;
162  // reset
163  if (m_first == m_last) {
164  m_first = m_data;
165  m_last = m_data;
166  }
167  return bytes_to_pop;
168  }
169 
170  private:
171  uint8_t* const m_data;
172  uint8_t* const m_data_end;
173  unsigned int m_capacity;
174  // Points to the valid data in the block.
175  uint8_t *m_first;
176  uint8_t *m_last; // points to one after last
177 };
178 } // namespace io
179 } // namespace ola
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