source: trunk/libtransmission/history.c @ 12918

Last change on this file since 12918 was 12328, checked in by jordan, 11 years ago

(trunk libT) more heap pruning: avoid four unnecessary malloc() + free() calls per tr_peer.

This commit also changes tr_recentHistory from being a general-purpose tool to being a little more hardcoded for the only purpose it's used, in tr_peerMgr. If its files (history.[ch]) don't find any other "customers" in libtransmission, eventually it should be demoted to being a private helper class inside of peer-mgr.c and have the history.[ch] files removed from the build.

  • Property svn:keywords set to Date Rev Author Id
File size: 1.3 KB
RevLine 
[10332]1/*
[11709]2 * This file Copyright (C) Mnemosyne LLC
[10332]3 *
[11599]4 * This file is licensed by the GPL version 2. Works owned by the
[10332]5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
[11280]10 * $Id: history.c 12328 2011-04-06 23:27:11Z jordan $
[10332]11 */
12
[11425]13#include <assert.h>
[12177]14#include <string.h> /* memset() */
[11425]15
[10332]16#include "transmission.h"
17#include "history.h"
18#include "utils.h"
19
20void
[11425]21tr_historyAdd( tr_recentHistory * h, time_t now, unsigned int n )
[10332]22{
[12328]23    if( h->slices[h->newest].date == now )
[10332]24        h->slices[h->newest].n += n;
25    else {
[12328]26        if( ++h->newest == TR_RECENT_HISTORY_PERIOD_SEC ) h->newest = 0;
[10332]27        h->slices[h->newest].date = now;
28        h->slices[h->newest].n = n;
29    }
30}
31
[10909]32unsigned int
[11425]33tr_historyGet( const tr_recentHistory * h, time_t now, unsigned int sec )
[10332]34{
[10909]35    unsigned int n = 0;
[11425]36    const time_t cutoff = (now?now:tr_time()) - sec;
[10332]37    int i = h->newest;
38
39    for( ;; )
40    {
41        if( h->slices[i].date <= cutoff )
42            break;
43
44        n += h->slices[i].n;
45
[12328]46        if( --i == -1 ) i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
[10332]47        if( i == h->newest ) break; /* we've come all the way around */
48    }
49
50    return n;
51}
Note: See TracBrowser for help on using the repository browser.