-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework how an allocator is associated with a context (#160)
- Loading branch information
Showing
61 changed files
with
509 additions
and
543 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
crates/musli/src/allocator/allocator.rs → crates/musli-core/src/alloc/allocator.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/// A raw buffer allocated through an [`Allocator`]. | ||
/// | ||
/// [`Allocator`]: super::Allocator | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ``` | ||
/// use musli::alloc::{Allocator, Buf, Vec}; | ||
/// | ||
/// let values: [u32; 4] = [1, 2, 3, 4]; | ||
/// | ||
/// musli::alloc::default!(|alloc| { | ||
/// let mut buf = alloc.alloc::<u32>(); | ||
/// let mut len = 0; | ||
/// | ||
/// for value in values { | ||
/// if !buf.resize(len, 1) { | ||
/// panic!("Allocation failed"); | ||
/// } | ||
/// | ||
/// // SAFETY: We've just resized the above buffer. | ||
/// unsafe { | ||
/// buf.as_mut_ptr().add(len).write(value); | ||
/// } | ||
/// | ||
/// len += 1; | ||
/// } | ||
/// | ||
/// // SAFETY: Slice does not outlive the buffer it references. | ||
/// let bytes = unsafe { core::slice::from_raw_parts(buf.as_ptr(), len) }; | ||
/// assert_eq!(bytes, values); | ||
/// }); | ||
/// ``` | ||
pub trait Buf<T> { | ||
/// Resize the buffer. | ||
fn resize(&mut self, len: usize, additional: usize) -> bool; | ||
|
||
/// Get a pointer into the buffer. | ||
fn as_ptr(&self) -> *const T; | ||
|
||
/// Get a mutable pointer into the buffer. | ||
fn as_mut_ptr(&mut self) -> *mut T; | ||
|
||
/// Try to merge one buffer with another. | ||
/// | ||
/// The two length parameters refers to the initialized length of the two | ||
/// buffers. | ||
/// | ||
/// If this returns `Err(B)` if merging was not possible. | ||
fn try_merge<B>(&mut self, this_len: usize, other: B, other_len: usize) -> Result<(), B> | ||
where | ||
B: Buf<T>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! Traits related to memory allocation. | ||
|
||
mod allocator; | ||
#[doc(inline)] | ||
pub use self::allocator::Allocator; | ||
|
||
mod buf; | ||
#[doc(inline)] | ||
pub use self::buf::Buf; |
This file was deleted.
Oops, something went wrong.
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
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
12 changes: 5 additions & 7 deletions
12
crates/musli/src/allocator/disabled.rs → crates/musli/src/alloc/disabled.rs
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
Oops, something went wrong.