-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) Do not expose that our arena allocator is based on Bumpalo, by removing `Deref` and `DerefMut` impls. Instead implement the methods we use on `Allocator` itself. That our allocator is based on Bumpalo is now an internal implementation detail. This will allow us to replace the allocator in future, and enables statically preventing `Drop` types from being stored in the arena (next PR in this stack). I've intentionally only implemented a small subset of `Bump`'s methods - only `alloc`, `alloc_str`, and `reset`. This will make it simpler to implement a new allocator in future, without having to cover all of bumpalo's large API surface. In the meantime, if it turns out we need additional methods, it will be simple to add them, by just delegating to that method on `Bump`.
- Loading branch information
1 parent
ac05134
commit 95bc0d7
Showing
5 changed files
with
126 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,47 +1,50 @@ | ||
// All methods just delegate to `bumpalo`, so all marked `#[inline(always)]` | ||
#![expect(clippy::inline_always)] | ||
|
||
use std::{alloc::Layout, ptr::NonNull}; | ||
|
||
use allocator_api2::alloc::{AllocError, Allocator}; | ||
|
||
/// SAFETY: | ||
/// <https://github.com/fitzgen/bumpalo/blob/4eeab8847c85d5cde135ca21ae14a54e56b05224/src/lib.rs#L1938> | ||
unsafe impl Allocator for &crate::Allocator { | ||
#[inline] | ||
#[inline(always)] | ||
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
(&self.bump).allocate(layout) | ||
self.bump().allocate(layout) | ||
} | ||
|
||
#[inline] | ||
#[inline(always)] | ||
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { | ||
(&self.bump).deallocate(ptr, layout); | ||
self.bump().deallocate(ptr, layout); | ||
} | ||
|
||
#[inline] | ||
#[inline(always)] | ||
unsafe fn shrink( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
(&self.bump).shrink(ptr, old_layout, new_layout) | ||
self.bump().shrink(ptr, old_layout, new_layout) | ||
} | ||
|
||
#[inline] | ||
#[inline(always)] | ||
unsafe fn grow( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
(&self.bump).grow(ptr, old_layout, new_layout) | ||
self.bump().grow(ptr, old_layout, new_layout) | ||
} | ||
|
||
#[inline] | ||
#[inline(always)] | ||
unsafe fn grow_zeroed( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
(&self.bump).grow_zeroed(ptr, old_layout, new_layout) | ||
self.bump().grow_zeroed(ptr, old_layout, new_layout) | ||
} | ||
} |
This file contains 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 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 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 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