1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_JACOBSON_KARELS_H
3 #define CCAN_JACOBSON_KARELS_H
7 #include <ccan/minmax/minmax.h>
9 #define JACOBSON_KARELS(_name, _type, _a1, _a2, _b1, _b2, _g, _k) \
10 struct _name##_state { \
11 _type rtt, variance; \
13 static inline void _name##_init(struct _name##_state *s, \
14 _type rtt0, _type var0) \
19 static inline void _name##_update(struct _name##_state *s, _type sample) \
21 _type diff = sample - s->rtt; \
22 s->rtt += (_a2) * diff / ((_a1) + (_a2)); \
23 diff = (diff < 0) ? -diff : diff; \
24 s->variance = ((_b1)*s->variance + (_b2) * diff) \
27 static inline _type _name##_timeout(struct _name##_state *s, \
28 _type tmin, _type tmax) \
30 return clamp((_g) * s->rtt + (_k)*s->variance, tmin, tmax); \
33 JACOBSON_KARELS(jacobson_karels, unsigned long, 7, 1, 3, 1, 1, 4)
35 #endif /* CCAN_JACOBSON_KARELS_H */