DABC (Data Acquisition Backbone Core)  2.9.9
Buffer.h
Go to the documentation of this file.
1 // $Id: Buffer.h 4506 2020-05-26 07:55:57Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Buffer
17 #define DABC_Buffer
18 
19 #include <cstdint>
20 #include <cstdlib>
21 
22 #ifndef DABC_Object
23 #include "dabc/Object.h"
24 #endif
25 
26 #ifndef DABC_defines
27 #include "dabc/defines.h"
28 #endif
29 
30 namespace dabc {
31 
32  typedef uint32_t BufferSize_t;
33 
34  extern const BufferSize_t BufferSizeError;
35 
36  class Pointer;
37 
38  enum EBufferTypes {
39  mbt_Null = 0,
41  mbt_Int64 = 2,
45  mbt_EOF = 6, // such packet produced when transport is closed completely
46  mbt_EOL = 7, // this is more like end-of-line case, where transport is not closed, but may deliver new kind of data (new event ids) afterwards
47  mbt_User = 1000
48  };
49 
50  class MemoryPool;
51 
53  struct MemSegment {
54  unsigned id;
55  unsigned datasize;
56  void* buffer;
57  void copy_from(MemSegment* src)
58  { id = src->id; datasize = src->datasize; buffer = src->buffer; }
59  };
60 
69  class BufferContainer : public Object {
70  friend class Buffer;
71  friend class MemoryPool;
72 
73  protected:
74  unsigned fTypeId;
75 
76  unsigned fNumSegments;
77  unsigned fCapacity;
78 
80 
82 
84  Object(0, "", flAutoDestroy | flNoMutex),
85  fTypeId(0),
86  fNumSegments(0),
87  fCapacity(0),
88  fPool(),
89  fSegm(0)
90  {
91  #ifdef DABC_EXTRA_CHECKS
92  DebugObject("Buffer", this, 1);
93  #endif
94  }
95 
96  void* operator new(size_t sz) {
97  return malloc(sz);
98  }
99 
100  void* operator new(size_t sz, void* area) {
101  if (area) return area;
102  return malloc(sz);
103  }
104 
105  void operator delete(void* area)
106  {
107  free(area);
108  }
109 
110  public:
111 
112  virtual ~BufferContainer();
113  };
114 
135  class Buffer : public Reference {
136 
137  friend class MemoryPool;
138 
140 
141  protected:
142 
143  void Locate(BufferSize_t p, unsigned& seg_indx, unsigned& seg_shift) const;
144 
145  void AllocateContainer(unsigned capacity);
146 
147  MemoryPool* PoolPtr() const;
148 
149  public:
150 
151  void SetTypeId(unsigned tid) { if (!null()) GetObject()->fTypeId = tid; }
152  inline unsigned GetTypeId() const { return null() ? 0 : GetObject()->fTypeId; }
153 
157  Reference GetPool() const;
158 
160  bool IsPool(const Reference& pool) const { return null() ? false : pool == GetObject()->fPool; }
161 
163  unsigned NumSegments() const { return null() ? 0 : GetObject()->fNumSegments; }
164 
165  MemSegment* Segments() const { return null() ? 0 : GetObject()->fSegm; }
166 
168  inline unsigned SegmentId(unsigned n = 0) const { return GetObject()->fSegm[n].id; }
169 
171  inline void* SegmentPtr(unsigned n = 0) const { return GetObject()->fSegm[n].buffer; }
172 
174  inline unsigned SegmentSize(unsigned n = 0) const { return GetObject()->fSegm[n].datasize; }
175 
177  BufferSize_t GetTotalSize() const;
178 
189  Buffer Duplicate() const;
190 
211  Buffer HandOver() { return Take(); }
212 
213 
216  void MakeEmpty(unsigned capacity = 8) { AllocateContainer(capacity); }
217 
220  void SetTotalSize(BufferSize_t len);
221 
223  void CutFromBegin(BufferSize_t len);
224 
231  bool Append(Buffer& src, bool moverefs = true) throw();
232 
239  bool Prepend(Buffer& src, bool moverefs = true) throw();
240 
248  bool Insert(BufferSize_t pos, Buffer& src, bool moverefs = true);
249 
251  std::string AsStdString();
252 
258  BufferSize_t CopyFrom(const Buffer& srcbuf, BufferSize_t len = 0) throw();
259 
265  BufferSize_t CopyFromStr(const char* src, unsigned len = 0) throw();
266 
272  BufferSize_t CopyTo(void* ptr, BufferSize_t len) const throw();
273 
274 
300  Buffer GetNextPart(Pointer& ptr, BufferSize_t len, bool allowsegmented = true) throw();
301 
304  bool CanSafelyChange() const;
305 
306  // ===================================================================================
307 
310  static Buffer CreateBuffer(BufferSize_t sz) throw();
311 
314  static Buffer CreateBuffer(const void* ptr, unsigned size, bool owner = false, bool makecopy = false) throw();
315  };
316 
317 };
318 
319 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Container for data, referenced by Buffer class.
Definition: Buffer.h:69
MemSegment * fSegm
array of segments
Definition: Buffer.h:81
unsigned fNumSegments
number of entries in segments list
Definition: Buffer.h:76
virtual ~BufferContainer()
Definition: Buffer.cxx:53
unsigned fTypeId
buffer type, identifies content of the buffer
Definition: Buffer.h:74
Reference fPool
reference on the memory pool (or other object, containing memory)
Definition: Buffer.h:79
unsigned fCapacity
capacity of segments list
Definition: Buffer.h:77
Reference on memory from memory pool.
Definition: Buffer.h:135
Buffer Duplicate() const
Duplicates instance of Buffer with new segments list independent from source.
Definition: Buffer.cxx:192
unsigned NumSegments() const
Returns number of segment in buffer.
Definition: Buffer.h:163
bool Insert(BufferSize_t pos, Buffer &src, bool moverefs=true)
Insert content of buffer at specified position.
Definition: Buffer.cxx:225
bool Append(Buffer &src, bool moverefs=true)
Append content of provided buffer.
Definition: Buffer.cxx:215
bool IsPool(const Reference &pool) const
Returns true if buffer produced by the pool provided as reference.
Definition: Buffer.h:160
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
Definition: Buffer.h:174
void AllocateContainer(unsigned capacity)
Definition: Buffer.cxx:462
void MakeEmpty(unsigned capacity=8)
Method produce buffer with empty segments list Such buffer can be used to collect segments from other...
Definition: Buffer.h:216
BufferSize_t CopyTo(void *ptr, BufferSize_t len) const
Copy content into provided raw buffer.
Definition: Buffer.cxx:344
void CutFromBegin(BufferSize_t len)
Remove part of buffer from the beginning.
Definition: Buffer.cxx:139
void SetTotalSize(BufferSize_t len)
Set total length of the buffer to specified value Size cannot be bigger than original size of the buf...
Definition: Buffer.cxx:99
BufferSize_t CopyFrom(const Buffer &srcbuf, BufferSize_t len=0)
Copy content from source buffer.
Definition: Buffer.cxx:333
unsigned GetTypeId() const
Definition: Buffer.h:152
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
MemoryPool * PoolPtr() const
Definition: Buffer.cxx:83
MemSegment * Segments() const
Definition: Buffer.h:165
Buffer GetNextPart(Pointer &ptr, BufferSize_t len, bool allowsegmented=true)
Returns reference on the part of the memory, referenced by the object.
Definition: Buffer.cxx:349
std::string AsStdString()
Convert content of the buffer into std::string.
Definition: Buffer.cxx:317
BufferSize_t CopyFromStr(const char *src, unsigned len=0)
Copy data from string.
Definition: Buffer.cxx:339
bool Prepend(Buffer &src, bool moverefs=true)
Prepend content of provided buffer.
Definition: Buffer.cxx:220
unsigned SegmentId(unsigned n=0) const
Returns id of the segment, no any boundary checks.
Definition: Buffer.h:168
Buffer HandOver()
Method hands over buffer, that source will be emptied at the end.
Definition: Buffer.h:211
bool CanSafelyChange() const
Returns true when user can modify buffer content without any doubts.
Definition: Buffer.cxx:451
Reference GetPool() const
Returns reference on the pool, in user code MemoryPoolRef can be used like dabc::Buffer buf = Recv();...
Definition: Buffer.cxx:78
static Buffer CreateBuffer(BufferSize_t sz)
This static method create independent buffer for any other memory pools Therefore it can be used in s...
Definition: Buffer.cxx:419
void SetTypeId(unsigned tid)
Definition: Buffer.h:151
void Locate(BufferSize_t p, unsigned &seg_indx, unsigned &seg_shift) const
Definition: Buffer.cxx:172
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
Base class for most of the DABC classes.
Definition: Object.h:116
@ flNoMutex
object will be created without mutex, only can be used in constructor
Definition: Object.h:169
@ flAutoDestroy
object will be automatically destroyed when no references exists, normally set in constructor,...
Definition: Object.h:167
Manipulator with dabc::Buffer class.
Definition: Pointer.h:34
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Reference Take()
Copy reference to output object.
Definition: Reference.cxx:102
Event manipulation API.
Definition: api.h:23
const BufferSize_t BufferSizeError
Definition: Buffer.cxx:21
uint32_t BufferSize_t
Definition: Buffer.h:32
EBufferTypes
Definition: Buffer.h:38
@ mbt_RawData
Definition: Buffer.h:44
@ mbt_TymeSync
Definition: Buffer.h:42
@ mbt_EOF
Definition: Buffer.h:45
@ mbt_User
Definition: Buffer.h:47
@ mbt_EOL
Definition: Buffer.h:46
@ mbt_AcknCounter
Definition: Buffer.h:43
@ mbt_Generic
Definition: Buffer.h:40
@ mbt_Null
Definition: Buffer.h:39
@ mbt_Int64
Definition: Buffer.h:41
Structure with descriptor of single memory segment.
Definition: Buffer.h:53
unsigned id
id of the buffer
Definition: Buffer.h:54
void * buffer
pointer on the beginning of buffer (must be in the area of id)
Definition: Buffer.h:56
unsigned datasize
length of data
Definition: Buffer.h:55
void copy_from(MemSegment *src)
Definition: Buffer.h:57