Skip to content

Commit b63fa99

Browse files
authored
Rollup merge of #92274 - woppopo:const_deallocate, r=oli-obk
Add `intrinsics::const_deallocate` Tracking issue: #79597 Related: #91884 This allows deallocation of a memory allocated by `intrinsics::const_allocate`. At the moment, this can be only used to reduce memory usage, but in the future this may be useful to detect memory leaks (If an allocated memory remains after evaluation, raise an error...?).
2 parents 2316f6f + e4ba832 commit b63fa99

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

core/src/intrinsics.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,31 @@ extern "rust-intrinsic" {
19141914
#[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
19151915
pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
19161916

1917-
/// Allocate at compile time. Should not be called at runtime.
1917+
/// Allocates a block of memory at compile time.
1918+
/// At runtime, just returns a null pointer.
1919+
///
1920+
/// # Safety
1921+
///
1922+
/// - The `align` argument must be a power of two.
1923+
/// - At compile time, a compile error occurs if this constraint is violated.
1924+
/// - At runtime, it is not checked.
19181925
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
19191926
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
19201927

1928+
/// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
1929+
/// At runtime, does nothing.
1930+
///
1931+
/// # Safety
1932+
///
1933+
/// - The `align` argument must be a power of two.
1934+
/// - At compile time, a compile error occurs if this constraint is violated.
1935+
/// - At runtime, it is not checked.
1936+
/// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
1937+
/// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
1938+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
1939+
#[cfg(not(bootstrap))]
1940+
pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
1941+
19211942
/// Determines whether the raw bytes of the two values are equal.
19221943
///
19231944
/// This is particularly handy for arrays, since it allows things like just

core/tests/intrinsics.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,25 @@ fn test_hints_in_const_contexts() {
8080
assert!(42u32 == core::hint::black_box(42u32));
8181
}
8282
}
83+
84+
#[cfg(not(bootstrap))]
85+
#[test]
86+
fn test_const_allocate_at_runtime() {
87+
use core::intrinsics::const_allocate;
88+
unsafe {
89+
assert!(const_allocate(4, 4).is_null());
90+
}
91+
}
92+
93+
#[cfg(not(bootstrap))]
94+
#[test]
95+
fn test_const_deallocate_at_runtime() {
96+
use core::intrinsics::const_deallocate;
97+
const X: &u32 = &42u32;
98+
let x = &0u32;
99+
unsafe {
100+
const_deallocate(X as *const _ as *mut u8, 4, 4); // nop
101+
const_deallocate(x as *const _ as *mut u8, 4, 4); // nop
102+
const_deallocate(core::ptr::null_mut(), 1, 1); // nop
103+
}
104+
}

core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(const_bool_to_option)]
1414
#![feature(const_cell_into_inner)]
1515
#![feature(const_convert)]
16+
#![feature(const_heap)]
1617
#![feature(const_maybe_uninit_as_mut_ptr)]
1718
#![feature(const_maybe_uninit_assume_init)]
1819
#![feature(const_maybe_uninit_assume_init_read)]

0 commit comments

Comments
 (0)