@@ -385,10 +385,25 @@ impl From<AllocErr> for CollectionAllocErr {
385
385
}
386
386
}
387
387
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.
389
390
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?
390
399
unsafe fn alloc ( & self , layout : Layout ) -> * mut Opaque ;
391
400
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.)
392
407
unsafe fn dealloc ( & self , ptr : * mut Opaque , layout : Layout ) ;
393
408
394
409
unsafe fn alloc_zeroed ( & self , layout : Layout ) -> * mut Opaque {
@@ -400,24 +415,43 @@ pub unsafe trait GlobalAlloc {
400
415
ptr
401
416
}
402
417
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
+ ///
403
427
/// # Safety
404
428
///
405
429
/// `new_size`, when rounded up to the nearest multiple of `old_layout.align()`,
406
430
/// 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 ( ) ) ;
409
436
let new_ptr = self . alloc ( new_layout) ;
410
437
if !new_ptr. is_null ( ) {
411
438
ptr:: copy_nonoverlapping (
412
439
ptr as * const u8 ,
413
440
new_ptr as * mut u8 ,
414
- cmp:: min ( old_layout . size ( ) , new_size) ,
441
+ cmp:: min ( layout . size ( ) , new_size) ,
415
442
) ;
416
- self . dealloc ( ptr, old_layout ) ;
443
+ self . dealloc ( ptr, layout ) ;
417
444
}
418
445
new_ptr
419
446
}
420
447
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.
421
455
fn oom ( & self ) -> ! {
422
456
unsafe { :: intrinsics:: abort ( ) }
423
457
}
0 commit comments