DABC (Data Acquisition Backbone Core)  2.9.9
Pointer.cxx
Go to the documentation of this file.
1 // $Id: Pointer.cxx 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 #include "dabc/Pointer.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 
21 #include "dabc/logging.h"
22 #include "dabc/Exception.h"
23 
24 
26 {
27  if (fFullSize == len) {
28  fPtr += len;
29  fFullSize = 0;
30  fRawSize = 0;
31  return;
32  }
33 
34  fFullSize -= rawsize();
35  len -= rawsize();
36  fRawSize = 0;
37  fPtr = 0;
38  fSegm++;
39 
40  while ((fSegm < fBuf.NumSegments()) && (fBuf.SegmentSize(fSegm) < len)) {
41  len -= fBuf.SegmentSize(fSegm);
43  fSegm++;
44  }
45 
46  if (fSegm >= fBuf.NumSegments())
47  throw dabc::Exception(ex_Pointer, "Pointer has invalid full length field", "Pointer");
48 
49  fPtr = (unsigned char*) fBuf.SegmentPtr(fSegm) + len;
50  fRawSize = fBuf.SegmentSize(fSegm) - len;
51  fFullSize -= len;
52 }
53 
55 {
56  if (sz==0) sz = src.fullsize();
57 
58  // this is the simplest case - just raw copy of original data
59  if ((sz <= rawsize()) && (sz <= src.rawsize())) {
60  ::memcpy(ptr(), src(), sz);
61  return sz;
62  }
63 
64  if (sz > src.fullsize()) {
65  EOUT("source pointer has no so much memory %u", (unsigned) sz);
66  sz = src.fullsize();
67  }
68 
69  if (sz > fullsize()) {
70  EOUT("target pointer has no so much memory %u", (unsigned) sz);
71  sz = fullsize();
72  }
73 
74  Pointer tgtptr(*this), srcptr(src);
75  unsigned res(0);
76 
77  while (sz>0) {
78  unsigned copylen = (tgtptr.rawsize() < srcptr.rawsize()) ? tgtptr.rawsize() : srcptr.rawsize();
79  if (copylen>sz) copylen = sz;
80  if (copylen==0) break;
81 
82  ::memcpy(tgtptr(), srcptr(), copylen);
83 
84  res+=copylen;
85  sz-=copylen;
86  tgtptr.shift(copylen);
87  srcptr.shift(copylen);
88  }
89 
90  return res;
91 }
92 
94 {
95  return src.copyfrom(*this, sz ? sz : fullsize());
96 }
97 
99 {
100  if (sz==0) return 0;
101 
102  if (sz <= rawsize()) {
103  ::memcpy(tgt, ptr(), sz);
104  return sz;
105  }
106 
107  return Pointer(tgt, sz).copyfrom(*this, sz);
108 }
109 
111 {
112  if (!src) return 0;
113 
114  if (sz > fullsize()) {
115  EOUT("target pointer has no so much memory %lu", sz);
116  sz = fullsize();
117  }
118 
119  if (sz==0) return sz;
120 
121  // this is special and simple case, treat is separately
122  if (sz > rawsize())
123  throw dabc::Exception(ex_Pointer,"Cannot copy memory outside current segment, use Buffer::copyfrom() method", "Pointer");
124 
125  ::memcpy(ptr(), src, sz);
126  return sz;
127 }
128 
129 dabc::BufferSize_t dabc::Pointer::copyfromstr(const char* str, unsigned len) throw()
130 {
131  return str ? copyfrom(str, len ? len : strlen(str)) : 0;
132 }
133 
134 int dabc::Pointer::distance_to(const Pointer& child) const
135 {
136  if (fBuf != child.fBuf)
137  throw dabc::Exception(ex_Pointer,"Pointer with wrong segment id is specified", "Pointer");
138 
139  if (fSegm==child.fSegm)
140  return (fPtr <= child.fPtr) ? (child.fPtr - fPtr) : -(fPtr - child.fPtr);
141 
142  if (fSegm > child.fSegm) return -child.distance_to(*this);
143 
144  Pointer left(*this);
145  BufferSize_t dist(0);
146 
147  while (left.fSegm < child.fSegm) {
148  dist += left.rawsize();
149  left.shift(left.rawsize());
150  }
151 
152  return dist + (child.fPtr - left.fPtr);
153 }
154 
156 {
157  BufferSize_t bufsz = fBuf.GetTotalSize();
158  return (bufsz == 0) ? 0. : 1. * distance_to_ownbuf() / bufsz;
159 }
160 
unsigned NumSegments() const
Returns number of segment in buffer.
Definition: Buffer.h:163
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
Definition: Buffer.h:174
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Definition: Buffer.h:171
DABC exception.
Definition: Exception.h:57
Manipulator with dabc::Buffer class.
Definition: Pointer.h:34
BufferSize_t shift(BufferSize_t sz)
Definition: Pointer.h:153
BufferSize_t fRawSize
size of contiguous memory, pointed by fPtr
Definition: Pointer.h:42
int distance_to(const Pointer &child) const
Definition: Pointer.cxx:134
unsigned char * fPtr
pointer on the raw buffer
Definition: Pointer.h:41
BufferSize_t copyfrom(const Pointer &src, BufferSize_t sz=0)
Returns actual size copied.
Definition: Pointer.cxx:54
BufferSize_t copyto(void *tgt, BufferSize_t sz) const
Definition: Pointer.cxx:98
unsigned fSegm
segment id
Definition: Pointer.h:40
void long_shift(BufferSize_t sz)
Definition: Pointer.cxx:25
BufferSize_t copyfromstr(const char *str, unsigned len=0)
Definition: Pointer.cxx:129
float consumed_size() const
Returns ratio between filled size and buffer size, 0 when exmpy.
Definition: Pointer.cxx:155
BufferSize_t rawsize() const
Definition: Pointer.h:148
Buffer fBuf
we keep reference on the buffer, could be only used in same thread
Definition: Pointer.h:39
BufferSize_t fFullSize
full size of memory from pointer till the end
Definition: Pointer.h:43
#define EOUT(args ...)
Definition: logging.h:150
uint32_t BufferSize_t
Definition: Buffer.h:32
@ ex_Pointer
Definition: Exception.h:40