Belle II Software  release-08-01-10
Buffer.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include "daq/slc/system/Buffer.h"
9 
10 #include <cstring>
11 
12 using namespace Belle2;
13 
14 Buffer::Buffer() : m_memory(NULL), m_size(0), m_allocated(false) {}
15 
16 Buffer::Buffer(unsigned int size, unsigned char* memory)
17  : m_memory(memory), m_size(size), m_allocated(false)
18 {
19  if (memory == NULL && size > 0) {
20  m_memory = new unsigned char[size];
21  ::memset(m_memory, 0, size);
22  m_allocated = true;
23  }
24 }
25 
26 Buffer::Buffer(const Buffer& buf)
27  : m_memory(NULL), m_size(buf.m_size),
28  m_allocated(buf.m_allocated)
29 {
30  if (m_allocated) {
31  m_memory = new unsigned char [buf.m_size];
32  ::memset(m_memory, 0, buf.m_size);
33  ::memcpy(m_memory, buf.m_memory, buf.m_size);
34  } else {
35  m_memory = buf.m_memory;
36  }
37  m_size = buf.m_size;
38 }
39 
40 Buffer::~Buffer()
41 {
42  if (m_allocated && m_memory != NULL) {
43  delete [] m_memory;
44  m_allocated = false;
45  m_memory = NULL;
46  }
47 }
48 
49 const Buffer& Buffer::operator=(const Buffer& buf)
50 {
51  if (m_allocated) {
52  delete [] m_memory;
53  }
54  m_allocated = buf.m_allocated;
55  if (m_allocated) {
56  m_memory = new unsigned char [buf.m_size];
57  ::memset(m_memory, 0, buf.m_size);
58  ::memcpy(m_memory, buf.m_memory, buf.m_size);
59  } else {
60  m_memory = buf.m_memory;
61  }
62  m_size = buf.m_size;
63  return *this;
64 }
Abstract base class for different kinds of events.