Add reference counts to state and stats saved attributes. This way, the
[lttv.git] / ltt / branches / poly / ltt / time.h
1 /* This file is part of the Linux Trace Toolkit trace reading library
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License Version 2.1 as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
17 */
18
19 #ifndef LTT_TIME_H
20 #define LTT_TIME_H
21
22
23 typedef struct _LttTime {
24 unsigned long tv_sec;
25 unsigned long tv_nsec;
26 } LttTime;
27
28
29 static const unsigned long NANOSECONDS_PER_SECOND = 1000000000;
30
31 static const LttTime ltt_time_zero = { 0, 0};
32
33
34 static inline LttTime ltt_time_sub(LttTime t1, LttTime t2)
35 {
36 LttTime res;
37 res.tv_sec = t1.tv_sec - t2.tv_sec;
38 if(t1.tv_nsec < t2.tv_nsec) {
39 res.tv_sec--;
40 res.tv_nsec = NANOSECONDS_PER_SECOND + t1.tv_nsec - t2.tv_nsec;
41 }
42 else {
43 res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
44 }
45 return res;
46 }
47
48
49 static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
50 {
51 LttTime res;
52 res.tv_sec = t1.tv_sec + t2.tv_sec;
53 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
54 if(res.tv_nsec >= NANOSECONDS_PER_SECOND) {
55 res.tv_sec++;
56 res.tv_nsec -= NANOSECONDS_PER_SECOND;
57 }
58 return res;
59 }
60
61
62 static inline LttTime ltt_time_mul(LttTime t1, float f)
63 {
64 LttTime res;
65 float d;
66 double sec;
67
68 if(f == 0.0){
69 res.tv_sec = 0;
70 res.tv_nsec = 0;
71 }else{
72 d = 1.0/f;
73 sec = t1.tv_sec / (double)d;
74 res.tv_sec = sec;
75 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
76 NANOSECONDS_PER_SECOND;
77 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
78 res.tv_nsec %= NANOSECONDS_PER_SECOND;
79 }
80 return res;
81 }
82
83
84 static inline LttTime ltt_time_div(LttTime t1, float f)
85 {
86 double sec;
87 LttTime res;
88
89 sec = t1.tv_sec / (double)f;
90 res.tv_sec = sec;
91 res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) *
92 NANOSECONDS_PER_SECOND;
93 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
94 res.tv_nsec %= NANOSECONDS_PER_SECOND;
95 return res;
96 }
97
98
99 static inline int ltt_time_compare(LttTime t1, LttTime t2)
100 {
101 if(t1.tv_sec > t2.tv_sec) return 1;
102 if(t1.tv_sec < t2.tv_sec) return -1;
103 if(t1.tv_nsec > t2.tv_nsec) return 1;
104 if(t1.tv_nsec < t2.tv_nsec) return -1;
105 return 0;
106 }
107
108
109 static inline double ltt_time_to_double(LttTime t1)
110 {
111 return (double)t1.tv_sec + (double)t1.tv_nsec / NANOSECONDS_PER_SECOND;
112 }
113
114
115 static inline LttTime ltt_time_from_double(double t1)
116 {
117 LttTime res;
118 res.tv_sec = t1;
119 res.tv_nsec = (t1 - res.tv_sec) * NANOSECONDS_PER_SECOND;
120 return res;
121 }
122
123 #endif // LTT_TIME_H
This page took 0.035695 seconds and 4 git commands to generate.