1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00
transmission/libtransmission/history.c
Jordan Lee 879a2afcbd Update the copyright year in the source code comments.
The Berne Convention says that the copyright year is moot, so instead of adding another year to each file as in previous years, I've removed the year altogether from the source code comments in libtransmission, gtk, qt, utils, daemon, and cli.

Juliusz's copyright notice in tr-dht and Johannes' copyright notice in tr-lpd have been left alone; it didn't seem appropriate to modify them.
2011-01-19 13:48:47 +00:00

86 lines
1.8 KiB
C

/*
* This file Copyright (C) Mnemosyne LLC
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
* $Id$
*/
#include <assert.h>
#include "transmission.h"
#include "history.h"
#include "utils.h"
struct history_slice
{
unsigned int n;
time_t date;
};
struct tr_recentHistory
{
int newest;
int sliceCount;
unsigned int precision;
struct history_slice * slices;
};
void
tr_historyAdd( tr_recentHistory * h, time_t now, unsigned int n )
{
if( h->slices[h->newest].date + (time_t)h->precision >= now )
h->slices[h->newest].n += n;
else {
if( ++h->newest == h->sliceCount ) h->newest = 0;
h->slices[h->newest].date = now;
h->slices[h->newest].n = n;
}
}
unsigned int
tr_historyGet( const tr_recentHistory * h, time_t now, unsigned int sec )
{
unsigned int n = 0;
const time_t cutoff = (now?now:tr_time()) - sec;
int i = h->newest;
for( ;; )
{
if( h->slices[i].date <= cutoff )
break;
n += h->slices[i].n;
if( --i == -1 ) i = h->sliceCount - 1; /* circular history */
if( i == h->newest ) break; /* we've come all the way around */
}
return n;
}
tr_recentHistory *
tr_historyNew( unsigned int seconds, unsigned int precision )
{
tr_recentHistory * h;
assert( precision <= seconds );
h = tr_new0( tr_recentHistory, 1 );
h->precision = precision;
h->sliceCount = seconds / precision;
h->slices = tr_new0( struct history_slice, h->sliceCount );
return h;
}
void
tr_historyFree( tr_recentHistory * h )
{
tr_free( h->slices );
tr_free( h );
}