From 6d9af5fa543c563e855a04d63f2ea18e62c4f4d5 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 8 Sep 2015 14:53:40 +0200 Subject: [PATCH] Fix ref leak + renamed some functions + improved MSList handling --- coreapi/call_log.c | 59 +++++++++++++++++++----------------------- coreapi/linphonecall.c | 2 +- coreapi/linphonecore.c | 6 ++--- coreapi/linphonecore.h | 2 +- coreapi/private.h | 10 +++---- 5 files changed, 36 insertions(+), 43 deletions(-) diff --git a/coreapi/call_log.c b/coreapi/call_log.c index 2cd94d9d8..d40260166 100644 --- a/coreapi/call_log.c +++ b/coreapi/call_log.c @@ -274,7 +274,7 @@ void linphone_call_log_unref(LinphoneCallLog *cl) { * Constructor and destructor functions * ******************************************************************************/ -static void _linphone_call_log_destroy(LinphoneCallLog *cl){ +static void _linphone_call_log_destroy(LinphoneCallLog *cl) { if (cl->from!=NULL) linphone_address_destroy(cl->from); if (cl->to!=NULL) linphone_address_destroy(cl->to); if (cl->refkey!=NULL) ms_free(cl->refkey); @@ -283,7 +283,7 @@ static void _linphone_call_log_destroy(LinphoneCallLog *cl){ if (cl->reporting.reports[LINPHONE_CALL_STATS_VIDEO]!=NULL) linphone_reporting_destroy(cl->reporting.reports[LINPHONE_CALL_STATS_VIDEO]); } -LinphoneCallLog * linphone_call_log_new(LinphoneCallDir dir, LinphoneAddress *from, LinphoneAddress *to){ +LinphoneCallLog * linphone_call_log_new(LinphoneCallDir dir, LinphoneAddress *from, LinphoneAddress *to) { LinphoneCallLog *cl=belle_sip_object_new(LinphoneCallLog); cl->dir=dir; cl->start_date_time=time(NULL); @@ -321,7 +321,7 @@ BELLE_SIP_INSTANCIATE_VPTR(LinphoneCallLog, belle_sip_object_t, #ifdef CALL_LOGS_STORAGE_ENABLED -static void linphone_create_table(sqlite3* db){ +static void linphone_create_table(sqlite3* db) { char* errmsg=NULL; int ret; ret=sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS call_history (" @@ -390,7 +390,7 @@ void linphone_core_call_log_storage_init(LinphoneCore *lc) { lc->logs_db = db; // Load the existing call logs - linphone_call_log_get_history(lc); + linphone_core_get_call_history(lc); } void linphone_core_call_log_storage_close(LinphoneCore *lc) { @@ -413,7 +413,7 @@ void linphone_core_call_log_storage_close(LinphoneCore *lc) { * | 9 | quality */ static int create_call_log(void *data, int argc, char **argv, char **colName) { - LinphoneCore *lc = (LinphoneCore *)data; + MSList **list = (MSList **)data; LinphoneAddress *from; LinphoneAddress *to; LinphoneCallDir dir; @@ -433,22 +433,22 @@ static int create_call_log(void *data, int argc, char **argv, char **colName) { log->video_enabled = atoi(argv[8]) == 1; log->quality = atof(argv[9]); - lc->call_logs = ms_list_prepend(lc->call_logs, linphone_call_log_ref(log)); + *list = ms_list_prepend(*list, log); return 0; } -void linphone_sql_request_call_log(sqlite3 *db, const char *stmt, LinphoneCore *lc){ +void linphone_sql_request_call_log(sqlite3 *db, const char *stmt, MSList **list) { char* errmsg = NULL; int ret; - ret = sqlite3_exec(db, stmt, create_call_log, lc, &errmsg); + ret = sqlite3_exec(db, stmt, create_call_log, list, &errmsg); if (ret != SQLITE_OK) { ms_error("Error in creation: %s.", errmsg); sqlite3_free(errmsg); } } -int linphone_sql_request_generic(sqlite3* db, const char *stmt){ +int linphone_sql_request_generic(sqlite3* db, const char *stmt) { char* errmsg = NULL; int ret; ret = sqlite3_exec(db, stmt, NULL, NULL, &errmsg); @@ -459,7 +459,7 @@ int linphone_sql_request_generic(sqlite3* db, const char *stmt){ return ret; } -void linphone_call_log_store(LinphoneCore *lc, LinphoneCallLog *log) { +void linphone_core_store_call_log(LinphoneCore *lc, LinphoneCallLog *log) { if (lc && lc->logs_db){ char *from, *to; char *buf; @@ -489,19 +489,18 @@ void linphone_call_log_store(LinphoneCore *lc, LinphoneCallLog *log) { } } -MSList *linphone_call_log_get_history(LinphoneCore *lc) { +const MSList *linphone_core_get_call_history(LinphoneCore *lc) { char *buf; uint64_t begin,end; if (!lc || lc->logs_db == NULL) return NULL; - ms_list_for_each(lc->call_logs, (void (*)(void*))linphone_call_log_unref); - lc->call_logs = ms_list_free(lc->call_logs); + lc->call_logs = ms_list_free_with_data(lc->call_logs, (void (*)(void*))linphone_call_log_unref); buf = sqlite3_mprintf("SELECT * FROM call_history ORDER BY id ASC LIMIT %i", lc->max_call_logs); begin = ortp_get_cur_time_ms(); - linphone_sql_request_call_log(lc->logs_db, buf, lc); + linphone_sql_request_call_log(lc->logs_db, buf, &lc->call_logs); end = ortp_get_cur_time_ms(); ms_message("%s(): completed in %i ms",__FUNCTION__, (int)(end-begin)); sqlite3_free(buf); @@ -509,7 +508,7 @@ MSList *linphone_call_log_get_history(LinphoneCore *lc) { return lc->call_logs; } -void linphone_call_log_delete_history(LinphoneCore *lc) { +void linphone_core_delete_call_history(LinphoneCore *lc) { char *buf; if (!lc || lc->logs_db == NULL) return ; @@ -519,7 +518,7 @@ void linphone_call_log_delete_history(LinphoneCore *lc) { sqlite3_free(buf); } -void linphone_call_log_delete_log(LinphoneCore *lc, LinphoneCallLog *log) { +void linphone_core_delete_call_log(LinphoneCore *lc, LinphoneCallLog *log) { char *buf; if (!lc || lc->logs_db == NULL) return ; @@ -532,7 +531,7 @@ void linphone_call_log_delete_log(LinphoneCore *lc, LinphoneCallLog *log) { linphone_call_log_unref(log); } -int linphone_call_log_get_history_size(LinphoneCore *lc) { +int linphone_core_get_call_history_size(LinphoneCore *lc) { int numrows = 0; char *buf; sqlite3_stmt *selectStatement; @@ -553,60 +552,54 @@ int linphone_call_log_get_history_size(LinphoneCore *lc) { return numrows; } -const MSList * linphone_core_get_call_logs_for_address(LinphoneCore *lc, LinphoneAddress *addr) { +const MSList * linphone_core_get_call_history_for_address(LinphoneCore *lc, LinphoneAddress *addr) { char *buf; char *sipAddress; uint64_t begin,end; - MSList *result, *tmp; + MSList *result = NULL; if (!lc || lc->logs_db == NULL || addr == NULL) return NULL; - tmp = lc->call_logs; - lc->call_logs = NULL; - /*since we want to append query parameters depending on arguments given, we use malloc instead of sqlite3_mprintf*/ sipAddress = linphone_address_as_string_uri_only(addr); buf = sqlite3_mprintf("SELECT * FROM call_history WHERE caller LIKE '%%%q%%' OR callee LIKE '%%%q%%' ORDER BY id ASC", sipAddress, sipAddress); // The '%%%q%%' takes care of the eventual presence of a display name begin = ortp_get_cur_time_ms(); - linphone_sql_request_call_log(lc->logs_db, buf, lc); + linphone_sql_request_call_log(lc->logs_db, buf, &result); end = ortp_get_cur_time_ms(); ms_message("%s(): completed in %i ms",__FUNCTION__, (int)(end-begin)); sqlite3_free(buf); ms_free(sipAddress); - result = lc->call_logs; - lc->call_logs = tmp; - tmp = NULL; return result; } #else -void linphone_core_call_log_storage_init(LinphoneCore *lc) { +void linphone_core_call_log_storage_init(LinphoneCore *lc) { } void linphone_core_call_log_storage_close(LinphoneCore *lc) { } -void linphone_call_log_store(LinphoneCore *lc, LinphoneCallLog *log) { +void linphone_core_store_call_log(LinphoneCore *lc, LinphoneCallLog *log) { } -MSList *linphone_call_log_get_history(LinphoneCore *lc) { +const MSList *linphone_core_get_call_history(LinphoneCore *lc) { return NULL; } -void linphone_call_log_delete_history(LinphoneCore *lc) { +void linphone_core_delete_call_history(LinphoneCore *lc) { } -void linphone_call_log_delete_log(LinphoneCore *lc, LinphoneCallLog *log) { +void linphone_core_delete_call_log(LinphoneCore *lc, LinphoneCallLog *log) { } -int linphone_call_log_get_history_size(LinphoneCore *lc) { +int linphone_core_get_call_history_size(LinphoneCore *lc) { return 0; } -const MSList * linphone_core_get_call_logs_for_address(LinphoneCore *lc, LinphoneAddress *addr) { +const MSList * linphone_core_get_call_history_for_address(LinphoneCore *lc, LinphoneAddress *addr) { return NULL; } diff --git a/coreapi/linphonecall.c b/coreapi/linphonecall.c index 56164bf96..c8d3d458f 100644 --- a/coreapi/linphonecall.c +++ b/coreapi/linphonecall.c @@ -3946,7 +3946,7 @@ void linphone_call_log_completed(LinphoneCall *call){ ms_free(info); } #ifdef CALL_LOGS_STORAGE_ENABLED - linphone_call_log_store(lc, call->log); + linphone_core_store_call_log(lc, call->log); #else lc->call_logs=ms_list_prepend(lc->call_logs,linphone_call_log_ref(call->log)); if (ms_list_size(lc->call_logs)>lc->max_call_logs){ diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index f94358d6d..89634e511 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -4945,7 +4945,7 @@ void linphone_core_set_call_logs_database_path(LinphoneCore *lc, const char *pat const MSList* linphone_core_get_call_logs(LinphoneCore *lc) { #ifdef CALL_LOGS_STORAGE_ENABLED - linphone_call_log_get_history(lc); + linphone_core_get_call_history(lc); #endif return lc->call_logs; } @@ -4953,7 +4953,7 @@ const MSList* linphone_core_get_call_logs(LinphoneCore *lc) { void linphone_core_clear_call_logs(LinphoneCore *lc) { lc->missed_calls=0; #ifdef CALL_LOGS_STORAGE_ENABLED - linphone_call_log_delete_history(lc); + linphone_core_delete_call_history(lc); #else ms_list_for_each(lc->call_logs, (void (*)(void*))linphone_call_log_unref); lc->call_logs = ms_list_free(lc->call_logs); @@ -4971,7 +4971,7 @@ void linphone_core_reset_missed_calls_count(LinphoneCore *lc) { void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCallLog *cl){ #ifdef CALL_LOGS_STORAGE_ENABLED - linphone_call_log_delete_log(lc, cl); + linphone_core_delete_call_log(lc, cl); #else lc->call_logs = ms_list_remove(lc->call_logs, cl); call_logs_write_to_config_file(lc); diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 6e3f84cd5..9a0e215bb 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -3176,7 +3176,7 @@ LINPHONE_PUBLIC const MSList * linphone_core_get_call_logs(LinphoneCore *lc); * @param[in] lc LinphoneCore object * @return \mslist{LinphoneCallLog} **/ -LINPHONE_PUBLIC const MSList * linphone_core_get_call_logs_for_address(LinphoneCore *lc, LinphoneAddress *addr); +LINPHONE_PUBLIC const MSList * linphone_core_get_call_history_for_address(LinphoneCore *lc, LinphoneAddress *addr); /** * Erase the call log. diff --git a/coreapi/private.h b/coreapi/private.h index 045606e9f..55d7402f8 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -969,11 +969,11 @@ void call_logs_read_from_config_file(LinphoneCore *lc); void call_logs_write_to_config_file(LinphoneCore *lc); void linphone_core_call_log_storage_init(LinphoneCore *lc); void linphone_core_call_log_storage_close(LinphoneCore *lc); -void linphone_call_log_store(LinphoneCore *lc, LinphoneCallLog *log); -MSList *linphone_call_log_get_history(LinphoneCore *lc); -void linphone_call_log_delete_history(LinphoneCore *lc); -void linphone_call_log_delete_log(LinphoneCore *lc, LinphoneCallLog *log); -int linphone_call_log_get_history_size(LinphoneCore *lc); +void linphone_core_store_call_log(LinphoneCore *lc, LinphoneCallLog *log); +const MSList *linphone_core_get_call_history(LinphoneCore *lc); +void linphone_core_delete_call_history(LinphoneCore *lc); +void linphone_core_delete_call_log(LinphoneCore *lc, LinphoneCallLog *log); +int linphone_core_get_call_history_size(LinphoneCore *lc); int linphone_core_get_edge_bw(LinphoneCore *lc); int linphone_core_get_edge_ptime(LinphoneCore *lc);