core: use shared strings for tags on lines

It can reduce by about 30% the memory used for lines in a buffer.
This commit is contained in:
Sebastien Helleu 2013-08-10 18:11:10 +02:00
parent c46d7e44f1
commit 674403f734

View File

@ -86,6 +86,40 @@ gui_lines_free (struct t_gui_lines *lines)
free (lines); free (lines);
} }
/*
* Allocates array with tags in a line_data.
*/
void
gui_line_tags_alloc (struct t_gui_line_data *line_data, const char *tags)
{
if (tags)
{
line_data->tags_array = string_split_shared (tags, ",", 0, 0,
&line_data->tags_count);
}
else
{
line_data->tags_count = 0;
line_data->tags_array = NULL;
}
}
/*
* Frees array with tags in a line_data.
*/
void
gui_line_tags_free (struct t_gui_line_data *line_data)
{
if (line_data->tags_array)
{
string_free_split_shared (line_data->tags_array);
line_data->tags_count = 0;
line_data->tags_array = NULL;
}
}
/* /*
* Checks if prefix on line is a nick and is the same as nick on previous line. * Checks if prefix on line is a nick and is the same as nick on previous line.
* *
@ -838,8 +872,7 @@ gui_line_remove_from_list (struct t_gui_buffer *buffer,
{ {
if (line->data->str_time) if (line->data->str_time)
free (line->data->str_time); free (line->data->str_time);
if (line->data->tags_array) gui_line_tags_free (line->data);
string_free_split (line->data->tags_array);
if (line->data->prefix) if (line->data->prefix)
free (line->data->prefix); free (line->data->prefix);
if (line->data->message) if (line->data->message)
@ -1057,16 +1090,7 @@ gui_line_add (struct t_gui_buffer *buffer, time_t date,
new_line->data->date = date; new_line->data->date = date;
new_line->data->date_printed = date_printed; new_line->data->date_printed = date_printed;
new_line->data->str_time = gui_chat_get_time_string (date); new_line->data->str_time = gui_chat_get_time_string (date);
if (tags) gui_line_tags_alloc (new_line->data, tags);
{
new_line->data->tags_array = string_split (tags, ",", 0, 0,
&new_line->data->tags_count);
}
else
{
new_line->data->tags_count = 0;
new_line->data->tags_array = NULL;
}
new_line->data->refresh_needed = 0; new_line->data->refresh_needed = 0;
new_line->data->prefix = (prefix) ? new_line->data->prefix = (prefix) ?
strdup (prefix) : ((date != 0) ? strdup ("") : NULL); strdup (prefix) : ((date != 0) ? strdup ("") : NULL);
@ -1488,18 +1512,8 @@ gui_line_hdata_line_data_update_cb (void *data,
if (hashtable_has_key (hashtable, "tags_array")) if (hashtable_has_key (hashtable, "tags_array"))
{ {
value = hashtable_get (hashtable, "tags_array"); value = hashtable_get (hashtable, "tags_array");
if (line_data->tags_array) gui_line_tags_free (line_data);
string_free_split (line_data->tags_array); gui_line_tags_alloc (line_data, value);
if (value)
{
line_data->tags_array = string_split (value, ",", 0, 0,
&line_data->tags_count);
}
else
{
line_data->tags_count = 0;
line_data->tags_array = NULL;
}
rc++; rc++;
} }