1use core::ffi::c_void;
11use core::mem::{offset_of, size_of};
12use core::ptr;
13use num_bigint::BigInt;
14use std::alloc::{self, Layout};
15use std::cell::Cell;
16
17#[repr(C)]
19pub struct Object {
20 pub m_rc: i32,
21 pub m_cs_sz: u16,
22 pub m_other: u8,
23 pub m_tag: u8,
24}
25
26pub type Obj = *mut Object;
28
29pub const LEAN_MAX_CTOR_TAG: u8 = 244;
30pub const LEAN_PROMISE: u8 = 244;
31pub const LEAN_CLOSURE: u8 = 245;
32pub const LEAN_ARRAY: u8 = 246;
33pub const LEAN_STRUCT_ARRAY: u8 = 247;
34pub const LEAN_SCALAR_ARRAY: u8 = 248;
35pub const LEAN_STRING: u8 = 249;
36pub const LEAN_MPZ: u8 = 250;
37pub const LEAN_THUNK: u8 = 251;
38pub const LEAN_TASK: u8 = 252;
39pub const LEAN_REF: u8 = 253;
40pub const LEAN_EXTERNAL: u8 = 254;
41pub const LEAN_RESERVED: u8 = 255;
42
43pub const LEAN_LINEAR_MARK_MASK: u8 = 0x80;
45
46pub const FREED_RC: i32 = -0x5EAD_DEAD;
48
49#[repr(C)]
50pub struct CtorObject {
51 pub m_header: Object,
52 pub m_objs: [Obj; 0],
53}
54
55#[repr(C)]
56pub struct ArrayObject {
57 pub m_header: Object,
58 pub m_size: usize,
59 pub m_capacity: usize,
60 pub m_data: [Obj; 0],
61}
62
63#[repr(C)]
64pub struct SArrayObject {
65 pub m_header: Object,
66 pub m_size: usize,
67 pub m_capacity: usize,
68 pub m_data: [u8; 0],
69}
70
71#[repr(C)]
72pub struct StringObject {
73 pub m_header: Object,
74 pub m_size: usize,
76 pub m_capacity: usize,
77 pub m_length: usize,
79 pub m_data: [u8; 0],
80}
81
82#[repr(C)]
83pub struct ClosureObject {
84 pub m_header: Object,
85 pub m_fun: *const (),
86 pub m_arity: u16,
87 pub m_num_fixed: u16,
88 pub m_objs: [Obj; 0],
89}
90
91#[repr(C)]
92pub struct RefObject {
93 pub m_header: Object,
94 pub m_value: Obj,
95}
96
97#[repr(C)]
98pub struct ThunkObject {
99 pub m_header: Object,
100 pub m_value: Obj,
101 pub m_closure: Obj,
102}
103
104#[repr(C)]
107pub struct TaskObject {
108 pub m_header: Object,
109 pub m_value: Obj,
110 pub m_imp: *mut c_void,
111}
112
113#[repr(C)]
115pub struct ExternalClass {
116 pub m_finalize: unsafe fn(*mut c_void),
117 pub m_foreach: unsafe fn(*mut c_void, Obj),
118}
119
120#[repr(C)]
121pub struct ExternalObject {
122 pub m_header: Object,
123 pub m_class: *const ExternalClass,
124 pub m_data: *mut c_void,
125}
126
127#[repr(C)]
129pub struct MpzObject {
130 pub m_header: Object,
131 pub m_value: BigInt,
132}
133
134const CTOR_OBJS: usize = offset_of!(CtorObject, m_objs);
135
136#[inline]
139fn layout_of(sz: usize) -> Layout {
140 match Layout::from_size_align((sz + 7) & !7, 8) {
141 Ok(l) => l,
142 Err(_) => lean_internal_panic_out_of_memory(),
143 }
144}
145
146#[inline]
148pub unsafe fn lean_alloc_object(sz: usize) -> Obj {
149 let layout = layout_of(sz);
150 let p = alloc::alloc(layout);
151 if p.is_null() {
152 alloc::handle_alloc_error(layout);
153 }
154 p as Obj
155}
156
157#[inline]
158pub unsafe fn lean_alloc_small_object(sz: usize) -> Obj {
159 lean_alloc_object(sz)
160}
161
162#[inline]
164pub unsafe fn lean_set_st_header(o: Obj, tag: u32, other: u32) {
165 (*o).m_rc = 1;
166 (*o).m_tag = tag as u8;
167 (*o).m_other = other as u8;
168 (*o).m_cs_sz = 0;
169}
170
171#[inline]
173pub unsafe fn lean_set_non_heap_header(o: Obj, sz: usize, tag: u32, other: u32) {
174 (*o).m_rc = 0;
175 (*o).m_tag = tag as u8;
176 (*o).m_other = other as u8;
177 (*o).m_cs_sz = sz as u16;
178}
179
180pub unsafe fn lean_object_byte_size(o: Obj) -> usize {
182 let tag = (*o).m_tag;
183 if tag <= LEAN_MAX_CTOR_TAG {
184 return (*o).m_cs_sz as usize;
185 }
186 match tag {
187 LEAN_CLOSURE => {
188 size_of::<ClosureObject>()
189 + size_of::<Obj>() * (*(o as *mut ClosureObject)).m_arity as usize
190 }
191 LEAN_ARRAY => {
192 size_of::<ArrayObject>() + size_of::<Obj>() * (*(o as *mut ArrayObject)).m_capacity
193 }
194 LEAN_SCALAR_ARRAY => {
195 size_of::<SArrayObject>()
196 + lean_sarray_elem_size(o) as usize * (*(o as *mut SArrayObject)).m_capacity
197 }
198 LEAN_STRING => size_of::<StringObject>() + (*(o as *mut StringObject)).m_capacity,
199 LEAN_MPZ => size_of::<MpzObject>(),
200 LEAN_THUNK => size_of::<ThunkObject>(),
201 LEAN_TASK => size_of::<TaskObject>(),
202 LEAN_REF => size_of::<RefObject>(),
203 LEAN_EXTERNAL => size_of::<ExternalObject>(),
204 _ => lean_internal_panic_unreachable(),
205 }
206}
207
208#[cfg(all(debug_assertions, not(miri)))]
210const QUARANTINE: usize = 1 << 14;
211
212#[cfg(all(debug_assertions, not(miri)))]
213thread_local! {
214 static FREED: core::cell::RefCell<std::collections::VecDeque<(*mut u8, Layout)>> =
215 const { core::cell::RefCell::new(std::collections::VecDeque::new()) };
216}
217
218#[inline]
220pub unsafe fn lean_dealloc(o: Obj, sz: usize) {
221 let layout = layout_of(sz);
222 #[cfg(debug_assertions)]
223 {
224 (*o).m_rc = FREED_RC;
225 }
226 #[cfg(all(debug_assertions, not(miri)))]
227 {
228 let evicted = FREED.with(|q| {
229 let mut q = q.borrow_mut();
230 q.push_back((o as *mut u8, layout));
231 if q.len() > QUARANTINE {
232 q.pop_front()
233 } else {
234 None
235 }
236 });
237 if let Some((p, l)) = evicted {
238 alloc::dealloc(p, l);
239 }
240 }
241 #[cfg(not(all(debug_assertions, not(miri))))]
242 alloc::dealloc(o as *mut u8, layout);
243}
244
245pub unsafe fn lean_free_object(o: Obj) {
247 debug_check_live(o, "free");
248 if (*o).m_tag == LEAN_MPZ {
249 ptr::drop_in_place(ptr::addr_of_mut!((*(o as *mut MpzObject)).m_value));
250 }
251 lean_dealloc(o, lean_object_byte_size(o));
252}
253
254#[inline]
255pub unsafe fn lean_free_small_object(o: Obj) {
256 lean_free_object(o);
257}
258
259#[inline]
262pub fn lean_is_scalar(o: Obj) -> bool {
263 o.addr() & 1 == 1
264}
265
266#[inline]
267pub fn lean_box(n: usize) -> Obj {
268 ptr::without_provenance_mut((n << 1) | 1)
269}
270
271#[inline]
272pub fn lean_unbox(o: Obj) -> usize {
273 o.addr() >> 1
274}
275
276#[cold]
279#[inline(never)]
280fn rc_error(o: Obj, rc: i32, op: &str) -> ! {
281 if rc == FREED_RC {
282 panic!("leanfran-runtime: {op} of the freed object {o:p}");
283 }
284 panic!("leanfran-runtime: {op} of the object {o:p} with the reference count {rc}");
285}
286
287#[inline(always)]
289pub unsafe fn debug_check_live(o: Obj, op: &str) {
290 #[cfg(debug_assertions)]
291 if !lean_is_scalar(o) && (*o).m_rc < 0 {
292 rc_error(o, (*o).m_rc, op);
293 }
294 let _ = (o, op);
295}
296
297#[inline]
298pub unsafe fn lean_is_st(o: Obj) -> bool {
299 (*o).m_rc > 0
300}
301
302#[inline]
303pub unsafe fn lean_is_persistent(o: Obj) -> bool {
304 (*o).m_rc == 0
305}
306
307#[inline]
308pub unsafe fn lean_has_rc(o: Obj) -> bool {
309 (*o).m_rc != 0
310}
311
312#[inline]
313pub unsafe fn lean_inc_ref_n(o: Obj, n: usize) {
314 let rc = (*o).m_rc;
315 if rc > 0 {
316 match rc.checked_add(n as i32) {
317 Some(r) if n <= i32::MAX as usize => (*o).m_rc = r,
318 _ => lean_internal_panic_rc_overflow(),
319 }
320 } else if rc != 0 {
321 rc_error(o, rc, "increment");
322 }
323}
324
325#[inline]
326pub unsafe fn lean_inc_ref(o: Obj) {
327 let rc = (*o).m_rc;
328 if rc > 0 {
329 #[cfg(debug_assertions)]
330 if rc == i32::MAX {
331 lean_internal_panic_rc_overflow();
332 }
333 (*o).m_rc = rc + 1;
334 } else if rc != 0 {
335 rc_error(o, rc, "increment");
336 }
337}
338
339#[inline]
340pub unsafe fn lean_inc(o: Obj) {
341 if !lean_is_scalar(o) {
342 lean_inc_ref(o);
343 }
344}
345
346#[inline]
347pub unsafe fn lean_inc_n(o: Obj, n: usize) {
348 if !lean_is_scalar(o) {
349 lean_inc_ref_n(o, n);
350 }
351}
352
353#[inline]
354pub unsafe fn lean_dec_ref(o: Obj) {
355 let rc = (*o).m_rc;
356 if rc > 1 {
357 (*o).m_rc = rc - 1;
358 } else if rc == 1 {
359 lean_dec_ref_cold(o);
360 } else if rc != 0 {
361 rc_error(o, rc, "decrement");
362 }
363}
364
365#[inline]
366pub unsafe fn lean_dec(o: Obj) {
367 if !lean_is_scalar(o) {
368 lean_dec_ref(o);
369 }
370}
371
372thread_local! {
373 static DEL_TODO: Cell<Vec<Obj>> = const { Cell::new(Vec::new()) };
374}
375
376unsafe fn for_each_child(o: Obj, f: &mut impl FnMut(Obj)) {
378 let tag = (*o).m_tag;
379 if tag <= LEAN_MAX_CTOR_TAG {
380 let objs = lean_ctor_obj_cptr(o);
381 for i in 0..lean_ctor_num_objs(o) as usize {
382 f(*objs.add(i));
383 }
384 return;
385 }
386 match tag {
387 LEAN_CLOSURE => {
388 let c = o as *mut ClosureObject;
389 let objs = lean_closure_arg_cptr(o);
390 for i in 0..(*c).m_num_fixed as usize {
391 f(*objs.add(i));
392 }
393 }
394 LEAN_ARRAY => {
395 let data = lean_array_cptr(o);
396 for i in 0..(*(o as *mut ArrayObject)).m_size {
397 f(*data.add(i));
398 }
399 }
400 LEAN_THUNK => {
401 let t = o as *mut ThunkObject;
402 if !(*t).m_value.is_null() {
403 f((*t).m_value);
404 }
405 if !(*t).m_closure.is_null() {
406 f((*t).m_closure);
407 }
408 }
409 LEAN_TASK => {
410 let t = o as *mut TaskObject;
411 if !(*t).m_value.is_null() {
412 f((*t).m_value);
413 }
414 }
415 LEAN_REF => {
416 let r = o as *mut RefObject;
417 if !(*r).m_value.is_null() {
418 f((*r).m_value);
419 }
420 }
421 _ => {}
422 }
423}
424
425#[cold]
428#[inline(never)]
429pub unsafe fn lean_dec_ref_cold(o: Obj) {
430 let mut todo = DEL_TODO.take();
431 todo.push(o);
432 while let Some(o) = todo.pop() {
433 let mut child = |c: Obj| {
434 if lean_is_scalar(c) {
435 return;
436 }
437 let rc = (*c).m_rc;
438 if rc > 1 {
439 (*c).m_rc = rc - 1;
440 } else if rc == 1 {
441 (*c).m_rc = 0;
442 todo.push(c);
443 } else if rc != 0 {
444 rc_error(c, rc, "decrement");
445 }
446 };
447 for_each_child(o, &mut child);
448 if (*o).m_tag == LEAN_EXTERNAL {
449 let e = o as *mut ExternalObject;
450 ((*(*e).m_class).m_finalize)((*e).m_data);
451 }
452 lean_free_object_unchecked(o);
453 }
454 DEL_TODO.set(todo);
455}
456
457unsafe fn lean_free_object_unchecked(o: Obj) {
458 if (*o).m_tag == LEAN_MPZ {
459 ptr::drop_in_place(ptr::addr_of_mut!((*(o as *mut MpzObject)).m_value));
460 }
461 lean_dealloc(o, lean_object_byte_size(o));
462}
463
464pub unsafe fn lean_mark_persistent(o: Obj) {
466 let mut todo = vec![o];
467 while let Some(o) = todo.pop() {
468 if lean_is_scalar(o) || !lean_has_rc(o) {
469 continue;
470 }
471 debug_check_live(o, "mark_persistent");
472 (*o).m_rc = 0;
473 if (*o).m_tag == LEAN_EXTERNAL {
474 let e = o as *mut ExternalObject;
475 let f = crate::closure::lean_alloc_closure(mark_persistent_fn as *const (), 1, 0);
476 ((*(*e).m_class).m_foreach)((*e).m_data, f);
477 lean_dec(f);
478 } else {
479 for_each_child(o, &mut |c| todo.push(c));
480 }
481 }
482}
483
484unsafe fn mark_persistent_fn(o: Obj) -> Obj {
485 lean_mark_persistent(o);
486 lean_box(0)
487}
488
489#[inline]
490pub unsafe fn lean_mark_mt(_o: Obj) {}
491
492#[inline]
495pub unsafe fn lean_ptr_tag(o: Obj) -> u8 {
496 debug_check_live(o, "tag read");
497 (*o).m_tag
498}
499
500#[inline]
501pub unsafe fn lean_ptr_other(o: Obj) -> u32 {
502 (*o).m_other as u32
503}
504
505#[inline]
506pub unsafe fn lean_obj_tag(o: Obj) -> u32 {
507 if lean_is_scalar(o) {
508 lean_unbox(o) as u32
509 } else {
510 lean_ptr_tag(o) as u32
511 }
512}
513
514#[inline]
515pub unsafe fn lean_is_exclusive(o: Obj) -> bool {
516 if lean_is_scalar(o) {
517 return false;
518 }
519 debug_check_live(o, "exclusivity check");
520 (*o).m_rc == 1
521}
522
523#[inline]
524pub unsafe fn lean_is_exclusive_obj(o: Obj) -> u8 {
525 lean_is_exclusive(o) as u8
526}
527
528#[inline]
529pub unsafe fn lean_is_shared(o: Obj) -> bool {
530 !lean_is_scalar(o) && (*o).m_rc > 1
531}
532
533#[inline]
534pub unsafe fn lean_nonzero_rc(o: Obj) -> bool {
535 (*o).m_rc != 0
536}
537
538#[inline]
539pub fn lean_ptr_addr(o: Obj) -> usize {
540 o.addr()
541}
542
543#[inline]
544pub unsafe fn lean_is_ctor(o: Obj) -> bool {
545 lean_ptr_tag(o) <= LEAN_MAX_CTOR_TAG
546}
547#[inline]
548pub unsafe fn lean_is_closure(o: Obj) -> bool {
549 lean_ptr_tag(o) == LEAN_CLOSURE
550}
551#[inline]
552pub unsafe fn lean_is_array(o: Obj) -> bool {
553 lean_ptr_tag(o) == LEAN_ARRAY
554}
555#[inline]
556pub unsafe fn lean_is_sarray(o: Obj) -> bool {
557 lean_ptr_tag(o) == LEAN_SCALAR_ARRAY
558}
559#[inline]
560pub unsafe fn lean_is_string(o: Obj) -> bool {
561 lean_ptr_tag(o) == LEAN_STRING
562}
563#[inline]
564pub unsafe fn lean_is_mpz(o: Obj) -> bool {
565 lean_ptr_tag(o) == LEAN_MPZ
566}
567#[inline]
568pub unsafe fn lean_is_thunk(o: Obj) -> bool {
569 lean_ptr_tag(o) == LEAN_THUNK
570}
571#[inline]
572pub unsafe fn lean_is_task(o: Obj) -> bool {
573 lean_ptr_tag(o) == LEAN_TASK
574}
575#[inline]
576pub unsafe fn lean_is_external(o: Obj) -> bool {
577 lean_ptr_tag(o) == LEAN_EXTERNAL
578}
579#[inline]
580pub unsafe fn lean_is_ref(o: Obj) -> bool {
581 lean_ptr_tag(o) == LEAN_REF
582}
583
584#[inline]
587pub unsafe fn lean_ctor_num_objs(o: Obj) -> u32 {
588 (*o).m_other as u32
589}
590
591#[inline]
592pub unsafe fn lean_ctor_obj_cptr(o: Obj) -> *mut Obj {
593 (o as *mut u8).add(CTOR_OBJS) as *mut Obj
594}
595
596#[inline]
597pub unsafe fn lean_ctor_scalar_cptr(o: Obj) -> *mut u8 {
598 lean_ctor_obj_cptr(o).add(lean_ctor_num_objs(o) as usize) as *mut u8
599}
600
601#[inline]
604pub unsafe fn lean_alloc_ctor(tag: u32, num_objs: u32, scalar_sz: u32) -> Obj {
605 debug_assert!(tag <= LEAN_MAX_CTOR_TAG as u32 && num_objs < 256);
606 let objs_end = CTOR_OBJS + size_of::<Obj>() * num_objs as usize;
607 let sz = (objs_end + scalar_sz as usize + 7) & !7;
608 if sz > u16::MAX as usize {
609 lean_internal_panic("constructor object is too big");
610 }
611 let o = lean_alloc_object(sz);
612 lean_set_st_header(o, tag, num_objs);
613 (*o).m_cs_sz = sz as u16;
614 ptr::write_bytes((o as *mut u8).add(objs_end), 0, sz - objs_end);
615 o
616}
617
618#[inline]
619pub unsafe fn lean_ctor_get(o: Obj, i: u32) -> Obj {
620 debug_check_live(o, "field read");
621 debug_assert!(i < lean_ctor_num_objs(o));
622 *lean_ctor_obj_cptr(o).add(i as usize)
623}
624
625#[inline]
626pub unsafe fn lean_ctor_set(o: Obj, i: u32, v: Obj) {
627 debug_check_live(o, "field write");
628 debug_assert!(i < lean_ctor_num_objs(o));
629 *lean_ctor_obj_cptr(o).add(i as usize) = v;
630}
631
632#[inline]
633pub unsafe fn lean_ctor_set_tag(o: Obj, new_tag: u32) {
634 debug_check_live(o, "tag write");
635 (*o).m_tag = new_tag as u8;
636}
637
638#[inline]
639pub unsafe fn lean_ctor_release(o: Obj, i: u32) {
640 let objs = lean_ctor_obj_cptr(o);
641 lean_dec(*objs.add(i as usize));
642 *objs.add(i as usize) = lean_box(0);
643}
644
645#[inline]
646pub unsafe fn lean_ctor_get_usize(o: Obj, i: u32) -> usize {
647 debug_check_live(o, "field read");
648 ptr::read_unaligned(lean_ctor_obj_cptr(o).add(i as usize) as *const usize)
649}
650
651#[inline]
652pub unsafe fn lean_ctor_set_usize(o: Obj, i: u32, v: usize) {
653 debug_check_live(o, "field write");
654 ptr::write_unaligned(lean_ctor_obj_cptr(o).add(i as usize) as *mut usize, v)
655}
656
657macro_rules! ctor_scalar {
658 ($get:ident, $set:ident, $t:ty) => {
659 #[inline]
661 pub unsafe fn $get(o: Obj, offset: u32) -> $t {
662 debug_check_live(o, "field read");
663 ptr::read_unaligned(
664 (lean_ctor_obj_cptr(o) as *const u8).add(offset as usize) as *const $t
665 )
666 }
667 #[inline]
669 pub unsafe fn $set(o: Obj, offset: u32, v: $t) {
670 debug_check_live(o, "field write");
671 ptr::write_unaligned(
672 (lean_ctor_obj_cptr(o) as *mut u8).add(offset as usize) as *mut $t,
673 v,
674 )
675 }
676 };
677}
678
679ctor_scalar!(lean_ctor_get_uint8, lean_ctor_set_uint8, u8);
680ctor_scalar!(lean_ctor_get_uint16, lean_ctor_set_uint16, u16);
681ctor_scalar!(lean_ctor_get_uint32, lean_ctor_set_uint32, u32);
682ctor_scalar!(lean_ctor_get_uint64, lean_ctor_set_uint64, u64);
683ctor_scalar!(lean_ctor_get_float, lean_ctor_set_float, f64);
684ctor_scalar!(lean_ctor_get_float32, lean_ctor_set_float32, f32);
685
686#[inline]
689pub unsafe fn lean_box_uint32(v: u32) -> Obj {
690 lean_box(v as usize)
691}
692
693#[inline]
694pub unsafe fn lean_unbox_uint32(o: Obj) -> u32 {
695 lean_unbox(o) as u32
696}
697
698#[inline]
699pub unsafe fn lean_box_uint64(v: u64) -> Obj {
700 let r = lean_alloc_ctor(0, 0, 8);
701 lean_ctor_set_uint64(r, 0, v);
702 r
703}
704
705#[inline]
706pub unsafe fn lean_unbox_uint64(o: Obj) -> u64 {
707 lean_ctor_get_uint64(o, 0)
708}
709
710#[inline]
711pub unsafe fn lean_box_usize(v: usize) -> Obj {
712 let r = lean_alloc_ctor(0, 0, size_of::<usize>() as u32);
713 lean_ctor_set_usize(r, 0, v);
714 r
715}
716
717#[inline]
718pub unsafe fn lean_unbox_usize(o: Obj) -> usize {
719 lean_ctor_get_usize(o, 0)
720}
721
722#[inline]
723pub unsafe fn lean_box_float(v: f64) -> Obj {
724 let r = lean_alloc_ctor(0, 0, 8);
725 lean_ctor_set_float(r, 0, v);
726 r
727}
728
729#[inline]
730pub unsafe fn lean_unbox_float(o: Obj) -> f64 {
731 lean_ctor_get_float(o, 0)
732}
733
734#[inline]
735pub unsafe fn lean_box_float32(v: f32) -> Obj {
736 let r = lean_alloc_ctor(0, 0, 4);
737 lean_ctor_set_float32(r, 0, v);
738 r
739}
740
741#[inline]
742pub unsafe fn lean_unbox_float32(o: Obj) -> f32 {
743 lean_ctor_get_float32(o, 0)
744}
745
746#[inline]
749pub unsafe fn lean_alloc_ref(v: Obj) -> Obj {
750 let o = lean_alloc_small_object(size_of::<RefObject>());
751 lean_set_st_header(o, LEAN_REF as u32, 0);
752 (*(o as *mut RefObject)).m_value = v;
753 o
754}
755
756#[inline]
759pub unsafe fn lean_alloc_external(cls: *const ExternalClass, data: *mut c_void) -> Obj {
760 let o = lean_alloc_small_object(size_of::<ExternalObject>());
761 lean_set_st_header(o, LEAN_EXTERNAL as u32, 0);
762 let e = o as *mut ExternalObject;
763 (*e).m_class = cls;
764 (*e).m_data = data;
765 o
766}
767
768#[inline]
769pub unsafe fn lean_get_external_class(o: Obj) -> *const ExternalClass {
770 (*(o as *mut ExternalObject)).m_class
771}
772
773#[inline]
774pub unsafe fn lean_get_external_data(o: Obj) -> *mut c_void {
775 (*(o as *mut ExternalObject)).m_data
776}
777
778pub unsafe fn lean_set_external_data(o: Obj, data: *mut c_void) -> Obj {
779 if lean_is_exclusive(o) {
780 (*(o as *mut ExternalObject)).m_data = data;
781 o
782 } else {
783 let r = lean_alloc_external(lean_get_external_class(o), data);
784 lean_dec_ref(o);
785 r
786 }
787}
788
789#[inline]
792pub unsafe fn lean_closure_arg_cptr(o: Obj) -> *mut Obj {
793 (o as *mut u8).add(offset_of!(ClosureObject, m_objs)) as *mut Obj
794}
795
796#[inline]
797pub unsafe fn lean_array_cptr(o: Obj) -> *mut Obj {
798 (o as *mut u8).add(offset_of!(ArrayObject, m_data)) as *mut Obj
799}
800
801#[inline]
802pub unsafe fn lean_sarray_elem_size(o: Obj) -> u32 {
803 ((*o).m_other & !LEAN_LINEAR_MARK_MASK) as u32
804}
805
806pub use crate::panic::{
807 lean_internal_panic, lean_internal_panic_out_of_memory, lean_internal_panic_rc_overflow,
808 lean_internal_panic_unreachable,
809};
810
811#[inline]
813pub unsafe fn lean_del_object(o: Obj) {
814 if !lean_is_scalar(o) {
815 lean_free_object(o);
816 }
817}