Skip to content

Commit bc86bb4

Browse files
authored
Drop o2::byte (AliceO2Group#6253)
Replace it with C++17 std::byte. This does not endorse the actual idea behind std::byte usage, merely gets rid of an O2 specific type and header for it.
1 parent 6a207cb commit bc86bb4

20 files changed

Lines changed: 74 additions & 99 deletions

File tree

Algorithm/include/Algorithm/TableView.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TableView
7979

8080
/// descriptor pointing to payload of one frame
8181
struct FrameData {
82-
const byte* buffer = nullptr;
82+
const std::byte* buffer = nullptr;
8383
size_t size = 0;
8484
};
8585

@@ -94,7 +94,7 @@ class TableView
9494
* @param seqSize Length of sequence
9595
* @return number of inserted elements
9696
*/
97-
size_t addRow(RowDescType rowData, byte* seqData, size_t seqSize)
97+
size_t addRow(RowDescType rowData, std::byte* seqData, size_t seqSize)
9898
{
9999
unsigned nFrames = mFrames.size();
100100
unsigned currentRow = mRowData.size();
@@ -118,7 +118,7 @@ class TableView
118118

119119
// insert frame descriptor under key composed from header and row
120120
auto result = mFrames.emplace(FrameIndex{*entry.header, currentRow},
121-
FrameData{entry.payload, entry.length});
121+
FrameData{(std::byte*)entry.payload, entry.length});
122122
return result.second;
123123
});
124124
auto insertedFrames = mFrames.size() - nFrames;

Algorithm/test/tableview.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ BOOST_AUTO_TEST_CASE(test_tableview_reverse)
7878
dh2.subSpecification = 0xdeadbeef;
7979
dh2.payloadSize = 0;
8080

81-
heartbeatview.addRow(dh1, tf1.buffer.get(), tf1.size());
82-
heartbeatview.addRow(dh2, tf2.buffer.get(), tf2.size());
81+
heartbeatview.addRow(dh1, (std::byte*)tf1.buffer.get(), tf1.size());
82+
heartbeatview.addRow(dh2, (std::byte*)tf2.buffer.get(), tf2.size());
8383

8484
std::cout << "slots: " << heartbeatview.getNRows()
8585
<< " columns: " << heartbeatview.getNColumns()
@@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(test_tableview_formaterror)
145145
dh.subSpecification = 0;
146146
dh.payloadSize = 0;
147147

148-
heartbeatview.addRow(dh, tf1.buffer.get(), tf1.size());
148+
heartbeatview.addRow(dh, (std::byte*)tf1.buffer.get(), tf1.size());
149149

150150
BOOST_CHECK(heartbeatview.getNRows() == 0);
151151
BOOST_CHECK(heartbeatview.getNColumns() == 0);

DataFormats/Headers/include/Headers/DataHeader.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@
3030
#ifndef ALICEO2_BASE_DATA_HEADER_
3131
#define ALICEO2_BASE_DATA_HEADER_
3232

33+
#include <cstddef>
3334
#include <cstdint>
3435
#include <memory>
3536
#include <cassert>
3637
#include <cstring> //needed for memcmp
37-
#include <algorithm> // std::min
38-
#include <stdexcept>
3938
#include <string>
39+
#include <stdexcept>
4040
#include <climits>
4141
#include <limits>
42-
// FIXME: for o2::byte. Use std::byte as soon as we move to C++17..
43-
#include "MemoryResources/Types.h"
44-
#include <cerrno>
4542

