Skip to content

Commit

Permalink
Added changes for rust-lang/rust#95295.
Browse files Browse the repository at this point in the history
  • Loading branch information
RustEnthusiast committed Nov 6, 2022
1 parent b764061 commit 70b4a92
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 25 deletions.
10 changes: 8 additions & 2 deletions include/nstd/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ typedef enum {
/// Getting a handle to a heap failed.
NSTD_ALLOC_ERROR_HEAP_NOT_FOUND,
/// A heap is invalid.
NSTD_ALLOC_ERROR_INVALID_HEAP
NSTD_ALLOC_ERROR_INVALID_HEAP,
/// An allocation function received input parameters that resulted in an invalid memory layout.
NSTD_ALLOC_ERROR_INVALID_LAYOUT
} NSTDAllocError;

/// Allocates a block of memory on the heap.
Expand Down Expand Up @@ -84,11 +86,15 @@ NSTDAPI NSTDAllocError nstd_alloc_reallocate(NSTDAnyMut *ptr, NSTDUInt size, NST
///
/// - `NSTDUInt size` - The number of bytes to free.
///
/// # Returns
///
/// `NSTDAllocError errc` - The allocation operation error code.
///
/// # Safety
///
/// - Behavior is undefined if `ptr` is not a value returned by `nstd_alloc_allocate[_zeroed]`.
///
/// - `size` must be the same value that was used to allocate the memory buffer.
NSTDAPI void nstd_alloc_deallocate(NSTDAnyMut *ptr, NSTDUInt size);
NSTDAPI NSTDAllocError nstd_alloc_deallocate(NSTDAnyMut *ptr, NSTDUInt size);

#endif
2 changes: 1 addition & 1 deletion include/nstd/cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ NSTDAPI NSTDChar nstd_cstring_pop(NSTDCString *cstring);
///
/// # Panics
///
/// This operation may panic if getting a handle to the heap fails.
/// Panics if deallocating fails.
NSTDAPI void nstd_cstring_free(NSTDCString cstring);

#endif
4 changes: 4 additions & 0 deletions include/nstd/heap_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ NSTDAPI NSTDAnyMut nstd_heap_ptr_get_mut(NSTDHeapPtr *hptr);
/// # Parameters:
///
/// - `NSTDHeapPtr hptr` - A pointer to the heap object.
///
/// # Panics
///
/// Panics if freeing the heap memory fails.
NSTDAPI void nstd_heap_ptr_free(NSTDHeapPtr hptr);

#endif
5 changes: 5 additions & 0 deletions include/nstd/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ NSTDAPI NSTDAny nstd_shared_ptr_get(const NSTDSharedPtr *shared_ptr);
/// # Parameters:
///
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
///
/// # Panics
///
/// Panics if there are no more shared pointers referencing the shared data and freeing the heap
/// memory fails.
NSTDAPI void nstd_shared_ptr_free(NSTDSharedPtr shared_ptr);

#endif
2 changes: 1 addition & 1 deletion include/nstd/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ NSTDAPI NSTDString nstd_string_from_u64(NSTDUInt64 v);
///
/// # Panics
///
/// This operation may panic if getting a handle to the heap fails.
/// Panics if deallocating fails.
NSTDAPI void nstd_string_free(NSTDString string);

#endif
4 changes: 4 additions & 0 deletions include/nstd/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ NSTDAPI NSTDAllocError nstd_vec_shrink(NSTDVec *vec);
/// # Parameters:
///
/// - `NSTDVec vec` - The vector to free.
///
/// # Panics
///
/// Panics if deallocating fails.
NSTDAPI void nstd_vec_free(NSTDVec vec);

#endif
37 changes: 33 additions & 4 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum NSTDAllocError {
NSTD_ALLOC_ERROR_HEAP_NOT_FOUND,
/// A heap is invalid.
NSTD_ALLOC_ERROR_INVALID_HEAP,
/// An allocation function received input parameters that resulted in an invalid memory layout.
NSTD_ALLOC_ERROR_INVALID_LAYOUT,
}
impl NSTDAllocError {
/// Converts an [NSTDWindowsAllocError] into an [NSTDAllocError].
Expand Down Expand Up @@ -77,7 +79,12 @@ impl NSTDAllocError {
pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
{
use crate::NSTD_NULL;
use alloc::alloc::Layout;
// Make sure `size` is valid for `layout`.
if size > isize::MAX as usize {
return NSTD_NULL;
}
let layout = Layout::from_size_align_unchecked(size, 1);
alloc::alloc::alloc(layout).cast()
}
Expand Down Expand Up @@ -125,7 +132,12 @@ pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMut {
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
{
use crate::NSTD_NULL;
use alloc::alloc::Layout;
// Make sure `size` is valid for `layout`.
if size > isize::MAX as usize {
return NSTD_NULL;
}
let layout = Layout::from_size_align_unchecked(size, 1);
alloc::alloc::alloc_zeroed(layout).cast()
}
Expand Down Expand Up @@ -186,7 +198,7 @@ pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMu
/// nstd_alloc_deallocate(&mut mem, SIZE);
/// }
/// ```
#[inline]
#[cfg_attr(any(target_family = "unix", target_os = "windows"), inline)]
#[cfg_attr(feature = "clib", no_mangle)]
#[cfg_attr(
any(target_family = "unix", target_os = "windows"),
Expand All @@ -200,6 +212,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
{
use alloc::alloc::Layout;
// Make sure `size` is valid for `layout`.
if size > isize::MAX as usize {
return NSTDAllocError::NSTD_ALLOC_ERROR_INVALID_LAYOUT;
}
let layout = Layout::from_size_align_unchecked(size, 1);
let new_mem = alloc::alloc::realloc((*ptr).cast(), layout, new_size);
if !new_mem.is_null() {
Expand Down Expand Up @@ -229,6 +245,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
///
/// - `NSTDUInt size` - The number of bytes to free.
///
/// # Returns
///
/// `NSTDAllocError errc` - The allocation operation error code.
///
/// # Safety
///
/// - Behavior is undefined if `ptr` is not a value returned by `nstd_alloc_allocate[_zeroed]`.
Expand All @@ -246,27 +266,36 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
/// nstd_alloc_deallocate(&mut mem, 24);
/// }
/// ```
#[inline]
#[cfg_attr(any(target_family = "unix", target_os = "windows"), inline)]
#[cfg_attr(feature = "clib", no_mangle)]
#[cfg_attr(
any(target_family = "unix", target_os = "windows"),
allow(unused_variables)
)]
pub unsafe extern "C" fn nstd_alloc_deallocate(ptr: &mut NSTDAnyMut, size: NSTDUInt) {
pub unsafe extern "C" fn nstd_alloc_deallocate(
ptr: &mut NSTDAnyMut,
size: NSTDUInt,
) -> NSTDAllocError {
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
{
use crate::NSTD_NULL;
use alloc::alloc::Layout;
// Make sure `size` is valid for `layout`.
if size > isize::MAX as usize {
return NSTDAllocError::NSTD_ALLOC_ERROR_INVALID_LAYOUT;
}
let layout = Layout::from_size_align_unchecked(size, 1);
alloc::alloc::dealloc((*ptr).cast(), layout);
*ptr = NSTD_NULL;
NSTDAllocError::NSTD_ALLOC_ERROR_NONE
}
#[cfg(target_family = "unix")]
{
nstd_os_unix_alloc_deallocate(ptr);
NSTDAllocError::NSTD_ALLOC_ERROR_NONE
}
#[cfg(target_os = "windows")]
{
nstd_os_windows_alloc_deallocate(ptr);
NSTDAllocError::from_windows(nstd_os_windows_alloc_deallocate(ptr))
}
}
2 changes: 1 addition & 1 deletion src/cstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ pub extern "C" fn nstd_cstring_pop(cstring: &mut NSTDCString) -> NSTDChar {
///
/// # Panics
///
/// This operation may panic if getting a handle to the heap fails.
/// Panics if deallocating fails.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
#[allow(unused_variables)]
Expand Down
17 changes: 15 additions & 2 deletions src/heap_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! A pointer type for single value heap allocation.
use crate::{
alloc::{nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate},
alloc::{
nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate,
NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
},
core::mem::nstd_core_mem_copy,
NSTDAny, NSTDAnyMut, NSTDUInt,
};
Expand All @@ -16,10 +19,16 @@ pub struct NSTDHeapPtr {
}
impl Drop for NSTDHeapPtr {
/// [NSTDHeapPtr]'s destructor.
///
/// # Panics
///
/// Panics if deallocating fails.
#[inline]
fn drop(&mut self) {
// SAFETY: Heap pointers are always non-null.
unsafe { nstd_alloc_deallocate(&mut self.ptr, self.size) };
unsafe {
assert!(nstd_alloc_deallocate(&mut self.ptr, self.size) == NSTD_ALLOC_ERROR_NONE);
}
}
}

Expand Down Expand Up @@ -223,6 +232,10 @@ pub extern "C" fn nstd_heap_ptr_get_mut(hptr: &mut NSTDHeapPtr) -> NSTDAnyMut {
/// # Parameters:
///
/// - `NSTDHeapPtr hptr` - A pointer to the heap object.
///
/// # Panics
///
/// Panics if freeing the heap memory fails.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
#[allow(unused_variables)]
Expand Down
16 changes: 14 additions & 2 deletions src/shared_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! A reference counting smart pointer.
use crate::{
alloc::{nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate},
alloc::{
nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate,
NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
},
core::mem::nstd_core_mem_copy,
NSTDAny, NSTDAnyMut, NSTDUInt,
};
Expand Down Expand Up @@ -38,6 +41,10 @@ impl NSTDSharedPtr {
}
impl Drop for NSTDSharedPtr {
/// [NSTDSharedPtr]'s destructor.
///
/// # Panics
///
/// Panics if deallocating fails.
fn drop(&mut self) {
// SAFETY: Shared pointers are always non-null.
unsafe {
Expand All @@ -46,7 +53,7 @@ impl Drop for NSTDSharedPtr {
*ptrs -= 1;
// If the pointer count is zero, free the data.
if *ptrs == 0 {
nstd_alloc_deallocate(&mut self.ptr, self.size);
assert!(nstd_alloc_deallocate(&mut self.ptr, self.size) == NSTD_ALLOC_ERROR_NONE);
}
}
}
Expand Down Expand Up @@ -304,6 +311,11 @@ pub extern "C" fn nstd_shared_ptr_get(shared_ptr: &NSTDSharedPtr) -> NSTDAny {
/// # Parameters:
///
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
///
/// # Panics
///
/// Panics if there are no more shared pointers referencing the shared data and freeing the heap
/// memory fails.
#[cfg_attr(feature = "clib", no_mangle)]
#[allow(unused_variables)]
pub extern "C" fn nstd_shared_ptr_free(shared_ptr: NSTDSharedPtr) {}
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ gen_from_primitive!(
///
/// # Panics
///
/// This operation may panic if getting a handle to the heap fails.
/// Panics if deallocating fails.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
#[allow(unused_variables)]
Expand Down
Loading

0 comments on commit 70b4a92

Please sign in to comment.