source: trunk/libtransmission/history.c @ 12177

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

(trunk) it's bad form to #include so many system headers in libtransmission/utils.h...

  • Property svn:keywords set to Date Rev Author Id
File size: 1.6 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
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 *
10 * $Id: history.c 12177 2011-03-16 18:04:23Z jordan $
11 */
12
13#include <assert.h>
14#include <string.h> /* memset() */
15
16#include "transmission.h"
17#include "history.h"
18#include "utils.h"
19
20void
21tr_historyAdd( tr_recentHistory * h, time_t now, unsigned int n )
22{
23    if( h->slices[h->newest].date + (time_t)h->precision >= now )
24        h->slices[h->newest].n += n;
25    else {
26        if( ++h->newest == h->sliceCount ) h->newest = 0;
27        h->slices[h->newest].date = now;
28        h->slices[h->newest].n = n;
29    }
30}
31
32unsigned int
33tr_historyGet( const tr_recentHistory * h, time_t now, unsigned int sec )
34{
35    unsigned int n = 0;
36    const time_t cutoff = (now?now:tr_time()) - sec;
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
46        if( --i == -1 ) i = h->sliceCount - 1; /* circular history */
47        if( i == h->newest ) break; /* we've come all the way around */
48    }
49
50    return n;
51}
52
53void
54tr_historyConstruct( tr_recentHistory * h, unsigned int seconds, unsigned int precision )
55{
56    memset( h, 0, sizeof( tr_recentHistory ) );
57
58    assert( precision <= seconds );
59
60    h->precision = precision;
61    h->sliceCount = seconds / precision;
62    h->slices = tr_new0( struct tr_history_slice, h->sliceCount );
63}
64
65void
66tr_historyDestruct( tr_recentHistory * h )
67{
68    tr_free( h->slices );
69}
Note: See TracBrowser for help on using the repository browser.