Replace the lambda function by an inlined function

This commit is contained in:
Xiaoge Su 2023-01-12 11:40:34 -08:00
parent 0f695e8e6c
commit d7e3b1b301
3 changed files with 17 additions and 10 deletions

View File

@ -233,6 +233,8 @@ class ObjectWriter {
};
public:
typedef uint8_t* (*CustomAllocatorFunc_t)(const size_t, void*);
template <class VersionOptions>
explicit ObjectWriter(VersionOptions vo) : customAllocator(nullptr), customAllocatorContext(nullptr) {
vo.write(*this);
@ -242,7 +244,7 @@ public:
// capture. By downgrading it to a function pointer, the compile time can be reduced. The trade is an additional
// void* must be used to carry the captured environment.
template <class VersionOptions>
explicit ObjectWriter(uint8_t* (*customAllocator_)(size_t, void*), void* customAllocatorContext_, VersionOptions vo)
explicit ObjectWriter(CustomAllocatorFunc_t customAllocator_, void* customAllocatorContext_, VersionOptions vo)
: customAllocator(customAllocator_), customAllocatorContext(customAllocatorContext_) {
vo.write(*this);
}
@ -283,7 +285,7 @@ public:
private:
Arena arena;
uint8_t* (*customAllocator)(size_t, void*) = nullptr;
CustomAllocatorFunc_t customAllocator = nullptr;
void* customAllocatorContext = nullptr;
uint8_t* data = nullptr;
int size = 0;

View File

@ -868,6 +868,9 @@ struct PacketWriter {
return result;
}
// This is used by MakeSerializeSource::serializePacketWriter
static uint8_t* packetWriterAlloc(const size_t size, void* self);
private:
void serializeBytesAcrossBoundary(const void* data, int bytes);
void nextBuffer(size_t size = 0 /* downstream it will default to at least 4k minus some padding */);
@ -886,14 +889,12 @@ template <class T, class V>
class MakeSerializeSource : public ISerializeSource {
public:
using value_type = V;
void serializePacketWriter(PacketWriter& w) const override {
ObjectWriter writer(
+[](size_t size, void* pPacketWriter) {
return static_cast<PacketWriter*>(pPacketWriter)->writeBytes(size);
},
&w,
AssumeVersion(w.protocolVersion()));
writer.serialize(get()); // Writes directly into buffer supplied by |w|
void serializePacketWriter(PacketWriter& packetWriter) const override {
ObjectWriter objectWriter(
PacketWriter::packetWriterAlloc, &packetWriter, AssumeVersion(packetWriter.protocolVersion()));
// Writes directly into buffer supplied by packetWriter
objectWriter.serialize(get());
}
virtual value_type const& get() const = 0;
};

View File

@ -42,6 +42,10 @@ const void* BinaryReader::readBytes(int bytes) {
return b;
}
uint8_t* PacketWriter::packetWriterAlloc(const size_t size, void* self) {
return static_cast<PacketWriter*>(self)->writeBytes(size);
}
namespace {
struct _Struct {