PennMilliSliceWriter.cc
Go to the documentation of this file.
2 
4 PennMilliSliceWriter(uint8_t* address, uint32_t max_size_bytes) :
5  PennMilliSlice(address), max_size_bytes_(max_size_bytes)
6 {
7  header_()->sequence_id = 0;
8  header_()->version = 0xFFFF;
9  header_()->millislice_size = sizeof(Header);
10  header_()->payload_count = 0;
14  header_()->end_timestamp = 0;
15  header_()->width_in_ticks = 0;
17 }
18 
19 #ifdef PENN_DONT_REBLOCK_USLICES
20 std::shared_ptr<dune::PennMicroSliceWriter>
21 dune::PennMilliSliceWriter::reserveMicroSlice(uint32_t ms_max_bytes)
22 {
23  // finalize the most recent PennMicroSlice, in case that hasn't
24  // already been done
26 
27  // test if this new PennMicroSlice could overflow our maximum size
28  if ((size() + ms_max_bytes) > max_size_bytes_) {
29  return false;
30  }
31 
32  // create the next PennMicroSlice in our buffer, and update our
33  // counters to include the new PennMicroSlice
34  uint8_t* ms_ptr = data_(header_()->microslice_count);
35  latest_microslice_ptr_.reset(new PennMicroSliceWriter(ms_ptr, ms_max_bytes));
36  ++(header_()->microslice_count);
37  header_()->millislice_size += ms_max_bytes;
39 }
40 #endif
41 
42 int32_t dune::PennMilliSliceWriter::finalize(bool override, uint32_t data_size_bytes,
43 #ifdef PENN_DONT_REBLOCK_USLICES
44  uint32_t microslice_count,
45 #endif
46  uint16_t sequence_id,
47  uint16_t payload_count, uint16_t payload_count_counter,
48  uint16_t payload_count_trigger, uint16_t payload_count_timestamp,
49  uint64_t end_timestamp, uint32_t width_in_ticks, uint32_t overlap_in_ticks)
50 {
51  // first, we need to finalize the last MicroSlice, in case that
52  // hasn't already been done
54 
55  //Override the current size if requested
56  if(override) {
57  header_()->millislice_size = sizeof(Header) + data_size_bytes;
58 #ifdef PENN_DONT_REBLOCK_USLICES
59  header_()->microslice_count = microslice_count;
60 #endif
61  header_()->payload_count = payload_count;
62  header_()->payload_count_counter = payload_count_counter;
63  header_()->payload_count_trigger = payload_count_trigger;
64  header_()->payload_count_timestamp = payload_count_timestamp;
65  header_()->end_timestamp = end_timestamp;
66  header_()->width_in_ticks = width_in_ticks;
67  header_()->overlap_in_ticks = overlap_in_ticks;
68  header_()->sequence_id = sequence_id;
69  }
70 
71 #ifdef ENABLE_PENNMILLISLICE_CHECKSUM
72  //Calculate checksum
73  //TODO decide if a footer is the right place to put the checksum. Alternative is associate it with the fragment
74  uint8_t* end_ptr = buffer_ + header_()->millislice_size;
75  *(reinterpret_cast<dune::PennMilliSliceWriter::checksum_t *>(end_ptr)) = calculateChecksum();
76  header_()->millislice_size += sizeof(dune::PennMilliSliceWriter::checksum_t);
77 #endif
78  // next, we update our maximum size so that no more MicroSlices
79  // can be added
80  int32_t size_diff = max_size_bytes_ - header_()->millislice_size;
82  return size_diff;
83 }
84 
86 {
87  if (
88 #ifdef PENN_DONT_REBLOCK_USLICES
89  header_()->microslice_count > 0 &&
90 #endif
91  latest_microslice_ptr_.get() != 0) {
92  int size_change = latest_microslice_ptr_->finalize();
93  header_()->millislice_size -= size_change;
94  }
95 }
96 
98 {
99  return reinterpret_cast<Header *>(buffer_);
100 }
101 
103 {
104  uint8_t* ms_ptr = buffer_ + sizeof(Header);
105  for (int idx = 0; idx < index; ++idx) {
106  PennMicroSlice tmp_ms(ms_ptr);
107  ms_ptr += tmp_ms.size();
108  }
109  return ms_ptr;
110 }
PennMilliSliceWriter(uint8_t *address, uint32_t max_size_bytes)
int32_t finalize(bool override=false, uint32_t data_size_bytes=0, uint16_t sequence_id=0, uint16_t payload_count=0, uint16_t payload_count_counter=0, uint16_t payload_count_trigger=0, uint16_t payload_count_timestamp=0, uint64_t end_timestamp=0, uint32_t width_in_ticks=0, uint32_t overlap_in_ticks=0)
Header::millislice_size_t size() const
std::shared_ptr< PennMicroSliceWriter > latest_microslice_ptr_
dune::PennMicroSlice::microslice_size_t size() const