Skip to content

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

[core] Rename memory functions (#36296)

Tomasz Jankowski65855d31e3e6
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index 29ee1627646..d578af6c94d 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -8,6 +8,7 @@
#include <cstdint>
#include <cstdlib>
#include <string>
+#include <system_error>
namespace ov::util {
@@ -76,40 +77,38 @@ void* aligned_alloc(size_t size, size_t alignment) noexcept;
void aligned_free(void* ptr) noexcept;
/**
- * @brief Reserves a block of memory of the specified size without actually allocating physical memory.
- * The reserved memory block is not accessible until acquire_buffer() is called on it. The reserved memory block should
- * be released with release_buffer() when it is no longer needed.
- * @param size [in] Size of the memory block to reserve in bytes. Must be greater than 0.
- * @param error [out] Optional Error message in case of failure, empty otherwise.
- * @return Pointer to the reserved memory block, or nullptr if the reservation fail.
+ * @brief Reserves virtual address space of the given size without backing it with physical memory.
+ * The region is inaccessible until vm_commit() is called. Release with vm_release() when no longer needed.
+ * @param size Size in bytes to reserve. Must be greater than 0.
+ * @param ec Set to the OS error code on failure, cleared on success.
+ * @return Pointer to the reserved region, or nullptr on failure.
*/
-void* reserve_buffer(size_t size, std::string* error = nullptr) noexcept;
+void* vm_reserve(size_t size, std::error_code& ec) noexcept;
/**
- * @brief Acquires the reserved memory block, making it accessible for read/write operations.
- * @param reserved_buffer [in] Pointer to the reserved memory block to acquire.
- * @param size [in] Size of the memory block to acquire in bytes. Must be greater than 0.
- * @param error [out] Optional Error message in case of failure, empty otherwise.
- * @note The reserved memory block should be successfully acquired before it can be used.
+ * @brief Commits a previously reserved region, making it readable and writable.
+ * @param ptr Pointer returned by vm_reserve().
+ * @param size Size in bytes to commit. Must be greater than 0.
+ * @param ec Set to the OS error code on failure, cleared on success.
*/
-void acquire_buffer(void* reserved_buffer, size_t size, std::string* error = nullptr) noexcept;
+void vm_commit(void* ptr, size_t size, std::error_code& ec) noexcept;
/**
- * @brief Evicts the acquired memory block, making it inaccessible and allowing the system to free physical memory.
- * @param reserved_buffer [in] Pointer to the reserved memory block to evict.
- * @param size [in] Size of the memory block to evict in bytes. Must be greater than 0.
- * @param error [out] Optional Error message in case of failure, empty otherwise.
- * @note After eviction, the reserved memory block can be acquired again with acquire_buffer().
+ * @brief Decommits a committed region: revokes access and returns physical pages to the OS.
+ * The virtual address range remains reserved and can be committed again with vm_commit().
+ * @param ptr Pointer returned by vm_reserve(). Must not be nullptr.
+ * @param size Size in bytes to decommit. Must be greater than 0.
+ * @pre ptr != nullptr && size > 0; violated preconditions are a programming error (assert fires in debug).
*/
-void evict_buffer(void* reserved_buffer, size_t size, std::string* error = nullptr) noexcept;
+void vm_decommit(void* ptr, size_t size) noexcept;
/**
- * @brief Releases the reserved memory block, freeing any associated resources. After this call, the reserved memory
- * block is no longer valid and should not be used.
- * @param reserved_buffer [in] Pointer to the reserved memory block to release.
- * @param size [in] Size of the memory block to release in bytes. Must be greater than 0.
- * @param error [out] Optional Error message in case of failure, empty otherwise.
+ * @brief Releases the reserved virtual address range. Can be called without a prior vm_decommit().
+ * After this call the pointer is invalid and must not be used.
+ * @param ptr Pointer returned by vm_reserve(). Must not be nullptr.
+ * @param size Size in bytes originally passed to vm_reserve(). Must be greater than 0.
+ * @pre ptr != nullptr && size > 0; violated preconditions are a programming error (assert fires in debug).
*/
-void release_buffer(void* reserved_buffer, size_t size, std::string* error = nullptr) noexcept;
+void vm_release(void* ptr, size_t size) noexcept;
} // namespace ov::util
diff --git a/src/common/util/src/os/lin/lin_memory.cpp b/src/common/util/src/os/lin/lin_memory.cpp
index 468494211a9..df92832c405 100644
--- a/src/common/util/src/os/lin/lin_memory.cpp
+++ b/src/common/util/src/os/lin/lin_memory.cpp
@@ -4,10 +4,12 @@
#include <sys/mman.h>
+#include <cassert>
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
+#include <tuple>
#include "openvino/util/memory.hpp"
@@ -24,39 +26,40 @@ void aligned_free(void* ptr) noexcept {
std::free(ptr);
}
-void* reserve_buffer(size_t size, std::string* error) noexcept {
+void* vm_reserve(size_t size, std::error_code& ec) noexcept {
const auto p = mmap(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED) {
- if (error) {
- *error = std::string{"mmap failed, err: "} + std::strerror(errno);
- }
+ ec = std::error_code(errno, std::system_category());
return nullptr;
}
+ ec = {};
return p;
}
-void acquire_buffer(void* reserved_buffer, size_t size, std::string* error) noexcept {
- if (mprotect(reserved_buffer, size, PROT_READ | PROT_WRITE) == -1) {
- if (error) {
- *error = std::string{"mprotect failed, err: "} + std::strerror(errno);
- }
+void vm_commit(void* ptr, size_t size, std::error_code& ec) noexcept {
+ if (mprotect(ptr, size, PROT_READ | PROT_WRITE) == -1) {
+ ec = std::error_code(errno, std::system_category());
+ } else {
+ ec = {};
}
}
-void evict_buffer(void* reserved_buffer, size_t size, std::string* error) noexcept {
- if (mmap(reserved_buffer, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED) {
- if (error) {
- *error = std::string{"mprotect failed, err: "} + std::strerror(errno);
- }
- }
+void vm_decommit(void* ptr, size_t size) noexcept {
+ assert(ptr != nullptr && size > 0);
+#if defined(__linux__)
+ std::ignore = mprotect(ptr, size, PROT_NONE);
+ std::ignore = madvise(ptr, size, MADV_DONTNEED);
+#elif defined(__APPLE__) && defined(MADV_FREE_REUSABLE)
+ std::ignore = mprotect(ptr, size, PROT_NONE);
+ std::ignore = madvise(ptr, size, MADV_FREE_REUSABLE);
+#else
+ std::ignore = mmap(ptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+#endif
}
-void release_buffer(void* reserved_buffer, size_t byte_size, std::string* error) noexcept {
- if (munmap(reserved_buffer, byte_size) == -1) {
- if (error) {
- *error = std::string{"munmap failed, err: "} + std::strerror(errno);
- }
- }
+void vm_release(void* ptr, size_t size) noexcept {
+ assert(ptr != nullptr && size > 0);
+ std::ignore = munmap(ptr, size);
}
} // namespace ov::util
diff --git a/src/common/util/src/os/win/win_memory.cpp b/src/common/util/src/os/win/win_memory.cpp
index 31ebf86e97a..8d03e1d7873 100644
--- a/src/common/util/src/os/win/win_memory.cpp
+++ b/src/common/util/src/os/win/win_memory.cpp
@@ -9,8 +9,10 @@
#include <malloc.h>
#include <windows.h>
+#include <cassert>
#include <cstddef>
#include <cstring>
+#include <tuple>
#include "openvino/util/memory.hpp"
@@ -27,39 +29,32 @@ void aligned_free(void* ptr) noexcept {
_aligned_free(ptr);
}
-void* reserve_buffer(size_t size, std::string* error) noexcept {
+void* vm_reserve(size_t size, std::error_code& ec) noexcept {
const auto p = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
if (p == NULL) {
- if (error) {
- *error = std::string{"VirtualAlloc reserve failed, err: "} + std::to_string(GetLastError());
- }
+ ec = std::error_code(GetLastError(), std::system_category());
return nullptr;
}
+ ec = {};
return p;
}
-void acquire_buffer(void* reserved_buffer, size_t size, std::string* error) noexcept {
- if (VirtualAlloc(reserved_buffer, size, MEM_COMMIT, PAGE_READWRITE) == NULL) {
- if (error) {
- *error = std::string{"VirtualAlloc commit failed, err: "} + std::to_string(GetLastError());
- }
+void vm_commit(void* ptr, size_t size, std::error_code& ec) noexcept {
+ if (VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) == NULL) {
+ ec = std::error_code(GetLastError(), std::system_category());
+ } else {
+ ec = {};
}
}
-void evict_buffer(void* reserved_buffer, size_t size, std::string* error) noexcept {
- if (VirtualFree(reserved_buffer, size, MEM_DECOMMIT) == 0) {
- if (error) {
- *error = std::string{"VirtualFree decommit failed, err: "} + std::to_string(GetLastError());
- }
- }
+void vm_decommit(void* ptr, size_t size) noexcept {
+ assert(ptr != nullptr && size > 0);
+ std::ignore = VirtualFree(ptr, size, MEM_DECOMMIT);
}
-void release_buffer(void* reserved_buffer, size_t /* byte_size */, std::string* error) noexcept {
- if (VirtualFree(reserved_buffer, 0, MEM_RELEASE) == 0) {
- if (error) {
- *error = std::string{"VirtualFree release failed, err: "} + std::to_string(GetLastError());
- }
- }
+void vm_release(void* ptr, size_t) noexcept {
+ assert(ptr != nullptr);
+ std::ignore = VirtualFree(ptr, 0, MEM_RELEASE);
}
} // namespace ov::util
diff --git a/src/core/src/runtime/lazy_buffer.cpp b/src/core/src/runtime/lazy_buffer.cpp
index 55d006cd166..97ea52e74c6 100644
--- a/src/core/src/runtime/lazy_buffer.cpp
+++ b/src/core/src/runtime/lazy_buffer.cpp
@@ -37,13 +37,16 @@ LazyBuffer::LazyBuffer(std::filesystem::path file_path, size_t offset, size_t by
" for file: ",
m_file_path);
- m_aligned_buffer = static_cast<char*>(util::reserve_buffer(m_byte_size));
- OPENVINO_ASSERT(m_aligned_buffer != nullptr, "Failed to reserve memory for LazyBuffer");
+ std::error_code ec;
+ m_aligned_buffer = static_cast<char*>(util::vm_reserve(m_byte_size, ec));
+ OPENVINO_ASSERT(m_aligned_buffer != nullptr, "Failed to reserve memory for LazyBuffer. Error: ", ec.message());
}
LazyBuffer::~LazyBuffer() {
- util::release_buffer(m_aligned_buffer, m_byte_size);
- m_aligned_buffer = nullptr;
+ if (m_aligned_buffer) {
+ util::vm_release(m_aligned_buffer, m_byte_size);
+ m_aligned_buffer = nullptr;
+ }
m_byte_size = 0;
}
@@ -70,11 +73,9 @@ void LazyBuffer::hint_prefetch() const {
return;
}
- std::string error;
- util::acquire_buffer(m_aligned_buffer, m_byte_size, &error);
- if (!error.empty()) {
- OPENVINO_THROW("Failed to acquire memory for LazyBuffer. Error: ", error);
- }
+ std::error_code ec;
+ util::vm_commit(m_aligned_buffer, m_byte_size, ec);
+ OPENVINO_ASSERT(!ec, "Failed to commit memory for LazyBuffer. Error: ", ec.message());
try {
util::ParallelReadStreamBuf par_buf(m_file_path, static_cast<std::streamoff>(m_offset));
@@ -84,7 +85,7 @@ void LazyBuffer::hint_prefetch() const {
OPENVINO_ASSERT(file, "Failed to read data from file: ", m_file_path);
m_loaded.store(true, std::memory_order_release);
} catch (...) {
- util::evict_buffer(m_aligned_buffer, m_byte_size);
+ util::vm_decommit(m_aligned_buffer, m_byte_size);
throw;
}
}
@@ -99,7 +100,7 @@ void LazyBuffer::hint_evict(size_t offset, size_t size) noexcept {
try {
std::lock_guard lock{m_loading};
if (m_loaded.load(std::memory_order_relaxed)) {
- util::evict_buffer(m_aligned_buffer, m_byte_size);
+ util::vm_decommit(m_aligned_buffer, m_byte_size);
m_loaded.store(false, std::memory_order_release);
}
} catch (...) {