# HG changeset patch # User Sebastien Jodogne # Date 1779034227 -7200 # Node ID ebd42c3fb06ad21792e49aab2c5527075017e40d # Parent f45d29cd02dfef69106f22a275786137beddc185 added IDataIdentifier::EstimateValueSize() diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp --- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp Sun May 17 18:10:27 2026 +0200 @@ -32,7 +32,8 @@ { DataSourceMemoryBudget::DataSourceMemoryBudget(uint64_t maximumMemory) : maximumMemory_(maximumMemory), - currentMemory_(0) + currentMemory_(0), + reservations_(0) { } @@ -41,13 +42,14 @@ { boost::mutex::scoped_lock lock(mutex_); - while (currentMemory_ != 0 && // Make sure that 1 thread can always proceed + while (reservations_ != 0 && // Make sure that 1 thread can always proceed currentMemory_ + memory > maximumMemory_) { cond_.wait(lock); } currentMemory_ += memory; + reservations_++; } @@ -58,6 +60,9 @@ assert(currentMemory_ >= memory); currentMemory_ -= memory; + assert(reservations_ > 0); + reservations_--; + cond_.notify_all(); } diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h --- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h Sun May 17 18:10:27 2026 +0200 @@ -44,6 +44,7 @@ boost::condition_variable cond_; const uint64_t maximumMemory_; uint64_t currentMemory_; + unsigned int reservations_; public: explicit DataSourceMemoryBudget(uint64_t maximumMemory); @@ -53,6 +54,27 @@ void Release(size_t memory) ORTHANC_NOEXCEPT; uint64_t GetCurrentMemory() ORTHANC_NOEXCEPT; + + class Lock : public boost::noncopyable + { + private: + DataSourceMemoryBudget& that_; + size_t memory_; + + public: + explicit Lock(DataSourceMemoryBudget& that, + size_t memory) : + that_(that), + memory_(memory) + { + that_.Acquire(memory_); + } + + ~Lock() + { + that_.Release(memory_); + } + }; }; } } diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/Sources/DataSource/DataSourceReader.cpp --- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Sun May 17 18:10:27 2026 +0200 @@ -84,6 +84,14 @@ if (!value) { + std::unique_ptr preReservation; + + size_t estimatedSize = 0; + if (id_->EstimateValueSize(estimatedSize)) + { + preReservation.reset(new Internals::DataSourceMemoryBudget::Lock(*budget_, estimatedSize)); + } + value.reset(source_.Load(*id_)); if (!error && !value) diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/Sources/DataSource/IDataIdentifier.h --- a/OrthancFramework/Sources/DataSource/IDataIdentifier.h Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/IDataIdentifier.h Sun May 17 18:10:27 2026 +0200 @@ -40,5 +40,7 @@ // If this method return "false", the loaded value bypasses the reader cache (if present) virtual bool GetCacheKey(std::string& key) const = 0; + + virtual bool EstimateValueSize(size_t& target) const = 0; }; } diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp --- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Sun May 17 18:10:27 2026 +0200 @@ -84,6 +84,10 @@ end_(end), customData_(customData) { + if (start > end) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } } Value* Read(IPluginStorageArea& area) const @@ -91,7 +95,7 @@ return new Value(area.ReadRange(uuid_, type_, start_, end_, customData_)); } - bool GetCacheKey(std::string& key) const + virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE { // custom data is not part of the cache key, as it is for internal use by the plugin // (it is opaque to the Orthanc core) @@ -101,6 +105,12 @@ boost::lexical_cast(end_)); return true; } + + virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE + { + assert(start_ <= end_); + return end_ - start_; + } }; diff -r f45d29cd02df -r ebd42c3fb06a OrthancFramework/UnitTestsSources/JobsTests.cpp --- a/OrthancFramework/UnitTestsSources/JobsTests.cpp Sun May 17 17:51:50 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/JobsTests.cpp Sun May 17 18:10:27 2026 +0200 @@ -1944,6 +1944,13 @@ boost::lexical_cast(attachment_.GetContentType())); return true; } + + virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE + { + // TODO - Support "read until pixel data" + target = attachment_.GetUncompressedSize(); + return true; + } }; class Value : public IDynamicObject