Skip to content

Commit 3654aa5

Browse files
committed
restate GlobalAlloc method safety preconditions in terms of what the caller has to do for greater clarity
1 parent a8a88fe commit 3654aa5

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

library/core/src/alloc/global.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ pub unsafe trait GlobalAlloc {
125125
///
126126
/// # Safety
127127
///
128-
/// This function is unsafe because undefined behavior can result
129-
/// if the caller does not ensure that `layout` has non-zero size.
128+
/// The caller has to ensure that `layout` has non-zero size. Zero sized
129+
/// `layout` can result in undefined behaviour.
130130
///
131131
/// (Extension subtraits might provide more specific bounds on
132132
/// behavior, e.g., guarantee a sentinel address or a null pointer
@@ -157,14 +157,14 @@ pub unsafe trait GlobalAlloc {
157157
///
158158
/// # Safety
159159
///
160-
/// This function is unsafe because undefined behavior can result
161-
/// if the caller does not ensure all of the following:
160+
/// The caller has to ensure that
162161
///
163-
/// * `ptr` must denote a block of memory currently allocated via
164-
/// this allocator,
162+
/// * `ptr` is a block of memory currently allocated via this allocator and,
165163
///
166-
/// * `layout` must be the same layout that was used
167-
/// to allocate that block of memory.
164+
/// * `layout` is the same layout that was used to allocate that block of
165+
/// memory.
166+
///
167+
/// Otherwise undefined behavior can result.
168168
#[stable(feature = "global_alloc", since = "1.28.0")]
169169
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
170170

@@ -173,7 +173,8 @@ pub unsafe trait GlobalAlloc {
173173
///
174174
/// # Safety
175175
///
176-
/// This function is unsafe for the same reasons that `alloc` is.
176+
/// The caller has to ensure that `layout` has non-zero size. Like `alloc`
177+
/// zero sized `layout` can result in undefined behaviour.
177178
/// However the allocated block of memory is guaranteed to be initialized.
178179
///
179180
/// # Errors
@@ -221,20 +222,21 @@ pub unsafe trait GlobalAlloc {
221222
///
222223
/// # Safety
223224
///
224-
/// This function is unsafe because undefined behavior can result
225-
/// if the caller does not ensure all of the following:
225+
/// The caller has to ensure that
226226
///
227-
/// * `ptr` must be currently allocated via this allocator,
227+
/// * `ptr` is allocated via this allocator,
228228
///
229-
/// * `layout` must be the same layout that was used
229+
/// * `layout` is the same layout that was used
230230
/// to allocate that block of memory,
231231
///
232-
/// * `new_size` must be greater than zero.
232+
/// * `new_size` is greater than zero.
233233
///
234234
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`,
235-
/// must not overflow isize (i.e., the rounded value must be less than or
235+
/// does not overflow isize (i.e., the rounded value must be less than or
236236
/// equal to `isize::MAX`).
237237
///
238+
/// Otherwise undefined behaviour can result.
239+
///
238240
/// (Extension subtraits might provide more specific bounds on
239241
/// behavior, e.g., guarantee a sentinel address or a null pointer
240242
/// in response to a zero-size allocation request.)

0 commit comments

Comments
 (0)