Compare commits

...

2 Commits

Author SHA1 Message Date
Leonardo de Moura
b918bd145c feat: add isExclusiveUnsafe 2024-07-17 12:49:57 -07:00
Leonardo de Moura
09ddc75f29 feat: add lean_set_external_data 2024-07-17 11:39:29 -07:00
2 changed files with 22 additions and 0 deletions

View File

@@ -45,6 +45,13 @@ def dbgSleep {α : Type u} (ms : UInt32) (f : Unit → α) : α := f ()
@[extern "lean_ptr_addr"]
unsafe opaque ptrAddrUnsafe {α : Type u} (a : @& α) : USize
/--
Returns `true` if `a` is an exclusive object.
We say an object is exclusive if it is single-threaded and its reference counter is 1.
-/
@[extern "lean_is_exclusive_obj"]
unsafe opaque isExclusiveUnsafe {α : Type u} (a : @& α) : Bool
set_option linter.unusedVariables.funArgs false in
@[inline] unsafe def withPtrAddrUnsafe {α : Type u} {β : Type v} (a : α) (k : USize β) (h : u₁ u₂, k u₁ = k u₂) : β :=
k (ptrAddrUnsafe a)

View File

@@ -481,6 +481,10 @@ static inline bool lean_is_exclusive(lean_object * o) {
}
}
static inline uint8_t lean_is_exclusive_obj(lean_object * o) {
return lean_is_exclusive(o);
}
static inline bool lean_is_shared(lean_object * o) {
if (LEAN_LIKELY(lean_is_st(o))) {
return o->m_rc > 1;
@@ -1133,6 +1137,17 @@ static inline void * lean_get_external_data(lean_object * o) {
return lean_to_external(o)->m_data;
}
static inline lean_object * lean_set_external_data(lean_object * o, void * data) {
if (lean_is_exclusive(o)) {
lean_to_external(o)->m_data = data;
return o;
} else {
lean_object * o_new = lean_alloc_external(lean_get_external_class(o), data);
lean_dec_ref(o);
return o_new;
}
}
/* Natural numbers */
#define LEAN_MAX_SMALL_NAT (SIZE_MAX >> 1)