Skip to content

Commit 5fc8833

Browse files
committed
DPL: revamp / remove Endian.h
C++20 (and even more so 23) has understanding of endianess. No need to have a special wrapper include anymore.
1 parent 1e1c6e5 commit 5fc8833

6 files changed

Lines changed: 81 additions & 112 deletions

File tree

Framework/AnalysisSupport/src/TTreePlugin.cxx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "Framework/RootArrowFilesystem.h"
1313
#include "Framework/Plugins.h"
1414
#include "Framework/Signpost.h"
15-
#include "Framework/Endian.h"
15+
#include <bit>
1616
#include <TBufferFile.h>
1717
#include <TBufferIO.h>
1818
#include <arrow/buffer.h>
@@ -187,6 +187,32 @@ arrow::Result<std::shared_ptr<arrow::Buffer>> TTreeDeferredReadOutputStream::Fin
187187

188188
arrow::Result<int64_t> TTreeDeferredReadOutputStream::Tell() const { return position_; }
189189

190+
static void bigEndianCopy(uint8_t* dest, char* src, int count, size_t typeSize) noexcept
191+
{
192+
if constexpr (std::endian::native == std::endian::big) {
193+
std::memcpy(dest, src, count * typeSize);
194+
return;
195+
}
196+
switch (typeSize) {
197+
case 2:
198+
for (int i = 0; i < count; ++i) {
199+
reinterpret_cast<uint16_t*>(dest)[i] = __builtin_bswap16(reinterpret_cast<uint16_t*>(src)[i]);
200+
}
201+
return;
202+
case 4:
203+
for (int i = 0; i < count; ++i) {
204+
reinterpret_cast<uint32_t*>(dest)[i] = __builtin_bswap32(reinterpret_cast<uint32_t*>(src)[i]);
205+
}
206+
return;
207+
case 8:
208+
for (int i = 0; i < count; ++i) {
209+
reinterpret_cast<uint64_t*>(dest)[i] = __builtin_bswap64(reinterpret_cast<uint64_t*>(src)[i]);
210+
}
211+
return;
212+
}
213+
std::memcpy(dest, src, count * typeSize);
214+
}
215+
190216
auto readValues = [](uint8_t* target, ReadOps& op, TBufferFile& rootBuffer) {
191217
int readEntries = 0;
192218
rootBuffer.Reset();
@@ -197,7 +223,7 @@ auto readValues = [](uint8_t* target, ReadOps& op, TBufferFile& rootBuffer) {
197223
}
198224
int size = readLast * op.listSize;
199225
readEntries += readLast;
200-
swapCopy(target, rootBuffer.GetCurrent(), size, op.typeSize);
226+
bigEndianCopy(target, rootBuffer.GetCurrent(), size, op.typeSize);
201227
target += (ptrdiff_t)(size * op.typeSize);
202228
}
203229
};
@@ -230,7 +256,7 @@ auto readVLAValues = [](uint8_t* target, ReadOps& op, ReadOps const& offsetOp, T
230256
auto readLast = op.branch->GetBulkRead().GetEntriesSerialized(readEntries, rootBuffer);
231257
int size = offsets[readEntries + readLast] - offsets[readEntries];
232258
readEntries += readLast;
233-
swapCopy(target, rootBuffer.GetCurrent(), size, op.typeSize);
259+
bigEndianCopy(target, rootBuffer.GetCurrent(), size, op.typeSize);
234260
target += (ptrdiff_t)(size * op.typeSize);
235261
}
236262
};
@@ -581,7 +607,8 @@ auto readOffsets = [](ReadOps& op, TBufferFile& rootBuffer) {
581607
readEntries += readLast;
582608
for (auto i = 0; i < readLast; ++i) {
583609
offsets[count++] = (int)offset;
584-
offset += swap32_(reinterpret_cast<uint32_t*>(rootBuffer.GetCurrent())[i]);
610+
uint32_t raw = reinterpret_cast<uint32_t*>(rootBuffer.GetCurrent())[i];
611+
offset += (std::endian::native == std::endian::little) ? __builtin_bswap32(raw) : raw;
585612
}
586613
}
587614
offsets[count] = (int)offset;

