Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::Buffer Class Reference

Wrapper class for a region of bytes in memor in an attempt to add some memory safety. Automatically allocates the size specified and frees it once done, adds boundary to I/O. More...

#include <buffer.h>

Public Member Functions

 Buffer (size_t size, bool update_offset=true)
 Creates a buffer of the specified size.
 
 Buffer (void *source, size_t size, bool update_offset=true)
 Creates a buffer pointing to a source.
 
 ~Buffer ()
 Destroy the buffer, freeing the memory.
 
uint8_traw () const
 The raw pointer to the bytes stored in memory, use is not recommended.
 
void clear ()
 Fulls the buffer with 0's and resets the offset.
 
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.
 
void resize (size_t size)
 Grow the buffer to fit a new size.
 
void write (uint8_t byte)
 Safely writes a byte to the buffer at the current offset.
 
void write (size_t offset, uint8_t byte)
 Safely writes a byte to the buffer at the specified offset.
 
uint8_t read ()
 Safely reads a byte from the buffer at the current offset.
 
uint8_t read (size_t offset)
 Safely reads a byte from the buffer at the specified offset.
 
void copy_from (const Buffer *buffer)
 Copies all the bytes from another buffer into this buffer.
 
void copy_from (const Buffer *buffer, size_t length)
 Copies a range of bytes from another buffer into this buffer.
 
void copy_from (const Buffer *buffer, size_t length, size_t offset)
 Copies a range of bytes from another buffer into this buffer at a specified offset.
 
void copy_from (const Buffer *buffer, size_t length, size_t offset, size_t offset_other)
 Copies a range of bytes from another buffer into this buffer at a specified offset for both.
 
void copy_from (const void *source, size_t length)
 Copies a range of bytes from a source into this buffer.
 
void copy_from (const void *source, size_t length, size_t offset)
 Copies a range of bytes from a source into this buffer at a specified offset.
 
void copy_to (Buffer *buffer)
 Copies all the bytes from this buffer into another buffer.
 
void copy_to (Buffer *buffer, size_t length)
 Writes a range of bytes from this buffer into another buffer.
 
void copy_to (Buffer *buffer, size_t length, size_t offset)
 Copies a range of bytes from this buffer into another buffer at a specified offset.
 
void copy_to (Buffer *buffer, size_t length, size_t offset, size_t offset_other)
 Copies a range of bytes from this buffer into another at an offset for each.
 
void copy_to (void *destination, size_t length)
 Copies a range of bytes from this buffer into a source.
 
void copy_to (void *destination, size_t length, size_t offset)
 Copies a range of bytes from this buffer at an offset into a source.
 

Public Attributes

bool update_offset = true
 Should operations write/read/copy update the offset in the buffer?
 

Detailed Description

Wrapper class for a region of bytes in memor in an attempt to add some memory safety. Automatically allocates the size specified and frees it once done, adds boundary to I/O.

Definition at line 25 of file buffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

Buffer::Buffer ( size_t  size,
bool  update_offset = true 
)
explicit

Creates a buffer of the specified size.

Parameters
sizeThe capacity of the buffer
update_offsetShould operations update the offset of the buffer?

Definition at line 20 of file buffer.cpp.

21 : m_capacity(size),
23
24 // Create the buffer
25 m_bytes = new uint8_t[size];
26
27}
bool update_offset
Should operations write/read/copy update the offset in the buffer?
Definition buffer.h:44
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22

◆ Buffer() [2/2]

Buffer::Buffer ( void source,
size_t  size,
bool  update_offset = true 
)

Creates a buffer pointing to a source.

Parameters
sourceThe source
sizeThe capacity of the source
update_offsetShould buffer operations update the internal offset?

Definition at line 36 of file buffer.cpp.

38
39 m_bytes = (uint8_t*) source;
40 m_capacity = size;
41 m_dont_delete = true;
42
43}

◆ ~Buffer()

Buffer::~Buffer ( )

Destroy the buffer, freeing the memory.

Definition at line 48 of file buffer.cpp.

48 {
49 if (!m_dont_delete && m_bytes)
50 delete[] m_bytes;
51}

Member Function Documentation

◆ capacity()

size_t Buffer::capacity ( ) const

The max bytes the buffer can currently store.

See also
Buffer:grow(size_t size) to increase this.
Returns
The length of the buffer storage array

Definition at line 95 of file buffer.cpp.

95 {
96 return m_capacity;
97}

Referenced by copy_from(), copy_from(), copy_from(), copy_from(), copy_to(), copy_to(), copy_to(), copy_to(), read(), write(), and MaxOS::drivers::disk::Disk::write().

◆ clear()

void Buffer::clear ( )

Fulls the buffer with 0's and resets the offset.

Definition at line 65 of file buffer.cpp.

65 {
66
67 m_offset = 0;
68 memset(m_bytes, 0, m_capacity);
69}
void * memset(void *ptr, uint32_t value, uint64_t num)
Fills a block of memory with a specified value.
Definition memoryIO.cpp:215

