(trunk gtk) more heap pruning: avoid unnecessary malloc() + free() calls in gtr_get_host_from_url()

This commit is contained in:
Jordan Lee 2011-04-04 16:54:09 +00:00
parent 94e11008b9
commit d233ea82e7
4 changed files with 17 additions and 19 deletions

View File

@ -178,7 +178,7 @@ gtr_get_favicon_from_url( tr_session * session,
GFunc pixbuf_ready_func,
gpointer pixbuf_ready_func_data )
{
char * host = gtr_get_host_from_url( url );
char host[1024];
gtr_get_host_from_url( host, sizeof( host ), url );
gtr_get_favicon( session, host, pixbuf_ready_func, pixbuf_ready_func_data );
g_free( host );
}

View File

@ -159,8 +159,11 @@ category_filter_model_update( GtkTreeStore * store )
for( i=0, n=inf->trackerCount; i<n; ++i )
{
int k;
char * key = gtr_get_host_from_url( inf->trackers[i].announce );
int * count = g_hash_table_lookup( hosts_hash, key );
int * count;
char key[1024];
gtr_get_host_from_url( key, sizeof( key ), inf->trackers[i].announce );
count = g_hash_table_lookup( hosts_hash, key );
if( count == NULL )
{
count = tr_new0( int, 1 );
@ -172,9 +175,7 @@ category_filter_model_update( GtkTreeStore * store )
if( !strcmp( keys[k], key ) )
break;
if( k==keyCount )
keys[keyCount++] = key;
else
g_free( key );
keys[keyCount++] = g_strdup( key );
}
for( i=0; i<keyCount; ++i )
@ -557,12 +558,11 @@ testCategory( GtkWidget * category_combo, tr_torrent * tor )
case CAT_FILTER_TYPE_HOST: {
int i;
char * host;
char tmp[1024];
gtk_tree_model_get( model, &iter, CAT_FILTER_COL_HOST, &host, -1 );
for( i=0; i<inf->trackerCount; ++i ) {
char * tmp = gtr_get_host_from_url( inf->trackers[i].announce );
const gboolean hit = !strcmp( tmp, host );
g_free( tmp );
if( hit )
gtr_get_host_from_url( tmp, sizeof( tmp ), inf->trackers[i].announce );
if( !strcmp( tmp, host ) )
break;
}
g_free( host );

View File

@ -205,27 +205,25 @@ gtr_mkdir_with_parents( const char * path, int mode )
}
/* pattern-matching text; ie, legaltorrents.com */
char*
gtr_get_host_from_url( const char * url )
void
gtr_get_host_from_url( char * buf, size_t buflen, const char * url )
{
char * name;
char * h = NULL;
tr_urlParse( url, -1, NULL, &h, NULL, NULL );
if( tr_addressIsIP( h ) )
name = g_strdup( h );
g_strlcpy( buf, url, buflen );
else {
const char * first_dot = strchr( h, '.' );
const char * last_dot = strrchr( h, '.' );
if( ( first_dot ) && ( last_dot ) && ( first_dot != last_dot ) )
name = g_strdup( first_dot + 1 );
g_strlcpy( buf, first_dot + 1, buflen );
else
name = g_strdup( h );
g_strlcpy( buf, h, buflen );
}
tr_free( h );
return name;
}
gboolean

View File

@ -68,7 +68,7 @@ char* tr_strltime( char * buf, int secs, size_t buflen );
***/
/* http://www.legaltorrents.com/some/announce/url --> legaltorrents.com */
char* gtr_get_host_from_url( const char * url );
void gtr_get_host_from_url( char * buf, size_t buflen, const char * url );
gboolean gtr_is_supported_url( const char * str );