Skip to content

Commit c5ffdd7

Browse files
committed
Initial docs for the GlobalAlloc trait
1 parent f607a38 commit c5ffdd7

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/libcore/alloc.rs

+39-5
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,25 @@ impl From<AllocErr> for CollectionAllocErr {
385385
}
386386
}
387387

388-
// FIXME: docs
388+
/// A memory allocator that can be registered to be the one backing `std::alloc::Global`
389+
/// though the `#[global_allocator]` attributes.
389390
pub unsafe trait GlobalAlloc {
391+
/// Allocate memory as described by the given `layout`.
392+
///
393+
/// Returns a pointer to newly-allocated memory,
394+
/// or NULL to indicate allocation failure.
395+
///
396+
/// # Safety
397+
///
398+
/// **FIXME:** what are the exact requirements?
390399
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque;
391400

401+
/// Deallocate the block of memory at the given `ptr` pointer with the given `layout`.
402+
///
403+
/// # Safety
404+
///
405+
/// **FIXME:** what are the exact requirements?
406+
/// In particular around layout *fit*. (See docs for the `Alloc` trait.)
392407
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout);
393408

394409
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
@@ -400,24 +415,43 @@ pub unsafe trait GlobalAlloc {
400415
ptr
401416
}
402417

418+
/// Shink or grow a block of memory to the given `new_size`.
419+
/// The block is described by the given `ptr` pointer and `layout`.
420+
///
421+
/// Return a new pointer (which may or may not be the same as `ptr`),
422+
/// or NULL to indicate reallocation failure.
423+
///
424+
/// If reallocation is successful, the old `ptr` pointer is considered
425+
/// to have been deallocated.
426+
///
403427
/// # Safety
404428
///
405429
/// `new_size`, when rounded up to the nearest multiple of `old_layout.align()`,
406430
/// must not overflow (i.e. the rounded value must be less than `usize::MAX`).
407-
unsafe fn realloc(&self, ptr: *mut Opaque, old_layout: Layout, new_size: usize) -> *mut Opaque {
408-
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
431+
///
432+
/// **FIXME:** what are the exact requirements?
433+
/// In particular around layout *fit*. (See docs for the `Alloc` trait.)
434+
unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque {
435+
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
409436
let new_ptr = self.alloc(new_layout);
410437
if !new_ptr.is_null() {
411438
ptr::copy_nonoverlapping(
412439
ptr as *const u8,
413440
new_ptr as *mut u8,
414-
cmp::min(old_layout.size(), new_size),
441+
cmp::min(layout.size(), new_size),
415442
);
416-
self.dealloc(ptr, old_layout);
443+
self.dealloc(ptr, layout);
417444
}
418445
new_ptr
419446
}
420447

448+
/// Aborts the thread or process, optionally performing
449+
/// cleanup or logging diagnostic information before panicking or
450+
/// aborting.
451+
///
452+
/// `oom` is meant to be used by clients unable to cope with an
453+
/// unsatisfied allocation request, and wish to abandon
454+
/// computation rather than attempt to recover locally.
421455
fn oom(&self) -> ! {
422456
unsafe { ::intrinsics::abort() }
423457
}

0 commit comments

Comments
 (0)