added timespec functions & tests, changed default build options to c11

AuthorKonata <konata@posteo.jp>
Date
Commitf8cc9c919672bffb9c8e8b833df4cf163ee54e6a
Parent9a24c67
6 files changed, 111 insertions(+), 5 deletions(-)
Minclude/cutils/cutils.h
@@ -6,5 +6,4 @@
66 #include <cutils/misc.h>
77 #include <cutils/byte_array.h>
88
9-
109 #endif /* CUTILS_H */
Minclude/cutils/misc.h
@@ -13,7 +13,24 @@
1313 #include <stdint.h>
1414 #include <string.h>
1515
16+#if __STDC_VERSION__ >= 199901L
17+ typedef uintmax_t max_uint_t;
18+#else
19+ typedef unsigned long max_uint_t;
20+#endif
21+
22+
23+#if __STDC_VERSION__ >= 201112L
24+ #include <time.h>
25+#endif
26+
1627 void sleep_ms(unsigned int milliseconds);
1728 uint32_t ntoh32(uint32_t const net);
1829
30+#if __STDC_VERSION__ >= 201112L
31+ struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
32+ struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
33+ max_uint_t timespec_ms(const struct timespec* ts);
34+#endif
35+
1936 #endif /* MISC_H */
Minclude/meson.build
@@ -1,2 +1,2 @@
11 inc = include_directories('.')
2-install_headers('cutils/cutils.h', 'cutils/dyn_string.h', 'cutils/vector.h', 'cutils/misc.h', subdir: 'cutils')
2+install_headers('cutils/cutils.h', 'cutils/dyn_string.h', 'cutils/vector.h', 'cutils/misc.h', subdir: 'cutils')
Mmeson.build
@@ -1,7 +1,7 @@
11 project('cutils', 'c')
22
33 WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter']
4-CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS
4+CFLAGS = ['-std=c11', '-fstrict-aliasing', '-fPIC'] + WARNINGS
55
66 subdir('include')
77 subdir('src')
@@ -11,4 +11,4 @@ pkg = import('pkgconfig')
1111 libs = 'cutils'
1212 headers = 'include'
1313
14-pkg.generate(libraries : cutils_lib, subdirs : headers, version : '1.0', name : 'libcutils', filebase : 'cutils', description : 'Collection of some portable helper function and data structures in C89.')
14+pkg.generate(libraries : cutils_lib, subdirs : headers, version : '1.0', name : 'libcutils', filebase : 'cutils', description : 'Collection of some portable helper function and data structures in C89.')
Msrc/misc.c
@@ -20,3 +20,43 @@ uint32_t ntoh32(uint32_t const net)
2020 | ((uint32_t) data[1] << 16)
2121 | ((uint32_t) data[0] << 24);
2222 }
23+
24+#if __STDC_VERSION__ >= 201112L
25+struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
26+{
27+ struct timespec diff;
28+
29+ if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
30+ {
31+ diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
32+ diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
33+ } else {
34+ diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
35+ diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
36+ }
37+
38+ return diff;
39+}
40+
41+struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
42+{
43+ struct timespec res;
44+
45+ if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec)
46+ {
47+ res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1;
48+ res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ;
49+ } else {
50+ res.tv_sec = ts_1->tv_sec + ts_2->tv_sec;
51+ res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec;
52+ }
53+
54+ return res;
55+};
56+
57+
58+max_uint_t timespec_ms(const struct timespec* ts)
59+{
60+ return (max_uint_t)ts->tv_sec * 1000 + (max_uint_t)ts->tv_nsec/1000000;
61+}
62+#endif
Msrc/test.c
@@ -177,7 +177,6 @@ static void test3(void)
177177
178178 my_struct.a = 2;
179179 my_struct.b = 2;
180- printf("%d\n", my_structptr->a);
181180 assert(my_structptr->a == my_struct.a);
182181 assert(my_structptr->b == my_struct.b);
183182 free(my_structptr);
@@ -188,11 +187,62 @@ static void test3(void)
188187
189188 }
190189
190+static void test4(void)
191+{
192+ struct timespec ts1 = {10, 10};
193+ struct timespec ts2 = {10, 10};
194+ struct timespec res;
195+
196+ res = timespec_add(&ts1, &ts2);
197+ assert(res.tv_sec == 20 && res.tv_nsec == 20);
198+
199+ ts1.tv_sec = 10;
200+ ts1.tv_nsec = 999999999;
201+ ts2.tv_sec = 0;
202+ ts2.tv_nsec = 1;
203+
204+ res = timespec_add(&ts1, &ts2);
205+ assert(res.tv_sec == 11 && res.tv_nsec == 0);
206+
207+ ts1.tv_sec = 0;
208+ ts1.tv_nsec = 1;
209+ ts2.tv_sec = 10;
210+ ts2.tv_nsec = 999999999;
211+
212+ res = timespec_add(&ts1, &ts2);
213+ assert(res.tv_sec == 11 && res.tv_nsec == 0);
214+
215+ ts1.tv_sec = 0;
216+ ts1.tv_nsec = 1;
217+ ts2.tv_sec = 10;
218+ ts2.tv_nsec = 999999999;
219+
220+ res = timespec_diff(&ts1, &ts2);
221+ assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
222+
223+ ts1.tv_sec = 0;
224+ ts1.tv_nsec = 999999999;
225+ ts2.tv_sec = 10;
226+ ts2.tv_nsec = 1;
227+
228+ res = timespec_diff(&ts1, &ts2);
229+ assert(res.tv_sec == 9 && res.tv_nsec == 2);
230+
231+ ts1.tv_sec = 0;
232+ ts1.tv_nsec = 1;
233+ ts2.tv_sec = 10;
234+ ts2.tv_nsec = 999999999;
235+
236+ res = timespec_diff(&ts1, &ts2);
237+ assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
238+}
239+
191240 int main(int argc, char** argv)
192241 {
193242 test1();
194243 test2();
195244 test3();
245+ test4();
196246 sleep_ms(1000);
197247 return 0;
198248 }