1 #include "daq/storage/EventBuffer.h"
9 unsigned int EventBuffer::size() throw()
11 return sizeof(int) * (m_nword);
14 EventBuffer::EventBuffer(
unsigned int nword)
17 char* buf = (
char*) malloc(size());
22 memset(&m_header, 0,
sizeof(Header));
23 for (
unsigned long long i = 0; i < m_nword; i++) {
28 void EventBuffer::clear()
30 if (m_buf == NULL)
return;
31 memset(&m_header, 0,
sizeof(Header));
32 for (
unsigned long long i = 0; i < m_nword; i++) {
37 EventBuffer::~EventBuffer()
42 bool EventBuffer::isWritable(
int nword)
throw()
44 if (m_buf == NULL)
return false;
45 bool writable = m_header.nword_in - m_header.nword_out < m_nword - (nword + 1);
49 bool EventBuffer::isReadable() throw()
51 if (m_buf == NULL)
return false;
52 bool readable = m_header.nword_in - m_header.nword_out > 0;
56 unsigned int EventBuffer::write(
const int* buf,
unsigned int nword,
59 if (m_buf == NULL)
return 0;
60 if (nword == 0)
return 0;
61 if (nword > m_nword)
return -1;
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)) {
70 unsigned int count = m_nword - i_w;
71 if (nword + 1 < count) {
73 memcpy((m_buf + i_w + 1), buf,
sizeof(
int) * nword);
76 memcpy((m_buf + i_w + 1), buf,
sizeof(
int) * count);
78 memcpy(m_buf, buf + count,
sizeof(
int) * (nword - count));
82 memcpy((m_buf + i_w + 1), buf,
sizeof(
int) * nword);
87 m_header.nword_in += nword + 1;
88 unsigned int count = ++m_header.count_in;
94 if (m_buf == NULL)
return 0;
98 unsigned int nword = 0;
100 i_w = m_header.nword_in % m_nword;
101 i_r = m_header.nword_out % m_nword;
104 if (m_header.nword_in - m_header.nword_out >= (nword + 1)) {
106 memcpy(buf, (m_buf + i_r + 1),
sizeof(
int) * nword);
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);
113 unsigned int count = m_nword - i_r;
114 memcpy(buf, (m_buf + i_r + 1),
sizeof(
int) * count);
116 memcpy(buf + count, m_buf,
sizeof(
int) * (nword - count));
124 m_header.nword_out += nword + 1;
125 unsigned int count = ++m_header.count_out;