Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
Public Member Functions | Friends | List of all members
rofl::cmemory Class Reference

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.
cmemoryoperator= (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...
 
cmemoryoperator+= (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)
 

Detailed Description

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.

Inheritance diagram for rofl::cmemory:
rofl::caddress rofl::cpacket rofl::crandom rofl::csockaddr rofl::gtpuext rofl::openflow::cofhello_elem rofl::openflow::cofport rofl::openflow::coftable_feature_prop rofl::openflow::coftable_features rofl::openflow::coxmatch rofl::openflow::experimental::capwap::cofaction_exp_body_capwap rofl::openflow::experimental::gre::cofaction_exp_body_gre rofl::openflow::experimental::gtp::cofaction_exp_body_gtp rofl::openflow::experimental::pppoe::cofaction_exp_body_pppoe rofl::openflow::experimental::wlan::cofaction_exp_body_wlan

Constructor & Destructor Documentation

cmemory::cmemory ( size_t  len = CMEMORY_DEFAULT_SIZE)

Constructor. Allocates a new memory area with specified size (default: 1024 bytes).

Parameters
lenlength 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.

Parameters
datapointer to start of memory area to be cloned
datalenlength of memory area to be cloned
cmemory::cmemory ( cmemory const &  m)

Copy constructor.

Parameters
mcmemory instance

Member Function Documentation

void cmemory::assign ( uint8_t *  buf,
size_t  buflen 
)
virtual

Overwrites memory area with plain buffer specified. Resizes internal memory as necessary.

Parameters
bufpointer to start of buffer to be copied
buflenlength 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".

Parameters
valuesought value
startrelative begin within allocated memory area for search
Exceptions
eMemNotFoundis 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.

Parameters
ptrstart location of new inserted memory block
lennumber of bytes inserted in the allocated memory area
Exceptions
eMemInvalis 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.

Parameters
offsetrelative start location of new inserted memory block
lennumber of bytes inserted in the allocated memory area
Exceptions
eMemInvalis thrown when offset refers to a location outside of the allocated memory area
bool cmemory::operator!= ( cmemory const &  m) const

Comparison operator (unequal).

Parameters
mcmemory instance for operation
cmemory cmemory::operator& ( cmemory const &  m) const

AND operator.

Parameters
mcmemory instance for operation
Exceptions
eMemInvalthrown when memory areas differ in length
cmemory cmemory::operator+ ( cmemory const &  m)

Append operator with assignment to temporary cmemory instance.

Parameters
mcmemory instance for operation
cmemory & cmemory::operator+= ( cmemory const &  m)

Append operator with assignment to this cmemory instance.

Parameters
mcmemory instance for operation
bool cmemory::operator< ( cmemory const &  m) const

Less than operator.

Parameters
mcmemory instance for operation
cmemory & cmemory::operator= ( cmemory const &  m)

Assignment operator.

Parameters
mcmemory instance for operation
bool cmemory::operator== ( cmemory const &  m) const

Comparison operator.

Parameters
mcmemory instance for operation
bool cmemory::operator> ( cmemory const &  m) const

Less than operator.

Parameters
mcmemory instance for operation
uint8_t & cmemory::operator[] ( size_t  index) const

Index operator.

Parameters
indexijndex 0 refers to first byte in memory area
Returns
reference to byte value as uint8_t
Exceptions
eMemOutOfRangethrown 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.

Parameters
ptrstart location of memory block to be removed
lenlength of memory block to be removed
Exceptions
eMemInvalis 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.

Parameters
offsetrelative start location of memory block to be removed
lenlength of memory block to be removed
Exceptions
eMemInvalis thrown when the relative start location points to a location outside of the allocated memory area
uint8_t * cmemory::resize ( size_t  len)
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

See Also
somem() will yield valid results.
Parameters
lennew length of allocated memory area after resize operation
Exceptions
eMemAllocFailedis 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.


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