Tizen 2.1 base
[external/device-mapper.git] / lib / misc / timestamp.c
1 /*
2  * Copyright (C) 2006 Rackable Systems All rights reserved.
3  *
4  * This file is part of LVM2.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU Lesser General Public License v.2.1.
9  *
10  * You should have received a copy of the GNU Lesser General Public License
11  * along with this program; if not, write to the Free Software Foundation,
12  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13  */
14
15 /*
16  * Abstract out the time methods used so they can be adjusted later -
17  * the results of these routines should stay in-core.  This implementation
18  * requires librt.
19  */
20
21 #include "lib.h"
22 #include <stdlib.h>
23
24 #include "timestamp.h"
25
26 /*
27  * The realtime section uses clock_gettime with the CLOCK_MONOTONIC
28  * parameter to prevent issues with time warps
29  */
30 #ifdef HAVE_REALTIME
31
32 #include <time.h>
33 #include <bits/time.h>
34
35 struct timestamp {
36         struct timespec t;
37 };
38
39 struct timestamp *get_timestamp(void)
40 {
41         struct timestamp *ts = NULL;
42
43         if (!(ts = dm_malloc(sizeof(*ts))))
44                 return_NULL;
45
46         if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) {
47                 log_sys_error("clock_gettime", "get_timestamp");
48                 return NULL;
49         }
50
51         return ts;
52 }
53
54 /* cmp_timestamp: Compare two timestamps
55  *
56  * Return: -1 if t1 is less than t2
57  *          0 if t1 is equal to t2
58  *          1 if t1 is greater than t2
59  */
60 int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
61 {
62         if(t1->t.tv_sec < t2->t.tv_sec)
63                 return -1;
64         if(t1->t.tv_sec > t2->t.tv_sec)
65                 return 1;
66
67         if(t1->t.tv_nsec < t2->t.tv_nsec)
68                 return -1;
69         if(t1->t.tv_nsec > t2->t.tv_nsec)
70                 return 1;
71
72         return 0;
73 }
74
75 #else /* ! HAVE_REALTIME */
76
77 /*
78  * The !realtime section just uses gettimeofday and is therefore subject
79  * to ntp-type time warps - not sure if should allow that.
80  */
81
82 #include <sys/time.h>
83
84 struct timestamp {
85         struct timeval t;
86 };
87
88 struct timestamp *get_timestamp(void)
89 {
90         struct timestamp *ts = NULL;
91
92         if (!(ts = dm_malloc(sizeof(*ts))))
93                 return_NULL;
94
95         if (gettimeofday(&ts->t, NULL)) {
96                 log_sys_error("gettimeofday", "get_timestamp");
97                 return NULL;
98         }
99
100         return ts;
101 }
102
103 /* cmp_timestamp: Compare two timestamps
104  *
105  * Return: -1 if t1 is less than t2
106  *          0 if t1 is equal to t2
107  *          1 if t1 is greater than t2
108  */
109 int cmp_timestamp(struct timestamp *t1, struct timestamp *t2)
110 {
111         if(t1->t.tv_sec < t2->t.tv_sec)
112                 return -1;
113         if(t1->t.tv_sec > t2->t.tv_sec)
114                 return 1;
115
116         if(t1->t.tv_usec < t2->t.tv_usec)
117                 return -1;
118         if(t1->t.tv_usec > t2->t.tv_usec)
119                 return 1;
120
121         return 0;
122 }
123
124 #endif /* HAVE_REALTIME */
125
126 void destroy_timestamp(struct timestamp *t)
127 {
128         if (t)
129                 dm_free(t);
130 }