References memset().

Referenced by MaxOS::filesystem::format::Fat32File::read(), and MaxOS::filesystem::format::Fat32File::write().

◆ copy_from() [1/6]

void Buffer::copy_from ( const Buffer buffer)

Copies all the bytes from another buffer into this buffer.

Parameters
bufferWhere to read from

Definition at line 195 of file buffer.cpp.

195 {
196
197 // Copy the buffer
198 ASSERT(buffer->capacity() <= capacity(), "Copy exceeds buffer capacity");
199 copy_from(buffer->raw(), buffer->capacity());
200
201}
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
void copy_from(const Buffer *buffer)
Copies all the bytes from another buffer into this buffer.
Definition buffer.cpp:195
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100

References ASSERT, capacity(), copy_from(), and raw().

Referenced by copy_from(), copy_from(), copy_from(), copy_from(), MaxOS::filesystem::format::Fat32File::read(), MaxOS::filesystem::format::ext2::Ext2File::write(), MaxOS::filesystem::format::Fat32File::write(), and MaxOS::filesystem::format::ext2::Ext2Volume::write_inode().

◆ copy_from() [2/6]

void Buffer::copy_from ( const Buffer buffer,
size_t  length 
)

Copies a range of bytes from another buffer into this buffer.

Parameters
bufferWhere to read from
lengthHow much to read

Definition at line 209 of file buffer.cpp.

209 {
210
211 // Copy the buffer
212 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
213 copy_from(buffer->raw(), length);
214
215}

References ASSERT, capacity(), copy_from(), and raw().

◆ copy_from() [3/6]

void Buffer::copy_from ( const Buffer buffer,
size_t  length,
size_t  offset 
)

Copies a range of bytes from another buffer into this buffer at a specified offset.

Parameters
bufferWhere to read from
lengthHow much to read
offsetWhere to start writing into this at

Definition at line 223 of file buffer.cpp.

223 {
224
225 // Copy the buffer
226 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
227 copy_from(buffer->raw(), length, offset);
228
229}

References ASSERT, capacity(), copy_from(), and raw().

◆ copy_from() [4/6]

void Buffer::copy_from ( const Buffer buffer,
size_t  length,
size_t  offset,
size_t  offset_other 
)

Copies a range of bytes from another buffer into this buffer at a specified offset for both.

Parameters
bufferWhere to read from
lengthHow much to read
offsetWhere to start writing into this at
offset_otherWhere to start reading from the other buffer

Definition at line 239 of file buffer.cpp.

239 {
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}

References ASSERT, capacity(), copy_from(), and raw().

◆ copy_from() [5/6]

void Buffer::copy_from ( const void source,
size_t  length 
)

Copies a range of bytes from a source into this buffer.

Parameters
sourceWhere to read from
lengthHow much to read

Definition at line 253 of file buffer.cpp.

253 {
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}
void set_offset(size_t offset)
Set the offset for where operations should begin from.
Definition buffer.cpp:128
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165

References ASSERT, memcpy(), and set_offset().

◆ copy_from() [6/6]

void Buffer::copy_from ( const void source,
size_t  length,
size_t  offset 
)

Copies a range of bytes from a source into this buffer at a specified offset.

Parameters
sourceWhere to read from
lengthHow much to read
offsetWhere to start writing at

Definition at line 268 of file buffer.cpp.

268 {
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}

References ASSERT, memcpy(), and set_offset().

◆ copy_to() [1/6]

void Buffer::copy_to ( Buffer buffer)

Copies all the bytes from this buffer into another buffer.

Parameters
bufferWhere to write to

Definition at line 282 of file buffer.cpp.

282 {
283
284 // Copy the buffer
285 ASSERT(capacity() <= buffer->capacity(), "Copy exceeds external buffer capacity");
286 copy_to(buffer->raw(), capacity());
287
288}
void copy_to(Buffer *buffer)
Copies all the bytes from this buffer into another buffer.
Definition buffer.cpp:282

References ASSERT, capacity(), copy_to(), and raw().

Referenced by copy_to(), copy_to(), copy_to(), copy_to(), MaxOS::filesystem::format::ext2::Ext2File::read(), and MaxOS::filesystem::format::ext2::Ext2Volume::read_inode().

◆ copy_to() [2/6]

void Buffer::copy_to ( Buffer buffer,
size_t  length 
)

Writes a range of bytes from this buffer into another buffer.

Parameters
bufferWhere to write to
lengthHow much to write

Definition at line 296 of file buffer.cpp.

296 {
297
298 // Copy the buffer
299 ASSERT(length <= buffer->capacity(), "Copy exceeds external buffer capacity");
300 copy_to(buffer->raw(), length);
301
302}

References ASSERT, capacity(), copy_to(), and raw().

◆ copy_to() [3/6]

