comparison libpurple/dnssrv.c @ 32202:4739488955a7

dnssrv: Avoid a few extraneous iterations through the list. Refs #12647
author Paul Aurich <paul@darkrain42.org>
date Sun, 17 Jul 2011 17:23:17 +0000
parents a40d1cb09beb
children 451242c455ca
comparison
equal deleted inserted replaced
32201:a40d1cb09beb 32202:4739488955a7
1016 * Only used as the callback for the ui ops. 1016 * Only used as the callback for the ui ops.
1017 */ 1017 */
1018 static void 1018 static void
1019 purple_srv_query_resolved(PurpleSrvTxtQueryData *query_data, GList *records) 1019 purple_srv_query_resolved(PurpleSrvTxtQueryData *query_data, GList *records)
1020 { 1020 {
1021 GList *sorted_records, *l; 1021 GList *l;
1022 PurpleSrvResponse *records_array; 1022 PurpleSrvResponse *records_array;
1023 int i; 1023 int i = 0, length;
1024 1024
1025 g_return_if_fail(records != NULL); 1025 g_return_if_fail(records != NULL);
1026 1026
1027 if (query_data->cb.srv == NULL) { 1027 if (query_data->cb.srv == NULL) {
1028 purple_srv_txt_query_destroy(query_data); 1028 purple_srv_txt_query_destroy(query_data);
1029 1029
1030 g_list_foreach(records, (GFunc)g_free, NULL); 1030 while (records) {
1031 g_list_free(records); 1031 g_free(records->data);
1032 records = g_list_delete_link(records, records);
1033 }
1032 return; 1034 return;
1033 } 1035 }
1034 1036
1035 sorted_records = purple_srv_sort(records); 1037 records = purple_srv_sort(records);
1036 1038 length = g_list_length(records);
1037 purple_debug_info("dnssrv", "SRV records resolved for %s, count: %d\n", query_data->query, g_list_length(sorted_records)); 1039
1038 1040 purple_debug_info("dnssrv", "SRV records resolved for %s, count: %d\n",
1039 records_array = g_new(PurpleSrvResponse, g_list_length(sorted_records)); 1041 query_data->query, length);
1040 1042
1041 i = 0; 1043 records_array = g_new(PurpleSrvResponse, length);
1042 1044 for (l = records; l; l = l->next, i++) {
1043 for (l = sorted_records; l; l = l->next, i++) {
1044 records_array[i] = *(PurpleSrvResponse *)l->data; 1045 records_array[i] = *(PurpleSrvResponse *)l->data;
1045 } 1046 }
1046 1047
1047 query_data->cb.srv(records_array, i, query_data->extradata); 1048 query_data->cb.srv(records_array, length, query_data->extradata);
1048 1049
1049 purple_srv_txt_query_destroy(query_data); 1050 purple_srv_txt_query_destroy(query_data);
1050 1051
1051 g_list_foreach(sorted_records, (GFunc)g_free, NULL); 1052 while (records) {
1052 g_list_free(sorted_records); 1053 g_free(records->data);
1054 records = g_list_delete_link(records, records);
1055 }
1053 } 1056 }
1054 1057
1055 /* 1058 /*
1056 * Only used as the callback for the ui ops. 1059 * Only used as the callback for the ui ops.
1057 */ 1060 */
1065 /* the callback should g_free the entries. 1068 /* the callback should g_free the entries.
1066 */ 1069 */
1067 if (query_data->cb.txt != NULL) 1070 if (query_data->cb.txt != NULL)
1068 query_data->cb.txt(entries, query_data->extradata); 1071 query_data->cb.txt(entries, query_data->extradata);
1069 else { 1072 else {
1070 g_list_foreach(entries, (GFunc)g_free, NULL); 1073 while (entries) {
1071 g_list_free(entries); 1074 g_free(entries->data);
1075 entries = g_list_delete_link(entries, entries);
1076 }
1072 } 1077 }
1073 1078
1074 purple_srv_txt_query_destroy(query_data); 1079 purple_srv_txt_query_destroy(query_data);
1075 } 1080 }
1076 1081