Pass around const struct timeval * instead of struct timeval.

Suggested by nickm.
This commit is contained in:
Karsten Loesing 2013-09-12 10:10:38 +02:00
parent 6553bdde8c
commit d5f0d792dd
3 changed files with 28 additions and 28 deletions

View File

@ -2552,10 +2552,10 @@ static uint32_t global_relayed_read_emptied = 0,
/** Helper: convert given <b>tvnow</b> time value to milliseconds since
* midnight. */
static uint32_t
msec_since_midnight(struct timeval tvnow)
msec_since_midnight(const struct timeval *tvnow)
{
return (uint32_t)(((tvnow.tv_sec % 86400L) * 1000L) +
((uint32_t)tvnow.tv_usec / (uint32_t)1000L));
return (uint32_t)(((tvnow->tv_sec % 86400L) * 1000L) +
((uint32_t)tvnow->tv_usec / (uint32_t)1000L));
}
/** Check if a bucket which had <b>tokens_before</b> tokens and which got
@ -2565,7 +2565,7 @@ msec_since_midnight(struct timeval tvnow)
void
connection_buckets_note_empty_ts(uint32_t *timestamp_var,
int tokens_before, size_t tokens_removed,
struct timeval tvnow)
const struct timeval *tvnow)
{
if (tokens_before > 0 && (uint32_t)tokens_before <= tokens_removed)
*timestamp_var = msec_since_midnight(tvnow);
@ -2600,20 +2600,20 @@ connection_buckets_decrement(connection_t *conn, time_t now,
tor_gettimeofday_cached(&tvnow);
if (connection_counts_as_relayed_traffic(conn, now)) {
connection_buckets_note_empty_ts(&global_relayed_read_emptied,
global_relayed_read_bucket, num_read, tvnow);
global_relayed_read_bucket, num_read, &tvnow);
connection_buckets_note_empty_ts(&global_relayed_write_emptied,
global_relayed_write_bucket, num_written, tvnow);
global_relayed_write_bucket, num_written, &tvnow);
}
connection_buckets_note_empty_ts(&global_read_emptied,
global_read_bucket, num_read, tvnow);
global_read_bucket, num_read, &tvnow);
connection_buckets_note_empty_ts(&global_write_emptied,
global_write_bucket, num_written, tvnow);
global_write_bucket, num_written, &tvnow);
if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
or_connection_t *or_conn = TO_OR_CONN(conn);
connection_buckets_note_empty_ts(&or_conn->read_emptied_time,
or_conn->read_bucket, num_read, tvnow);
or_conn->read_bucket, num_read, &tvnow);
connection_buckets_note_empty_ts(&or_conn->write_emptied_time,
or_conn->write_bucket, num_written, tvnow);
or_conn->write_bucket, num_written, &tvnow);
}
}
@ -2732,7 +2732,7 @@ connection_bucket_refill_helper(int *bucket, int rate, int burst,
uint32_t
bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
int tokens_after, int milliseconds_elapsed,
struct timeval tvnow)
const struct timeval *tvnow)
{
uint32_t result = 0, refilled;
if (tokens_before <= 0 && tokens_after > tokens_before) {
@ -2801,20 +2801,20 @@ connection_bucket_refill(int milliseconds_elapsed, time_t now)
tor_gettimeofday_cached(&tvnow);
global_read_empty_time = bucket_millis_empty(prev_global_read,
global_read_emptied, global_read_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
global_write_empty_time = bucket_millis_empty(prev_global_write,
global_write_emptied, global_write_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
control_event_tb_empty("GLOBAL", global_read_empty_time,
global_write_empty_time, milliseconds_elapsed);
relay_read_empty_time = bucket_millis_empty(prev_relay_read,
global_relayed_read_emptied,
global_relayed_read_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
relay_write_empty_time = bucket_millis_empty(prev_relay_write,
global_relayed_write_emptied,
global_relayed_write_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
control_event_tb_empty("RELAY", relay_read_empty_time,
relay_write_empty_time, milliseconds_elapsed);
}
@ -2854,11 +2854,11 @@ connection_bucket_refill(int milliseconds_elapsed, time_t now)
conn_read_empty_time = bucket_millis_empty(prev_conn_read,
or_conn->read_emptied_time,
or_conn->read_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
conn_write_empty_time = bucket_millis_empty(prev_conn_write,
or_conn->write_emptied_time,
or_conn->write_bucket,
milliseconds_elapsed, tvnow);
milliseconds_elapsed, &tvnow);
control_event_tb_empty(bucket, conn_read_empty_time,
conn_write_empty_time,
milliseconds_elapsed);

View File

@ -218,11 +218,11 @@ void connection_enable_rate_limiting(connection_t *conn);
/* Used only by connection.c and test*.c */
uint32_t bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
int tokens_after, int milliseconds_elapsed,
struct timeval tvnow);
const struct timeval *tvnow);
void connection_buckets_note_empty_ts(uint32_t *timestamp_var,
int tokens_before,
size_t tokens_removed,
struct timeval tvnow);
const struct timeval *tvnow);
#endif
#endif

