C++ abstraction for malloc'ed memory areas. More...
#include <cmemory.h>
Public Member Functions | |
cmemory (size_t len=CMEMORY_DEFAULT_SIZE) | |
Constructor. Allocates a new memory area with specified size (default: 1024 bytes). More... | |
cmemory (uint8_t *data, size_t datalen) | |
Constructor. Allocates a new memory area and clones specified buffer. More... | |
cmemory (cmemory const &m) | |
Copy constructor. More... | |
virtual | ~cmemory () |
Destructor. Calls C-function free() for allocated memory area. | |
Memory operations. | |
cmemory & | operator= (cmemory const &m) |
Assignment operator. More... | |
uint8_t & | operator[] (size_t index) const |
Index operator. More... | |
bool | operator== (cmemory const &m) const |
Comparison operator. More... | |
bool | operator!= (cmemory const &m) const |
Comparison operator (unequal). More... | |
bool | operator< (cmemory const &m) const |
Less than operator. More... | |
bool | operator> (cmemory const &m) const |
Less than operator. More... | |
cmemory | operator& (cmemory const &m) const |
AND operator. More... | |
cmemory & | operator+= (cmemory const &m) |
Append operator with assignment to this cmemory instance. More... | |
cmemory | operator+ (cmemory const &m) |
Append operator with assignment to temporary cmemory instance. More... | |
uint8_t * | somem () const |
Returns pointer to start of allocated memory area. | |
size_t | memlen () const |
Returns length of allocated memory area. | |
virtual void | assign (uint8_t *buf, size_t buflen) |
Overwrites memory area with plain buffer specified. Resizes internal memory as necessary. More... | |
virtual size_t | length () const |
virtual uint8_t * | resize (size_t len) |
Resizes allocated memory area by calling C-function realloc(). More... | |
uint8_t * | insert (uint8_t *ptr, size_t len) |
Inserts len bytes at pointer ptr into allocated memory area. More... | |
uint8_t * | insert (unsigned int offset, size_t len) |
Inserts len bytes at start of allocated memory area plus offset. More... | |
void | remove (uint8_t *ptr, size_t len) |
Removes len bytes starting at pointer ptr from allocated memory area. More... | |
void | remove (unsigned int offset, size_t len) |
Removes len bytes starting at offset relative to the allocated memory area. More... | |
unsigned int | find_first_of (uint8_t value, unsigned int start) |
Returns index of first byte with value "value" found in allocated memory area starting at offset "start". More... | |
void | clear () |
Clears the allocated memory area by setting all bytes to 0. | |
virtual void | pack (uint8_t *buf, size_t buflen) |
Copies content of this cmemory instance to specified buffer. | |
virtual void | unpack (uint8_t *buf, size_t buflen) |
Copies content of specified buffer into this cmemory instance . | |
virtual std::string | toString () const |
map onto std::string instance | |
virtual bool | empty () const |
Returns boolean value empty/non empty. | |
Friends | |
std::ostream & | operator<< (std::ostream &os, cmemory const &mem) |
C++ abstraction for malloc'ed memory areas.
This class provides a set of convenience methods for dealing with memory areas managed by the C-functions malloc/realloc/free. It is not hiding or preventing access from the memory area. The user can get access to the memory addresses via somem() and calculate offsets. However, when resizing the memory area, internal memory addresses may have changed. cmemory tracks such changes and updates its internal variables appropriately. Memory addresses kept outside of cmemory must be updated by the developer explicitly.
cmemory::cmemory | ( | size_t | len = CMEMORY_DEFAULT_SIZE | ) |
Constructor. Allocates a new memory area with specified size (default: 1024 bytes).
len | length of new memory area to be allocated via malloc. |
cmemory::cmemory | ( | uint8_t * | data, |
size_t | datalen | ||
) |
Constructor. Allocates a new memory area and clones specified buffer.
data | pointer to start of memory area to be cloned |
datalen | length of memory area to be cloned |
cmemory::cmemory | ( | cmemory const & | m | ) |
Copy constructor.
m | cmemory instance |
|
virtual |
Overwrites memory area with plain buffer specified. Resizes internal memory as necessary.
buf | pointer to start of buffer to be copied |
buflen | length of buffer to be copied |
Reimplemented in rofl::cpacket.
unsigned int cmemory::find_first_of | ( | uint8_t | value, |
unsigned int | start | ||
) |
Returns index of first byte with value "value" found in allocated memory area starting at offset "start".
value | sought value |
start | relative begin within allocated memory area for search |
eMemNotFound | is thrown when a byte with the specified value cannot be found |
uint8_t * cmemory::insert | ( | uint8_t * | ptr, |
size_t | len | ||
) |
Inserts len bytes at pointer ptr into allocated memory area.
ptr | start location of new inserted memory block |
len | number of bytes inserted in the allocated memory area |
eMemInval | is thrown when ptr points to a location outside of the allocated memory area |
uint8_t * cmemory::insert | ( | unsigned int | offset, |
size_t | len | ||
) |
Inserts len bytes at start of allocated memory area plus offset.
offset | relative start location of new inserted memory block |
len | number of bytes inserted in the allocated memory area |
eMemInval | is thrown when offset refers to a location outside of the allocated memory area |
bool cmemory::operator!= | ( | cmemory const & | m | ) | const |
Comparison operator (unequal).
m | cmemory instance for operation |
AND operator.
m | cmemory instance for operation |
eMemInval | thrown when memory areas differ in length |
Append operator with assignment to temporary cmemory instance.
m | cmemory instance for operation |
Append operator with assignment to this cmemory instance.
m | cmemory instance for operation |
bool cmemory::operator< | ( | cmemory const & | m | ) | const |
Less than operator.
m | cmemory instance for operation |
Assignment operator.
m | cmemory instance for operation |
bool cmemory::operator== | ( | cmemory const & | m | ) | const |
Comparison operator.
m | cmemory instance for operation |
bool cmemory::operator> | ( | cmemory const & | m | ) | const |
Less than operator.
m | cmemory instance for operation |
uint8_t & cmemory::operator[] | ( | size_t | index | ) | const |
Index operator.
index | ijndex 0 refers to first byte in memory area |
eMemOutOfRange | thrown when index points beyond end of allocated memory area |
void cmemory::remove | ( | uint8_t * | ptr, |
size_t | len | ||
) |
Removes len bytes starting at pointer ptr from allocated memory area.
ptr | start location of memory block to be removed |
len | length of memory block to be removed |
eMemInval | is thrown when ptr points to a location outside of the allocated memory area |
void cmemory::remove | ( | unsigned int | offset, |
size_t | len | ||
) |
Removes len bytes starting at offset relative to the allocated memory area.
offset | relative start location of memory block to be removed |
len | length of memory block to be removed |
eMemInval | is thrown when the relative start location points to a location outside of the allocated memory area |
|
virtual |
Resizes allocated memory area by calling C-function realloc().
This method resizes the internal memory area and adjusts all internal parameters accordingly, so that any subsequent calls to
len | new length of allocated memory area after resize operation |
eMemAllocFailed | is thrown when the C-function realloc() fails |
Reimplemented in rofl::openflow::coftable_feature_prop_oxm, rofl::openflow::coftable_feature_prop_actions, rofl::openflow::coftable_feature_prop_next_tables, rofl::openflow::coftable_feature_prop_instructions, rofl::cpacket, rofl::openflow::coftable_feature_prop, rofl::openflow::cofhello_elem_versionbitmap, and rofl::openflow::cofhello_elem.