4643
namespace o2::header
4744
{
@@ -241,7 +238,7 @@ struct Descriptor {
241238
{
242239
static_assert(L <= N + 1, "initializer string must not exceed descriptor size");
243240
unsigned i = 0;
244-
for (; in[i] && i < std::min(N, L); ++i) {
241+
for (; in[i] && i < (N < L ? N : L); ++i) {
245242
str[i] = in[i];
246243
}
247244
}
@@ -392,7 +389,7 @@ struct BaseHeader {
392389
/// @brief access header in buffer
393390
///
394391
/// this is to guess if the buffer starting at b looks like a header
395-
inline static const BaseHeader* get(const o2::byte* b, size_t /*len*/ = 0)
392+
inline static const BaseHeader* get(const std::byte* b, size_t /*len*/ = 0)
396393
{
397394
return (b != nullptr && *(reinterpret_cast<const uint32_t*>(b)) == sMagicString)
398395
? reinterpret_cast<const BaseHeader*>(b)
@@ -402,27 +399,27 @@ struct BaseHeader {
402399
/// @brief access header in buffer
403400
///
404401
/// this is to guess if the buffer starting at b looks like a header
405-
inline static BaseHeader* get(o2::byte* b, size_t /*len*/ = 0)
402+
inline static BaseHeader* get(std::byte* b, size_t /*len*/ = 0)
406403
{
407404
return (b != nullptr && *(reinterpret_cast<uint32_t*>(b)) == sMagicString) ? reinterpret_cast<BaseHeader*>(b)
408405
: nullptr;
409406
}
410407

411408
constexpr uint32_t size() const noexcept { return headerSize; }
412-
inline const o2::byte* data() const noexcept { return reinterpret_cast<const o2::byte*>(this); }
409+
inline const std::byte* data() const noexcept { return reinterpret_cast<const std::byte*>(this); }
413410

414411
/// get the next header if any (const version)
415412
inline const BaseHeader* next() const noexcept
416413
{
417414
// BaseHeader::get checks that next header starts with the BaseHeader information at the
418415
// offset given by the size of the current header.
419-
return (flagsNextHeader) ? BaseHeader::get(reinterpret_cast<const o2::byte*>(this) + headerSize) : nullptr;
416+
return (flagsNextHeader) ? BaseHeader::get(reinterpret_cast<const std::byte*>(this) + headerSize) : nullptr;
420417
}
421418

422419
/// get the next header if any (non-const version)
423420
inline BaseHeader* next() noexcept
424421
{
425-
return (flagsNextHeader) ? BaseHeader::get(reinterpret_cast<o2::byte*>(this) + headerSize) : nullptr;
422+
return (flagsNextHeader) ? BaseHeader::get(reinterpret_cast<std::byte*>(this) + headerSize) : nullptr;
426423
}
427424

428425
/// check if the header matches expected version
@@ -441,7 +438,7 @@ struct BaseHeader {
441438
/// use like this:
442439
/// HeaderType* h = get<HeaderType*>(buffer)
443440
template <typename HeaderType, typename std::enable_if_t<std::is_pointer<HeaderType>::value, int> = 0>
444-
auto get(const o2::byte* buffer, size_t /*len*/ = 0)
441+
auto get(const std::byte* buffer, size_t /*len*/ = 0)
445442
{
446443
using HeaderConstPtrType = const typename std::remove_pointer<HeaderType>::type*;
447444
using HeaderValueType = typename std::remove_pointer<HeaderType>::type;
@@ -482,7 +479,7 @@ auto get(const o2::byte* buffer, size_t /*len*/ = 0)
482479
template <typename HeaderType, typename std::enable_if_t<std::is_pointer<HeaderType>::value, int> = 0>
483480
auto get(const void* buffer, size_t len = 0)
484481
{
485-
return get<HeaderType>(reinterpret_cast<const byte*>(buffer), len);
482+
return get<HeaderType>(reinterpret_cast<const std::byte*>(buffer), len);
486483
}
487484

488485
//__________________________________________________________________________________________________

DataFormats/Headers/include/Headers/Stack.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ struct Stack {
3939
struct freeobj {
4040
freeobj(memory_resource* mr) : resource(mr) {}
4141
memory_resource* resource{nullptr};
42-
void operator()(o2::byte* ptr) { resource->deallocate(ptr, 0, 0); }
42+
void operator()(std::byte* ptr) { resource->deallocate(ptr, 0, 0); }
4343
};
4444

4545
public:
46-
using allocator_type = boost::container::pmr::polymorphic_allocator<o2::byte>;
47-
using value_type = o2::byte;
46+
using allocator_type = boost::container::pmr::polymorphic_allocator<std::byte>;
47+
using value_type = std::byte;
4848
using BufferType = std::unique_ptr<value_type[], freeobj>; //this gives us proper default move semantics for free
4949

5050
Stack() = default;
@@ -57,16 +57,16 @@ struct Stack {
5757
size_t size() const { return bufferSize; }
5858
allocator_type get_allocator() const { return allocator; }
5959
const BaseHeader* first() const { return reinterpret_cast<const BaseHeader*>(this->data()); }
60-
static const BaseHeader* firstHeader(o2::byte const* buf) { return BaseHeader::get(buf); }
61-
static const BaseHeader* lastHeader(o2::byte const* buf)
60+
static const BaseHeader* firstHeader(std::byte const* buf) { return BaseHeader::get(buf); }
61+
static const BaseHeader* lastHeader(std::byte const* buf)
6262
{
6363
const BaseHeader* last{firstHeader(buf)};
6464
while (last && last->flagsNextHeader) {
6565
last = last->next();
6666
}
6767
return last;
6868
}
69-
static size_t headerStackSize(o2::byte const* buf)
69+
static size_t headerStackSize(std::byte const* buf)
7070
{
7171
size_t result = 0;
7272
const BaseHeader* last{firstHeader(buf)};
@@ -88,7 +88,7 @@ struct Stack {
8888
/// all headers must derive from BaseHeader, in addition also other stacks can be passed to ctor.
8989
template <typename FirstArgType, typename... Headers,
9090
typename std::enable_if_t<
91-
!std::is_convertible<FirstArgType, boost::container::pmr::polymorphic_allocator<o2::byte>>::value, int> = 0>
91+
!std::is_convertible<FirstArgType, boost::container::pmr::polymorphic_allocator<std::byte>>::value, int> = 0>
9292
Stack(FirstArgType&& firstHeader, Headers&&... headers)
9393
: Stack(boost::container::pmr::new_delete_resource(), std::forward<FirstArgType>(firstHeader),
9494
std::forward<Headers>(headers)...)
@@ -100,7 +100,7 @@ struct Stack {
100100
Stack(const allocator_type allocatorArg, Headers&&... headers)
101101
: allocator{allocatorArg},
102102
bufferSize{calculateSize(std::forward<Headers>(headers)...)},
103-
buffer{static_cast<o2::byte*>(allocator.resource()->allocate(bufferSize, alignof(std::max_align_t))), freeobj{allocator.resource()}}
103+
buffer{static_cast<std::byte*>(allocator.resource()->allocate(bufferSize, alignof(std::max_align_t))), freeobj{allocator.resource()}}
104104
{
105105
inject(buffer.get(), std::forward<Headers>(headers)...);
106106
}
@@ -117,7 +117,7 @@ struct Stack {
117117
constexpr static size_t calculateSize(T&& h) noexcept
118118
{
119119
//if it's a pointer (to a stack) traverse it
120-
if constexpr (std::is_convertible_v<T, o2::byte*>) {
120+
if constexpr (std::is_convertible_v<T, std::byte*>) {
121121
const BaseHeader* next = BaseHeader::get(std::forward<T>(h));
122122
if (!next) {
123123
return 0;
@@ -143,7 +143,7 @@ struct Stack {
143143

144144
//______________________________________________________________________________________________
145145
template <typename T>
146-
static o2::byte* inject(o2::byte* here, T&& h, bool more = false) noexcept
146+
static std::byte* inject(std::byte* here, T&& h, bool more = false) noexcept
147147
{
148148
using headerType = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
149149
if (here == nullptr) {
@@ -168,7 +168,7 @@ struct Stack {
168168
::new (static_cast<void*>(here)) headerType(std::forward<T>(h));
169169
reinterpret_cast<BaseHeader*>(here)->flagsNextHeader = more;
170170
return here + h.size();
171-
} else if constexpr (std::is_same_v<headerType, o2::byte*>) {
171+
} else if constexpr (std::is_same_v<headerType, std::byte*>) {
172172
BaseHeader* from{BaseHeader::get(h)};
173173
BaseHeader* last{nullptr};
174174
while (from) {
@@ -188,7 +188,7 @@ struct Stack {
188188

189189
//______________________________________________________________________________________________
190190
template <typename T, typename... Args>
191-
static o2::byte* inject(o2::byte* here, T&& h, Args&&... args) noexcept
191+
static std::byte* inject(std::byte* here, T&& h, Args&&... args) noexcept
192192
{
193193
bool more = hasNonEmptyArg(args...);
194194
auto alsohere = inject(here, h, more);
@@ -210,7 +210,7 @@ struct Stack {
210210
template <typename T>
211211
static bool hasNonEmptyArg(const T& h) noexcept
212212
{
213-
if constexpr (std::is_convertible_v<T, o2::byte*>) {
213+
if constexpr (std::is_convertible_v<T, std::byte*>) {
214214
return get<BaseHeader*>(h);
215215
} else {
216216
if (h.size() > 0) {

DataFormats/Headers/src/DataHeader.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void o2::header::hexDump(const char* desc, const void* voidaddr, size_t len, siz
108108
size_t i;
109109
unsigned char buff[17]; // stores the ASCII data
110110
memset(&buff[0], '\0', 17);
111-
const byte* addr = reinterpret_cast<const byte*>(voidaddr);
111+
const std::byte* addr = reinterpret_cast<const std::byte*>(voidaddr);
112112

113113
// Output description if given.
114114
if (desc != nullptr) {
@@ -143,13 +143,13 @@ void o2::header::hexDump(const char* desc, const void* voidaddr, size_t len, siz
143143
}
144144

145145
// Now the hex code for the specific character.
146-
printf(" %02x", addr[i]);
146+
printf(" %02x", (char)addr[i]);
147147

148148
// And store a printable ASCII character for later.
149-
if ((addr[i] < 0x20) || (addr[i] > 0x7e)) {
149+
if (((char)addr[i] < 0x20) || ((char)addr[i] > 0x7e)) {
150150
buff[i % 16] = '.';
151151
} else {
152-
buff[i % 16] = addr[i];
152+
buff[i % 16] = (char)addr[i];
153153
}
154154
buff[(i % 16) + 1] = '\0';
155155
fflush(stdout);

DataFormats/MemoryResources/include/MemoryResources/MemoryResources.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include <FairMQTransportFactory.h>
4242
#include <fairmq/MemoryResources.h>
4343
#include <fairmq/MemoryResourceTools.h>
44-
#include "Types.h"
4544

4645
namespace o2
4746
{
@@ -305,8 +304,8 @@ class NoConstructAllocator : public boost::container::pmr::polymorphic_allocator
305304
//__________________________________________________________________________________________________
306305
//__________________________________________________________________________________________________
307306

308-
using ByteSpectatorAllocator = SpectatorAllocator<o2::byte>;
309-
using BytePmrAllocator = boost::container::pmr::polymorphic_allocator<o2::byte>;
307+
using ByteSpectatorAllocator = SpectatorAllocator<std::byte>;
308+
using BytePmrAllocator = boost::container::pmr::polymorphic_allocator<std::byte>;
310309
template <class T>
311310
using vector = std::vector<T, o2::pmr::polymorphic_allocator<T>>;
312311

DataFormats/MemoryResources/include/MemoryResources/Types.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

DataFormats/MemoryResources/test/testMemoryResources.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(allocator_test)
8282
v.emplace_back(1);
8383
v.emplace_back(2);
8484
v.emplace_back(3);
85-
BOOST_CHECK((byte*)&(*v.end()) - (byte*)&(*v.begin()) == 3 * sizeof(testData));
85+
BOOST_CHECK((std::byte*)&(*v.end()) - (std::byte*)&(*v.begin()) == 3 * sizeof(testData));
8686
BOOST_CHECK(testData::nconstructions == 3);
8787
}
8888

Detectors/TPC/reconstruction/include/TPCReconstruction/RawReaderCRU.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <gsl/span>
3232
#include "DetectorsRaw/RDHUtils.h"
3333

34-
#include "MemoryResources/Types.h"
3534
#include "TPCBase/CRU.h"
3635
#include "Headers/RAWDataHeader.h"
3736
#include "Headers/RDHAny.h"
@@ -239,7 +238,7 @@ class GBTFrame
239238
void getAdcValues(ADCRawData& rawData);
240239

241240
/// read from memory
242-
void readFromMemory(gsl::span<const o2::byte> data);
241+
void readFromMemory(gsl::span<const std::byte> data);
243242

244243
/// read from istream
245244
void streamFrom(std::istream& input);
@@ -610,7 +609,7 @@ class RawReaderCRU
610609

611610
/// Process data from memory for a single link
612611
/// The data must be collected before, merged over 8k packets
613-
int processMemory(const std::vector<o2::byte>& data, ADCRawData& rawData);
612+
int processMemory(const std::vector<std::byte>& data, ADCRawData& rawData);
614613

615614
/// process links
616615
void processLinks(const uint32_t linkMask = 0);
@@ -798,7 +797,7 @@ class RawReaderCRU
798797
std::ifstream mFileHandle; ///< file handle for input file
799798

800799
/// collect raw GBT data
801-
void collectGBTData(std::vector<o2::byte>& data);
800+
void collectGBTData(std::vector<std::byte>& data);
802801

803802
/// fill adc data to output map
804803
void fillADCdataMap(const ADCRawData& rawData);
@@ -915,7 +914,7 @@ inline void GBTFrame::getAdcValues(ADCRawData& rawData)
915914
// std::cout << std::endl;
916915
}
917916

918-
inline void GBTFrame::readFromMemory(gsl::span<const o2::byte> data)
917+
inline void GBTFrame::readFromMemory(gsl::span<const std::byte> data)
919918
{
920919
assert(sizeof(mData) == data.size_bytes());
921920
memcpy(mData.data(), data.data(), data.size_bytes());

Detectors/TPC/reconstruction/src/RawReaderCRU.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ int RawReaderCRU::processPacket(GBTFrame& gFrame, uint32_t startPos, uint32_t si
491491
return 0;
492492
}
493493

494-
int RawReaderCRU::processMemory(const std::vector<o2::byte>& data, ADCRawData& rawData)
494+
int RawReaderCRU::processMemory(const std::vector<std::byte>& data, ADCRawData& rawData)
495495
{
496496
GBTFrame gFrame;
497497

@@ -507,7 +507,7 @@ int RawReaderCRU::processMemory(const std::vector<o2::byte>& data, ADCRawData& r
507507
// reinterpret_cast<const uint32_t*>(data.data() + iFrame * 16), so it could be accessed the
508508
// same way as the mData array.
509509
// however, this was ~5% slower in execution time. I suspect due to cache misses
510-
gFrame.readFromMemory(gsl::span<const o2::byte>(data.data() + iFrame * 16, 16));
510+
gFrame.readFromMemory(gsl::span<const std::byte>(data.data() + iFrame * 16, 16));
511511

512512
// extract the half words from the 4 32-bit words
513513
gFrame.getFrameHalfWords();
@@ -702,7 +702,7 @@ void RawReaderCRU::processDataMemory()
702702
//dataSize = 4000 * 16;
703703
//}
704704

705-
std::vector<o2::byte> data;
705+
std::vector<std::byte> data;
706706
data.reserve(dataSize);
707707
collectGBTData(data);
708708

@@ -718,7 +718,7 @@ void RawReaderCRU::processDataMemory()
718718
}
719719
}
720720

721-
void RawReaderCRU::collectGBTData(std::vector<o2::byte>& data)
721+
void RawReaderCRU::collectGBTData(std::vector<std::byte>& data)
722722
{
723723
const auto& linkInfoArray = mManager->mEventSync.getLinkInfoArrayForEvent(mEventNumber, mCRU);
724724
auto& file = getFileHandle();
@@ -732,7 +732,7 @@ void RawReaderCRU::collectGBTData(std::vector<o2::byte>& data)
732732

733733
const auto payloadStart = packet.getPayloadOffset();
734734
const auto payloadSize = size_t(packet.getPayloadSize());
735-
data.insert(data.end(), payloadSize, 0);
735+
data.insert(data.end(), payloadSize, (std::byte)0);
736736
// jump to the start position of the packet
737737
file.seekg(payloadStart, std::ios::beg);
738738

0 commit comments

Comments
 (0)