Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
- name: Upload report
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: reports
sarif_file: reports
4 changes: 2 additions & 2 deletions src/rc_ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ use std::rc::Rc;
/// If allocation is not possible due to issues with memory layouts or not enough memory,
/// it will return an [AllocError](crate::alloc_error::AllocError)
pub fn alloc<T: Sized>() -> Result<Rc<T>, alloc_error::AllocError> {
let value_ptr = util_alloc::alloc_value()?;
Ok(unsafe { Rc::from_raw(value_ptr) })
let rc_ptr: *mut Rc<T> = util_alloc::alloc_zeroed_value()?;
Ok((unsafe { &*rc_ptr }).clone())
}
16 changes: 16 additions & 0 deletions src/util/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ pub fn alloc_value<T: Sized>() -> Result<*mut T, alloc_error::AllocError> {
Ok(value_ptr as *mut T)
}
}

#[inline]
pub fn alloc_zeroed_value<T: Sized>() -> Result<*mut T, alloc_error::AllocError> {
let layout = alloc::Layout::new::<T>();

let value_ptr = unsafe { alloc::alloc_zeroed(layout) };

if value_ptr.is_null() {
Err(alloc_error::AllocError::new(
"Failed to allocate a value",
alloc_error::AllocErrorType::FailedAllocation,
))
} else {
Ok(value_ptr as *mut T)
}
}
2 changes: 1 addition & 1 deletion tests/rc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ unsafe impl GlobalAlloc for SmallMemoryAllocator {
static GLOBAL: SmallMemoryAllocator = SmallMemoryAllocator;

struct SmallType([u128; 10]);
struct BigType([u128; 1000000]);
struct BigType([u128; 10000000000]);

#[test]
fn with_enough_mem_it_allocates() {
Expand Down