Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/VecSim/memory/vecsim_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const VecSimAllocationHeader *>(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;
Expand Down
8 changes: 8 additions & 0 deletions src/VecSim/memory/vecsim_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void, Deleter> allocate_aligned_unique(size_t size, size_t alignment);
std::unique_ptr<void, Deleter> allocate_unique(size_t size);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
100 changes: 100 additions & 0 deletions tests/unit/test_allocation_transfer.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <cstdlib>

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();
Comment on lines +19 to +20

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: const auto for both source and destination on all tests.

const auto source_before = source->getAllocationSize();
const auto destination_before = destination->getAllocationSize();
auto *ptr = static_cast<unsigned char *>(source->allocate_aligned(size, alignment));
ASSERT_NE(ptr, nullptr);
for (size_t i = 0; i < size; ++i) {
ptr[i] = static_cast<unsigned char>(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<uintptr_t>(ptr) % alignment, 0u);
}
for (size_t i = 0; i < size; ++i) {
EXPECT_EQ(ptr[i], static_cast<unsigned char>(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);
}
Loading