//! # memalloc //! //! Memory allocation in stable rust, providing a similar interface to `std::rt::heap`, //! notably these functions align everything according to the alignment of `u8`, rather //! than using a user-provided alignment. //! //! Additionally, they do not allow for handling allocation failure, and will simply //! abort the process on OOM. Unfortunately, this limitation is unavoidable if we want //! to use only stable APIs. //!
use std::mem;
/// Returns a pointer to `size` bytes of memory aligned to `mem::align_of::<u8>()`. /// /// On failure, aborts the process. /// /// Behavior is undefined if the requested size is 0. #[inline] pubunsafefn allocate(size: usize) -> *mut u8 {
ptr_from_vec(Vec::with_capacity(size))
}
/// Resizes the allocation referenced by `ptr` to `new_size` bytes. /// /// On failure, aborts the process. /// /// If the allocation was relocated, the memory at the passed-in pointer is /// undefined after the call. /// /// Behavior is undefined if the requested `new_size` is 0. /// /// The `old_size` parameter is the size used to create the allocation /// referenced by `ptr`, or the `new_size` passed to previous reallocations. pubunsafefn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 { if old_size > new_size { letmut buf = Vec::from_raw_parts(ptr, new_size, old_size);
buf.shrink_to_fit();
/// Deallocates the memory referenced by `ptr`. /// /// Behavior is undefined if `ptr` is null. /// /// The `old_size` parameter is the size used to create the allocation /// referenced by `ptr`, or the `new_size` passed to the last reallocation. #[inline] pubunsafefn deallocate(ptr: *mut u8, old_size: usize) {
Vec::from_raw_parts(ptr, 0, old_size);
}
/// A token empty allocation which cannot be read from or written to, /// but which can be used as a placeholder when a 0-sized allocation is /// required. pubfn empty() -> *mut u8 { 1as *mut u8
}
unsafe { // Put some data in the buffer
ptr::write(buffer.offset(0), 8);
ptr::write(buffer.offset(1), 4);
ptr::write(buffer.offset(5), 3);
ptr::write(buffer.offset(7), 6);
};
// Allocate so in-place fails. unsafe { allocate(128) };
unsafe { // Ensure the data is still there.
assert_eq!(ptr::read(buffer.offset(0)), 8);
assert_eq!(ptr::read(buffer.offset(1)), 4);
assert_eq!(ptr::read(buffer.offset(5)), 3);
assert_eq!(ptr::read(buffer.offset(7)), 6);
};
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.8 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.