Skip to content

Commit d746a5f

Browse files
committed
Improve docs for HoleList
1 parent 1892866 commit d746a5f

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/hole.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ impl HoleList {
3333
}
3434
}
3535

36-
/// Creates a `HoleList` that contains the given hole. This function is unsafe because it
36+
/// Creates a `HoleList` that contains the given hole.
37+
///
38+
/// ## Safety
39+
///
40+
/// This function is unsafe because it
3741
/// creates a hole at the given `hole_addr`. This can cause undefined behavior if this address
38-
/// is invalid or if memory from the `[hole_addr, hole_addr+size) range is used somewhere else.
42+
/// is invalid or if memory from the `[hole_addr, hole_addr+size)` range is used somewhere else.
3943
///
4044
/// The pointer to `hole_addr` is automatically aligned.
4145
pub unsafe fn new(hole_addr: usize, hole_size: usize) -> HoleList {
@@ -56,8 +60,14 @@ impl HoleList {
5660
}
5761
}
5862

59-
/// Align layout. Returns a layout with size increased to
60-
/// fit at least `HoleList::min_size` and proper alignment of a `Hole`.
63+
/// Aligns the given layout for use with `HoleList`.
64+
///
65+
/// Returns a layout with size increased to fit at least `HoleList::min_size` and proper
66+
/// alignment of a `Hole`.
67+
///
68+
/// The [`allocate_first_fit`][HoleList::allocate_first_fit] and
69+
/// [`deallocate`][HoleList::deallocate] methods perform the required alignment
70+
/// themselves, so calling this function manually is not necessary.
6171
pub fn align_layout(layout: Layout) -> Layout {
6272
let mut size = layout.size();
6373
if size < Self::min_size() {
@@ -69,11 +79,14 @@ impl HoleList {
6979
layout
7080
}
7181

72-
/// Searches the list for a big enough hole. A hole is big enough if it can hold an allocation
73-
/// of `layout.size()` bytes with the given `layout.align()`. If such a hole is found in the
74-
/// list, a block of the required size is allocated from it. Then the start address of that
82+
/// Searches the list for a big enough hole.
83+
///
84+
/// A hole is big enough if it can hold an allocation of `layout.size()` bytes with
85+
/// the given `layout.align()`. If such a hole is found in the list, a block of the
86+
/// required size is allocated from it. Then the start address of that
7587
/// block and the aligned layout are returned. The automatic layout alignment is required
76-
/// because the HoleList has some additional layout requirements for each memory block.
88+
/// because the `HoleList` has some additional layout requirements for each memory block.
89+
///
7790
/// This function uses the “first fit” strategy, so it uses the first hole that is big
7891
/// enough. Thus the runtime is in O(n) but it should be reasonably fast for small allocations.
7992
pub fn allocate_first_fit(&mut self, layout: Layout) -> Result<(NonNull<u8>, Layout), ()> {
@@ -94,16 +107,18 @@ impl HoleList {
94107
})
95108
}
96109

97-
/// Frees the allocation given by `ptr` and `layout`. `ptr` must be a pointer returned by a call
98-
/// to the `allocate_first_fit` function with identical layout. Undefined behavior may occur for
99-
/// invalid arguments.
100-
/// The function performs exactly the same layout adjustments as [allocate_first_fit] and
110+
/// Frees the allocation given by `ptr` and `layout`.
111+
///
112+
/// `ptr` must be a pointer returned by a call to the [`allocate_first_fit`] function with
113+
/// identical layout. Undefined behavior may occur for invalid arguments.
114+
/// The function performs exactly the same layout adjustments as [`allocate_first_fit`] and
101115
/// returns the aligned layout.
116+
///
102117
/// This function walks the list and inserts the given block at the correct place. If the freed
103118
/// block is adjacent to another free block, the blocks are merged again.
104119
/// This operation is in `O(n)` since the list needs to be sorted by address.
105120
///
106-
/// [allocate_first_fit]: ./struct.HoleList.html#method.allocate_first_fit
121+
/// [`allocate_first_fit`]: HoleList::allocate_first_fit
107122
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout) -> Layout {
108123
let aligned_layout = Self::align_layout(layout);
109124
deallocate(

0 commit comments

Comments
 (0)