void Buffer::copy_to ( Buffer buffer,
size_t  length,
size_t  offset 
)

Copies a range of bytes from this buffer into another buffer at a specified offset.

Parameters
bufferWhere to write to
lengthHow much to write
offsetWhere to start reading at

Definition at line 311 of file buffer.cpp.

311 {
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}

References ASSERT, capacity(), copy_to(), and raw().

◆ copy_to() [4/6]

void Buffer::copy_to ( Buffer buffer,
size_t  length,
size_t  offset,
size_t  offset_other 
)

Copies a range of bytes from this buffer into another at an offset for each.

Parameters
bufferWhere to write to
lengthHow much to write
offsetWhere to start reading from
offset_otherWhere to start writing to

Definition at line 327 of file buffer.cpp.

327 {
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}

References ASSERT, capacity(), copy_to(), and raw().

◆ copy_to() [5/6]

void Buffer::copy_to ( void destination,
size_t  length 
)

Copies a range of bytes from this buffer into a source.

Parameters
destinationWhere to write to
lengthHow much to write

Definition at line 342 of file buffer.cpp.

342 {
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}

References ASSERT, memcpy(), and set_offset().

◆ copy_to() [6/6]

void Buffer::copy_to ( void destination,
size_t  length,
size_t  offset 
)

Copies a range of bytes from this buffer at an offset into a source.

Parameters
destinationWhere to write to
lengthHow much to write
offsetWhere to start reading from

Definition at line 358 of file buffer.cpp.

358 {
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}

References ASSERT, memcpy(), and set_offset().

◆ full()

void Buffer::full ( uint8_t  byte,
size_t  offset = 0,
size_t  amount = 0 
)

Full the buffer with a specified byte at an offset.

Parameters
byteThe byte to write
offsetWhere to start writing (deafults to 0)
amount

Definition at line 78 of file buffer.cpp.

78 {
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}

References ASSERT, and memset().

◆ raw()

uint8_t * Buffer::raw ( ) const

The raw pointer to the bytes stored in memory, use is not recommended.

Returns
The address of buffer's storage

Definition at line 57 of file buffer.cpp.

57 {
58
59 return m_bytes;
60}

Referenced by copy_from(), copy_from(), copy_from(), copy_from(), copy_to(), copy_to(), copy_to(), and copy_to().

◆ read() [1/2]

uint8_t Buffer::read ( )

Safely reads a byte from the buffer at the current offset.

Returns
The read byte

Definition at line 168 of file buffer.cpp.

168 {
169 set_offset(m_offset + 1);
170 return m_bytes[m_offset - 1];
171}

References set_offset().

Referenced by MaxOS::drivers::disk::AdvancedTechnologyAttachment::write().

◆ read() [2/2]

uint8_t Buffer::read ( size_t  offset)

Safely reads a byte from the buffer at the specified offset.

Parameters
offsetThe offset into the buffer storage array
Returns
The read byte

Definition at line 180 of file buffer.cpp.

180 {
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}

References ASSERT, capacity(), and set_offset().

◆ resize()

void Buffer::resize ( size_t  size)

Grow the buffer to fit a new size.

Parameters
sizeThe new max capacity of the buffer

Definition at line 104 of file buffer.cpp.

104 {
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}

References memcpy().

◆ set_offset()

void Buffer::set_offset ( size_t  offset)

Set the offset for where operations should begin from.

Parameters
offsetThe new offset

Definition at line 128 of file buffer.cpp.

128 {
129
130 if (update_offset)
131 m_offset = offset;
132
133}

References update_offset.

Referenced by copy_from(), copy_from(), copy_to(), copy_to(), read(), MaxOS::filesystem::format::Fat32File::read(), read(), MaxOS::filesystem::format::ext2::Ext2Volume::read_block(), MaxOS::filesystem::format::Fat32File::write(), write(), write(), and MaxOS::filesystem::format::ext2::Ext2Volume::write_block().

◆ write() [1/2]

void Buffer::write ( size_t  offset,
uint8_t  byte 
)

Safely writes a byte to the buffer at the specified offset.

Parameters
offsetThe offset into the buffer storage array
byteThe byte to write

Definition at line 152 of file buffer.cpp.

152 {
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}

References ASSERT, capacity(), and set_offset().

◆ write() [2/2]

void Buffer::write ( uint8_t  byte)

Safely writes a byte to the buffer at the current offset.

Parameters
byteThe byte to write

Definition at line 140 of file buffer.cpp.

140 {
141 m_bytes[m_offset] = byte;
142 set_offset(m_offset + 1);
143}

References set_offset().

Member Data Documentation

◆ update_offset

bool MaxOS::common::Buffer::update_offset = true

Should operations write/read/copy update the offset in the buffer?

Definition at line 44 of file buffer.h.

Referenced by set_offset(), and MaxOS::filesystem::format::ext2::Ext2Volume::write_block().


The documentation for this class was generated from the following files: