Revert "flowtop: Fix hanging while waiting for collector"
[netsniff-ng.git] / taia.h
blob5d79cb3acc1f5e1f450084a8cd36d73ee6af807e
1 #ifndef TAIA_H
2 #define TAIA_H
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <sys/time.h>
9 #include "rnd.h"
11 struct tai {
12 uint64_t x;
15 struct taia {
16 struct tai sec;
17 uint32_t nano;
18 uint32_t atto;
21 static inline void tai_pack(unsigned char *s, struct tai *t)
23 uint64_t x;
25 x = t->x;
27 s[7] = x & 255; x >>= 8;
28 s[6] = x & 255; x >>= 8;
29 s[5] = x & 255; x >>= 8;
30 s[4] = x & 255; x >>= 8;
31 s[3] = x & 255; x >>= 8;
32 s[2] = x & 255; x >>= 8;
33 s[1] = x & 255; x >>= 8;
34 s[0] = x;
37 static inline void tai_unpack(unsigned char *s, struct tai *t)
39 uint64_t x;
41 x = (unsigned char) s[0];
42 x <<= 8; x += (unsigned char) s[1];
43 x <<= 8; x += (unsigned char) s[2];
44 x <<= 8; x += (unsigned char) s[3];
45 x <<= 8; x += (unsigned char) s[4];
46 x <<= 8; x += (unsigned char) s[5];
47 x <<= 8; x += (unsigned char) s[6];
48 x <<= 8; x += (unsigned char) s[7];
50 t->x = x;
53 static inline void taia_pack(unsigned char *s, struct taia *t)
55 unsigned long x;
57 tai_pack(s, &t->sec);
59 s += 8;
61 x = t->atto;
63 s[7] = x & 255; x >>= 8;
64 s[6] = x & 255; x >>= 8;
65 s[5] = x & 255; x >>= 8;
66 s[4] = x;
68 x = t->nano;
70 s[3] = x & 255; x >>= 8;
71 s[2] = x & 255; x >>= 8;
72 s[1] = x & 255; x >>= 8;
73 s[0] = x;
76 static inline void taia_unpack(unsigned char *s, struct taia *t)
78 unsigned long x;
80 tai_unpack(s, &t->sec);
82 s += 8;
84 x = (unsigned char) s[4];
85 x <<= 8; x += (unsigned char) s[5];
86 x <<= 8; x += (unsigned char) s[6];
87 x <<= 8; x += (unsigned char) s[7];
89 t->atto = x;
91 x = (unsigned char) s[0];
92 x <<= 8; x += (unsigned char) s[1];
93 x <<= 8; x += (unsigned char) s[2];
94 x <<= 8; x += (unsigned char) s[3];
96 t->nano = x;
99 #define tai_unix(t, u) \
100 ((void) ((t)->x = 4611686018427387914ULL + \
101 (uint64_t) (u)))
103 static inline void taia_now(struct taia *t)
105 struct timeval now;
107 gettimeofday(&now, NULL);
109 tai_unix(&t->sec, now.tv_sec);
110 t->nano = 1000 * now.tv_usec + 500;
111 /* We don't really have it, but bring some noise in. */
112 t->atto = secrand();
115 static inline void taia_sub(struct taia *res, const struct taia *u,
116 const struct taia *v)
118 unsigned long unano = u->nano;
119 unsigned long uatto = u->atto;
121 res->sec.x = u->sec.x - v->sec.x;
122 res->nano = unano - v->nano;
123 res->atto = uatto - v->atto;
125 if (res->atto > uatto) {
126 res->atto += 1000000000UL;
127 --res->nano;
130 if (res->nano > unano) {
131 res->nano += 1000000000UL;
132 --res->sec.x;
136 static inline void taia_add(struct taia *res, const struct taia *u,
137 const struct taia *v)
139 res->sec.x = u->sec.x + v->sec.x;
140 res->nano = u->nano + v->nano;
141 res->atto = u->atto + v->atto;
143 if (res->atto > 999999999UL) {
144 res->atto -= 1000000000UL;
145 ++res->nano;
148 if (res->nano > 999999999UL) {
149 res->nano -= 1000000000UL;
150 ++res->sec.x;
154 static inline int taia_less(const struct taia *t, const struct taia *u)
156 if (t->sec.x < u->sec.x)
157 return 1;
158 if (t->sec.x > u->sec.x)
159 return 0;
160 if (t->nano < u->nano)
161 return 1;
162 if (t->nano > u->nano)
163 return 0;
164 return t->atto < u->atto;
167 extern bool taia_looks_good(struct taia *arr_taia, struct taia *pkt_taia);
169 #endif /* TAIA_H */