Commit 38ef8808 authored by Simon Morlat's avatar Simon Morlat

fix bug in pool management

parent d375955f
......@@ -312,8 +312,12 @@ typedef struct belle_sip_interface_desc{
**/
typedef struct belle_sip_object_pool belle_sip_object_pool_t;
/**
* Push a new object pool for use for creation of new objects.
* When no longer needed, this pool can be destroyed with belle_sip_object_unref().
**/
BELLESIP_EXPORT belle_sip_object_pool_t * belle_sip_object_pool_push(void);
BELLESIP_EXPORT void belle_sip_object_pool_pop(void);
belle_sip_object_pool_t * belle_sip_object_pool_get_current();
int belle_sip_object_pool_cleanable(belle_sip_object_pool_t *pool);
void belle_sip_object_pool_clean(belle_sip_object_pool_t *obj);
......
......@@ -251,7 +251,7 @@ static void belle_sip_main_loop_destroy(belle_sip_main_loop_t *ml){
while (ml->sources){
belle_sip_main_loop_remove_source(ml,(belle_sip_source_t*)ml->sources->data);
}
belle_sip_object_pool_pop();
belle_sip_object_unref(ml->pool);
}
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_main_loop_t);
......@@ -344,10 +344,11 @@ void belle_sip_main_loop_iterate(belle_sip_main_loop_t *ml){
uint64_t cur;
belle_sip_list_t *copy;
int can_clean=belle_sip_object_pool_cleanable(ml->pool); /*iterate might not be called by the thread that created the main loop*/
belle_sip_object_pool_t *tmp_pool=NULL;
if (!can_clean){
/*Push a temporary pool for the time of the iterate loop*/
belle_sip_object_pool_push();
tmp_pool=belle_sip_object_pool_push();
}
/*prepare the pollfd table */
......@@ -419,8 +420,8 @@ void belle_sip_main_loop_iterate(belle_sip_main_loop_t *ml){
}else belle_sip_main_loop_remove_source(ml,s);
}
belle_sip_list_free_with_data(copy,belle_sip_object_unref);
if (belle_sip_object_pool_cleanable(ml->pool)) belle_sip_object_pool_clean(ml->pool);
else belle_sip_object_pool_pop();
if (can_clean) belle_sip_object_pool_clean(ml->pool);
else if (tmp_pool) belle_sip_object_unref(tmp_pool);
}
void belle_sip_main_loop_run(belle_sip_main_loop_t *ml){
......
......@@ -18,6 +18,7 @@
#include "belle_sip_internal.h"
static void _belle_sip_object_pool_remove_from_stack(belle_sip_object_pool_t *pool);
static int has_type(belle_sip_object_t *obj, belle_sip_type_id_t id){
belle_sip_object_vptr_t *vptr=obj->vptr;
......@@ -356,6 +357,7 @@ struct belle_sip_object_pool{
static void belle_sip_object_pool_destroy(belle_sip_object_pool_t *pool){
belle_sip_object_pool_clean(pool);
_belle_sip_object_pool_remove_from_stack(pool);
}
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_object_pool_t);
......@@ -451,6 +453,26 @@ static belle_sip_list_t** get_current_pool_stack(int *first_time){
return pool_stack;
}
static void _belle_sip_object_pool_remove_from_stack(belle_sip_object_pool_t *pool){
belle_sip_list_t **pools=get_current_pool_stack(NULL);
belle_sip_thread_t tid=belle_sip_thread_self();
if (tid!=pool->thread_id){
belle_sip_fatal("It is forbidden to destroy a pool outside the thread that created it.");
return;
}
if (pools==NULL) {
belle_sip_fatal("Not possible to pop a pool.");
return;
}
if (*pools==NULL){
belle_sip_fatal("There is no current pool in stack.");
return;
}
*pools=belle_sip_list_remove(*pools,pool);
}
belle_sip_object_pool_t * belle_sip_object_pool_push(void){
belle_sip_list_t **pools=get_current_pool_stack(NULL);
belle_sip_object_pool_t *pool;
......@@ -463,21 +485,7 @@ belle_sip_object_pool_t * belle_sip_object_pool_push(void){
return pool;
}
void belle_sip_object_pool_pop(void){
belle_sip_list_t **pools=get_current_pool_stack(NULL);
belle_sip_object_pool_t *pool;
if (pools==NULL) {
belle_sip_error("Not possible to pop a pool.");
return;
}
if (*pools==NULL){
belle_sip_error("There is no current pool in stack.");
return;
}
pool=(belle_sip_object_pool_t*)(*pools)->data;
*pools=belle_sip_list_remove_link(*pools,*pools);
belle_sip_object_unref(pool);
}
belle_sip_object_pool_t *belle_sip_object_pool_get_current(void){
int first_time;
......
......@@ -121,7 +121,8 @@ void belle_sip_tester_uninit(void) {
}
int belle_sip_tester_run_tests(const char *suite_name, const char *test_name) {
int i;
belle_sip_object_pool_t *pool;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
......@@ -129,7 +130,7 @@ int belle_sip_tester_run_tests(const char *suite_name, const char *test_name) {
for (i = 0; i < belle_sip_tester_nb_test_suites(); i++) {
run_test_suite(test_suite[i]);
}
belle_sip_object_pool_push();
pool=belle_sip_object_pool_push();
#if HAVE_CU_GET_SUITE
if (suite_name){
......@@ -158,7 +159,7 @@ int belle_sip_tester_run_tests(const char *suite_name, const char *test_name) {
}
}
belle_sip_object_pool_pop();
belle_sip_object_unref(pool);
CU_cleanup_registry();
return CU_get_error();
......
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