DABC (Data Acquisition Backbone Core)  2.9.9
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends
dabc::Buffer Class Reference

Reference on memory from memory pool. More...

#include <dabc/Buffer.h>

Inheritance diagram for dabc::Buffer:
dabc::Reference

Public Member Functions

bool Append (Buffer &src, bool moverefs=true) throw ()
 Append content of provided buffer. More...
 
std::string AsStdString ()
 Convert content of the buffer into std::string. More...
 
bool CanSafelyChange () const
 Returns true when user can modify buffer content without any doubts. More...
 
BufferSize_t CopyFrom (const Buffer &srcbuf, BufferSize_t len=0) throw ()
 Copy content from source buffer. More...
 
BufferSize_t CopyFromStr (const char *src, unsigned len=0) throw ()
 Copy data from string. More...
 
BufferSize_t CopyTo (void *ptr, BufferSize_t len) const throw ()
 Copy content into provided raw buffer. More...
 
void CutFromBegin (BufferSize_t len)
 Remove part of buffer from the beginning. More...
 
Buffer Duplicate () const
 Duplicates instance of Buffer with new segments list independent from source. More...
 
Buffer GetNextPart (Pointer &ptr, BufferSize_t len, bool allowsegmented=true) throw ()
 Returns reference on the part of the memory, referenced by the object. More...
 
Reference GetPool () const
 Returns reference on the pool, in user code MemoryPoolRef can be used like dabc::Buffer buf = Recv(); dabc::MemoryPoolRef pool = buf.GetPool();. More...
 
BufferSize_t GetTotalSize () const
 Return total size of all buffer segments. More...
 
unsigned GetTypeId () const
 
Buffer HandOver ()
 Method hands over buffer, that source will be emptied at the end. More...
 
bool Insert (BufferSize_t pos, Buffer &src, bool moverefs=true)
 Insert content of buffer at specified position. More...
 
bool IsPool (const Reference &pool) const
 Returns true if buffer produced by the pool provided as reference. More...
 
void MakeEmpty (unsigned capacity=8)
 Method produce buffer with empty segments list Such buffer can be used to collect segments from other buffers without disturbing other bufs. More...
 
unsigned NumSegments () const
 Returns number of segment in buffer. More...
 
bool Prepend (Buffer &src, bool moverefs=true) throw ()
 Prepend content of provided buffer. More...
 
unsigned SegmentId (unsigned n=0) const
 Returns id of the segment, no any boundary checks. More...
 
void * SegmentPtr (unsigned n=0) const
 Returns pointer on the segment, no any boundary checks. More...
 
MemSegmentSegments () const
 
unsigned SegmentSize (unsigned n=0) const
 Returns size on the segment, no any boundary checks. More...
 
void SetTotalSize (BufferSize_t len)
 Set total length of the buffer to specified value Size cannot be bigger than original size of the buffer. More...
 
void SetTypeId (unsigned tid)
 
- Public Member Functions inherited from dabc::Reference
bool AddChild (Object *obj)
 Add child to list of object children. More...
 
const char * ClassName () const
 Return class name of referenced object, if object not assigned, returns "---". More...
 
void Destroy () throw ()
 Release reference and starts destroyment of referenced object. More...
 
Reference FindChild (const char *name) const
 Searches for child in referenced object. More...
 
bool GetAllChildRef (ReferencesVector *vect) const
 Return references for all childs. More...
 
Reference GetChild (unsigned n) const
 Return reference on child n. More...
 
Reference GetFolder (const std::string &name, bool force=false) throw ()
 Return folder of specified name, no special symbols are allowed. More...
 
const char * GetName () const
 Return name of referenced object, if object not assigned, returns "---". More...
 
ObjectGetObject () const
 Return pointer on the object. More...
 
ObjectGetParent () const
 Returns pointer on parent object. More...
 
Reference GetParentRef () const
 Returns reference on parent object. More...
 
bool IsName (const char *name) const
 Returns true if object name is the same as specified one. More...
 
std::string ItemName (bool compact=true) const
 Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call. More...
 
bool null () const
 Returns true if reference contains nullptr. More...
 
unsigned NumChilds () const
 Return number of childs in referenced object. More...
 
unsigned NumReferences () const
 Returns number of references on the object. More...
 