View File

@ -21,7 +21,7 @@ help_test_bucket_note_empty(uint32_t expected_msec_since_midnight,
tvnow.tv_sec = msec_since_epoch / 1000;
tvnow.tv_usec = (msec_since_epoch % 1000) * 1000;
connection_buckets_note_empty_ts(&timestamp_var, tokens_before,
tokens_removed, tvnow);
tokens_removed, &tvnow);
tt_int_op(expected_msec_since_midnight, ==, timestamp_var);
done:
@ -57,20 +57,20 @@ test_cntev_bucket_millis_empty(void *arg)
tvnow.tv_usec = 200000;
/* Bucket has not been refilled. */
tt_int_op(0, ==, bucket_millis_empty(0, 42120, 0, 100, tvnow));
tt_int_op(0, ==, bucket_millis_empty(-10, 42120, -10, 100, tvnow));
tt_int_op(0, ==, bucket_millis_empty(0, 42120, 0, 100, &tvnow));
tt_int_op(0, ==, bucket_millis_empty(-10, 42120, -10, 100, &tvnow));
/* Bucket was not empty. */
tt_int_op(0, ==, bucket_millis_empty(10, 42120, 20, 100, tvnow));
tt_int_op(0, ==, bucket_millis_empty(10, 42120, 20, 100, &tvnow));
/* Bucket has been emptied 80 msec ago and has just been refilled. */
tt_int_op(80, ==, bucket_millis_empty(-20, 42120, -10, 100, tvnow));
tt_int_op(80, ==, bucket_millis_empty(-10, 42120, 0, 100, tvnow));
tt_int_op(80, ==, bucket_millis_empty(0, 42120, 10, 100, tvnow));
tt_int_op(80, ==, bucket_millis_empty(-20, 42120, -10, 100, &tvnow));
tt_int_op(80, ==, bucket_millis_empty(-10, 42120, 0, 100, &tvnow));
tt_int_op(80, ==, bucket_millis_empty(0, 42120, 10, 100, &tvnow));
/* Bucket has been emptied 180 msec ago, last refill was 100 msec ago
* which was insufficient to make it positive, so cap msec at 100. */
tt_int_op(100, ==, bucket_millis_empty(0, 42020, 1, 100, tvnow));
tt_int_op(100, ==, bucket_millis_empty(0, 42020, 1, 100, &tvnow));
/* 1970-01-02 00:00:00:050000 */
tvnow.tv_sec = 86400;
@ -78,7 +78,7 @@ test_cntev_bucket_millis_empty(void *arg)
/* Last emptied 30 msec before midnight, tvnow is 50 msec after
* midnight, that's 80 msec in total. */
tt_int_op(80, ==, bucket_millis_empty(0, 86400000 - 30, 1, 100, tvnow));
tt_int_op(80, ==, bucket_millis_empty(0, 86400000 - 30, 1, 100, &tvnow));
done:
;