This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,19 @@ use crate::ptr;
53
53
/// * `Layout` queries and calculations in general must be correct. Callers of
54
54
/// this trait are allowed to rely on the contracts defined on each method,
55
55
/// and implementors must ensure such contracts remain true.
56
+ ///
57
+ /// * You may not rely on allocations actually happening, even if there are explicit
58
+ /// heap allocations in the source. The optimizer may detect allocation/deallocation
59
+ /// pairs that it can instead move to stack allocations/deallocations and thus never
60
+ /// invoke the allocator here.
61
+ /// More concretely, the following code example is unsound, irrespective of whether your
62
+ /// custom allocator allows counting how many allocations have happened.
63
+ ///
64
+ /// ```rust,ignore
65
+ /// drop(Box::new(42));
66
+ /// let number_of_heap_allocs = /* call private allocator API */;
67
+ /// unsafe { std::intrinsics::assume(number_of_heap_allocs > 0); }
68
+ /// ```
56
69
#[ stable( feature = "global_alloc" , since = "1.28.0" ) ]
57
70
pub unsafe trait GlobalAlloc {
58
71
/// Allocate memory as described by the given `layout`.
Original file line number Diff line number Diff line change @@ -94,6 +94,18 @@ pub unsafe trait AllocRef {
94
94
/// The returned block may have a larger size than specified by `layout.size()`, and may or may
95
95
/// not have its contents initialized.
96
96
///
97
+ /// Note that you may not rely on this method actually getting called, even if there are calls
98
+ /// to it in the source. The optimizer may detect allocation/deallocation pairs that it can
99
+ /// instead move to stack allocations/deallocations and thus never invoke the allocator here.
100
+ /// More concretely, the following code example is unsound, irrespective of whether your
101
+ /// custom allocator allows counting how many allocations have happened.
102
+ ///
103
+ /// ```rust,ignore
104
+ /// Global::dealloc(Global::alloc(some_layout));
105
+ /// let number_of_heap_allocs = /* call private allocator API */;
106
+ /// unsafe { std::intrinsics::assume(number_of_heap_allocs > 0); }
107
+ /// ```
108
+ ///
97
109
/// # Errors
98
110
///
99
111
/// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
You can’t perform that action at this time.
0 commit comments