From 29518ddeb1d268459fb20b2adb3aa43f962dcde4 Mon Sep 17 00:00:00 2001 From: GuyAv46 Date: Thu, 12 Nov 2026 12:50:10 +0200 Subject: [PATCH] [MOD-18996] Transfer allocation accounting between allocators --- src/VecSim/memory/vecsim_malloc.cpp | 10 +++ src/VecSim/memory/vecsim_malloc.h | 8 ++ tests/unit/CMakeLists.txt | 4 + tests/unit/test_allocation_transfer.cpp | 100 ++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 tests/unit/test_allocation_transfer.cpp diff --git a/src/VecSim/memory/vecsim_malloc.cpp b/src/VecSim/memory/vecsim_malloc.cpp index caa28fc1f..e51a46abb 100644 --- a/src/VecSim/memory/vecsim_malloc.cpp +++ b/src/VecSim/memory/vecsim_malloc.cpp @@ -80,6 +80,16 @@ void *VecSimAllocator::reallocate(void *p, size_t size) { return nullptr; } +void VecSimAllocator::transferAllocationTo(void *p, VecSimAllocator &destination) noexcept { + if (!p || this == &destination) { + return; + } + const auto *header = static_cast(p) - 1; + const size_t bytes = header->allocation_size + allocation_header_size; + this->allocated -= bytes; + destination.allocated += bytes; +} + void VecSimAllocator::free_allocation(void *p) { if (!p) return; diff --git a/src/VecSim/memory/vecsim_malloc.h b/src/VecSim/memory/vecsim_malloc.h index 2962c69a4..5dd1bc080 100644 --- a/src/VecSim/memory/vecsim_malloc.h +++ b/src/VecSim/memory/vecsim_malloc.h @@ -40,6 +40,14 @@ struct VecSimAllocator { void *reallocate(void *p, size_t size); void free_allocation(void *p); + /** + * Transfer accounting for an allocation owned by this allocator to destination. + * The pointer, contents and alignment are preserved. Null and self transfers are no-ops. + * The caller must exclusively own p and update any owning deleter before freeing it + * through destination. The two accounting counters are not updated atomically together. + */ + void transferAllocationTo(void *p, VecSimAllocator &destination) noexcept; + // Allocations for scope-life-time memory. std::unique_ptr allocate_aligned_unique(size_t size, size_t alignment); std::unique_ptr allocate_unique(size_t size); diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 67a8a00f3..1ef14c2cd 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -64,7 +64,11 @@ target_link_libraries(test_uint8 PUBLIC gtest VectorSimilarity) target_link_libraries(test_index_test_utils PUBLIC gtest VectorSimilarity) target_link_libraries(test_svs PUBLIC gtest VectorSimilarity) +add_executable(test_allocation_transfer ../utils/test_main_with_timeout.cpp test_allocation_transfer.cpp) +target_link_libraries(test_allocation_transfer PUBLIC gtest VectorSimilarity) + include(GoogleTest) +gtest_discover_tests(test_allocation_transfer) gtest_discover_tests(test_hnsw) gtest_discover_tests(test_hnsw_sq8) diff --git a/tests/unit/test_allocation_transfer.cpp b/tests/unit/test_allocation_transfer.cpp new file mode 100644 index 000000000..b3ded2bd3 --- /dev/null +++ b/tests/unit/test_allocation_transfer.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2006-Present, Redis Ltd. + * All rights reserved. + * + * Licensed under your choice of the Redis Source Available License 2.0 + * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the + * GNU Affero General Public License v3 (AGPLv3). + */ +#include "gtest/gtest.h" +#include "VecSim/memory/vecsim_malloc.h" +#include +#include + +TEST(AllocationTransferTest, PreservesAllocationAndBalancesAccounting) { + for (unsigned char alignment : {0, 16, 64}) { + for (size_t size : {0, 1, 127}) { + SCOPED_TRACE(::testing::Message() + << "alignment=" << int(alignment) << " size=" << size); + auto source = VecSimAllocator::newVecsimAllocator(); + auto destination = VecSimAllocator::newVecsimAllocator(); + const auto source_before = source->getAllocationSize(); + const auto destination_before = destination->getAllocationSize(); + auto *ptr = static_cast(source->allocate_aligned(size, alignment)); + ASSERT_NE(ptr, nullptr); + for (size_t i = 0; i < size; ++i) { + ptr[i] = static_cast(i); + } + const auto charged = source->getAllocationSize() - source_before; + source->transferAllocationTo(ptr, *destination); + EXPECT_EQ(source->getAllocationSize(), source_before); + EXPECT_EQ(destination->getAllocationSize(), destination_before + charged); + if (alignment) { + EXPECT_EQ(reinterpret_cast(ptr) % alignment, 0u); + } + for (size_t i = 0; i < size; ++i) { + EXPECT_EQ(ptr[i], static_cast(i)); + } + source.reset(); + destination->free_allocation(ptr); + EXPECT_EQ(destination->getAllocationSize(), destination_before); + } + } +} + +TEST(AllocationTransferTest, NullAndSelfTransfersPreserveAccounting) { + auto source = VecSimAllocator::newVecsimAllocator(); + auto destination = VecSimAllocator::newVecsimAllocator(); + const auto baseline = source->getAllocationSize(); + source->transferAllocationTo(nullptr, *destination); + EXPECT_EQ(source->getAllocationSize(), baseline); + EXPECT_EQ(destination->getAllocationSize(), baseline); + void *ptr = source->callocate(37); + ASSERT_NE(ptr, nullptr); + const auto allocated = source->getAllocationSize(); + source->transferAllocationTo(ptr, *source); + EXPECT_EQ(source->getAllocationSize(), allocated); + source->transferAllocationTo(ptr, *destination); + destination->transferAllocationTo(ptr, *source); + EXPECT_EQ(source->getAllocationSize(), allocated); + EXPECT_EQ(destination->getAllocationSize(), baseline); + source->free_allocation(ptr); + EXPECT_EQ(source->getAllocationSize(), baseline); +} + +TEST(AllocationTransferTest, DoesNotAllocateOrFree) { + static size_t allocations = 0; + static size_t frees = 0; + struct RestoreMemoryFunctions { + ~RestoreMemoryFunctions() { + VecSimAllocator::setMemoryFunctions({malloc, calloc, realloc, free}); + } + } restore; + VecSimAllocator::setMemoryFunctions({[](size_t n) -> void * { + ++allocations; + return malloc(n); + }, + [](size_t n, size_t size) -> void * { + ++allocations; + return calloc(n, size); + }, + [](void *p, size_t n) -> void * { + ++allocations; + return realloc(p, n); + }, + [](void *p) { + ++frees; + free(p); + }}); + auto source = VecSimAllocator::newVecsimAllocator(); + auto destination = VecSimAllocator::newVecsimAllocator(); + void *ptr = source->allocate_aligned(127, 64); + ASSERT_NE(ptr, nullptr); + const auto allocations_before = allocations; + const auto frees_before = frees; + source->transferAllocationTo(ptr, *destination); + EXPECT_EQ(allocations, allocations_before); + EXPECT_EQ(frees, frees_before); + destination->free_allocation(ptr); + EXPECT_EQ(frees, frees_before + 1); +}