bool operator! () const
 Returns true if reference contains nullptr. More...
 
bool operator!= (const Reference &src) const
 Compare operator - return true if references refer to different object. More...
 
bool operator!= (Object *obj) const
 Compare operator - return true if reference refer to different objects. More...
 
Objectoperator() () const
 Return pointer on the object. More...
 
Referenceoperator<< (Reference &src) throw ()
 Move operator - reference moved from source to target. More...
 
Referenceoperator= (const Reference &src) throw ()
 Assignment operator - copy reference. More...
 
Referenceoperator= (Object *obj) throw ()
 Assignment operator - copy reference. More...
 
bool operator== (const Reference &src) const
 Compare operator - return true if references refer to same object. More...
 
bool operator== (Object *obj) const
 Compare operator - return true if reference refer to same object. More...
 
void Print (int lvl=0, const char *from=0) const
 Show on debug output content of reference. More...
 
 Reference (const Reference &src) throw ()
 Copy constructor, if source is transient than source reference will be emptied. More...
 
 Reference (Object *obj=nullptr)
 Constructor, creates reference on the object. More...
 
std::string RelativeName (const dabc::Reference &topitem)
 Produce name, which can be used to find item, calling topitem.FindChild(). More...
 
void Release () throw ()
 Releases reference on the object. More...
 
bool RemoveChild (const char *name, bool cleanup=true)
 Remove child with given name and return reference on that child. More...
 
bool RemoveChilds (bool cleanup=true)
 Remove all childs in referenced object If cleanup true (default) and object is owner, all objects will be destroyed. More...
 
void SetAutoDestroy (bool on=true)
 Set autodestroy flag for the object Once enabled, object will be destroyed when last reference will be cleared. More...
 
void SetObject (Object *obj, bool withmutex=true)
 Direct set of object to reference. More...
 
Reference Take ()
 Copy reference to output object. More...
 
virtual ~Reference ()
 Destructor, releases reference on the object. More...
 

Static Public Member Functions

static Buffer CreateBuffer (BufferSize_t sz) throw ()
 This static method create independent buffer for any other memory pools Therefore it can be used in standalone case. More...
 
static Buffer CreateBuffer (const void *ptr, unsigned size, bool owner=false, bool makecopy=false) throw ()
 This static method create Buffer instance, which contains pointer on specified peace of memory Therefore it can be used in standalone case. More...
 

Protected Member Functions

void AllocateContainer (unsigned capacity)
 
void Locate (BufferSize_t p, unsigned &seg_indx, unsigned &seg_shift) const
 
MemoryPoolPoolPtr () const
 
- Protected Member Functions inherited from dabc::Reference
bool AcquireRefWithoutMutex (Reference &ref)
 Special method, which allows to generate new reference when object mutex is locked. More...
 
void Assign (const Reference &src)
 Method used in copy constructor and assigned operations. More...
 
MutexObjectMutex () const
 
template<class T >
bool verify_object (Object *src, T *&tgt)
 Method used in reference constructor/assignments to verify is object is suitable. More...
 

Friends

class MemoryPool
 

Additional Inherited Members

- Protected Attributes inherited from dabc::Reference
ObjectfObj
 pointer on the object More...
 

Detailed Description

Reference on memory from memory pool.

Central class for memory usage concept in DABC framework.

All memory for data transfer should be pre-allocated in dabc::MemoryPool. Buffer class provides access to that memory.

Main concepts:

Actual class description need to be done.

Definition at line 135 of file Buffer.h.

Member Function Documentation

◆ Locate()

void dabc::Buffer::Locate ( BufferSize_t  p,
unsigned &  seg_indx,
unsigned &  seg_shift 
) const
protected

Definition at line 172 of file Buffer.cxx.

◆ AllocateContainer()

void dabc::Buffer::AllocateContainer ( unsigned  capacity)
protected

Definition at line 462 of file Buffer.cxx.

◆ PoolPtr()

dabc::MemoryPool * dabc::Buffer::PoolPtr ( ) const
protected

Definition at line 83 of file Buffer.cxx.

◆ SetTypeId()

void dabc::Buffer::SetTypeId ( unsigned  tid)
inline

Definition at line 151 of file Buffer.h.

