Memory Buffer Class


Topics:

Overview
Functions


Overview

The MemoryBuffer is a general-purpose class is used to create and manipulate resizable memory buffers. The MemoryBuffer class guarantees that each object is unique by storing a unique copy of each buffer with each object. This ensures that MemoryBuffer objects can be safely copy constructed, assigned, resized, and deleted by multiple threads. NOTE: Multiple threads accessing shared memory segments must be handled by the application.


Functions

MemoryBuffer::MemoryBuffer()
MemoryBuffer::~MemoryBuffer()
MemoryBuffer::Alloc()
MemoryBuffer::Cat()
MemoryBuffer::Clear()
MemoryBuffer::DeleteAt()
MemoryBuffer::Destroy()
MemoryBuffer::Find()
MemoryBuffer::FreeBytes()
MemoryBuffer::InsertAt()
MemoryBuffer::Load()
MemoryBuffer::Realloc()
MemoryBuffer::ReplaceAt()
MemoryBuffer::dlength()
MemoryBuffer::is_null()
MemoryBuffer::length()
MemoryBuffer::m_buf()
MemoryBuffer::resize()
MemroyBuffer Overloaded Operators

MemoryBuffer::MemoryBuffer() - Default class constructor.

MemoryBuffer::MemoryBuffer(unsigned bytes) - Class constructor used to allocate a specified number of bytes in memory for a buffer.

MemoryBuffer::MemoryBuffer(const void *buf, unsigned bytes) - Class constructor used to allocate a specified number of bytes for buffer "buf" and load the memory buffer.

MemoryBuffer::MemoryBuffer(const MemoryBuffer &buf) - Class copy constructor. This copy constructor does not use share semantics and guarantees that each copy will be unique.

MemoryBuffer::~MemoryBuffer() - Class destructor responsible for freeing the memory allocated for this buffer.

void *MemoryBuffer::Alloc(unsigned bytes) - Public member function used to allocate a specified number of bytes for this memory buffer. This function will try to re-use the current memory segment allocated for this buffer before re-allocating memory for the buffer. Returns a void pointer to the buffer or a null value if a memory allocation error occurs.

void MemoryBuffer::Cat(const void *buf, unsigned bytes) - Public member function used to concatenate a specified number of bytes from a memory location to the end of the object that invoked the call.

void MemoryBuffer::Cat(unsigned char byte) - Public member function used to concatenate a single byte to the end of the object that invoked the call.

void MemoryBuffer::Cat(char byte) - Public member function used to concatenate a single byte to the end of the object that invoked the call.

void MemoryBuffer::Clear() - Public member function used to clear this buffer.

unsigned MemoryBuffer::DeleteAt(unsigned position, unsigned bytes) - Public member function used to delete a specified number of bytes, starting at the specified position. Returns the number of bytes deleted.

void MemoryBuffer::Destroy() - Public member function used to free the memory allocated for the buffer and reset the length variables.

unsigned MemoryBuffer::Find(void *buf, unsigned offset = 0) - Public member function that returns the index of first occurrence of pattern "buf." Returns -1 if pattern not found.

unsigned MemoryBuffer::Find(void *buf, unsigned bytes, unsigned offset = 0) - Public member function that returns the index of first occurrence of pattern "buf." Returns -1 if pattern not found.

unsigned MemoryBuffer::Find(const MemoryBuffer &buf, unsigned offset = 0) - Public member function that returns the index of first occurrence of pattern "buf." Returns -1 if pattern not found.

void *MemoryBuffer::FreeBytes() - Public member function used to free any unused bytes allocated for this buffer. Returns a pointer to the re-allocated memory buffer or a null value if an error occurs.

unsigned MemoryBuffer::InsertAt(unsigned position, const void *buf, unsigned bytes) - Public member function used to insert a specified number of bytes a the current position, keeping the current data intact. Returns the number of bytes inserted or zero if an error occurs.

unsigned MemoryBuffer::InsertAt(unsigned position, const MemoryBuffer &buf) - Public member function used to insert a specified number of bytes a the current position, keeping the current data intact. Returns the number of bytes inserted or zero if an error occurs.

int MemoryBuffer::Load(const void *buf, unsigned bytes) - Public member function used to load this object with a unique copy of the specified buffer. Returns true if successful or false if an error occurs.

void *MemoryBuffer::Realloc(unsigned bytes, int keep = 1, int reuse = 1) - Public member function used to resize the logical length of the buffer. If the "keep" variable is true the old data will be copied into the new space. By default the old data will not be deleted. Returns a pointer to the buffer or a null value if an error occurs.

unsigned MemoryBuffer::ReplaceAt(unsigned position, const void *buf, unsigned bytes) - Public member function used to replace a specified number of bytes at the specified position. Returns the number of bytes replaced or zero if an error occurs.

unsigned MemoryBuffer::ReplaceAt(unsigned position, const MemoryBuffer &buf) - Public member function used to replace a specified number of bytes at the specified position. Returns the number of bytes replaced or zero if an error occurs.

unsigned MemoryBuffer::dlength() - Public member function that returns the dimensioned length of this buffer. The dimensioned length refers to the total number of bytes that have been allocated and the logical length refers to the actual length of the buffer.

int MemoryBuffer::is_null() - Public member function that returns true if this buffer is null.

unsigned MemoryBuffer::length() -  Public member function that returns the logical length of this buffer. The logical length refers to the actual length of the buffer and the dimensioned length refers to the total number of bytes that have been allocated for this buffer.

unsigned char *MemoryBuffer::m_buf() - Public member function that returns a pointer to the memory buffer.

int MemoryBuffer::resize(unsigned bytes, int keep = 1) - Public member function used to resize the logical length of the buffer. If the "keep" variable is true the old data will be copied into the new space. By default the old data will not be deleted. Returns true if successful or false if an error occurs.

Overloaded operators:
MemoryBuffer &MemoryBuffer::operator=(const MemoryBuffer &buf) - Class assignment operator. This assignment operator does not use share semantics and guarantees that each copy will be unique.

int MemoryBuffer::operator!() - Overloaded not operator that returns true if the buffer is null.

operator MemoryBuffer::int () - Conversion function that returns true if the buffer is not null.

unsigned char& MemoryBuffer::operator[](unsigned i) - Overloaded subscript operator used to ensure that an index is in range before subscripting this object's data. If the index is out of range the index value will be set to the logical length.

void MemoryBuffer::operator+=(const MemoryBuffer &buf) - Overloaded member operator used to concatenate a memory buffer to the end of the object that invoked the call.

void MemoryBuffer::operator+=(const unsigned char &byte) - Overloaded member operator used to concatenate a single byte to the end of the object that invoked the call.

void MemoryBuffer::operator+=(const char &byte) - Overloaded member operator used to concatenate a single byte to the end of the object that invoked the call.

friend MemoryBuffer operator+(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator used to add two MemoryBuffer objects together.

friend int operator==(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operands are equal to each other.

friend int operator!=(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operands are not equal to each other.

friend int operator>(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operand "a" is greater then "b."

friend int operator>=(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operand "a" is greater then or equal to "b."

friend int operator<(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operand "a" is less then "b."

friend int operator<=(const MemoryBuffer &a, const MemoryBuffer &b) - Overloaded operator that returns true if its operand "a" is less then or equal to "b."


End Of Document