| 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 | #include <glib.h> |
| 23 | |
| 24 | |
| 25 | typedef struct _LttTime { |
| 26 | unsigned long tv_sec; |
| 27 | unsigned long tv_nsec; |
| 28 | } LttTime; |
| 29 | |
| 30 | |
| 31 | static const unsigned long NANOSECONDS_PER_SECOND = 1000000000; |
| 32 | |
| 33 | static const LttTime ltt_time_zero = { 0, 0}; |
| 34 | |
| 35 | static const LttTime ltt_time_infinite = { G_MAXUINT, G_MAXUINT }; |
| 36 | |
| 37 | static inline LttTime ltt_time_sub(LttTime t1, LttTime t2) |
| 38 | { |
| 39 | LttTime res; |
| 40 | res.tv_sec = t1.tv_sec - t2.tv_sec; |
| 41 | if(t1.tv_nsec < t2.tv_nsec) { |
| 42 | res.tv_sec--; |
| 43 | res.tv_nsec = NANOSECONDS_PER_SECOND + t1.tv_nsec - t2.tv_nsec; |
| 44 | } |
| 45 | else { |
| 46 | res.tv_nsec = t1.tv_nsec - t2.tv_nsec; |
| 47 | } |
| 48 | return res; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static inline LttTime ltt_time_add(LttTime t1, LttTime t2) |
| 53 | { |
| 54 | LttTime res; |
| 55 | res.tv_sec = t1.tv_sec + t2.tv_sec; |
| 56 | res.tv_nsec = t1.tv_nsec + t2.tv_nsec; |
| 57 | if(res.tv_nsec >= NANOSECONDS_PER_SECOND) { |
| 58 | res.tv_sec++; |
| 59 | res.tv_nsec -= NANOSECONDS_PER_SECOND; |
| 60 | } |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static inline LttTime ltt_time_mul(LttTime t1, float f) |
| 66 | { |
| 67 | LttTime res; |
| 68 | float d; |
| 69 | double sec; |
| 70 | |
| 71 | if(f == 0.0){ |
| 72 | res.tv_sec = 0; |
| 73 | res.tv_nsec = 0; |
| 74 | }else{ |
| 75 | d = 1.0/f; |
| 76 | sec = t1.tv_sec / (double)d; |
| 77 | res.tv_sec = sec; |
| 78 | res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) * |
| 79 | NANOSECONDS_PER_SECOND; |
| 80 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 81 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 82 | } |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static inline LttTime ltt_time_div(LttTime t1, float f) |
| 88 | { |
| 89 | double sec; |
| 90 | LttTime res; |
| 91 | |
| 92 | sec = t1.tv_sec / (double)f; |
| 93 | res.tv_sec = sec; |
| 94 | res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) * |
| 95 | NANOSECONDS_PER_SECOND; |
| 96 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 97 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 98 | return res; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static inline int ltt_time_compare(LttTime t1, LttTime t2) |
| 103 | { |
| 104 | if(t1.tv_sec > t2.tv_sec) return 1; |
| 105 | if(t1.tv_sec < t2.tv_sec) return -1; |
| 106 | if(t1.tv_nsec > t2.tv_nsec) return 1; |
| 107 | if(t1.tv_nsec < t2.tv_nsec) return -1; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static inline double ltt_time_to_double(LttTime t1) |
| 113 | { |
| 114 | return (double)t1.tv_sec + (double)t1.tv_nsec / NANOSECONDS_PER_SECOND; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static inline LttTime ltt_time_from_double(double t1) |
| 119 | { |
| 120 | LttTime res; |
| 121 | res.tv_sec = t1; |
| 122 | res.tv_nsec = (t1 - res.tv_sec) * NANOSECONDS_PER_SECOND; |
| 123 | return res; |
| 124 | } |
| 125 | |
| 126 | #endif // LTT_TIME_H |