Commit f1885e08 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added new unref method that returns TRUE if object has been destroyed

parent 2aafb7f0
...@@ -236,6 +236,10 @@ BELLESIP_EXPORT belle_sip_object_t * belle_sip_object_ref(void *obj); ...@@ -236,6 +236,10 @@ BELLESIP_EXPORT belle_sip_object_t * belle_sip_object_ref(void *obj);
**/ **/
BELLESIP_EXPORT void belle_sip_object_unref(void *obj); BELLESIP_EXPORT void belle_sip_object_unref(void *obj);
/**
* Decrements the reference counter. When it drops to zero, the object is destroyed and returns TRUE. Otherwise returns FALSE.
**/
BELLESIP_EXPORT bool_t belle_sip_object_unref_2(void *obj);
typedef void (*belle_sip_object_destroy_notify_t)(void *userpointer, belle_sip_object_t *obj_being_destroyed); typedef void (*belle_sip_object_destroy_notify_t)(void *userpointer, belle_sip_object_t *obj_being_destroyed);
/** /**
......
...@@ -123,14 +123,18 @@ belle_sip_object_t * belle_sip_object_ref(void *obj){ ...@@ -123,14 +123,18 @@ belle_sip_object_t * belle_sip_object_ref(void *obj){
return obj; return obj;
} }
void belle_sip_object_unref(void *ptr){ void belle_sip_object_unref(void *ptr) {
belle_sip_object_unref_2(ptr);
}
bool_t belle_sip_object_unref_2(void *ptr) {
belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr); belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
if (obj->ref <= -1) { if (obj->ref <= -1) {
belle_sip_error("Object [%p] freed twice or corrupted !",obj); belle_sip_error("Object [%p] freed twice or corrupted !",obj);
if (obj->vptr && obj->vptr->type_name) belle_sip_error("Object type might be [%s]",obj->vptr->type_name); if (obj->vptr && obj->vptr->type_name) belle_sip_error("Object type might be [%s]",obj->vptr->type_name);
if (obj->name) belle_sip_error("Object name might be [%s]",obj->name); if (obj->name) belle_sip_error("Object name might be [%s]",obj->name);
belle_sip_fatal("Fatal object error encountered, aborting."); belle_sip_fatal("Fatal object error encountered, aborting.");
return; return TRUE;
} }
if (obj->vptr->initially_unowned && obj->ref==0){ if (obj->vptr->initially_unowned && obj->ref==0){
...@@ -138,7 +142,7 @@ void belle_sip_object_unref(void *ptr){ ...@@ -138,7 +142,7 @@ void belle_sip_object_unref(void *ptr){
belle_sip_object_pool_remove(obj->pool,obj); belle_sip_object_pool_remove(obj->pool,obj);
obj->ref=-1; obj->ref=-1;
belle_sip_object_delete(obj); belle_sip_object_delete(obj);
return; return TRUE;
} }
...@@ -155,7 +159,9 @@ void belle_sip_object_unref(void *ptr){ ...@@ -155,7 +159,9 @@ void belle_sip_object_unref(void *ptr){
if (obj->ref == 0){ if (obj->ref == 0){
obj->ref = -1; obj->ref = -1;
belle_sip_object_delete(obj); belle_sip_object_delete(obj);
return TRUE;
} }
return FALSE;
} }
static weak_ref_t *weak_ref_new(belle_sip_object_destroy_notify_t destroy_notify, void *userpointer){ static weak_ref_t *weak_ref_new(belle_sip_object_destroy_notify_t destroy_notify, void *userpointer){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment