Commit 0c6ff18a authored by Simon Morlat's avatar Simon Morlat

add new on_first_ref() and on_last_ref() virtual methods to belle_sip_object_t .

The are going to be used to implement a kind of floating ref for liblinphone C objects.
parent c09442f1
......@@ -84,19 +84,23 @@ typedef unsigned int belle_sip_type_id_t;
(belle_sip_object_get_vptr_t)BELLE_SIP_OBJECT_GET_VPTR_FUNC(parent_type), \
(belle_sip_interface_desc_t**)object_type##interfaces_table
#define BELLE_SIP_INSTANCIATE_VPTR2(object_type,parent_type,destroy,clone,marshal,on_first_ref,on_last_ref,unowned) \
static BELLE_SIP_OBJECT_VPTR_TYPE(object_type) BELLE_SIP_OBJECT_VPTR_NAME(object_type)={ \
BELLE_SIP_VPTR_INIT(object_type,parent_type,unowned), \
(belle_sip_object_destroy_t)destroy, \
(belle_sip_object_clone_t)clone, \
(belle_sip_object_marshal_t)marshal,\
(belle_sip_object_on_first_ref_t)on_first_ref,\
(belle_sip_object_on_last_ref_t)on_last_ref,\
BELLE_SIP_DEFAULT_BUFSIZE_HINT\
}; \
BELLE_SIP_OBJECT_VPTR_TYPE(object_type) * BELLE_SIP_OBJECT_GET_VPTR_FUNC(object_type)(void){\
return &BELLE_SIP_OBJECT_VPTR_NAME(object_type); \
}
#define BELLE_SIP_INSTANCIATE_VPTR(object_type,parent_type,destroy,clone,marshal,unowned) \
static BELLE_SIP_OBJECT_VPTR_TYPE(object_type) BELLE_SIP_OBJECT_VPTR_NAME(object_type)={ \
BELLE_SIP_VPTR_INIT(object_type,parent_type,unowned), \
(belle_sip_object_destroy_t)destroy, \
(belle_sip_object_clone_t)clone, \
(belle_sip_object_marshal_t)marshal,\
BELLE_SIP_DEFAULT_BUFSIZE_HINT\
}; \
BELLE_SIP_OBJECT_VPTR_TYPE(object_type) * BELLE_SIP_OBJECT_GET_VPTR_FUNC(object_type)(void){\
return &BELLE_SIP_OBJECT_VPTR_NAME(object_type); \
}
#define BELLE_SIP_INSTANCIATE_VPTR(object_type,parent_type,destroy,clone,marshal,unowned) \
BELLE_SIP_INSTANCIATE_VPTR2(object_type, parent_type, destroy, clone, marshal, NULL, NULL, unowned)
/**
* belle_sip_object_t is the base object.
* It is the base class for all belle sip non trivial objects.
......@@ -143,6 +147,8 @@ typedef void (*belle_sip_object_destroy_t)(belle_sip_object_t*);
typedef void (*belle_sip_object_clone_t)(belle_sip_object_t* obj, const belle_sip_object_t *orig);
typedef belle_sip_error_code (*belle_sip_object_marshal_t)(belle_sip_object_t* obj, char* buff, size_t buff_size, size_t *offset);
typedef struct _belle_sip_object_vptr *(*belle_sip_object_get_vptr_t)(void);
typedef void (*belle_sip_object_on_first_ref_t)(belle_sip_object_t*);
typedef void (*belle_sip_object_on_last_ref_t)(belle_sip_object_t*);
struct _belle_sip_object_vptr{
belle_sip_type_id_t id;
......@@ -154,6 +160,8 @@ struct _belle_sip_object_vptr{
belle_sip_object_destroy_t destroy;
belle_sip_object_clone_t clone;
belle_sip_object_marshal_t marshal;
belle_sip_object_on_first_ref_t on_first_ref; /*called when object is ref'd for the first time*/
belle_sip_object_on_last_ref_t on_last_ref; /*called in unref() when the last reference of the object remains*/
int tostring_bufsize_hint; /*optimization: you can suggest here the typical size for a to_string() result.*/
};
......
......@@ -113,6 +113,11 @@ belle_sip_object_t * belle_sip_object_ref(void *obj){
if (o->ref==0 && o->pool){
belle_sip_object_pool_remove(o->pool,obj);
}
if (o->vptr->on_first_ref){
if (o->ref == 0 || (o->vptr->initially_unowned && o->ref == 1)){
o->vptr->on_first_ref(o);
}
}
o->ref++;
return obj;
}
......@@ -126,6 +131,13 @@ void belle_sip_object_unref(void *ptr){
belle_sip_fatal("Fatal object error encountered, aborting.");
return;
}
if (obj->vptr->on_last_ref){
if ((obj->vptr->initially_unowned && obj->ref==0)
|| (!obj->vptr->initially_unowned && obj->ref == 1)){
obj->vptr->on_last_ref(obj);
}
}
if (obj->vptr->initially_unowned && obj->ref==0){
if (obj->pool)
belle_sip_object_pool_remove(obj->pool,obj);
......
......@@ -75,6 +75,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_body_handler_t)
(belle_sip_object_destroy_t) belle_sip_body_handler_destroy,
(belle_sip_object_clone_t) belle_sip_body_handler_clone,
(belle_sip_object_marshal_t) belle_sip_body_handler_marshal,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
NULL, /* begin_recv_transfer */
......
......@@ -152,6 +152,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_channel_t)
(belle_sip_object_destroy_t)belle_sip_channel_destroy,
NULL, /*clone*/
NULL, /*marshal*/
NULL, /*on_first_ref*/
NULL, /*on_last_ref*/
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
NULL, /* transport */
......
......@@ -55,13 +55,9 @@ static void belle_sip_dialog_uninit(belle_sip_dialog_t *obj){
}
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_dialog_t);
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_dialog_t)
BELLE_SIP_VPTR_INIT(belle_sip_dialog_t, belle_sip_object_t,TRUE),
(belle_sip_object_destroy_t)belle_sip_dialog_uninit,
NULL,
NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_dialog_t, belle_sip_object_t, belle_sip_dialog_uninit, NULL, NULL, TRUE);
const char* belle_sip_dialog_state_to_string(const belle_sip_dialog_state_t state) {
switch(state) {
......
......@@ -210,6 +210,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_ict_t)
(belle_sip_object_destroy_t)ict_destroy,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
(void (*)(belle_sip_transaction_t*))on_ict_terminate
......
......@@ -174,6 +174,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_ist_t)
(belle_sip_object_destroy_t)ist_destroy,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
(void (*)(belle_sip_transaction_t *))ist_on_terminate
......
......@@ -106,6 +106,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_listening_point_t)
(belle_sip_object_destroy_t)belle_sip_listening_point_uninit,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
NULL,
......
......@@ -321,6 +321,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_server_transaction_t)
(belle_sip_object_destroy_t) server_transaction_destroy,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
NULL
......@@ -633,6 +635,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_client_transaction_t)
(belle_sip_object_destroy_t)client_transaction_destroy,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
NULL
......
......@@ -162,6 +162,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_stream_channel_t)
(belle_sip_object_destroy_t)stream_channel_uninit,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
"TCP",
......
......@@ -454,6 +454,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_tls_channel_t)
(belle_sip_object_destroy_t)tls_channel_uninit,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
"TLS",
......
......@@ -132,7 +132,10 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_tunnel_channel_t)
BELLE_SIP_VPTR_INIT(belle_sip_tunnel_channel_t,belle_sip_channel_t,FALSE),
(belle_sip_object_destroy_t)tunnel_channel_uninit,
NULL,
NULL
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
0
},
"UDP",
0, /*is_reliable*/
......
......@@ -38,7 +38,10 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_tunnel_listening_point_t)
BELLE_SIP_VPTR_INIT(belle_sip_tunnel_listening_point_t, belle_sip_listening_point_t,TRUE),
NULL,
NULL,
NULL
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
0
},
"UDP",
tunnel_create_channel
......
......@@ -95,6 +95,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_udp_channel_t)
(belle_sip_object_destroy_t)udp_channel_uninit,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
"UDP",
......
......@@ -52,6 +52,8 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_udp_listening_point_t)
(belle_sip_object_destroy_t)belle_sip_udp_listening_point_uninit,
NULL,
NULL,
(belle_sip_object_on_first_ref_t) NULL,
(belle_sip_object_on_last_ref_t) NULL,
BELLE_SIP_DEFAULT_BUFSIZE_HINT
},
"UDP",
......
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