12using namespace MaxOS::common;
22 update_offset(update_offset) {
37 : update_offset(update_offset) {
49 if (!m_dont_delete && m_bytes)
68 memset(m_bytes, 0, m_capacity);
81 size_t start = m_offset + offset;
82 ASSERT(
start <= m_capacity,
"Offset past buffer end");
114 if (!m_dont_delete && m_bytes)
119 m_dont_delete =
true;
141 m_bytes[m_offset] =
byte;
158 m_bytes[m_offset + offset] =
byte;
170 return m_bytes[m_offset - 1];
187 return m_bytes[(m_offset - 1) + offset];
256 ASSERT(length + m_offset <= m_capacity,
"Copy exceeds buffer capacity");
257 memcpy(m_bytes + m_offset, source, length);
271 ASSERT((length + offset + m_offset) <= m_capacity,
"Copy exceeds buffer capacity");
272 memcpy(m_bytes + offset + m_offset, source, length);
314 ASSERT((length + offset) <= buffer->
capacity(),
"Copy exceeds external buffer capacity");
330 ASSERT((length + offset) <= buffer->
capacity(),
"Copy exceeds external buffer capacity");
345 ASSERT(length + m_offset <= (m_capacity),
"Copy exceeds buffer capacity");
346 memcpy(destination, m_bytes + m_offset, length);
361 ASSERT((length + offset + m_offset) <= m_capacity,
"Copy exceeds buffer capacity");
362 memcpy(destination, m_bytes + offset + m_offset, length);
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....
void write(uint8_t byte)
Safely writes a byte to the buffer at the current offset.
~Buffer()
Destroy the buffer, freeing the memory.
void full(uint8_t byte, size_t offset=0, size_t amount=0)
Full the buffer with a specified byte at an offset.
void set_offset(size_t offset)
Set the offset for where operations should begin from.
size_t capacity() const
The max bytes the buffer can currently store.
uint8_t * raw() const
The raw pointer to the bytes stored in memory, use is not recommended.
uint8_t read()
Safely reads a byte from the buffer at the current offset.
bool update_offset
Should operations write/read/copy update the offset in the buffer?
void copy_to(Buffer *buffer)
Copies all the bytes from this buffer into another buffer.
Buffer(size_t size, bool update_offset=true)
Creates a buffer of the specified size.
void copy_from(const Buffer *buffer)
Copies all the bytes from another buffer into this buffer.
void resize(size_t size)
Grow the buffer to fit a new size.
void clear()
Fulls the buffer with 0's and resets the offset.
Stores the left, top, width and height of a rectangle.
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
void * memset(void *ptr, uint32_t value, uint64_t num)
Fills a block of memory with a specified value.