DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
BufferCopy.hpp
Go to the documentation of this file.
1
9#ifndef DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFERCOPY_HPP_
10#define DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFERCOPY_HPP_
11
12#include <cstddef>
13#include <cstring>
14#include <cstdint>
15#include <algorithm>
16
17namespace dunedaq {
18namespace datahandlinglibs {
19
20/*
21 * Most common use-case:
22 *
23 * size_t msg_size = 1024; // source buffer size
24 * char message[msg_size]; // source buffer
25 * datahandlinglibs::types::DUMMY_FRAME_STRUCT target_payload; // target object
26 * uint32_t bytes_copied = 0; // book-keeping of copied bytes
27 * buffer_copy(&message[0], msg_size, static_cast<void*>(&target_payload), bytes_copied, sizeof(target_payload));
28 *
29 *
30 * Copy multiple char*, size_t pairs into destination buffer (serialize them together)
31 *
32 * const char* subchunk_data[NUMBER_OF_CHUNKS]; // source payloads
33 * size_t subchunk_length[NUMBER_OF_CHUNKS]; // source payloads' sizes
34 * TargetStruct payload; // target payload
35 * size_t target_size = sizeof(payload); // target payload size
36 * uint32_t bytes_copied_chunk = 0;
37 * for (unsigned i = 0; i < n_subchunks; i++) {
38 * buffer_copy(subchunk_data[i], subchunk_sizes[i], static_cast<void*>(&payload.data), bytes_copied_chunk, target_size);
39 * bytes_copied_chunk += subchunk_sizes[i];
40 * }
41 *
42 * */
43inline void
44buffer_copy(const char* data,
45 std::size_t size,
46 void* buffer,
47 std::uint32_t buffer_pos, // NOLINT
48 const std::size_t& buffer_size)
49{
50 auto bytes_to_copy = size; // NOLINT
51 while (bytes_to_copy > 0) {
52 auto n = std::min(bytes_to_copy, buffer_size - buffer_pos); // NOLINT
53 std::memcpy(static_cast<char*>(buffer) + buffer_pos, data, n);
54 buffer_pos += n;
55 bytes_to_copy -= n;
56 if (buffer_pos == buffer_size) {
57 buffer_pos = 0;
58 }
59 }
60}
61
62} // namespace datahandlinglibs
63} // namespace dunedaq
64
65#endif // DATAHANDLINGLIBS_INCLUDE_DATAHANDLINGLIBS_UTILS_BUFFERCOPY_HPP_
void buffer_copy(const char *data, std::size_t size, void *buffer, std::uint32_t buffer_pos, const std::size_t &buffer_size)
Including Qt Headers.
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size