OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Implement aligned memory allocation utilities and tests (#35872)
Pawel Raasz7f129faa7cc5
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hppnew file mode 100644index 00000000000..f1a6c8bd818--- /dev/null+++ b/src/common/util/include/openvino/util/memory.hpp@@ -0,0 +1,42 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <cstddef>+#include <cstdlib>++namespace ov::util {++/**+ * @brief Rounds @p size up to the nearest multiple of @p alignment.+ *+ * @param size Value to round up.+ * @param alignment Alignment boundary. Must be a power of two and greater than zero.+ * @return Smallest value >= @p size that is a multiple of @p alignment.+ */+constexpr size_t align_size_up(size_t size, size_t alignment) noexcept {+ return (size + alignment - 1) & ~(alignment - 1);+}++/**+ * @brief Allocates @p size bytes of uninitialized memory on the specified @p alignment boundary.+ *+ * Cross-platform wrapper: for aligned memory allocation and deallocation.+ *+ * @param size Number of bytes to allocate. Must be greater than zero.+ * @param alignment Desired alignment in bytes. Must be a power of two.+ * Passing `0` applies no specific alignment constraint (`alignof(std::max_align_t)` is used).+ * @return Pointer to the allocated memory, or `nullptr` on failure.+ */+void* aligned_alloc(size_t size, size_t alignment) noexcept;++/**+ * @brief Releases memory previously allocated by @ref aligned_alloc.+ *+ * @param ptr Pointer returned by @ref aligned_alloc. Passing `nullptr` is a no-op.+ */+void aligned_free(void* ptr) noexcept;++} // namespace ov::utildiff --git a/src/common/util/src/os/lin/lin_memory.cpp b/src/common/util/src/os/lin/lin_memory.cppnew file mode 100644index 00000000000..feec8bc1003--- /dev/null+++ b/src/common/util/src/os/lin/lin_memory.cpp@@ -0,0 +1,23 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <cstddef>+#include <cstdlib>++#include "openvino/util/memory.hpp"++namespace ov::util {++void* aligned_alloc(size_t size, size_t alignment) noexcept {+ if (alignment == 0) {+ alignment = alignof(std::max_align_t);+ }+ return std::aligned_alloc(alignment, align_size_up(size, alignment));+}++void aligned_free(void* ptr) noexcept {+ std::free(ptr);+}++} // namespace ov::utildiff --git a/src/common/util/src/os/win/win_memory.cpp b/src/common/util/src/os/win/win_memory.cppnew file mode 100644index 00000000000..5fe38d3d343--- /dev/null+++ b/src/common/util/src/os/win/win_memory.cpp@@ -0,0 +1,23 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <cstddef>+#include <malloc.h>++#include "openvino/util/memory.hpp"++namespace ov::util {++void* aligned_alloc(size_t size, size_t alignment) noexcept {+ if (alignment == 0) {+ alignment = alignof(std::max_align_t);+ }+ return _aligned_malloc(size, alignment);+}++void aligned_free(void* ptr) noexcept {+ _aligned_free(ptr);+}++} // namespace ov::utildiff --git a/src/core/dev_api/openvino/runtime/aligned_buffer.hpp b/src/core/dev_api/openvino/runtime/aligned_buffer.hppindex 7db74b66374..d43b37799ca 100644--- a/src/core/dev_api/openvino/runtime/aligned_buffer.hpp+++ b/src/core/dev_api/openvino/runtime/aligned_buffer.hpp@@ -22,10 +22,7 @@ public:virtual ~IBufferDescriptor();};-/// \brief Allocates a block of memory on the specified alignment. The actual size of the-/// allocated memory is larger than the requested size by the alignment, so allocating 1-/// byte-/// on 64 byte alignment will allocate 65 bytes.+/// \brief Allocates a block of memory which starts at an aligned address determined by \p alignment.class OPENVINO_API AlignedBuffer {public:// Allocator objects and the allocation interfaces are owned by the@@ -76,7 +73,6 @@ protected:virtual void hint_evict(size_t offset, size_t size) noexcept;static void invoke_evict(AlignedBuffer& buffer, size_t offset, size_t size) noexcept;- char* m_allocated_buffer;char* m_aligned_buffer;size_t m_byte_size;};@@ -87,6 +83,6 @@ class OPENVINO_API AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>>public:AttributeAdapter(std::shared_ptr<ov::AlignedBuffer>& value);~AttributeAdapter() override;- OPENVINO_RTTI("AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>");+ OPENVINO_RTTI("AttributeAdapter<std::shared_ptr<ov::AlignedBuffer>>");};} // namespace ovdiff --git a/src/core/dev_api/openvino/runtime/shared_buffer.hpp b/src/core/dev_api/openvino/runtime/shared_buffer.hppindex ef364030d17..005838fa1af 100644--- a/src/core/dev_api/openvino/runtime/shared_buffer.hpp+++ b/src/core/dev_api/openvino/runtime/shared_buffer.hpp@@ -7,6 +7,7 @@#include <type_traits>#include "openvino/runtime/aligned_buffer.hpp"+#include "openvino/util/common_util.hpp"#include "openvino/util/mmap_object.hpp"namespace ov {@@ -26,7 +27,6 @@ public:virtual ~SharedBufferBase() {m_aligned_buffer = nullptr;- m_allocated_buffer = nullptr;m_byte_size = 0;}@@ -60,26 +60,32 @@ protected:const std::shared_ptr<IBufferDescriptor>& descriptor): m_shared_object{shared_object},m_source_buffer{descriptor ? descriptor->get_source_buffer() : nullptr},- m_descriptor{} {- m_allocated_buffer = nullptr;+ m_descriptor{[&] {+ if (descriptor) {+ uintptr_t offset{};+ if (m_source_buffer) {+ const auto src_start = reinterpret_cast<uintptr_t>(m_source_buffer->get_ptr());+ const auto src_end = src_start + static_cast<uintptr_t>(m_source_buffer->size());+ const auto current = reinterpret_cast<uintptr_t>(data);+ const auto data_in_src_range = (current >= src_start) &&+ !ov::util::add_overflow(current, size, offset) &&+ (offset <= src_end);+ OPENVINO_ASSERT(data_in_src_range, "SharedBuffer data range is outside source buffer range");+ offset = current - src_start;+ }+ return create_base_descriptor(descriptor->get_id(), offset, m_source_buffer);+ } else {+ return std::shared_ptr<IBufferDescriptor>{};+ }+ }()} {m_aligned_buffer = data;m_byte_size = size;- if (descriptor) {- if (m_source_buffer) {- auto source_start = reinterpret_cast<uintptr_t>(m_source_buffer->get_ptr());- auto current = reinterpret_cast<uintptr_t>(m_aligned_buffer);- OPENVINO_ASSERT(current >= source_start && current <= source_start + m_source_buffer->size(),- "SharedBuffer data pointer is outside source buffer range");- }- m_descriptor = create_base_descriptor(descriptor->get_id(), get_offset(), m_source_buffer);- }}SharedBufferBase(char* data, size_t size, const T& shared_object): m_shared_object{shared_object},m_source_buffer{},m_descriptor{} {- m_allocated_buffer = nullptr;m_aligned_buffer = data;m_byte_size = size;}diff --git a/src/core/src/runtime/aligned_buffer.cpp b/src/core/src/runtime/aligned_buffer.cppindex b84665bf528..62ab4cad983 100644--- a/src/core/src/runtime/aligned_buffer.cpp+++ b/src/core/src/runtime/aligned_buffer.cpp@@ -6,48 +6,32 @@#include <algorithm>#include <memory>+#include <utility>-#include "openvino/core/memory_util.hpp"+#include "openvino/util/memory.hpp"namespace ov {IBufferDescriptor::~IBufferDescriptor() = default;-AlignedBuffer::AlignedBuffer() : m_allocated_buffer(nullptr), m_aligned_buffer(nullptr), m_byte_size(0) {}+AlignedBuffer::AlignedBuffer() : m_aligned_buffer(nullptr), m_byte_size(0) {}-AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment) : m_byte_size(byte_size) {- m_byte_size = std::max<size_t>(1, byte_size);- size_t allocation_size = m_byte_size + alignment;- m_allocated_buffer = new char[allocation_size];- m_aligned_buffer = m_allocated_buffer;- m_aligned_buffer += util::align_padding_size(alignment, reinterpret_cast<size_t>(m_allocated_buffer));+AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment) : m_byte_size(std::max<size_t>(1, byte_size)) {+ m_aligned_buffer = static_cast<char*>(util::aligned_alloc(m_byte_size, alignment));}AlignedBuffer::AlignedBuffer(AlignedBuffer&& other)- : m_allocated_buffer(other.m_allocated_buffer),- m_aligned_buffer(other.m_aligned_buffer),- m_byte_size(other.m_byte_size) {- other.m_allocated_buffer = nullptr;- other.m_aligned_buffer = nullptr;- other.m_byte_size = 0;-}+ : m_aligned_buffer(std::exchange(other.m_aligned_buffer, nullptr)),+ m_byte_size(std::exchange(other.m_byte_size, 0)) {}AlignedBuffer::~AlignedBuffer() {- if (m_allocated_buffer != nullptr) {- delete[] m_allocated_buffer;- }+ util::aligned_free(m_aligned_buffer); // safe with nullptr}AlignedBuffer& AlignedBuffer::operator=(AlignedBuffer&& other) {if (this != &other) {- if (m_allocated_buffer != nullptr) {- delete[] m_allocated_buffer;- }- m_allocated_buffer = other.m_allocated_buffer;- m_aligned_buffer = other.m_aligned_buffer;- m_byte_size = other.m_byte_size;- other.m_allocated_buffer = nullptr;- other.m_aligned_buffer = nullptr;- other.m_byte_size = 0;+ util::aligned_free(m_aligned_buffer);+ m_aligned_buffer = std::exchange(other.m_aligned_buffer, nullptr);+ m_byte_size = std::exchange(other.m_byte_size, 0);}return *this;}diff --git a/src/core/src/runtime/string_aligned_buffer.cpp b/src/core/src/runtime/string_aligned_buffer.cppindex 81c366c7c65..78f35a7e96d 100644--- a/src/core/src/runtime/string_aligned_buffer.cpp+++ b/src/core/src/runtime/string_aligned_buffer.cpp@@ -161,7 +161,7 @@ StringAlignedBuffer::StringAlignedBuffer(size_t num_elements, size_t byte_size,}StringAlignedBuffer::~StringAlignedBuffer() {- if (m_allocated_buffer) {+ if (m_aligned_buffer) {const auto first = get_ptr<std::string>();for_each(first, first + m_num_elements, [](std::string& s) {using std::string;@@ -171,7 +171,6 @@ StringAlignedBuffer::~StringAlignedBuffer() {}SharedStringAlignedBuffer::SharedStringAlignedBuffer(char* ptr, size_t size) {- m_allocated_buffer = ptr;m_aligned_buffer = ptr;m_byte_size = size;m_num_elements = size / ov::element::string.size();@@ -179,7 +178,7 @@ SharedStringAlignedBuffer::SharedStringAlignedBuffer(char* ptr, size_t size) {SharedStringAlignedBuffer::~SharedStringAlignedBuffer() {// to prevent deallocation in parent dtors.- m_allocated_buffer = nullptr;+ m_aligned_buffer = nullptr;}AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>::AttributeAdapter(diff --git a/src/core/tests/aligned_buffer.cpp b/src/core/tests/aligned_buffer.cppindex 92d4599da51..7ddb6bffb0a 100644--- a/src/core/tests/aligned_buffer.cpp+++ b/src/core/tests/aligned_buffer.cpp@@ -6,10 +6,11 @@#include "gtest/gtest.h"-using namespace ov;+namespace ov::test {TEST(aligned_buffer, alignment) {AlignedBuffer buffer(100, 64);+ ASSERT_NE(buffer.get_ptr(), nullptr);size_t addr = reinterpret_cast<size_t>(buffer.get_ptr()) % 64;EXPECT_EQ(addr, 0);}@@ -33,3 +34,4 @@ TEST(aligned_buffer, move) {EXPECT_NE(buffer2.get_ptr(), nullptr);}}+} // namespace ov::testdiff --git a/src/core/tests/weight_sharing_util_test.cpp b/src/core/tests/weight_sharing_util_test.cppindex deaf456195c..008aed8ae87 100644--- a/src/core/tests/weight_sharing_util_test.cpp+++ b/src/core/tests/weight_sharing_util_test.cpp@@ -238,15 +238,15 @@ TEST_F(WeightShareExtensionTest, set_constant_buffer_with_id) {auto buffer = std::make_shared<ov::AlignedBuffer>(4000);auto wt_buffer = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(buffer->get_ptr<char>() + 100,- buffer->size(),+ buffer->size() - 100,buffer,ov::create_base_descriptor(12, 0, buffer));- auto c = Constant(element::f32, Shape{1000}, wt_buffer);+ auto c = Constant(element::f32, Shape{975}, wt_buffer);ASSERT_TRUE(weight_sharing::set_constant(shared_ctx, c));const auto& [const_offset, const_size, const_type] = shared_ctx.m_weight_registry[12][100];EXPECT_EQ(const_offset, 100);- EXPECT_EQ(const_size, 4000);+ EXPECT_EQ(const_size, 4000 - 100);EXPECT_EQ(const_type, element::f32);}diff --git a/src/tests/test_utils/common_test_utils/tests/memory_test.cpp b/src/tests/test_utils/common_test_utils/tests/memory_test.cppnew file mode 100644index 00000000000..2dbe5a24c81--- /dev/null+++ b/src/tests/test_utils/common_test_utils/tests/memory_test.cpp@@ -0,0 +1,80 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <cstdint>++#include "openvino/util/memory.hpp"++namespace ov::test {++// Verify the constexpr contract at compile time for the most common alignments.+static_assert(ov::util::align_size_up(0, 64) == 0);+static_assert(ov::util::align_size_up(1, 64) == 64);+static_assert(ov::util::align_size_up(64, 64) == 64);+static_assert(ov::util::align_size_up(65, 64) == 128);+static_assert(ov::util::align_size_up(100, 16) == 112);+static_assert(ov::util::align_size_up(100, 1) == 100);+static_assert(ov::util::align_size_up(7, 8) == 8);+static_assert(ov::util::align_size_up(8, 8) == 8);+static_assert(ov::util::align_size_up(9, 8) == 16);+static_assert(ov::util::align_size_up(9, alignof(std::max_align_t)) == 16);++using AlignSizeUpTest = testing::Test;++TEST_F(AlignSizeUpTest, already_aligned_value_is_unchanged) {+ EXPECT_EQ(64u, util::align_size_up(64, 64));+ EXPECT_EQ(128u, util::align_size_up(128, 64));+ EXPECT_EQ(16u, util::align_size_up(16, 16));+}++TEST_F(AlignSizeUpTest, unaligned_value_is_rounded_up) {+ EXPECT_EQ(64u, util::align_size_up(1, 64));+ EXPECT_EQ(64u, util::align_size_up(63, 64));+ EXPECT_EQ(128u, util::align_size_up(65, 64));+ EXPECT_EQ(112u, util::align_size_up(100, 16));+}++TEST_F(AlignSizeUpTest, alignment_1_never_rounds) {+ EXPECT_EQ(0u, util::align_size_up(0, 1));+ EXPECT_EQ(100u, util::align_size_up(100, 1));+ EXPECT_EQ(255u, util::align_size_up(255, 1));+}++TEST_F(AlignSizeUpTest, zero_size_returns_zero) {+ EXPECT_EQ(0u, util::align_size_up(0, 64));+ EXPECT_EQ(0u, util::align_size_up(0, 4096));+}+++using AlignedAllocTest = testing::Test;++TEST_F(AlignedAllocTest, returns_non_null_for_valid_args) {+ void* ptr = util::aligned_alloc(128, 64);+ ASSERT_NE(nullptr, ptr);+ util::aligned_free(ptr);+}++TEST_F(AlignedAllocTest, pointer_satisfies_requested_alignment) {+ for (auto align : {1u, 2u, 4u, 8u, 16u, 32u, 64u, 128u, 256u}) {+ void* ptr = util::aligned_alloc(256, align);+ ASSERT_NE(nullptr, ptr) << "align=" << align;+ EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) % align) << "align=" << align;+ util::aligned_free(ptr);+ }+}++TEST_F(AlignedAllocTest, zero_alignment_uses_default_alignment) {+ void* ptr = util::aligned_alloc(64, 0);+ ASSERT_NE(nullptr, ptr);+ EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) % alignof(std::max_align_t));+ util::aligned_free(ptr);+}++TEST_F(AlignedAllocTest, free_nullptr_is_noop) {+ EXPECT_NO_FATAL_FAILURE(util::aligned_free(nullptr));+}++} // namespace ov::test