Belle II Software release-09-00-00
EventBuffer Class Reference
Collaboration diagram for EventBuffer:

Classes

struct  Header
 

Public Member Functions

 EventBuffer (unsigned int nword)
 
unsigned int size () throw ()
 
bool init ()
 
void clear ()
 
HeadergetHeader () throw ()
 
int * getBuffer () throw ()
 
bool isWritable (int nword) throw ()
 
bool isReadable () throw ()
 
unsigned int write (const int *buf, unsigned int nword, unsigned int serial=0)
 
unsigned int read (int *buf, Header *hdr=0)
 

Private Attributes

Header m_header
 
int * m_buf
 
unsigned int m_nword
 

Detailed Description

Definition at line 17 of file EventBuffer.h.

Constructor & Destructor Documentation

◆ EventBuffer() [1/2]

EventBuffer ( )
inline

Definition at line 33 of file EventBuffer.h.

34 {
35 m_buf = 0;
36 m_nword = 0;
37 }

◆ EventBuffer() [2/2]

EventBuffer ( unsigned int  nword)

Definition at line 21 of file EventBuffer.cc.

22{
23 m_nword = nword;
24 char* buf = (char*) malloc(size());
25 if (buf == NULL) {
26 return;
27 }
28 m_buf = (int*)buf;
29 memset(&m_header, 0, sizeof(Header));
30 for (unsigned long long i = 0; i < m_nword; i++) {
31 m_buf[i] = 0;
32 }
33}

◆ ~EventBuffer()

Definition at line 44 of file EventBuffer.cc.

45{
46 free(m_buf);
47}

Member Function Documentation

◆ clear()

void clear ( )

Definition at line 35 of file EventBuffer.cc.

36{
37 if (m_buf == NULL) return;
38 memset(&m_header, 0, sizeof(Header));
39 for (unsigned long long i = 0; i < m_nword; i++) {
40 m_buf[i] = 0;
41 }
42}

◆ getBuffer()

int * getBuffer ( )
throw (
)
inline

Definition at line 48 of file EventBuffer.h.

48{ return m_buf; }

◆ getHeader()

Header & getHeader ( )
throw (
)
inline

Definition at line 47 of file EventBuffer.h.

47{ return m_header; }

◆ isReadable()

bool isReadable ( )
throw (
)

Definition at line 56 of file EventBuffer.cc.

57{
58 if (m_buf == NULL) return false;
59 bool readable = m_header.nword_in - m_header.nword_out > 0;
60 return readable;
61}

◆ isWritable()

bool isWritable ( int  nword)
throw (
)

Definition at line 49 of file EventBuffer.cc.

50{
51 if (m_buf == NULL) return false;
52 bool writable = m_header.nword_in - m_header.nword_out < m_nword - (nword + 1);
53 return writable;
54}

◆ read()

unsigned int read ( int *  buf,
EventBuffer::Header hdr = 0 
)

Definition at line 98 of file EventBuffer.cc.

99{
100 if (m_buf == NULL) return 0;
101 m_header.nreader++;
102 unsigned int nword = 0;
103 while (true) {
104 unsigned int i_w = m_header.nword_in % m_nword;
105 unsigned int i_r = m_header.nword_out % m_nword;
106 nword = m_buf[i_r];
107 if (nword > 0) {
108 if (m_header.nword_in - m_header.nword_out >= (nword + 1)) {
109 if (i_w > i_r) {
110 memcpy(buf, (m_buf + i_r + 1), sizeof(int) * nword);
111 break;
112 } else if (i_w < i_r) {
113 if (m_nword - i_r > nword) {
114 memcpy(buf, (m_buf + i_r + 1), sizeof(int) * nword);
115 break;
116 } else {
117 unsigned int count = m_nword - i_r;
118 memcpy(buf, (m_buf + i_r + 1), sizeof(int) * count);
119 if (nword > count) {
120 memcpy(buf + count, m_buf, sizeof(int) * (nword - count));
121 }
122 break;
123 }
124 }
125 }
126 }
127 }
128 m_header.nword_out += nword + 1;
129 unsigned int count = ++m_header.count_out;
130 m_header.nreader--;
131 if (hdr != NULL) {
132 memcpy(hdr, &m_header, sizeof(EventBuffer::Header));
133 }
134 return count;
135}

◆ size()

unsigned int size ( )
throw (
)

Definition at line 16 of file EventBuffer.cc.

17{
18 return sizeof(int) * (m_nword);
19}

◆ write()

unsigned int write ( const int *  buf,
unsigned int  nword,
unsigned int  serial = 0 
)

Definition at line 63 of file EventBuffer.cc.

65{
66 if (m_buf == NULL) return 0;
67 if (nword == 0) return 0;
68 if (nword > m_nword) return -1;
69
70 while (true) {
71 unsigned int i_w = m_header.nword_in % m_nword;
72 unsigned int i_r = m_header.nword_out % m_nword;
73 if ((serial == 0 || serial - 1 == m_header.count_in) &&
74 m_header.nword_in - m_header.nword_out < m_nword - (nword + 1)) {
75 if (i_w >= i_r) {
76 unsigned int count = m_nword - i_w;
77 if (nword + 1 < count) {
78 m_buf[i_w] = nword;
79 memcpy((m_buf + i_w + 1), buf, sizeof(int) * nword);
80 } else {
81 m_buf[i_w] = nword;
82 memcpy((m_buf + i_w + 1), buf, sizeof(int) * count);
83 if (nword >= count)
84 memcpy(m_buf, buf + count, sizeof(int) * (nword - count));
85 }
86 } else {
87 m_buf[i_w] = nword;
88 memcpy((m_buf + i_w + 1), buf, sizeof(int) * nword);
89 }
90 break;
91 }
92 }
93 m_header.nword_in += nword + 1;
94 unsigned int count = ++m_header.count_in;
95 return count;
96}

Member Data Documentation

◆ m_buf

int* m_buf
private

Definition at line 57 of file EventBuffer.h.

◆ m_header

Header m_header
private

Definition at line 56 of file EventBuffer.h.

◆ m_nword

unsigned int m_nword
private

Definition at line 58 of file EventBuffer.h.


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