◆ GetTypeId()

unsigned dabc::Buffer::GetTypeId ( ) const
inline

Definition at line 152 of file Buffer.h.

◆ GetPool()

dabc::Reference dabc::Buffer::GetPool ( ) const

Returns reference on the pool, in user code MemoryPoolRef can be used like dabc::Buffer buf = Recv(); dabc::MemoryPoolRef pool = buf.GetPool();.

Definition at line 78 of file Buffer.cxx.

◆ IsPool()

bool dabc::Buffer::IsPool ( const Reference pool) const
inline

Returns true if buffer produced by the pool provided as reference.

Definition at line 160 of file Buffer.h.

◆ NumSegments()

unsigned dabc::Buffer::NumSegments ( ) const
inline

Returns number of segment in buffer.

Definition at line 163 of file Buffer.h.

◆ Segments()

MemSegment* dabc::Buffer::Segments ( ) const
inline

Definition at line 165 of file Buffer.h.

◆ SegmentId()

unsigned dabc::Buffer::SegmentId ( unsigned  n = 0) const
inline

Returns id of the segment, no any boundary checks.

Definition at line 168 of file Buffer.h.

◆ SegmentPtr()

void* dabc::Buffer::SegmentPtr ( unsigned  n = 0) const
inline

Returns pointer on the segment, no any boundary checks.

Definition at line 171 of file Buffer.h.

◆ SegmentSize()

unsigned dabc::Buffer::SegmentSize ( unsigned  n = 0) const
inline

Returns size on the segment, no any boundary checks.

Definition at line 174 of file Buffer.h.

◆ GetTotalSize()

dabc::BufferSize_t dabc::Buffer::GetTotalSize ( ) const

Return total size of all buffer segments.

Definition at line 91 of file Buffer.cxx.

◆ Duplicate()

dabc::Buffer dabc::Buffer::Duplicate ( ) const

Duplicates instance of Buffer with new segments list independent from source.

Means original instance will remain as is. Memory which is referenced by Buffer object is not duplicated or copied, means both instances are reference same memory regions. This method can be time consuming, while pool mutex should be locked and reference counter of all segments should be incremented. When created instance destroyed, same operation in reverse order will be performed. Method must be used when new instance should be used in other thread while initial instance Do not duplicate buffer without real need for that.

Definition at line 192 of file Buffer.cxx.

◆ HandOver()

Buffer dabc::Buffer::HandOver ( )
inline

Method hands over buffer, that source will be emptied at the end.

This method creates temporary object which could be deliver to the any other method as argument. At the same time original object forgets buffer and will be empty at the end of the call. Typical place where it should be used is return method when source want to forget buffer itself:

dabc::Buffer MyClass::GetLast() { fLastBuf.SetTotalSize(1024); fLastBuf.SetTypeId(129); return fLastBuf.HandOver(); }

If one wants to keep reference on the memory after such call one should do: ... return fLastBuf; ...

Definition at line 211 of file Buffer.h.

◆ MakeEmpty()

void dabc::Buffer::MakeEmpty ( unsigned  capacity = 8)
inline

Method produce buffer with empty segments list Such buffer can be used to collect segments from other buffers without disturbing other bufs.

Definition at line 216 of file Buffer.h.

◆ SetTotalSize()

void dabc::Buffer::SetTotalSize ( BufferSize_t  len)

Set total length of the buffer to specified value Size cannot be bigger than original size of the buffer.

Definition at line 99 of file Buffer.cxx.

◆ CutFromBegin()

void dabc::Buffer::CutFromBegin ( BufferSize_t  len)

Remove part of buffer from the beginning.

Definition at line 139 of file Buffer.cxx.

◆ Append()

bool dabc::Buffer::Append ( Buffer src,
bool  moverefs = true 
)
throw (
)

Append content of provided buffer.

If source buffer belong to other memory pool, content will be copied.

Parameters
[in]srcbuffer to append
[in]moverefsif true, references moved to the target and source buffer will be emptied
Returns
false if operation failed, otherwise true

Definition at line 215 of file Buffer.cxx.

◆ Prepend()

bool dabc::Buffer::Prepend ( Buffer src,
bool  moverefs = true 
)
throw (
)

Prepend content of provided buffer.

