Skip to content

Commit 11cb71b

Browse files
committed
feat(allocator): add data_end_ptr and set_cursor_ptr methods to Allocator
1 parent 3a1001c commit 11cb71b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

crates/oxc_allocator/src/from_raw_parts.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ impl Allocator {
229229
chunk_footer.ptr.get()
230230
}
231231

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

0 commit comments

Comments
 (0)