Belle II Software  release-05-01-25
EventBuffer.cc
1 #include "daq/storage/EventBuffer.h"
2 
3 #include <cstdio>
4 #include <cstdlib>
5 #include <cstring>
6 
7 using namespace Belle2;
8 
9 unsigned int EventBuffer::size() throw()
10 {
11  return sizeof(int) * (m_nword);
12 }
13 
14 EventBuffer::EventBuffer(unsigned int nword)
15 {
16  m_nword = nword;
17  char* buf = (char*) malloc(size());
18  if (buf == NULL) {
19  return;
20  }
21  m_buf = (int*)buf;
22  memset(&m_header, 0, sizeof(Header));
23  for (unsigned long long i = 0; i < m_nword; i++) {
24  m_buf[i] = 0;
25  }
26 }
27 
28 void EventBuffer::clear()
29 {
30  if (m_buf == NULL) return;
31  memset(&m_header, 0, sizeof(Header));
32  for (unsigned long long i = 0; i < m_nword; i++) {
33  m_buf[i] = 0;
34  }
35 }
36 
37 EventBuffer::~EventBuffer()
38 {
39  free(m_buf);
40 }
41 
42 bool EventBuffer::isWritable(int nword) throw()
43 {
44  if (m_buf == NULL) return false;
45  bool writable = m_header.nword_in - m_header.nword_out < m_nword - (nword + 1);
46  return writable;
47 }
48 
49 bool EventBuffer::isReadable() throw()
50 {
51  if (m_buf == NULL) return false;
52  bool readable = m_header.nword_in - m_header.nword_out > 0;
53  return readable;
54 }
55 
56 unsigned int EventBuffer::write(const int* buf, unsigned int nword,
57  unsigned int serial)
58 {
59  if (m_buf == NULL) return 0;
60  if (nword == 0) return 0;
61  if (nword > m_nword) return -1;
62  unsigned int i_w = 0;
63  unsigned int i_r = 0;
64  while (true) {
65  i_w = m_header.nword_in % m_nword;
66  i_r = m_header.nword_out % m_nword;
67  if ((serial == 0 || serial - 1 == m_header.count_in) &&
68  m_header.nword_in - m_header.nword_out < m_nword - (nword + 1)) {
69  if (i_w >= i_r) {
70  unsigned int count = m_nword - i_w;
71  if (nword + 1 < count) {
72  m_buf[i_w] = nword;
73  memcpy((m_buf + i_w + 1), buf, sizeof(int) * nword);
74  } else {
75  m_buf[i_w] = nword;
76  memcpy((m_buf + i_w + 1), buf, sizeof(int) * count);
77  if (nword >= count)
78  memcpy(m_buf, buf + count, sizeof(int) * (nword - count));
79  }
80  } else {
81  m_buf[i_w] = nword;
82  memcpy((m_buf + i_w + 1), buf, sizeof(int) * nword);
83  }
84  break;
85  }
86  }
87  m_header.nword_in += nword + 1;
88  unsigned int count = ++m_header.count_in;
89  return count;
90 }
91 
92 unsigned int EventBuffer::read(int* buf, EventBuffer::Header* hdr)
93 {
94  if (m_buf == NULL) return 0;
95  m_header.nreader++;
96  unsigned int i_w = 0;
97  unsigned int i_r = 0;
98  unsigned int nword = 0;
99  while (true) {
100  i_w = m_header.nword_in % m_nword;
101  i_r = m_header.nword_out % m_nword;
102  nword = m_buf[i_r];
103  if (nword > 0) {
104  if (m_header.nword_in - m_header.nword_out >= (nword + 1)) {
105  if (i_w > i_r) {
106  memcpy(buf, (m_buf + i_r + 1), sizeof(int) * nword);
107  break;
108  } else if (i_w < i_r) {
109  if (m_nword - i_r > nword) {
110  memcpy(buf, (m_buf + i_r + 1), sizeof(int) * nword);
111  break;
112  } else {
113  unsigned int count = m_nword - i_r;
114  memcpy(buf, (m_buf + i_r + 1), sizeof(int) * count);
115  if (nword > count) {
116  memcpy(buf + count, m_buf, sizeof(int) * (nword - count));
117  }
118  break;
119  }
120  }
121  }
122  }
123  }
124  m_header.nword_out += nword + 1;
125  unsigned int count = ++m_header.count_out;
126  m_header.nreader--;
127  if (hdr != NULL) {
128  memcpy(hdr, &m_header, sizeof(EventBuffer::Header));
129  }
130  return count;
131 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EventBuffer::Header
Definition: EventBuffer.h:13