Changeset 12352


Ignore:
Timestamp:
Apr 12, 2011, 10:04:05 AM (11 years ago)
Author:
jordan
Message:

(trunk gtk) more heap pruning: give torrent-cell-renderer an internal GString buffer so that we don't have to create & free so many short-term strings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/torrent-cell-renderer.c

    r12223 r12352  
    5656***/
    5757
    58 static char*
    59 getProgressString( const tr_torrent * tor,
     58static void
     59getProgressString( GString          * gstr,
     60                   const tr_torrent * tor,
    6061                   const tr_info    * info,
    6162                   const tr_stat    * st )
     
    6566    const int      isSeed = st->haveValid >= info->totalSize;
    6667    char           buf1[32], buf2[32], buf3[32], buf4[32], buf5[32], buf6[32];
    67     char *         str;
    6868    double         seedRatio;
    6969    const gboolean hasSeedRatio = tr_torrentGetSeedRatio( tor, &seedRatio );
     
    7171    if( !isDone ) /* downloading */
    7272    {
    73         str = g_strdup_printf(
     73        g_string_append_printf( gstr,
    7474            /* %1$s is how much we've got,
    7575               %2$s is how much we'll have when done,
     
    8484        if( hasSeedRatio )
    8585        {
    86             str = g_strdup_printf(
     86            g_string_append_printf( gstr,
    8787                /* %1$s is how much we've got,
    8888                   %2$s is the torrent's total size,
     
    101101        else
    102102        {
    103             str = g_strdup_printf(
     103            g_string_append_printf( gstr,
    104104                /* %1$s is how much we've got,
    105105                   %2$s is the torrent's total size,
     
    119119        if( hasSeedRatio )
    120120        {
    121             str = g_strdup_printf(
     121            g_string_append_printf( gstr,
    122122                /* %1$s is the torrent's total size,
    123123                   %2$s is how much we've uploaded,
     
    132132        else /* seeding w/o a ratio */
    133133        {
    134             str = g_strdup_printf(
     134            g_string_append_printf( gstr,
    135135                /* %1$s is the torrent's total size,
    136136                   %2$s is how much we've uploaded,
     
    148148    {
    149149        const int eta = st->eta;
    150         GString * gstr = g_string_new( str );
    151150        g_string_append( gstr, " - " );
    152151        if( eta < 0 )
     
    159158            g_string_append_printf( gstr, _( "%s remaining" ), timestr );
    160159        }
    161         g_free( str );
    162         str = g_string_free( gstr, FALSE );
    163     }
    164 
    165     return str;
     160    }
    166161}
    167162
     
    206201}
    207202
    208 static char*
    209 getShortStatusString( const tr_torrent  * tor,
     203static void
     204getShortStatusString( GString           * gstr,
     205                      const tr_torrent  * tor,
    210206                      const tr_stat     * st,
    211207                      double              uploadSpeed_KBps,
    212208                      double              downloadSpeed_KBps )
    213209{
    214     GString * gstr = g_string_new( NULL );
    215 
    216210    switch( st->activity )
    217211    {
    218212        case TR_STATUS_STOPPED:
    219213            if( st->finished )
    220                 g_string_assign( gstr, _( "Finished" ) );
     214                g_string_append( gstr, _( "Finished" ) );
    221215            else
    222                 g_string_assign( gstr, _( "Paused" ) );
     216                g_string_append( gstr, _( "Paused" ) );
    223217            break;
    224218
    225219        case TR_STATUS_CHECK_WAIT:
    226             g_string_assign( gstr, _( "Waiting to verify local data" ) );
     220            g_string_append( gstr, _( "Waiting to verify local data" ) );
    227221            break;
    228222
     
    251245            break;
    252246    }
    253 
    254     return g_string_free( gstr, FALSE );
    255 }
    256 
    257 static char*
    258 getStatusString( const tr_torrent  * tor,
     247}
     248
     249static void
     250getStatusString( GString           * gstr,
     251                 const tr_torrent  * tor,
    259252                 const tr_stat     * st,
    260253                 const double        uploadSpeed_KBps,
     
    264257    const int isChecking = st->activity == TR_STATUS_CHECK
    265258                        || st->activity == TR_STATUS_CHECK_WAIT;
    266 
    267     GString * gstr = g_string_new( NULL );
    268259
    269260    if( st->error )
     
    280271        case TR_STATUS_CHECK:
    281272        {
    282             char * pch = getShortStatusString( tor, st, uploadSpeed_KBps, downloadSpeed_KBps );
    283             g_string_assign( gstr, pch );
    284             g_free( pch );
     273            getShortStatusString( gstr, tor, st, uploadSpeed_KBps, downloadSpeed_KBps );
    285274            break;
    286275        }
     
    326315            g_string_append_printf( gstr, " - %s", buf );
    327316    }
    328 
    329     return g_string_free( gstr, FALSE );
    330317}
    331318
     
    342329    GtkCellRenderer  * progress_renderer;
    343330    GtkCellRenderer  * icon_renderer;
     331    GString          * gstr1;
     332    GString          * gstr2;
    344333    int bar_height;
    345334
     
    394383    GdkRectangle stat_area;
    395384    const char * name;
    396     char * status;
    397385    GdkPixbuf * icon;
    398386
     
    400388    const tr_torrent * tor = p->tor;
    401389    const tr_stat * st = tr_torrentStatCached( (tr_torrent*)tor );
     390    GString * gstr_stat = p->gstr1;
    402391
    403392    icon = get_icon( tor, COMPACT_ICON_SIZE, widget );
    404393    name = tr_torrentName( tor );
    405     status = getShortStatusString( tor, st, p->upload_speed_KBps, p->download_speed_KBps );
     394    g_string_truncate( gstr_stat, 0 );
     395    getShortStatusString( gstr_stat, tor, st, p->upload_speed_KBps, p->download_speed_KBps );
    406396    gtr_cell_renderer_get_padding( GTK_CELL_RENDERER( cell ), &xpad, &ypad );
    407397
     
    415405    name_area.width = w;
    416406    name_area.height = h;
    417     g_object_set( p->text_renderer, "text", status, "scale", SMALL_SCALE, NULL );
     407    g_object_set( p->text_renderer, "text", gstr_stat->str, "scale", SMALL_SCALE, NULL );
    418408    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &w, &h );
    419409    stat_area.width = w;
     
    431421
    432422    /* cleanup */
    433     g_free( status );
    434423    g_object_unref( icon );
    435424}
     
    450439    GdkRectangle prog_area;
    451440    const char * name;
    452     char * status;
    453     char * progress;
    454441    GdkPixbuf * icon;
    455442
     
    458445    const tr_stat * st = tr_torrentStatCached( (tr_torrent*)tor );
    459446    const tr_info * inf = tr_torrentInfo( tor );
     447    GString * gstr_prog = p->gstr1;
     448    GString * gstr_stat = p->gstr2;
    460449
    461450    icon = get_icon( tor, FULL_ICON_SIZE, widget );
    462451    name = tr_torrentName( tor );
    463     status = getStatusString( tor, st, p->upload_speed_KBps, p->download_speed_KBps );
    464     progress = getProgressString( tor, inf, st );
     452    g_string_truncate( gstr_stat, 0 );
     453    getStatusString( gstr_stat, tor, st, p->upload_speed_KBps, p->download_speed_KBps );
     454    g_string_truncate( gstr_prog, 0 );
     455    getProgressString( gstr_prog, tor, inf, st );
    465456    gtr_cell_renderer_get_padding( GTK_CELL_RENDERER( cell ), &xpad, &ypad );
    466457
     
    474465    name_area.width = w;
    475466    name_area.height = h;
    476     g_object_set( p->text_renderer, "text", progress, "weight", PANGO_WEIGHT_NORMAL, "scale", SMALL_SCALE, NULL );
     467    g_object_set( p->text_renderer, "text", gstr_prog->str, "weight", PANGO_WEIGHT_NORMAL, "scale", SMALL_SCALE, NULL );
    477468    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &w, &h );
    478469    prog_area.width = w;
    479470    prog_area.height = h;
    480     g_object_set( p->text_renderer, "text", status, NULL );
     471    g_object_set( p->text_renderer, "text", gstr_stat->str, NULL );
    481472    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &w, &h );
    482473    stat_area.width = w;
     
    493484
    494485    /* cleanup */
    495     g_free( status );
    496     g_free( progress );
    497486    g_object_unref( icon );
    498487}
     
    566555    GdkRectangle fill_area;
    567556    const char * name;
    568     char * status;
    569557    GdkPixbuf * icon;
    570558    GdkColor text_color;
     
    576564    const double percentDone = MAX( 0.0, st->percentDone );
    577565    const gboolean sensitive = active || st->error;
     566    GString * gstr_stat = p->gstr1;
    578567
    579568    icon = get_icon( tor, COMPACT_ICON_SIZE, widget );
    580569    name = tr_torrentName( tor );
    581     status = getShortStatusString( tor, st, p->upload_speed_KBps, p->download_speed_KBps );
     570    g_string_truncate( gstr_stat, 0 );
     571    getShortStatusString( gstr_stat, tor, st, p->upload_speed_KBps, p->download_speed_KBps );
    582572    gtr_cell_renderer_get_padding( GTK_CELL_RENDERER( cell ), &xpad, &ypad );
    583573    get_text_color( widget, st, &text_color );
     
    594584    g_object_set( p->text_renderer, "text", name, "ellipsize", PANGO_ELLIPSIZE_NONE, "scale", 1.0, NULL );
    595585    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &name_area.width, NULL );
    596     g_object_set( p->text_renderer, "text", status, "scale", SMALL_SCALE, NULL );
     586    g_object_set( p->text_renderer, "text", gstr_stat->str, "scale", SMALL_SCALE, NULL );
    597587    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &stat_area.width, NULL );
    598588
     
    613603    g_object_set( p->progress_renderer, "value", (int)(percentDone*100.0), "text", NULL, "sensitive", sensitive, NULL );
    614604    gtk_cell_renderer_render( p->progress_renderer, window, widget, &prog_area, &prog_area, &prog_area, flags );
    615     g_object_set( p->text_renderer, "text", status, "scale", SMALL_SCALE, "ellipsize", PANGO_ELLIPSIZE_END, "foreground-gdk", &text_color, NULL );
     605    g_object_set( p->text_renderer, "text", gstr_stat->str, "scale", SMALL_SCALE, "ellipsize", PANGO_ELLIPSIZE_END, "foreground-gdk", &text_color, NULL );
    616606    gtk_cell_renderer_render( p->text_renderer, window, widget, &stat_area, &stat_area, &stat_area, flags );
    617607    g_object_set( p->text_renderer, "text", name, "scale", 1.0, NULL );
     
    619609
    620610    /* cleanup */
    621     g_free( status );
    622611    g_object_unref( icon );
    623612}
     
    641630    GdkRectangle prct_area;
    642631    const char * name;
    643     char * status;
    644     char * progress;
    645632    GdkPixbuf * icon;
    646633    GdkColor text_color;
     
    653640    const double percentDone = MAX( 0.0, st->percentDone );
    654641    const gboolean sensitive = active || st->error;
     642    GString * gstr_prog = p->gstr1;
     643    GString * gstr_stat = p->gstr2;
    655644
    656645    icon = get_icon( tor, FULL_ICON_SIZE, widget );
    657646    name = tr_torrentName( tor );
    658     status = getStatusString( tor, st, p->upload_speed_KBps, p->download_speed_KBps );
    659     progress = getProgressString( tor, inf, st );
     647    g_string_truncate( gstr_prog, 0 );
     648    getProgressString( gstr_prog, tor, inf, st );
     649    g_string_truncate( gstr_stat, 0 );
     650    getStatusString( gstr_stat, tor, st, p->upload_speed_KBps, p->download_speed_KBps );
    660651    gtr_cell_renderer_get_padding( GTK_CELL_RENDERER( cell ), &xpad, &ypad );
    661652    get_text_color( widget, st, &text_color );
     
    670661    name_area.width = w;
    671662    name_area.height = h;
    672     g_object_set( p->text_renderer, "text", progress, "weight", PANGO_WEIGHT_NORMAL, "scale", SMALL_SCALE, NULL );
     663    g_object_set( p->text_renderer, "text", gstr_prog->str, "weight", PANGO_WEIGHT_NORMAL, "scale", SMALL_SCALE, NULL );
    673664    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &w, &h );
    674665    prog_area.width = w;
    675666    prog_area.height = h;
    676     g_object_set( p->text_renderer, "text", status, NULL );
     667    g_object_set( p->text_renderer, "text", gstr_stat->str, NULL );
    677668    gtk_cell_renderer_get_size( p->text_renderer, widget, NULL, NULL, NULL, &w, &h );
    678669    stat_area.width = w;
     
    722713    g_object_set( p->text_renderer, "text", name, "scale", 1.0, "foreground-gdk", &text_color, "ellipsize", PANGO_ELLIPSIZE_END, "weight", PANGO_WEIGHT_BOLD, NULL );
    723714    gtk_cell_renderer_render( p->text_renderer, window, widget, &name_area, &name_area, &name_area, flags );
    724     g_object_set( p->text_renderer, "text", progress, "scale", SMALL_SCALE, "weight", PANGO_WEIGHT_NORMAL, NULL );
     715    g_object_set( p->text_renderer, "text", gstr_prog->str, "scale", SMALL_SCALE, "weight", PANGO_WEIGHT_NORMAL, NULL );
    725716    gtk_cell_renderer_render( p->text_renderer, window, widget, &prog_area, &prog_area, &prog_area, flags );
    726717    g_object_set( p->progress_renderer, "value", (int)(percentDone*100.0), "text", "", "sensitive", sensitive, NULL );
    727718    gtk_cell_renderer_render( p->progress_renderer, window, widget, &prct_area, &prct_area, &prct_area, flags );
    728     g_object_set( p->text_renderer, "text", status, NULL );
     719    g_object_set( p->text_renderer, "text", gstr_stat->str, NULL );
    729720    gtk_cell_renderer_render( p->text_renderer, window, widget, &stat_area, &stat_area, &stat_area, flags );
    730721
    731722    /* cleanup */
    732     g_free( status );
    733     g_free( progress );
    734723    g_object_unref( icon );
    735724}
     
    813802    if( r && r->priv )
    814803    {
     804        g_string_free( r->priv->gstr1, TRUE );
     805        g_string_free( r->priv->gstr2, TRUE );
    815806        g_object_unref( G_OBJECT( r->priv->text_renderer ) );
    816807        g_object_unref( G_OBJECT( r->priv->progress_renderer ) );
     
    872863
    873864static void
    874 torrent_cell_renderer_init( GTypeInstance *  instance,
    875                             gpointer g_class UNUSED )
    876 {
    877     TorrentCellRenderer *               self = TORRENT_CELL_RENDERER(
    878         instance );
     865torrent_cell_renderer_init( GTypeInstance * instance, gpointer g_class UNUSED )
     866{
    879867    struct TorrentCellRendererPrivate * p;
     868    TorrentCellRenderer * self = TORRENT_CELL_RENDERER( instance );
    880869
    881870    p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE(
     
    886875
    887876    p->tor = NULL;
     877    p->gstr1 = g_string_new( NULL );
     878    p->gstr2 = g_string_new( NULL );
    888879    p->text_renderer = gtk_cell_renderer_text_new( );
    889880    g_object_set( p->text_renderer, "xpad", 0, "ypad", 0, NULL );
Note: See TracChangeset for help on using the changeset viewer.