|
4 | 4 | //! * [`Allocator::alloc_bytes_start`] |
5 | 5 | //! * [`Allocator::data_ptr`] |
6 | 6 | //! * [`Allocator::set_data_ptr`] |
| 7 | +//! * [`Allocator::set_cursor_ptr`] |
| 8 | +//! * [`Allocator::data_end_ptr`] |
7 | 9 | //! * [`Allocator::end_ptr`] |
8 | 10 |
|
9 | 11 | use std::{ |
@@ -229,6 +231,32 @@ impl Allocator { |
229 | 231 | chunk_footer.ptr.get() |
230 | 232 | } |
231 | 233 |
|
| 234 | + /// Set cursor pointer for this [`Allocator`]'s current chunk. |
| 235 | + /// |
| 236 | + /// This is dangerous, and this method should not ordinarily be used. |
| 237 | + /// It is only here for manually resetting the allocator. |
| 238 | + /// |
| 239 | + /// # SAFETY |
| 240 | + /// |
| 241 | + /// * Allocator must have at least 1 allocated chunk. |
| 242 | + /// It is UB to call this method on an `Allocator` which has not allocated |
| 243 | + /// i.e. fresh from `Allocator::new`. |
| 244 | + /// * `ptr` must point to within the allocation underlying this allocator. |
| 245 | + /// * `ptr` must be after `data_ptr` for this chunk. |
| 246 | + pub unsafe fn set_cursor_ptr(&self, ptr: NonNull<u8>) { |
| 247 | + // SAFETY: Caller guarantees `Allocator` has at least 1 allocated chunk. |
| 248 | + // We don't take any action with the `Allocator` while the `&mut ChunkFooter` reference |
| 249 | + // is alive, beyond setting the cursor pointer. |
| 250 | + let chunk_footer = unsafe { self.chunk_footer_mut() }; |
| 251 | + chunk_footer.ptr.set(ptr); |
| 252 | + } |
| 253 | + |
| 254 | + /// Get pointer to end of the data region of this [`Allocator`]'s current chunk |
| 255 | + /// i.e to the start of the `ChunkFooter`. |
| 256 | + pub fn data_end_ptr(&self) -> NonNull<u8> { |
| 257 | + self.chunk_footer_ptr().cast::<u8>() |
| 258 | + } |
| 259 | + |
232 | 260 | /// Get pointer to end of this [`Allocator`]'s current chunk (after the `ChunkFooter`). |
233 | 261 | pub fn end_ptr(&self) -> NonNull<u8> { |
234 | 262 | // SAFETY: `chunk_footer_ptr` returns pointer to a valid `ChunkFooter`, |
|
0 commit comments