Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Do not throw from ~AllocatedTensor (#30291)

Oleg Pipikin03e885aa0af2
diff --git a/src/core/include/openvino/runtime/allocator.hpp b/src/core/include/openvino/runtime/allocator.hpp
index ae62fb7b290..14e31395a1b 100644
--- a/src/core/include/openvino/runtime/allocator.hpp
+++ b/src/core/include/openvino/runtime/allocator.hpp
@@ -44,7 +44,7 @@ class OPENVINO_API Allocator {
}
virtual const std::type_info& type_info() const = 0;
virtual void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)) = 0;
- virtual void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) = 0;
+ virtual void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) noexcept = 0;
virtual bool is_equal(const Base& other) const = 0;
protected:
@@ -64,7 +64,7 @@ class OPENVINO_API Allocator {
void* allocate(const size_t bytes, const size_t alignment = alignof(max_align_t)) override {
return a.allocate(bytes, alignment);
}
- void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) override {
+ void deallocate(void* handle, const size_t bytes, size_t alignment = alignof(max_align_t)) noexcept override {
a.deallocate(handle, bytes, alignment);
}
bool is_equal(const Base& other) const override {
@@ -76,6 +76,22 @@ class OPENVINO_API Allocator {
A a;
};
+ template <typename T, typename = void>
+ struct HasNoexceptDeallocate : std::false_type {};
+
+ template <typename T>
+ struct HasNoexceptDeallocate<
+ T,
+ std::void_t<decltype(std::declval<std::decay_t<T>>().deallocate(std::declval<void*>(),
+ std::declval<const size_t>(),
+ std::declval<const size_t>()))>>
+ : std::bool_constant<noexcept(std::declval<std::decay_t<T>>().deallocate(std::declval<void*>(),
+ std::declval<const size_t>(),
+ std::declval<const size_t>()))> {};
+
+ template <typename T>
+ static constexpr auto has_noexcept_deallocate_v = HasNoexceptDeallocate<T>::value;
+
std::shared_ptr<Base> _impl;
std::shared_ptr<void> _so;
@@ -113,11 +129,22 @@ public:
*/
template <
typename A,
- typename std::enable_if<!std::is_same<typename std::decay<A>::type, Allocator>::value &&
- !std::is_abstract<typename std::decay<A>::type>::value &&
- !std::is_convertible<typename std::decay<A>::type, std::shared_ptr<Base>>::value,
- bool>::type = true>
- Allocator(A&& a) : _impl{std::make_shared<Impl<typename std::decay<A>::type>>(std::forward<A>(a))} {}
+ typename std::enable_if_t<
+ !std::is_same_v<typename std::decay_t<A>, Allocator> && !std::is_abstract_v<typename std::decay_t<A>> &&
+ !std::is_convertible_v<typename std::decay_t<A>, std::shared_ptr<Base>> && has_noexcept_deallocate_v<A>,
+ bool> = true>
+ Allocator(A&& a) : _impl{std::make_shared<Impl<typename std::decay_t<A>>>(std::forward<A>(a))} {}
+
+ template <typename A,
+ typename std::enable_if_t<!std::is_same_v<typename std::decay_t<A>, Allocator> &&
+ !std::is_abstract_v<typename std::decay_t<A>> &&
+ !std::is_convertible_v<typename std::decay_t<A>, std::shared_ptr<Base>> &&
+ !has_noexcept_deallocate_v<A>,
+ bool> = true>
+ OPENVINO_DEPRECATED("Please annotate your allocator's deallocate method with noexcept. This method will be "
+ "removed in 2026.0.0 release")
+ Allocator(A&& a)
+ : _impl{std::make_shared<Impl<typename std::decay_t<A>>>(std::forward<A>(a))} {}
/**
* @brief Allocates memory
@@ -135,7 +162,7 @@ public:
* @param bytes The size in bytes that was passed into allocate() method
* @param alignment The alignment of storage that was passed into allocate() method
*/
- void deallocate(void* ptr, const size_t bytes = 0, const size_t alignment = alignof(max_align_t));
+ void deallocate(void* ptr, const size_t bytes = 0, const size_t alignment = alignof(max_align_t)) noexcept;
/**
* @brief Compares with other Allocator
diff --git a/src/core/src/runtime/allocator.cpp b/src/core/src/runtime/allocator.cpp
index 729a61d9b0d..b119ad48822 100644
--- a/src/core/src/runtime/allocator.cpp
+++ b/src/core/src/runtime/allocator.cpp
@@ -28,7 +28,7 @@ struct DefaultAllocator {
}
}
- void deallocate(void* handle, const size_t bytes, const size_t alignment) {
+ void deallocate(void* handle, const size_t bytes, const size_t alignment) noexcept {
if (alignment == alignof(max_align_t)) {
::operator delete(handle);
} else {
@@ -67,11 +67,19 @@ Allocator::Allocator(const Allocator& other, const std::shared_ptr<void>& so) :
OPENVINO_ASSERT(false, "Unexpected exception"); \
}
+#define OV_ALLOCATOR_NO_THROW_STATEMENT(...) \
+ try { \
+ if (_impl) { \
+ __VA_ARGS__; \
+ } \
+ } catch (...) { \
+ }
+
void* Allocator::allocate(const size_t bytes, const size_t alignment) {
OV_ALLOCATOR_STATEMENT(return _impl->allocate(bytes, alignment));
}
-void Allocator::deallocate(void* handle, const size_t bytes, const size_t alignment) {
- OV_ALLOCATOR_STATEMENT(_impl->deallocate(handle, bytes, alignment));
+void Allocator::deallocate(void* handle, const size_t bytes, const size_t alignment) noexcept {
+ OV_ALLOCATOR_NO_THROW_STATEMENT(_impl->deallocate(handle, bytes, alignment));
}
bool Allocator::operator==(const Allocator& other) const {
OV_ALLOCATOR_STATEMENT({
diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cpp
index 6b318428f0f..be6e350e6c9 100644
--- a/src/core/tests/ov_tensor_test.cpp
+++ b/src/core/tests/ov_tensor_test.cpp
@@ -199,7 +199,7 @@ struct OVMockAllocator {
return impl->allocate(b, a);
}
- void deallocate(void* ptr, size_t b, size_t a) {
+ void deallocate(void* ptr, size_t b, size_t a) noexcept {
impl->deallocate(ptr, b, a);
}
bool is_equal(const OVMockAllocator& other) const {