Skip to content

Commit d0f8d62

Browse files
committed
Document the minimum size requirements for the heap initialization methods
Also, document the automatic alignment for the bottom pointer.
1 parent a505bfd commit d0f8d62

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/hole.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,19 @@ impl HoleList {
320320

321321
/// Creates a `HoleList` that contains the given hole.
322322
///
323+
/// The `hole_addr` pointer is automatically aligned, so the `bottom`
324+
/// field might be larger than the given `hole_addr`.
325+
///
326+
/// The given `hole_size` must be large enough to store the required
327+
/// metadata, otherwise this function will panic. Depending on the
328+
/// alignment of the `hole_addr` pointer, the minimum size is between
329+
/// `2 * size_of::<usize>` and `3 * size_of::<usize>`.
330+
///
323331
/// # Safety
324332
///
325333
/// This function is unsafe because it creates a hole at the given `hole_addr`.
326334
/// This can cause undefined behavior if this address is invalid or if memory from the
327335
/// `[hole_addr, hole_addr+size)` range is used somewhere else.
328-
///
329-
/// The pointer to `hole_addr` is automatically aligned.
330336
pub unsafe fn new(hole_addr: *mut u8, hole_size: usize) -> HoleList {
331337
assert_eq!(size_of::<Hole>(), Self::min_size());
332338
assert!(hole_size >= size_of::<Hole>());

src/lib.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ impl Heap {
5959

6060
/// Initializes an empty heap
6161
///
62+
/// The `heap_bottom` pointer is automatically aligned, so the [`bottom()`][Self::bottom]
63+
/// method might return a pointer that is larger than `heap_bottom` after construction.
64+
///
65+
/// The given `heap_size` must be large enough to store the required
66+
/// metadata, otherwise this function will panic. Depending on the
67+
/// alignment of the `hole_addr` pointer, the minimum size is between
68+
/// `2 * size_of::<usize>` and `3 * size_of::<usize>`.
69+
///
6270
/// # Safety
6371
///
6472
/// This function must be called at most once and must only be used on an
@@ -82,13 +90,17 @@ impl Heap {
8290
/// program's memory, from a mutable static, or by allocating and leaking such memory from
8391
/// another allocator.
8492
///
85-
/// The latter method may be especially useful if the underlying allocator does not perform
93+
/// The latter approach may be especially useful if the underlying allocator does not perform
8694
/// deallocation (e.g. a simple bump allocator). Then the overlaid linked-list-allocator can
8795
/// provide memory reclamation.
8896
///
8997
/// # Panics
9098
///
9199
/// This method panics if the heap is already initialized.
100+
///
101+
/// It also panics when the length of the given `mem` slice is not large enough to
102+
/// store the required metadata. Depending on the alignment of the slice, the minimum
103+
/// size is between `2 * size_of::<usize>` and `3 * size_of::<usize>`.
92104
pub fn init_from_slice(&mut self, mem: &'static mut [MaybeUninit<u8>]) {
93105
assert!(
94106
self.bottom().is_null(),
@@ -106,6 +118,14 @@ impl Heap {
106118

107119
/// Creates a new heap with the given `bottom` and `size`.
108120
///
121+
/// The `heap_bottom` pointer is automatically aligned, so the [`bottom()`][Self::bottom]
122+
/// method might return a pointer that is larger than `heap_bottom` after construction.
123+
///
124+
/// The given `heap_size` must be large enough to store the required
125+
/// metadata, otherwise this function will panic. Depending on the
126+
/// alignment of the `hole_addr` pointer, the minimum size is between
127+
/// `2 * size_of::<usize>` and `3 * size_of::<usize>`.
128+
///
109129
/// # Safety
110130
///
111131
/// The bottom address must be valid and the memory in the
@@ -123,8 +143,9 @@ impl Heap {
123143

124144
/// Creates a new heap from a slice of raw memory.
125145
///
126-
/// This has the same effect as [`init_from_slice`] on an empty heap, but it is combined into a
127-
/// single operation that can not panic.
146+
/// This is a convenience function that has the same effect as calling
147+
/// [`init_from_slice`] on an empty heap. All the requirements of `init_from_slice`
148+
/// apply to this function as well.
128149
pub fn from_slice(mem: &'static mut [MaybeUninit<u8>]) -> Heap {
129150
let size = mem.len();
130151
let address = mem.as_mut_ptr().cast();
@@ -168,6 +189,9 @@ impl Heap {
168189
}
169190

170191
/// Returns the bottom address of the heap.
192+
///
193+
/// The bottom pointer is automatically aligned, so the returned pointer
194+
/// might be larger than the bottom pointer used for initialization.
171195
pub fn bottom(&self) -> *mut u8 {
172196
self.holes.bottom
173197
}
@@ -282,6 +306,14 @@ impl LockedHeap {
282306

283307
/// Creates a new heap with the given `bottom` and `size`.
284308
///
309+
/// The `heap_bottom` pointer is automatically aligned, so the [`bottom()`][Heap::bottom]
310+
/// method might return a pointer that is larger than `heap_bottom` after construction.
311+
///
312+
/// The given `heap_size` must be large enough to store the required
313+
/// metadata, otherwise this function will panic. Depending on the
314+
/// alignment of the `hole_addr` pointer, the minimum size is between
315+
/// `2 * size_of::<usize>` and `3 * size_of::<usize>`.
316+
///
285317
/// # Safety
286318
///
287319
/// The bottom address must be valid and the memory in the

0 commit comments

Comments
 (0)