|
|
@ -7,13 +7,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
#define RC_CHK 0xabadcafe
|
|
|
|
#define RC_CHK 0xabadcafe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct rc {
|
|
|
|
struct rc {
|
|
|
|
uint32_t check;
|
|
|
|
uint32_t check;
|
|
|
|
_Atomic size_t refs;
|
|
|
|
_Atomic size_t refs;
|
|
|
|
|
|
|
|
_Atomic size_t weak_refs;
|
|
|
|
|
|
|
|
//TODO: Keep all weak pointers here. rc_weak_free() should remove its own pointer address from that.
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t ptr[];
|
|
|
|
uint8_t ptr[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct rc_weak {
|
|
|
|
|
|
|
|
_Atomic int is_valid;
|
|
|
|
|
|
|
|
const struct rc* _rc_attr((may_alias)) _ptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((noreturn)) static void panic(const char* string, ...)
|
|
|
|
_rc_attr((noreturn)) static void panic(const char* string, ...)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_list args;
|
|
|
@ -34,14 +42,26 @@ _rc_attr((noinline, noreturn, cold)) static void _fail_invptr()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
panic("malloc() failed to allocate %d bytes (%d + sizeof(struct rc))", sz+sizeof(struct rc), sz);
|
|
|
|
panic("malloc() failed to allocate %d bytes (%d + sizeof(struct rc))", sz+sizeof(struct rc), sz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_rc_attr((artificial, const, always_inline)) inline static const struct rc* ptr_crev(const void* ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!ptr) _fail_invptr();
|
|
|
|
|
|
|
|
return (const struct rc*)(((const uint8_t*)ptr) - sizeof(struct rc));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((artificial, always_inline)) inline static rc_t ptr_rev(void* ptr)
|
|
|
|
_rc_attr((artificial, const, always_inline)) inline static rc_t ptr_rev(void* ptr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!ptr) _fail_invptr();
|
|
|
|
if (!ptr) _fail_invptr();
|
|
|
|
return (rc_t)(((uint8_t*)ptr) - sizeof(struct rc));
|
|
|
|
return (rc_t)(((uint8_t*)ptr) - sizeof(struct rc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((nonnull)) inline static void rc_checkptr(const rc_t rc)
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((artificial, const, always_inline)) inline static rc_t ptr_ucrev(const void* ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!ptr) _fail_invptr();
|
|
|
|
|
|
|
|
return (rc_t)(((uintptr_t)ptr) - sizeof(struct rc));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((nonnull)) inline static void rc_checkptr(const struct rc* rc)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (rc->check != RC_CHK) _fail_invptr();
|
|
|
|
if (rc->check != RC_CHK) _fail_invptr();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -54,21 +74,58 @@ _rc_attr((nonnull)) inline static void rc_checkptr(const rc_t rc)
|
|
|
|
|
|
|
|
|
|
|
|
rc->check = RC_CHK;
|
|
|
|
rc->check = RC_CHK;
|
|
|
|
rc->refs = 1;
|
|
|
|
rc->refs = 1;
|
|
|
|
|
|
|
|
rc->weak_refs =0;
|
|
|
|
|
|
|
|
|
|
|
|
return (void*) rc->ptr;
|
|
|
|
return (void*) rc->ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((nonnull)) void rc_free(void* ptr)
|
|
|
|
_rc_gmut_fn void rc_free(rc_ptr_t ptr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
register rc_t rc = ptr_rev(ptr);
|
|
|
|
register rc_t rc = ptr_rev(ptr);
|
|
|
|
rc_checkptr(rc);
|
|
|
|
rc_checkptr(rc);
|
|
|
|
if (!--rc->refs) free(rc);
|
|
|
|
if (!--rc->refs) free(rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_rc_attr((pure, nonnull, returns_nonnull)) void* _rc_attr((may_alias)) rc_clone(void* ptr)
|
|
|
|
_rc_mut_fn _rc_attr((returns_nonnull)) rc_ptr_t rc_clone(rc_const_ptr_t ptr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
register rc_t rc = ptr_rev(ptr);
|
|
|
|
register rc_t rc = ptr_ucrev(ptr);
|
|
|
|
rc_checkptr(rc);
|
|
|
|
rc_checkptr(rc);
|
|
|
|
++rc->refs;
|
|
|
|
++rc->refs;
|
|
|
|
return ptr;
|
|
|
|
return rc->ptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rc_fn size_t rc_refs(rc_const_ptr_t ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
register const struct rc* rc = ptr_crev(ptr);
|
|
|
|
|
|
|
|
rc_checkptr(rc);
|
|
|
|
|
|
|
|
return rc->refs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rc_fn rc_bool rc_is_valid(rc_const_ptr_t ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
register const struct rc* rc = ptr_crev(ptr);
|
|
|
|
|
|
|
|
return rc->check == RC_CHK
|
|
|
|
|
|
|
|
&& rc->refs>0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rc_alloc_fn _rc_gmut_fn rc_weak_t* _rc_restrict rc_create_weak(rc_const_ptr_t ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
register rc_t rc = ptr_ucrev(ptr);
|
|
|
|
|
|
|
|
rc_weak_t* weak = calloc(sizeof(rc_weak_t), 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
weak->_ptr = rc;
|
|
|
|
|
|
|
|
weak->is_valid = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rc->weak_refs++;
|
|
|
|
|
|
|
|
return weak;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_rc_gmut_fn void rc_weak_free(rc_weak_t* _rc_restrict ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ptr->is_valid) {
|
|
|
|
|
|
|
|
struct rc* _rc_attr((may_alias)) rc = (struct rc* _rc_attr((may_alias)))ptr->_ptr;
|
|
|
|
|
|
|
|
rc_checkptr(rc);
|
|
|
|
|
|
|
|
rc->weak_refs--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|