alloc: allow containers to use custom allocators#77
alloc: allow containers to use custom allocators#77pawelrutkaq merged 1 commit intoeclipse-score:mainfrom
containers to use custom allocators#77Conversation
License Check Results🚀 The license check job ran with the Bazel command: bazel run --lockfile_mode=error //:license-checkStatus: Click to expand output |
There was a problem hiding this comment.
Pull request overview
This PR refactors the container/storage abstraction to support custom allocator implementations, and wires the containers crate to use the elementary allocator interfaces.
Changes:
- Reworks
Storageusage so generic containers are constructed with a concrete storage instance (instead ofStorage::new/try_newon the trait). - Adds allocator-parameterization to heap-backed storage and fixed-capacity containers via
BasicAllocator(defaulting toGlobalAllocator). - Reshapes
elementary’s public surface by re-exporting allocator types at the crate root and updatingGlobalAllocatorto the newBasicAllocatorsignatures.
Reviewed changes
Copilot reviewed 18 out of 19 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/sync/arc_in.rs | Updates imports to the new elementary re-exports. |
| src/elementary/lib.rs | Makes modules private and re-exports BasicAllocator, GlobalAllocator, etc. |
| src/elementary/global_allocator.rs | Updates GlobalAllocator to new BasicAllocator API + derives Default. |
| src/elementary/allocator_traits.rs | Changes BasicAllocator bounds/signatures (notably adds Default). |
| src/containers/storage/mod.rs | Removes constructor methods from Storage; adds test-only TestVec storage impl. |
| src/containers/storage/inline.rs | Updates inline storage construction (new() + Default). |
| src/containers/storage/heap.rs | Introduces allocator-generic heap storage and *_in constructors. |
| src/containers/inline/vec.rs | Adjusts to construct GenericVec with Inline storage instance. |
| src/containers/inline/string.rs | Adjusts to construct GenericString with Inline storage instance. |
| src/containers/inline/queue.rs | Adjusts to construct GenericQueue with Inline storage instance. |
| src/containers/generic/vec.rs | GenericVec::new now takes storage; tests use TestVec. |
| src/containers/generic/string.rs | GenericString::new now takes storage; tests use TestVec. |
| src/containers/generic/queue.rs | GenericQueue::new now takes storage; tests use TestVec. |
| src/containers/fixed_capacity/vec.rs | Adds allocator generic parameter + new_in/try_new_in. |
| src/containers/fixed_capacity/string.rs | Adds allocator generic parameter + new_in/try_new_in. |
| src/containers/fixed_capacity/queue.rs | Adds allocator generic parameter + new_in. |
| src/containers/Cargo.toml | Adds dependency on elementary. |
| src/containers/BUILD | Adds Bazel dep on //src/elementary. |
| Cargo.lock | Records containers -> elementary dependency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8319dec to
658f82e
Compare
Reworked abstraction to allow usage of custom allocator implementations.
658f82e to
4680977
Compare
| pub trait BasicAllocator { | ||
| /// Allocates a block of memory as described by `layout`. | ||
| fn allocate(&self, layout: core::alloc::Layout) -> Result<NonNull<[u8]>, AllocationError>; | ||
| fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocationError>; |
There was a problem hiding this comment.
why change ? At the end we allocate the array of u8 always.
There was a problem hiding this comment.
Because this prevents usage of NonNull::dangling due to the size for values of type [u8] cannot be known at compilation time.
| } | ||
|
|
||
| impl<T: fmt::Debug> fmt::Debug for FixedCapacityVec<T> { | ||
| impl<T: fmt::Debug, A: BasicAllocator> fmt::Debug for FixedCapacityVecIn<'_, T, A> { |
There was a problem hiding this comment.
maybe we shal ladd ScoreDebug too.
Reworked abstraction to allow usage of custom allocator implementations.
Notes for Reviewer
Pre-Review Checklist for the PR Author
Checklist for the PR Reviewer
Post-review Checklist for the PR Author
References
Closes #81