Framework/AnalysisSupport/src/TableTreeHelpers.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// or submit itself to any jurisdiction.
1111
#include "Framework/TableTreeHelpers.h"
1212
#include "Framework/Logger.h"
13-
#include "Framework/Endian.h"
1413
#include "Framework/Signpost.h"
1514

1615
#include <arrow/dataset/file_base.h>

Framework/Core/src/FragmentToBatch.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// or submit itself to any jurisdiction.
1111
#include "Framework/FragmentToBatch.h"
1212
#include "Framework/Logger.h"
13-
#include "Framework/Endian.h"
1413
#include "Framework/Signpost.h"
1514

1615
#include <arrow/dataset/file_base.h>

Framework/Core/src/HTTPParser.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void encode_websocket_frames(std::vector<uv_buf_t>& outputs, char const* src, si
9090
WebSocketFrameHuge* header = (WebSocketFrameHuge*)buffer;
9191
memset(buffer, 0, headerSize);
9292
header->len = 127;
93-
header->len64 = htonll(size);
93+
header->len64 = (std::endian::native == std::endian::little) ? __builtin_bswap64(size) : size;
9494
outputs.push_back(uv_buf_init(buffer, size + maskSize + headerSize));
9595
}
9696
size_t fullHeaderSize = maskSize + headerSize;
@@ -165,7 +165,7 @@ void decode_websocket(char* start, size_t size, WebSocketHandler& handler)
165165
headerSize = 2 + 2 + (header->mask ? 4 : 0);
166166
} else if (header->len == 127) {
167167
WebSocketFrameHuge* headerSmall = (WebSocketFrameHuge*)cur;
168-
payloadSize = ntohll(headerSmall->len64);
168+
payloadSize = (std::endian::native == std::endian::little) ? __builtin_bswap64(headerSmall->len64) : headerSmall->len64;
169169
headerSize = 2 + 8 + (header->mask ? 4 : 0);
170170
}
171171
size_t availableSize = size - (cur - start);

Framework/Core/src/HTTPParser.h

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#ifndef O2_FRAMEWORK_HTTPPARSER_H_
1313
#define O2_FRAMEWORK_HTTPPARSER_H_
1414

