Skip to content

Allocators: tests, alignment and PMR::Object fixes, ObjectPoolResource, PMR::makeOwned - #5

Merged
eyalamirmusic merged 2 commits into
mainfrom
feature/allocator-tests
Oct 30, 2026
Merged

eyalamirmusic merged 2 commits into
mainfrom
feature/allocator-tests

Conversation

@eyalamirmusic

Copy link
Copy Markdown
Owner

Summary

  • Test-only dependency: ScopedMemoryAllocations is fetched in Tests/CMakeLists.txt and linked only to the test executable, so it sits behind PROJECT_IS_TOP_LEVEL like NanoTest. Projects that add_subdirectory this repo never fetch or link it. The tests use it to check that pool and resource allocations don't touch the global heap, or touch it exactly when expected.
  • New tests in Tests/Allocators/ for PMR.h and MultiPoolAllocator.h: DynamicMemory, ResourceContainer, MemoryPool, MemoryPoolResource, PMR::Object / PMR::Vector, makeShared and makeOwned.
  • Fix: over-aligned allocation. MemoryPoolResource ignored the requested alignment. The alignment now goes through MemoryPool and DynamicMemory, which reserve bytes + alignment - 1 and align with std::align. The cutoff is __STDCPP_DEFAULT_NEW_ALIGNMENT__, the alignment the block storage already gets from operator new, so requests at or below it behave exactly as before.
  • Fix: PMR::Object assignment. Copy and move assignment now keep the target's own resource and assign only the contents, as std::pmr containers do, so getResource() always matches the resource the wrapped object allocates from. PMR::Object also gets a real move constructor, which doesn't allocate.
  • Behaviour change: smaller defaults. MemoryPoolResource used to default to 1000 pre-filled 1 MB blocks (about 1 GB at construction). It now defaults to 16 blocks of 64 KB with 1024 reserved bookkeeping slots (about 1 MB). MemoryPool's default counts changed to match (16 / 1024). Existing code that relied on the larger defaults will fall back to the heap sooner.
  • New: ObjectPoolResource<T>. A pool resource with blocks of sizeof(T) that honours alignof(T), pre-filled with N blocks (default 1024).
  • New: PMR::makeOwned<T>(resource, args...) returns PMR::Owned<T>, a std::unique_ptr whose deleter frees the memory back to the same resource with the size and alignment captured at creation. Converting to Owned<Base> is allowed only when Base has a virtual destructor.
  • Comments: MemoryPool and the pool resources now say they are heap-backed and that pointers stay put until deallocated.

Testing

ctest: 563/563 passed locally on macOS (Apple clang). That is the existing tests plus 53 new ones.

@eyalamirmusic
eyalamirmusic merged commit 9f26b4a into main Oct 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant