# HG changeset patch # User Sebastien Jodogne # Date 1749725344 -7200 # Node ID 7f985407c885f1a5311c350c3d193580eb1f74d0 # Parent 0d540324c14fbcc5b1b43ddbf3c220f43ff51b65 refactoring using PluginMemoryBuffer64 diff -r 0d540324c14f -r 7f985407c885 OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu Jun 12 12:29:39 2025 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu Jun 12 12:49:04 2025 +0200 @@ -54,7 +54,6 @@ #include "../../../OrthancFramework/Sources/MetricsRegistry.h" #include "../../../OrthancFramework/Sources/OrthancException.h" #include "../../../OrthancFramework/Sources/SerializationToolbox.h" -#include "../../../OrthancFramework/Sources/StringMemoryBuffer.h" #include "../../../OrthancFramework/Sources/Toolbox.h" #include "../../Sources/Database/VoidDatabaseListener.h" #include "../../Sources/OrthancConfiguration.h" @@ -592,7 +591,7 @@ namespace { - static IMemoryBuffer* GetRangeFromWhole(std::unique_ptr& whole, + static IMemoryBuffer* GetRangeFromWhole(std::unique_ptr& whole, uint64_t start /* inclusive */, uint64_t end /* exclusive */) { @@ -602,7 +601,7 @@ } else if (start == end) { - return new StringMemoryBuffer; // Empty + return new PluginMemoryBuffer64; // Empty } else { @@ -617,14 +616,11 @@ } else { - std::string range; - range.resize(end - start); - assert(!range.empty()); - - memcpy(&range[0], reinterpret_cast(whole->GetData()) + start, range.size()); - - whole.reset(NULL); - return StringMemoryBuffer::CreateFromSwap(range); + std::unique_ptr range(new PluginMemoryBuffer64); + range->Assign(reinterpret_cast(whole->GetData()) + start, end - start); + assert(range->GetSize() > 0); + + return range.release(); } } } @@ -713,6 +709,8 @@ uint64_t start /* inclusive */, uint64_t end /* exclusive */) ORTHANC_OVERRIDE { + std::unique_ptr whole(new MallocMemoryBuffer); + void* buffer = NULL; int64_t size = 0; @@ -720,8 +718,10 @@ if (error == OrthancPluginErrorCode_Success) { - std::unique_ptr whole(new MallocMemoryBuffer); - whole->Assign(buffer, size, free_); + // Beware that the buffer must be unallocated by the "free_" function provided by the plugin, + // so we cannot use "PluginMemoryBuffer64" + dynamic_cast(*whole).Assign(buffer, size, free_); + return GetRangeFromWhole(whole, start, end); } else @@ -765,16 +765,13 @@ { if (readRange_ == NULL) { - OrthancPluginMemoryBuffer64 buffer; - buffer.size = 0; - buffer.data = NULL; - - OrthancPluginErrorCode error = readWhole_(&buffer, uuid.c_str(), Plugins::Convert(type)); + std::unique_ptr whole(new PluginMemoryBuffer64); + + OrthancPluginErrorCode error = readWhole_(dynamic_cast(*whole).GetObject(), + uuid.c_str(), Plugins::Convert(type)); if (error == OrthancPluginErrorCode_Success) { - std::unique_ptr whole(new MallocMemoryBuffer); - whole->Assign(buffer.data, buffer.size, ::free); return GetRangeFromWhole(whole, start, end); } else @@ -830,8 +827,7 @@ OrthancPluginStorageCreate2 create_; OrthancPluginStorageReadRange2 readRange_; OrthancPluginStorageRemove2 remove_; - - PluginsErrorDictionary& errorDictionary_; + PluginsErrorDictionary& errorDictionary_; protected: PluginsErrorDictionary& GetErrorDictionary() const @@ -914,24 +910,20 @@ } else if (start == end) { - return new StringMemoryBuffer; + return new PluginMemoryBuffer64; } else { - std::string range; - range.resize(end - start); - assert(!range.empty()); - - OrthancPluginMemoryBuffer64 buffer; - buffer.data = &range[0]; - buffer.size = static_cast(range.size()); + std::unique_ptr buffer(new PluginMemoryBuffer64); + buffer->Resize(end - start); + assert(buffer->GetSize() > 0); OrthancPluginErrorCode error = - readRange_(&buffer, uuid.c_str(), Plugins::Convert(type), start, customData.empty() ? NULL : customData.c_str(), customData.size()); + readRange_(buffer->GetObject(), uuid.c_str(), Plugins::Convert(type), start, customData.empty() ? NULL : customData.c_str(), customData.size()); if (error == OrthancPluginErrorCode_Success) { - return StringMemoryBuffer::CreateFromSwap(range); + return buffer.release(); } else {