From 47293a4f116e04a88b0797bafad0c98b4393b744 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Thu, 5 Nov 2026 13:52:17 +0100 Subject: Clamp every elapsed time span at zero, using a set of helper functions --- src/trackerlogic.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/trackerlogic.h') diff --git a/src/trackerlogic.h b/src/trackerlogic.h index 692b6af..c8d859c 100644 --- a/src/trackerlogic.h +++ b/src/trackerlogic.h @@ -61,10 +61,10 @@ typedef struct { #define OT_PEER_TIMEOUT 45 /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs - Sort key is, of course, its hash */ -/* Set from the Makefile, 8 to 16. More buckets means shorter vectors and a - cheaper insert, which only pays for a tracker carrying millions of - torrents; see DESIGN. */ + Sort key is, of course, its hash. + Can be tweked from the Makefile, sensible values are between 8 in 16. More + buckets means shorter vectors and a cheaper insert, which only pays for a + tracker carrying millions of torrents. Consult the DESIGN document. */ #ifndef OT_BUCKET_COUNT_BITS #define OT_BUCKET_COUNT_BITS 10 #endif @@ -95,6 +95,22 @@ static inline ot_time time_now_minutes(void) { return time_now_seconds() / 60; } /* For the deadlines the tracker sets on itself, which read better as the span they allow than as an addition to the current second */ static inline ot_time time_future_seconds(int span) { return time_now_seconds() + span; } + +/* How long ago a stamp was taken. */ +_Static_assert((ot_time)-1 < 0, "ot_time has to be signed for time_since_* to clamp"); + +static inline ot_time time_since_seconds(ot_time then) { + ot_time now = time_now_seconds(); + return now > then ? now - then : 0; +} + +static inline ot_time time_since_minutes(ot_time then) { + ot_time now = time_now_minutes(); + return now > then ? now - then : 0; +} + +/* Has a deadline the tracker set on itself gone by? */ +static inline int time_is_past(ot_time deadline) { return time_now_seconds() > deadline; } /* Cleared from the interrupt handler and read by every worker */ extern _Atomic int g_opentracker_running; -- cgit v1.3