If source buffer belong to other memory pool, content will be copied.

Parameters
[in]srcbuffer to prepend
[in]moverefsif true, references moved to the target and source buffer will be emptied
Returns
false if operation failed, otherwise true

Definition at line 220 of file Buffer.cxx.

◆ Insert()

bool dabc::Buffer::Insert ( BufferSize_t  pos,
Buffer src,
bool  moverefs = true 
)

Insert content of buffer at specified position.

If source buffer belong to other memory pool, content will be copied.

Parameters
[in]posposition at which buffer must be inserted
[in]srcbuffer to insert
[in]moverefsif true, references moved to the target and source buffer will be emptied
Returns
false if operation failed, otherwise true

Definition at line 225 of file Buffer.cxx.

◆ AsStdString()

std::string dabc::Buffer::AsStdString ( )

Convert content of the buffer into std::string.

Definition at line 317 of file Buffer.cxx.

◆ CopyFrom()

dabc::BufferSize_t dabc::Buffer::CopyFrom ( const Buffer srcbuf,
BufferSize_t  len = 0 
)
throw (
)

Copy content from source buffer.

Parameters
[in]srcbufsource buffer
[in]lenbytes to copy, if 0 - whole buffer
Returns
actual number of bytes copied

Definition at line 333 of file Buffer.cxx.

◆ CopyFromStr()

dabc::BufferSize_t dabc::Buffer::CopyFromStr ( const char *  src,
unsigned  len = 0 
)
throw (
)

Copy data from string.

Parameters
[in]srcsource string
[in]lenbytes copied, (if 0, complete string excluding trailing null)
Returns
actual number of bytes copied

Definition at line 339 of file Buffer.cxx.

◆ CopyTo()

dabc::BufferSize_t dabc::Buffer::CopyTo ( void *  ptr,
BufferSize_t  len 
) const
throw (
)

Copy content into provided raw buffer.

Parameters
[in]ptrpointer to destination memory
[in]lennumber of bytes to copy.
Returns
actual number of bytes copied

Definition at line 344 of file Buffer.cxx.

◆ GetNextPart()

dabc::Buffer dabc::Buffer::GetNextPart ( Pointer ptr,
BufferSize_t  len,
bool  allowsegmented = true 
)
throw (
)

Returns reference on the part of the memory, referenced by the object.

Parameters
[in,out]ptrpointer where next part will be extracted
[in]lenlength of memory piece (0 - rest size of current segment will be delivered).
[in]allowsegmenteddefines if return memory peace could be segmented or not.
Returns
reference of buffer part or empty buffer when requested memory size is not available in the source buffer

This method could be used to manage small peaces of memory in the memory pool with big buffers. For instance, one need some small headers which should managed separately from the main data. Than one need to take big buffer from the pool and extract by small pieces:

// somewhere in the initialization Buffer buf = Pool()->TakeBuffer(64000);

// later in the code Buffer data = Recv(); Buffer hdr = buf.GetNextPart(16) data.Prepend(hdr); Send(data);

Of course, one should check if buffer is expired and one need to take next piece from the memory pool.

TODO: one can exclude pointer and just cut buffer pieces until buffer is empty

Definition at line 349 of file Buffer.cxx.

◆ CanSafelyChange()

bool dabc::Buffer::CanSafelyChange ( ) const

Returns true when user can modify buffer content without any doubts.

It happens when refcnt==1 for each memory segments

Definition at line 451 of file Buffer.cxx.

◆ CreateBuffer() [1/2]

dabc::Buffer dabc::Buffer::CreateBuffer ( BufferSize_t  sz)
throw (
)
static

This static method create independent buffer for any other memory pools Therefore it can be used in standalone case.

Definition at line 419 of file Buffer.cxx.

◆ CreateBuffer() [2/2]

dabc::Buffer dabc::Buffer::CreateBuffer ( const void *  ptr,
unsigned  size,
bool  owner = false,
bool  makecopy = false 
)
throw (
)
static

This static method create Buffer instance, which contains pointer on specified peace of memory Therefore it can be used in standalone case.

Definition at line 424 of file Buffer.cxx.

Friends And Related Function Documentation

◆ MemoryPool

friend class MemoryPool
friend

Definition at line 137 of file Buffer.h.


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