Skip to content
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

Fix virtual_block_allocator::deallocate_block #175

Merged
merged 1 commit into from
Jan 8, 2024
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
2 changes: 2 additions & 0 deletions include/foonathan/memory/virtual_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ namespace foonathan
explicit virtual_block_allocator(std::size_t block_size, std::size_t no_blocks);

/// \effects Releases the reserved virtual memory.
/// \requires All previously \ref allocate_block() committed blocks must be decommitted via
/// \ref deallocate_block(), otherwise the blocks that have not been deallocated are leaked.
~virtual_block_allocator() noexcept;

/// @{
Expand Down
2 changes: 1 addition & 1 deletion src/virtual_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void virtual_block_allocator::deallocate_block(memory_block block) noexcept
{ return static_cast<char*>(block.memory) == cur_ - block_size_; },
info(), block.memory);
cur_ -= block_size_;
virtual_memory_decommit(cur_, block_size_);
virtual_memory_decommit(cur_, block_size_ / virtual_memory_page_size);
}

allocator_info virtual_block_allocator::info() noexcept
Expand Down
13 changes: 13 additions & 0 deletions test/default_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "detail/align.hpp"
#include "memory_arena.hpp"

using namespace foonathan::memory;

Expand Down Expand Up @@ -72,3 +73,15 @@ TEST_CASE("virtual_memory_allocator")
virtual_memory_allocator alloc;
check_default_allocator(alloc, get_virtual_memory_page_size());
}

TEST_CASE("virtual_block_allocator")
{
auto const page_size = get_virtual_memory_page_size();
constexpr std::size_t no_blocks{8u};

virtual_block_allocator alloc{page_size, no_blocks};
auto block = alloc.allocate_block();
REQUIRE(block.memory != nullptr);
REQUIRE(block.size == page_size);
alloc.deallocate_block(block);
}
Loading