@@ -33,9 +33,13 @@ impl HoleList {
33
33
}
34
34
}
35
35
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
37
41
/// 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.
39
43
///
40
44
/// The pointer to `hole_addr` is automatically aligned.
41
45
pub unsafe fn new ( hole_addr : usize , hole_size : usize ) -> HoleList {
@@ -56,8 +60,14 @@ impl HoleList {
56
60
}
57
61
}
58
62
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.
61
71
pub fn align_layout ( layout : Layout ) -> Layout {
62
72
let mut size = layout. size ( ) ;
63
73
if size < Self :: min_size ( ) {
@@ -69,11 +79,14 @@ impl HoleList {
69
79
layout
70
80
}
71
81
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
75
87
/// 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
+ ///
77
90
/// This function uses the “first fit” strategy, so it uses the first hole that is big
78
91
/// enough. Thus the runtime is in O(n) but it should be reasonably fast for small allocations.
79
92
pub fn allocate_first_fit ( & mut self , layout : Layout ) -> Result < ( NonNull < u8 > , Layout ) , ( ) > {
@@ -94,16 +107,18 @@ impl HoleList {
94
107
} )
95
108
}
96
109
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
101
115
/// returns the aligned layout.
116
+ ///
102
117
/// This function walks the list and inserts the given block at the correct place. If the freed
103
118
/// block is adjacent to another free block, the blocks are merged again.
104
119
/// This operation is in `O(n)` since the list needs to be sorted by address.
105
120
///
106
- /// [allocate_first_fit]: ./struct. HoleList.html#method. allocate_first_fit
121
+ /// [` allocate_first_fit` ]: HoleList:: allocate_first_fit
107
122
pub unsafe fn deallocate ( & mut self , ptr : NonNull < u8 > , layout : Layout ) -> Layout {
108
123
let aligned_layout = Self :: align_layout ( layout) ;
109
124
deallocate (
0 commit comments