Skip to content

Commit 81db99e

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

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

crates/oxc_allocator/src/from_raw_parts.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! * [`Allocator::alloc_bytes_start`]
55
//! * [`Allocator::data_ptr`]
66
//! * [`Allocator::set_data_ptr`]
7+
//! * [`Allocator::set_cursor_ptr`]
8+
//! * [`Allocator::data_end_ptr`]
79
//! * [`Allocator::end_ptr`]
810
911
use std::{
@@ -229,6 +231,32 @@ impl Allocator {
229231
chunk_footer.ptr.get()
230232
}
231233

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+
232260
/// Get pointer to end of this [`Allocator`]'s current chunk (after the `ChunkFooter`).
233261
pub fn end_ptr(&self) -> NonNull<u8> {
234262
// SAFETY: `chunk_footer_ptr` returns pointer to a valid `ChunkFooter`,

0 commit comments

Comments
 (0)