Skip to content

Commit 53d786f

Browse files
authored
Rollup merge of #70728 - TimDiekmann:allocref-doc, r=Amanieu
Minor doc improvements on `AllocRef` r? @Amanieu
2 parents b91c376 + c061ff4 commit 53d786f

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/libcore/alloc/mod.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub enum ReallocPlacement {
119119
///
120120
/// Unlike [`GlobalAlloc`][], zero-sized allocations are allowed in `AllocRef`. If an underlying
121121
/// allocator does not support this (like jemalloc) or return a null pointer (such as
122-
/// `libc::malloc`), this case must be caught.
122+
/// `libc::malloc`), this must be caught by the implementation.
123123
///
124124
/// ### Currently allocated memory
125125
///
@@ -157,18 +157,20 @@ pub enum ReallocPlacement {
157157
/// # Safety
158158
///
159159
/// * Memory blocks returned from an allocator must point to valid memory and retain their validity
160-
/// until the instance and all of its clones are dropped, and
160+
/// until the instance and all of its clones are dropped,
161161
///
162162
/// * cloning or moving the allocator must not invalidate memory blocks returned from this
163-
/// allocator. A cloned allocator must behave like the same allocator.
163+
/// allocator. A cloned allocator must behave like the same allocator, and
164164
///
165165
/// * any pointer to a memory block which is [*currently allocated*] may be passed to any other
166166
/// method of the allocator.
167167
///
168168
/// [*currently allocated*]: #currently-allocated-memory
169169
#[unstable(feature = "allocator_api", issue = "32838")]
170170
pub unsafe trait AllocRef {
171-
/// On success, returns a memory block meeting the size and alignment guarantees of `layout`.
171+
/// Attempts to allocate a block of memory.
172+
///
173+
/// On success, returns a [`MemoryBlock`][] meeting the size and alignment guarantees of `layout`.
172174
///
173175
/// The returned block may have a larger size than specified by `layout.size()` and is
174176
/// initialized as specified by [`init`], all the way up to the returned size of the block.
@@ -190,26 +192,26 @@ pub unsafe trait AllocRef {
190192
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
191193
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr>;
192194

193-
/// Deallocates the memory denoted by `memory`.
195+
/// Deallocates the memory referenced by `ptr`.
194196
///
195197
/// # Safety
196198
///
197-
/// * `ptr` must be [*currently allocated*] via this allocator, and
198-
/// * `layout` must [*fit*] the `ptr`.
199+
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, and
200+
/// * `layout` must [*fit*] that block of memory.
199201
///
200202
/// [*currently allocated*]: #currently-allocated-memory
201203
/// [*fit*]: #memory-fitting
202204
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
203205

204206
/// Attempts to extend the memory block.
205207
///
206-
/// Returns a new memory block containing a pointer and the actual size of the allocated
207-
/// block. The pointer is suitable for holding data described by a new layout with `layout`’s
208+
/// Returns a new [`MemoryBlock`][] containing a pointer and the actual size of the allocated
209+
/// memory. The pointer is suitable for holding data described by a new layout with `layout`’s
208210
/// alignment and a size given by `new_size`. To accomplish this, the allocator may extend the
209211
/// allocation referenced by `ptr` to fit the new layout. If the [`placement`] is
210212
/// [`InPlace`], the returned pointer is guaranteed to be the same as the passed `ptr`.
211213
///
212-
/// If `ReallocPlacement::MayMove` is used then ownership of the memory block referenced by `ptr`
214+
/// If [`MayMove`] is used then ownership of the memory block referenced by `ptr`
213215
/// is transferred to this allocator. The memory may or may not be freed, and should be
214216
/// considered unusable (unless of course it is transferred back to the caller again via the
215217
/// return value of this method).
@@ -227,17 +229,18 @@ pub unsafe trait AllocRef {
227229
/// the size of the `MemoryBlock` returned by the `grow` call.
228230
///
229231
/// [`InPlace`]: ReallocPlacement::InPlace
232+
/// [`MayMove`]: ReallocPlacement::MayMove
230233
/// [`placement`]: ReallocPlacement
231234
/// [`init`]: AllocInit
232235
///
233236
/// # Safety
234237
///
235-
/// * `ptr` must be [*currently allocated*] via this allocator,
236-
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
238+
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator,
239+
/// * `layout` must [*fit*] that block of memory (The `new_size` argument need not fit it.),
237240
// We can't require that `new_size` is strictly greater than `memory.size` because of ZSTs.
238241
// An alternative would be
239242
// * `new_size must be strictly greater than `memory.size` or both are zero
240-
/// * `new_size` must be greater than or equal to `layout.size()`
243+
/// * `new_size` must be greater than or equal to `layout.size()`, and
241244
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`, must not overflow
242245
/// (i.e., the rounded value must be less than or equal to `usize::MAX`).
243246
///
@@ -289,8 +292,8 @@ pub unsafe trait AllocRef {
289292

290293
/// Attempts to shrink the memory block.
291294
///
292-
/// Returns a new memory block containing a pointer and the actual size of the allocated
293-
/// block. The pointer is suitable for holding data described by a new layout with `layout`’s
295+
/// Returns a new [`MemoryBlock`][] containing a pointer and the actual size of the allocated
296+
/// memory. The pointer is suitable for holding data described by a new layout with `layout`’s
294297
/// alignment and a size given by `new_size`. To accomplish this, the allocator may shrink the
295298
/// allocation referenced by `ptr` to fit the new layout. If the [`placement`] is
296299
/// [`InPlace`], the returned pointer is guaranteed to be the same as the passed `ptr`.
@@ -310,20 +313,20 @@ pub unsafe trait AllocRef {
310313
///
311314
/// # Safety
312315
///
313-
/// * `ptr` must be [*currently allocated*] via this allocator,
314-
/// * `layout` must [*fit*] the `ptr`. (The `new_size` argument need not fit it.)
316+
/// * `ptr` must denote a block of memory [*currently allocated*] via this allocator,
317+
/// * `layout` must [*fit*] that block of memory (The `new_size` argument need not fit it.), and
315318
// We can't require that `new_size` is strictly smaller than `memory.size` because of ZSTs.
316319
// An alternative would be
317320
// * `new_size must be strictly smaller than `memory.size` or both are zero
318-
/// * `new_size` must be smaller than or equal to `layout.size()`
321+
/// * `new_size` must be smaller than or equal to `layout.size()`.
319322
///
320323
/// [*currently allocated*]: #currently-allocated-memory
321324
/// [*fit*]: #memory-fitting
322325
///
323326
/// # Errors
324327
///
325328
/// Returns `Err` if the new layout does not meet the allocator's size and alignment
326-
/// constraints of the allocator, or if growing otherwise fails.
329+
/// constraints of the allocator, or if shrinking otherwise fails.
327330
///
328331
/// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
329332
/// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement

0 commit comments

Comments
 (0)