blob: 6985270d9e501177b5279e6a0471103c82cca8f4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#pragma once
#include "defaults.h"
#include "yassert.h"
#include <string.h>
#include <type_traits>
// The following code used to have smart tricks assuming that unaligned reads and writes are OK on x86. This assumption
// is wrong because compiler may emit alignment-sensitive x86 instructions e.g. movaps. See IGNIETFERRO-735.
template <class T>
inline T ReadUnaligned(const void* from) noexcept {
T ret;
memcpy(&ret, from, sizeof(T));
return ret;
}
// std::remove_reference_t for non-deduced context to prevent such code to blow below:
// ui8 first = f(); ui8 second = g();
// WriteUnaligned(to, first - second) (int will be deduced)
template <class T>
inline void WriteUnaligned(void* to, const std::remove_reference_t<T>& t) noexcept {
memcpy(to, &t, sizeof(T));
}
template <class T, unsigned Align = sizeof(T)>
class TUnalignedMemoryIterator {
public:
inline TUnalignedMemoryIterator(const void* buf, size_t len)
: C_((const unsigned char*)buf)
, E_(C_ + len)
, L_(E_ - (len % Align))
{
Y_FAKE_READ(buf);
}
inline bool AtEnd() const noexcept {
return C_ == L_;
}
inline T Cur() const noexcept {
Y_ASSERT(C_ < L_ || sizeof(T) < Align);
return ::ReadUnaligned<T>(C_);
}
inline T Next() noexcept {
T ret(Cur());
C_ += sizeof(T);
return ret;
}
inline const unsigned char* Last() const noexcept {
return C_;
}
inline size_t Left() const noexcept {
return E_ - C_;
}
private:
const unsigned char* C_;
const unsigned char* E_;
const unsigned char* L_;
};
|