Skip to content

Commit 50e025c

Browse files
committed
refactor: pass allocation size back to caller
1 parent 1d16814 commit 50e025c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/raw/alloc.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ mod inner {
1515
use core::ptr::NonNull;
1616

1717
#[allow(clippy::map_err_ignore)]
18-
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
18+
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
1919
match alloc.allocate(layout) {
20-
Ok(ptr) => Ok(ptr.as_non_null_ptr()),
20+
Ok(ptr) => Ok(ptr),
2121
Err(_) => Err(()),
2222
}
2323
}
@@ -38,9 +38,9 @@ mod inner {
3838
use core::ptr::NonNull;
3939

4040
#[allow(clippy::map_err_ignore)]
41-
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
41+
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
4242
match alloc.allocate(layout) {
43-
Ok(ptr) => Ok(ptr.cast()),
43+
Ok(ptr) => Ok(ptr),
4444
Err(_) => Err(()),
4545
}
4646
}
@@ -61,7 +61,7 @@ mod inner {
6161

6262
#[allow(clippy::missing_safety_doc)] // not exposed outside of this crate
6363
pub unsafe trait Allocator {
64-
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()>;
64+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()>;
6565
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
6666
}
6767

@@ -70,8 +70,11 @@ mod inner {
7070

7171
unsafe impl Allocator for Global {
7272
#[inline]
73-
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> {
74-
unsafe { NonNull::new(alloc(layout)).ok_or(()) }
73+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> {
74+
match unsafe { NonNull::new(alloc(layout)) } {
75+
Some(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
76+
None => Err(()),
77+
}
7578
}
7679
#[inline]
7780
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
@@ -86,7 +89,7 @@ mod inner {
8689
}
8790
}
8891

89-
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
92+
pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<[u8]>, ()> {
9093
alloc.allocate(layout)
9194
}
9295
}

0 commit comments

Comments
 (0)