Repository navigation
[MOD-18996] Transfer allocation accounting between allocators #1115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| 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); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
const autofor both source and destination on all tests.