15-
#include "Framework/Endian.h"
15+
#include <bit>
16+
#include <cstdint>
1617
#include <fmt/format.h>
1718
#include <uv.h>
1819
#include <string>
@@ -22,74 +23,91 @@
2223
namespace o2::framework
2324
{
2425

25-
struct __attribute__((__packed__)) WebSocketFrameTiny {
26-
#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
26+
template <std::endian E>
27+
struct __attribute__((__packed__)) WebSocketFrameTiny;
28+
29+
template <>
30+
struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::little> {
2731
unsigned char opcode : 4;
2832
unsigned char rsv3 : 1;
2933
unsigned char rsv2 : 1;
3034
unsigned char rsv1 : 1;
3135
unsigned char fin : 1;
3236
unsigned char len : 7;
3337
unsigned char mask : 1;
34-
#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
38+
};
39+
40+
template <>
41+
struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::big> {
3542
unsigned char fin : 1;
3643
unsigned char rsv1 : 1;
3744
unsigned char rsv2 : 1;
3845
unsigned char rsv3 : 1;
3946
unsigned char opcode : 4;
4047
unsigned char mask : 1;
4148
unsigned char len : 7;
42-
#else
43-
#error Uknown endiannes
44-
#endif
4549
};
4650

47-
struct __attribute__((__packed__)) WebSocketFrameShort {
48-
#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
51+
using WebSocketFrameTiny = WebSocketFrameTiny<std::endian::native>;
52+
53+
template <std::endian E>
54+
struct __attribute__((__packed__)) WebSocketFrameShort;
55+
56+
template <>
57+
struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::little> {
4958
unsigned char opcode : 4;
5059
unsigned char rsv3 : 1;
5160
unsigned char rsv2 : 1;
5261
unsigned char rsv1 : 1;
5362
unsigned char fin : 1;
5463
unsigned char len : 7;
5564
unsigned char mask : 1;
56-
#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
65+
uint16_t len16;
66+
};
67+
68+
template <>
69+
struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::big> {
5770
unsigned char fin : 1;
5871
unsigned char rsv1 : 1;
5972
unsigned char rsv2 : 1;
6073
unsigned char rsv3 : 1;
6174
unsigned char opcode : 4;
6275
unsigned char mask : 1;
6376
unsigned char len : 7;
64-
#else
65-
#error Uknown endiannes
66-
#endif
6777
uint16_t len16;
6878
};
6979

70-
struct __attribute__((__packed__)) WebSocketFrameHuge {
71-
#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
80+
using WebSocketFrameShort = WebSocketFrameShort<std::endian::native>;
81+
82+
template <std::endian E>
83+
struct __attribute__((__packed__)) WebSocketFrameHuge;
84+
85+
template <>
86+
struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::little> {
7287
unsigned char opcode : 4;
7388
unsigned char rsv3 : 1;
7489
unsigned char rsv2 : 1;
7590
unsigned char rsv1 : 1;
7691
unsigned char fin : 1;
7792
unsigned char len : 7;
7893
unsigned char mask : 1;
79-
#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
94+
uint64_t len64;
95+
};
96+
97+
template <>
98+
struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::big> {
8099
unsigned char fin : 1;
81100
unsigned char rsv1 : 1;
82101
unsigned char rsv2 : 1;
83102
unsigned char rsv3 : 1;
84103
unsigned char opcode : 4;
85104
unsigned char mask : 1;
86105
unsigned char len : 7;
87-
#else
88-
#error Uknown endiannes
89-
#endif
90106
uint64_t len64;
91107
};
92108

109+
using WebSocketFrameHuge = WebSocketFrameHuge<std::endian::native>;
110+
93111
enum struct WebSocketFrameKind {
94112
Tiny,
95113
Short,
@@ -138,9 +156,9 @@ struct WebSocketHandler {
138156
virtual ~WebSocketHandler() = default;
139157

140158
/// Invoked when all the headers are received.
141-
virtual void headers(std::map<std::string, std::string> const& headers){};
159+
virtual void headers(std::map<std::string, std::string> const& headers) {};
142160
/// FIXME: not implemented
143-
virtual void beginFragmentation(){};
161+
virtual void beginFragmentation() {};
144162
/// Invoked when a frame it's parsed. Notice you do not own the data and you must
145163
/// not free the memory.
146164
virtual void frame(char const* frame, size_t s) {}
@@ -205,18 +223,18 @@ struct HTTPParser {
205223
std::string remaining;
206224
std::string error;
207225
std::vector<HTTPState> states;
208-
virtual void method(std::string_view const& s){};
209-
virtual void target(std::string_view const& s){};
210-
virtual void version(std::string_view const& s){};
211-
virtual void header(std::string_view const& k, std::string_view const& v){};
212-
virtual void endHeaders(){};
226+
virtual void method(std::string_view const& s) {};
227+
virtual void target(std::string_view const& s) {};
228+
virtual void version(std::string_view const& s) {};
229+
virtual void header(std::string_view const& k, std::string_view const& v) {};
230+
virtual void endHeaders() {};
213231
/// Invoked whenever we are parsing data.
214232
/// In order to allow for xoring (as required by the websocket standard)
215233
/// in place, we pass it as a mutable pointer.
216-
virtual void body(char* data, size_t s){};
217-
virtual void replyVersion(std::string_view const& s){};
218-
virtual void replyCode(std::string_view const& s){};
219-
virtual void replyMessage(std::string_view const& s){};
234+
virtual void body(char* data, size_t s) {};
235+
virtual void replyVersion(std::string_view const& s) {};
236+
virtual void replyCode(std::string_view const& s) {};
237+
virtual void replyMessage(std::string_view const& s) {};
220238
};
221239

222240
struct HTTPParserHelpers {

Framework/Foundation/include/Framework/Endian.h

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

0 commit comments

Comments
 (0)