Skip to content

Commit 70703b0

Browse files
authored
Merge pull request #45 from mevinagrise/docs-safety-sections
docs: add missing safety sections for public unsafe APIs
2 parents 0e44835 + f916fb7 commit 70703b0

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/lib.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ impl<const ORDER: usize> Heap<ORDER> {
7979
Self::new()
8080
}
8181

82-
/// Add a range of memory [start, end) to the heap
82+
/// Add a range of memory `[start, end)` to the heap.
83+
///
84+
/// # Safety
85+
///
86+
/// The caller must ensure the memory range is valid, writable, and not currently managed by
87+
/// any other allocator or by this `Heap`. In particular, the provided `[start, end)` range
88+
/// must not overlap with any memory region that has already been added to this `Heap`. The
89+
/// range must remain available for the lifetime of this heap.
8390
pub unsafe fn add_to_heap(&mut self, mut start: usize, mut end: usize) {
8491
// avoid unaligned access on some platforms
8592
start = (start + size_of::<usize>() - 1) & (!size_of::<usize>() + 1);
@@ -109,7 +116,15 @@ impl<const ORDER: usize> Heap<ORDER> {
109116
self.total += total;
110117
}
111118

112-
/// Add a range of memory [start, start+size) to the heap
119+
/// Add a range of memory `[start, start + size)` to the heap.
120+
///
121+
/// # Safety
122+
///
123+
/// The caller must ensure the memory range is valid, writable, and not currently managed by
124+
/// any other allocator. Additionally, the range `[start, start + size)` must be disjoint from
125+
/// every memory region previously added to this heap instance, whether via
126+
/// [`Heap::add_to_heap`] or [`Heap::init`]. The range must remain available for the lifetime
127+
/// of this heap.
113128
pub unsafe fn init(&mut self, start: usize, size: usize) {
114129
self.add_to_heap(start, start + size);
115130
}
@@ -155,7 +170,12 @@ impl<const ORDER: usize> Heap<ORDER> {
155170
Err(())
156171
}
157172

158-
/// Dealloc a range of memory from the heap
173+
/// Dealloc a range of memory from the heap.
174+
///
175+
/// # Safety
176+
///
177+
/// `ptr` and `layout` must exactly match a previous successful allocation from this specific
178+
/// `Heap` instance, and that allocation must not already have been deallocated.
159179
pub unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
160180
let size = max(
161181
layout.size().next_power_of_two(),

src/linked_list.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ impl LinkedList {
3030
}
3131

3232
/// Push `item` to the front of the list
33+
///
34+
/// # Safety
35+
///
36+
/// `item` must be a valid, writable pointer, properly aligned for `usize` reads and writes.
37+
/// The caller must ensure that the pointed value can be used to store the next pointer for
38+
/// this intrusive linked list, and remains valid for as long as it is contained in the list.
3339
pub unsafe fn push(&mut self, item: *mut usize) {
3440
*item = self.head as usize;
3541
self.head = item;

0 commit comments

Comments
 (0)