@@ -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 ( ) ,
0 commit comments