Skip to content

Commit 9272d70

Browse files
committed
Support serializing Span<unsigned char> and use that instead of FLATDATA
1 parent 833bc08 commit 9272d70

File tree

3 files changed

+16
-48
lines changed

3 files changed

+16
-48
lines changed

src/compressor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <primitives/transaction.h>
1010
#include <script/script.h>
1111
#include <serialize.h>
12+
#include <span.h>
1213

1314
class CKeyID;
1415
class CPubKey;
@@ -51,12 +52,12 @@ class CScriptCompressor
5152
void Serialize(Stream &s) const {
5253
std::vector<unsigned char> compr;
5354
if (CompressScript(script, compr)) {
54-
s << CFlatData(compr);
55+
s << MakeSpan(compr);
5556
return;
5657
}
5758
unsigned int nSize = script.size() + nSpecialScripts;
5859
s << VARINT(nSize);
59-
s << CFlatData(script);
60+
s << MakeSpan(script);
6061
}
6162

6263
template<typename Stream>
@@ -65,7 +66,7 @@ class CScriptCompressor
6566
s >> VARINT(nSize);
6667
if (nSize < nSpecialScripts) {
6768
std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
68-
s >> CFlatData(vch);
69+
s >> MakeSpan(vch);
6970
DecompressScript(script, nSize, vch);
7071
return;
7172
}
@@ -76,7 +77,7 @@ class CScriptCompressor
7677
s.ignore(nSize);
7778
} else {
7879
script.resize(nSize);
79-
s >> CFlatData(script);
80+
s >> MakeSpan(script);
8081
}
8182
}
8283
};

src/netaddress.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <compat.h>
1313
#include <serialize.h>
14+
#include <span.h>
1415

1516
#include <stdint.h>
1617
#include <string>
@@ -167,10 +168,13 @@ class CService : public CNetAddr
167168
template <typename Stream, typename Operation>
168169
inline void SerializationOp(Stream& s, Operation ser_action) {
169170
READWRITE(ip);
171+
172+
// TODO: introduce native support for BE serialization in serialize.h
170173
unsigned short portN = htons(port);
171-
READWRITE(FLATDATA(portN));
172-
if (ser_action.ForRead())
174+
READWRITE(Span<unsigned char>((unsigned char*)&portN, 2));
175+
if (ser_action.ForRead()) {
173176
port = ntohs(portN);
177+
}
174178
}
175179
};
176180

src/serialize.h

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include <prevector.h>
25+
#include <span.h>
2526

2627
static const unsigned int MAX_SIZE = 0x02000000;
2728

@@ -41,7 +42,7 @@ constexpr deserialize_type deserialize {};
4142

4243
/**
4344
* Used to bypass the rule against non-const reference to temporary
44-
* where it makes sense with wrappers such as CFlatData or CTxDB
45+
* where it makes sense with wrappers.
4546
*/
4647
template<typename T>
4748
inline T& REF(const T& val)
@@ -185,6 +186,8 @@ template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_wri
185186
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
186187
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
187188
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
189+
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
190+
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
188191

189192
template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
190193
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
@@ -199,6 +202,7 @@ template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a =
199202
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
200203
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
201204
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
205+
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
202206

203207
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
204208
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
@@ -384,51 +388,10 @@ I ReadVarInt(Stream& is)
384388
}
385389
}
386390

387-
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
388391
#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
389392
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
390393
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
391394

392-
/**
393-
* Wrapper for serializing arrays and POD.
394-
*/
395-
class CFlatData
396-
{
397-
protected:
398-
char* pbegin;
399-
char* pend;
400-
public:
401-
CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
402-
template <class T, class TAl>
403-
explicit CFlatData(std::vector<T,TAl> &v)
404-
{
405-
pbegin = (char*)v.data();
406-
pend = (char*)(v.data() + v.size());
407-
}
408-
template <unsigned int N, typename T, typename S, typename D>
409-
explicit CFlatData(prevector<N, T, S, D> &v)
410-
{
411-
pbegin = (char*)v.data();
412-
pend = (char*)(v.data() + v.size());
413-
}
414-
char* begin() { return pbegin; }
415-
const char* begin() const { return pbegin; }
416-
char* end() { return pend; }
417-
const char* end() const { return pend; }
418-
419-
template<typename Stream>
420-
void Serialize(Stream& s) const
421-
{
422-
s.write(pbegin, pend - pbegin);
423-
}
424-
425-
template<typename Stream>
426-
void Unserialize(Stream& s)
427-
{
428-
s.read(pbegin, pend - pbegin);
429-
}
430-
};
431-
432395
template<VarIntMode Mode, typename I>
433396
class CVarInt
434397
{

0 commit comments

Comments
 (0)