nvim_oxi_types/arena.rs
1use core::cell::OnceCell;
2use core::ffi::c_char;
3use core::ptr;
4
5use libc::size_t;
6
7thread_local! {
8 static ARENA: OnceCell<Arena> = const { OnceCell::new() };
9}
10
11/// A memory arena that is passed to the C API for allocation.
12#[allow(dead_code)]
13#[repr(C)]
14pub struct Arena {
15 current_block: *mut c_char,
16 pos: size_t,
17 size: size_t,
18}
19
20impl Arena {
21 #[inline]
22 fn new() -> Self {
23 Self { current_block: ptr::null_mut(), pos: 0, size: 0 }
24 }
25}
26
27/// Initializes the [`Arena`].
28///
29/// This should be called as soon as the plugin is loaded.
30#[doc(hidden)]
31#[inline]
32pub fn arena_init() {
33 ARENA.with(|arena| {
34 let _ = arena.set(Arena::new());
35 });
36}
37
38/// Returns a pointer to the [`Arena`] that can be passed to the C API.
39///
40/// # Panics
41///
42/// Panics if the [`Arena`] wasn't initialized by calling [`arena_init`].
43#[doc(hidden)]
44#[inline]
45pub fn arena() -> *mut Arena {
46 ptr::null_mut()
47 // ARENA.with(|arena| {
48 // let Some(arena) = arena.get() else {
49 // panic!("Arena is not initialized")
50 // };
51 // arena as *const _ as *mut _
52 // })
53}