Skip to content

Commit

Permalink
Fix virtual_block_allocator::deallocate_block (#175)
Browse files Browse the repository at this point in the history
* deallocate_block() now calls virtual_memory_decommit with the number of pages to decommit
  rather than the block size
* extended virtual_block_allocator dtor requirements
  • Loading branch information
jeremimucha committed Jan 8, 2024
1 parent d989f41 commit 016c9fb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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);
}

0 comments on commit 016c9fb

Please sign in to comment.