Max OS 0.3
Loading...
Searching...
No Matches
buffer.cpp
Go to the documentation of this file.
1
9#include <common/buffer.h>
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13
20Buffer::Buffer(size_t size, bool update_offset)
21 : m_capacity(size),
22 update_offset(update_offset) {
23
24 // Create the buffer
25 m_bytes = new uint8_t[size];
26
27}
28
36Buffer::Buffer(void* source, size_t size, bool update_offset)
37 : update_offset(update_offset) {
38
39 m_bytes = (uint8_t*) source;
40 m_capacity = size;
41 m_dont_delete = true;
42
43}
44
49 if (!m_dont_delete && m_bytes)
50 delete[] m_bytes;
51}
52
58
59 return m_bytes;
60}
61
66
67 m_offset = 0;
68 memset(m_bytes, 0, m_capacity);
69}
70
78void Buffer::full(uint8_t byte, size_t offset, size_t amount) {
79
80 // Prevent writing past the buffer bounds
81 size_t start = m_offset + offset;
82 ASSERT(start <= m_capacity, "Offset past buffer end");
83
84 if (amount == 0) amount = m_capacity - start;
85 ASSERT(start + amount <= m_capacity, "Buffer overflow");
86
87 memset(m_bytes + start, byte, amount);
88
89}
90
95size_t Buffer::capacity() const {
96 return m_capacity;
97}
98
104void Buffer::resize(size_t size) {
105
106 // Create the new buffer
107 auto* new_buffer = new uint8_t[size];
108
109 // Copy the old buffer
110 if (m_bytes)
111 memcpy(new_buffer, m_bytes, m_capacity);
112
113 // Store the new buffer
114 if (!m_dont_delete && m_bytes)
115 delete[] m_bytes;
116
117 m_bytes = new_buffer;
118 m_capacity = size;
119 m_dont_delete = true;
120
121}
122
128void Buffer::set_offset(size_t offset) {
129
130 if (update_offset)
131 m_offset = offset;
132
133}
134
141 m_bytes[m_offset] = byte;
142 set_offset(m_offset + 1);
143}
144
145
152void Buffer::write(size_t offset, uint8_t byte) {
153
154 // Prevent writing past the buffer bounds
155 ASSERT(m_offset + offset < capacity(), "Buffer overflow");
156
157 // Set the byte
158 m_bytes[m_offset + offset] = byte;
159 set_offset(m_offset + 1);
160
161}
162
169 set_offset(m_offset + 1);
170 return m_bytes[m_offset - 1];
171}
172
180uint8_t Buffer::read(size_t offset) {
181
182 // Prevent writing past the buffer bounds
183 ASSERT(m_offset + offset < capacity(), "Buffer overflow");
184
185 // Set the byte
186 set_offset(m_offset + 1);
187 return m_bytes[(m_offset - 1) + offset];
188}
189
195void Buffer::copy_from(const Buffer* buffer) {
196
197 // Copy the buffer
198 ASSERT(buffer->capacity() <= capacity(), "Copy exceeds buffer capacity");
199 copy_from(buffer->raw(), buffer->capacity());
200
201}
202
209void Buffer::copy_from(const Buffer* buffer, size_t length) {
210
211 // Copy the buffer
212 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
213 copy_from(buffer->raw(), length);
214
215}
216
223void Buffer::copy_from(const Buffer* buffer, size_t length, size_t offset) {
224
225 // Copy the buffer
226 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
227 copy_from(buffer->raw(), length, offset);
228
229}
230
239void Buffer::copy_from(const Buffer* buffer, size_t length, size_t offset, size_t offset_other) {
240
241 // Copy the buffer
242 ASSERT(length + offset_other <= buffer->capacity(), "Copy exceeds external buffer capacity");
243 copy_from(buffer->raw() + offset_other, length, offset);
244
245}
246
253void Buffer::copy_from(void const* source, size_t length) {
254
255 // Copy the bytes
256 ASSERT(length + m_offset <= m_capacity, "Copy exceeds buffer capacity");
257 memcpy(m_bytes + m_offset, source, length);
258 set_offset(m_offset + length);
259}
260
268void Buffer::copy_from(void const* source, size_t length, size_t offset) {
269
270 // Copy the bytes
271 ASSERT((length + offset + m_offset) <= m_capacity, "Copy exceeds buffer capacity");
272 memcpy(m_bytes + offset + m_offset, source, length);
273 set_offset(m_offset + length);
274
275}
276
282void Buffer::copy_to(Buffer* buffer) {
283
284 // Copy the buffer
285 ASSERT(capacity() <= buffer->capacity(), "Copy exceeds external buffer capacity");
286 copy_to(buffer->raw(), capacity());
287
288}
289
296void Buffer::copy_to(Buffer* buffer, size_t length) {
297
298 // Copy the buffer
299 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
300 copy_to(buffer->raw(), length);
301
302}
303
311void Buffer::copy_to(Buffer* buffer, size_t length, size_t offset) {
312
313 // Copy the bytes
314 ASSERT((length + offset) <= buffer->capacity(), "Copy exceeds external buffer capacity");
315 copy_to(buffer->raw(), length, offset);
316
317}
318
327void Buffer::copy_to(Buffer* buffer, size_t length, size_t offset, size_t offset_other) {
328
329 // Copy the bytes
330 ASSERT((length + offset) <= buffer->capacity(), "Copy exceeds external buffer capacity");
331 copy_to(buffer->raw() + offset_other, length, offset);
332
333}
334
335
342void Buffer::copy_to(void* destination, size_t length) {
343
344 // Copy the bytes
345 ASSERT(length + m_offset <= (m_capacity), "Copy exceeds buffer capacity");
346 memcpy(destination, m_bytes + m_offset, length);
347 set_offset(m_offset + length);
348
349}
350
358void Buffer::copy_to(void* destination, size_t length, size_t offset) {
359
360 // Copy the bytes
361 ASSERT((length + offset + m_offset) <= m_capacity, "Copy exceeds buffer capacity");
362 memcpy(destination, m_bytes + offset + m_offset, length);
363 set_offset(m_offset + length);
364
365}
Defines a Buffer class for safe memory operations.
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
void write(uint8_t byte)
Safely writes a byte to the buffer at the current offset.
Definition buffer.cpp:140
~Buffer()
Destroy the buffer, freeing the memory.
Definition buffer.cpp:48
void full(uint8_t byte, size_t offset=0, size_t amount=0)
Full the buffer with a specified byte at an offset.
Definition buffer.cpp:78
void set_offset(size_t offset)
Set the offset for where operations should begin from.
Definition buffer.cpp:128
size_t capacity() const
The max bytes the buffer can currently store.
Definition buffer.cpp:95
uint8_t * raw() const
The raw pointer to the bytes stored in memory, use is not recommended.
Definition buffer.cpp:57
uint8_t read()
Safely reads a byte from the buffer at the current offset.
Definition buffer.cpp:168
bool update_offset
Should operations write/read/copy update the offset in the buffer?
Definition buffer.h:44
void copy_to(Buffer *buffer)
Copies all the bytes from this buffer into another buffer.
Definition buffer.cpp:282
Buffer(size_t size, bool update_offset=true)
Creates a buffer of the specified size.
Definition buffer.cpp:20
void copy_from(const Buffer *buffer)
Copies all the bytes from another buffer into this buffer.
Definition buffer.cpp:195
void resize(size_t size)
Grow the buffer to fit a new size.
Definition buffer.cpp:104
void clear()
Fulls the buffer with 0's and resets the offset.
Definition buffer.cpp:65
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165
void * memset(void *ptr, uint32_t value, uint64_t num)
Fills a block of memory with a specified value.
Definition memoryIO.cpp:215