From 9c50fab1e52d15a9a82b81fafafabcac062b52ac Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Tue, 30 Jul 2024 15:56:41 +0200 Subject: [PATCH] #268: Remove validate_memory_ member from UnpackerBuffer --- src/checkpoint/dispatch/dispatch.impl.h | 46 +++++++++++--------- src/checkpoint/serializers/base_serializer.h | 7 --- src/checkpoint/serializers/unpacker.h | 6 +-- src/checkpoint/serializers/unpacker.impl.h | 13 ++---- 4 files changed, 29 insertions(+), 43 deletions(-) diff --git a/src/checkpoint/dispatch/dispatch.impl.h b/src/checkpoint/dispatch/dispatch.impl.h index c5d4afc6..faf35db1 100644 --- a/src/checkpoint/dispatch/dispatch.impl.h +++ b/src/checkpoint/dispatch/dispatch.impl.h @@ -120,7 +120,7 @@ TraverserT& withMemUsed(TraverserT& t, SerialSizeType len) { auto val = cleanType(&serMemUsed); ap(t, val, memUsedLen); - if (t.shouldValidateMemory() && memUsed != serMemUsed) { + if (memUsed != serMemUsed) { using CleanT = typename CleanType::CleanT; std::string msg = "For type '" + typeregistry::getTypeName() + "' serialization used " + std::to_string(serMemUsed) + @@ -291,31 +291,17 @@ void deserializeType(InPlaceTag, SerialByteType* data, T* t) { Standard::unpack>(t, data); } -template -void validatePrefix(vrt::TypeIdx prefix) { - if (!vrt::objregistry::isValidIdx(prefix)) { - std::string const err = std::string("Unpacking invalid prefix type (") + - std::to_string(prefix) + std::string(") from object registry for type=") + - std::string(typeregistry::getTypeName()); - throw serialization_error(err); - } -} - template struct PrefixedType { using BaseType = vrt::checkpoint_base_type_t; - explicit PrefixedType(T* target) : target_(target) { + // Create PrefixedType for serialization purposes + explicit PrefixedType(BaseType* target) : target_(target) { prefix_ = target->_checkpointDynamicTypeIndex(); } - explicit PrefixedType(SerialByteType* allocBuf) - : unpack_buf_(allocBuf) { - } - - vrt::TypeIdx prefix_ = 0; - T* target_ = nullptr; - SerialByteType* unpack_buf_ = nullptr; + // Create PrefixedType for deserialization purposes + explicit PrefixedType(SerialByteType* allocBuf) : unpack_buf_(allocBuf) { } template void serialize(SerializerT& s) { @@ -323,7 +309,7 @@ struct PrefixedType { // Determine the correct type and allocate memory if (s.isUnpacking()) { - validatePrefix(prefix_); + validatePrefix(prefix_); auto mem = unpack_buf_ ? unpack_buf_ : vrt::objregistry::allocateConcreteType(prefix_); target_ = vrt::objregistry::constructConcreteType(prefix_, mem); @@ -331,6 +317,24 @@ struct PrefixedType { s | *target_; } + + BaseType* getTarget() const { + return target_; + } + +private: + void validatePrefix(vrt::TypeIdx prefix) { + if (!vrt::objregistry::isValidIdx(prefix)) { + std::string const err = std::string("Unpacking invalid prefix type (") + + std::to_string(prefix) + std::string(") from object registry for type=") + + std::string(typeregistry::getTypeName()); + throw serialization_error(err); + } + } + + vrt::TypeIdx prefix_ = 0; + BaseType* target_ = nullptr; + SerialByteType* unpack_buf_ = nullptr; }; template @@ -355,7 +359,7 @@ deserializeType(SerialByteType* data, SerialByteType* allocBuf) { auto prefixed = PrefixedType(allocBuf); auto* traverser = Standard::unpack>(&prefixed, data); - return static_cast(traverser->target_); + return static_cast(traverser->getTarget()); } }} /* end namespace checkpoint::dispatch */ diff --git a/src/checkpoint/serializers/base_serializer.h b/src/checkpoint/serializers/base_serializer.h index 5bcd5904..e64251cc 100644 --- a/src/checkpoint/serializers/base_serializer.h +++ b/src/checkpoint/serializers/base_serializer.h @@ -203,13 +203,6 @@ struct BaseSerializer { */ void setVirtualDisabled(bool val) { virtual_disabled_ = val; } - /** - * \brief Check if used memory should be validated - * - * \return whether memory should be validated - */ - bool shouldValidateMemory() const { return true; } - protected: ModeType cur_mode_ = ModeType::Invalid; /**< The current mode */ bool virtual_disabled_ = false; /**< Virtual serialization disabled */ diff --git a/src/checkpoint/serializers/unpacker.h b/src/checkpoint/serializers/unpacker.h index 4c31f82b..85e9a850 100644 --- a/src/checkpoint/serializers/unpacker.h +++ b/src/checkpoint/serializers/unpacker.h @@ -54,7 +54,7 @@ template struct UnpackerBuffer : MemorySerializer { using BufferPtrType = std::unique_ptr; - explicit UnpackerBuffer(SerialByteType* buf, bool validate = true); + explicit UnpackerBuffer(SerialByteType* buf); template explicit UnpackerBuffer(Args&&... args); @@ -62,15 +62,11 @@ struct UnpackerBuffer : MemorySerializer { void contiguousBytes(void* ptr, SerialSizeType size, SerialSizeType num_elms); SerialSizeType usedBufferSize() const; - bool shouldValidateMemory() const; - private: // Size of the actually used memory (for error checking) SerialSizeType usedSize_ = 0; BufferPtrType buffer_ = nullptr; - - bool validate_memory_ = true; }; using Unpacker = UnpackerBuffer; diff --git a/src/checkpoint/serializers/unpacker.impl.h b/src/checkpoint/serializers/unpacker.impl.h index 23faaeb7..ad2429b2 100644 --- a/src/checkpoint/serializers/unpacker.impl.h +++ b/src/checkpoint/serializers/unpacker.impl.h @@ -54,10 +54,9 @@ namespace checkpoint { template -UnpackerBuffer::UnpackerBuffer(SerialByteType* buf, bool validate) +UnpackerBuffer::UnpackerBuffer(SerialByteType* buf) : MemorySerializer(ModeType::Unpacking), - buffer_(std::make_unique(buf, 0)), - validate_memory_(validate) + buffer_(std::make_unique(buf, 0)) { MemorySerializer::initializeBuffer(buffer_->getBuffer()); @@ -72,8 +71,7 @@ template template UnpackerBuffer::UnpackerBuffer(Args&&... args) : MemorySerializer(ModeType::Unpacking), - buffer_(std::make_unique(std::forward(args)...)), - validate_memory_(true) + buffer_(std::make_unique(std::forward(args)...)) { MemorySerializer::initializeBuffer(buffer_->getBuffer()); @@ -105,11 +103,6 @@ SerialSizeType UnpackerBuffer::usedBufferSize() const { return usedSize_; } -template -bool UnpackerBuffer::shouldValidateMemory() const { - return validate_memory_; -} - } /* end namespace checkpoint */ #endif /*INCLUDED_SRC_CHECKPOINT_SERIALIZERS_UNPACKER_IMPL_H*/