Changeset 12330 for trunk/gtk/tr-core.c


Ignore:
Timestamp:
Apr 7, 2011, 4:29:37 AM (11 years ago)
Author:
jordan
Message:

(trunk gtk) more heap pruning: use a GStringChunk for the collated torrent names in tr-core.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr-core.c

    r12321 r12330  
    9494    GtkTreeModel * sorted_model;
    9595    tr_session   * session;
     96    GStringChunk * string_chunk;
    9697};
    9798
     
    99100core_is_disposed( const TrCore * core )
    100101{
    101     return !core || !core->priv;
    102 }
    103 
    104 static void
    105 core_dispose( GObject * obj )
    106 {
    107     TrCore * core = TR_CORE( obj );
    108 
    109     if( !core_is_disposed( core ) )
    110     {
    111         GObjectClass * parent;
    112 
    113         core->priv = NULL;
    114 
    115         parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
    116         parent->dispose( obj );
    117     }
     102    return !core || !core->priv->sorted_model;
     103}
     104
     105static void
     106core_dispose( GObject * o )
     107{
     108    TrCore * core = TR_CORE( o );
     109    GObjectClass * parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
     110
     111    if( core->priv->sorted_model != NULL )
     112    {
     113        g_object_unref( core->priv->sorted_model );
     114        core->priv->sorted_model = NULL;
     115        core->priv->raw_model = NULL;
     116    }
     117
     118    parent->dispose( o );
     119}
     120
     121static void
     122core_finalize( GObject * o )
     123{
     124    TrCore * core = TR_CORE( o );
     125    GObjectClass * parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
     126
     127    g_string_chunk_free( core->priv->string_chunk );
     128
     129    parent->finalize( o );
    118130}
    119131
     
    127139    gobject_class = G_OBJECT_CLASS( g_class );
    128140    gobject_class->dispose = core_dispose;
     141    gobject_class->finalize = core_finalize;
    129142
    130143    core_signals[ADD_ERROR_SIGNAL] = g_signal_new(
     
    225238    /* column types for the model used to store torrent information */
    226239    /* keep this in sync with the enum near the bottom of tr_core.h */
    227     GType types[] = { G_TYPE_STRING,    /* collated name */
     240    GType types[] = { G_TYPE_POINTER,   /* collated name */
    228241                      G_TYPE_POINTER,   /* tr_torrent* */
    229242                      G_TYPE_INT,       /* torrent id */
     
    249262    p->raw_model = GTK_TREE_MODEL( store );
    250263    p->sorted_model = gtk_tree_model_sort_new_with_model( p->raw_model );
     264    p->string_chunk = g_string_chunk_new( 2048 );
    251265    g_object_unref( p->raw_model );
    252266
     
    438452compare_by_name( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data UNUSED )
    439453{
    440     int ret = 0;
    441 
    442     if( !ret ) {
    443         char *ca, *cb;
    444         gtk_tree_model_get( m, a, MC_NAME_COLLATED, &ca, -1 );
    445         gtk_tree_model_get( m, b, MC_NAME_COLLATED, &cb, -1 );
    446         ret = tr_strcmp0( ca, cb );
    447         g_free( cb );
    448         g_free( ca );
    449     }
    450 
    451     return ret;
     454    char *ca, *cb;
     455    gtk_tree_model_get( m, a, MC_NAME_COLLATED, &ca, -1 );
     456    gtk_tree_model_get( m, b, MC_NAME_COLLATED, &cb, -1 );
     457    return tr_strcmp0( ca, cb );
    452458}
    453459
     
    896902***/
    897903
    898 static char*
    899 get_collated_name( const tr_torrent * tor )
    900 {
     904static const char*
     905get_collated_name( TrCore * core, const tr_torrent * tor )
     906{
     907    char buf[2048];
    901908    const char * name = tr_torrentName( tor );
     909    char * down = g_utf8_strdown( name ? name : "", -1 );
    902910    const tr_info * inf = tr_torrentInfo( tor );
    903     char * down = g_utf8_strdown( name ? name : "", -1 );
    904     char * collated = g_strdup_printf( "%s\t%s", down, inf->hashString );
     911    g_snprintf( buf, sizeof( buf ), "%s\t%s", down, inf->hashString );
    905912    g_free( down );
    906     return collated;
     913    return g_string_chunk_insert_const( core->priv->string_chunk, buf );
    907914}
    908915
     
    945952        GtkTreeModel * model = core_raw_model( data->core );
    946953        if( find_row_from_torrent_id( model, data->torrent_id, &iter ) ) {
    947             char * collated = get_collated_name( tor );
     954            const char * collated = get_collated_name( data->core, tor );
    948955            GtkListStore * store = GTK_LIST_STORE( model );
    949956            gtk_list_store_set( store, &iter, MC_NAME_COLLATED, collated, -1 );
    950             g_free( collated );
    951957        }
    952958    }
     
    10061012        GtkTreeIter unused;
    10071013        const tr_stat * st = tr_torrentStat( tor );
    1008         char * collated = get_collated_name( tor );
     1014        const char * collated = get_collated_name( core, tor );
    10091015        const unsigned int trackers_hash = build_torrent_trackers_hash( tor );
    10101016        GtkListStore * store = GTK_LIST_STORE( core_raw_model( core ) );
     
    10291035        tr_torrentSetMetadataCallback( tor, on_torrent_metadata_changed, core );
    10301036        tr_torrentSetCompletenessCallback( tor, on_torrent_completeness_changed, core );
    1031 
    1032         /* cleanup */
    1033         g_free( collated );
    10341037    }
    10351038}
Note: See TracChangeset for help on using the changeset viewer.