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 @@
#include <cutils/misc.h>
#include <cutils/byte_array.h>
#endif /* CUTILS_H */
▾Minclude/cutils/misc.h
@@ -13,7 +13,24 @@
#include <stdint.h>
#include <string.h>
#if __STDC_VERSION__ >= 199901L
typedef uintmax_t max_uint_t;
#else
typedef unsigned long max_uint_t;
#endif
#if __STDC_VERSION__ >= 201112L
#include <time.h>
#endif
void sleep_ms(unsigned int milliseconds);
uint32_t ntoh32(uint32_t const net);
#if __STDC_VERSION__ >= 201112L
struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
max_uint_t timespec_ms(const struct timespec* ts);
#endif
#endif /* MISC_H */
▾Minclude/meson.build
@@ -1,2 +1,2 @@
inc = include_directories('.')
install_headers('cutils/cutils.h', 'cutils/dyn_string.h', 'cutils/vector.h', 'cutils/misc.h', subdir: 'cutils')
install_headers('cutils/cutils.h', 'cutils/dyn_string.h', 'cutils/vector.h', 'cutils/misc.h', subdir: 'cutils')
▾Mmeson.build
@@ -1,7 +1,7 @@
project('cutils', 'c')
WARNINGS = ['-Wall', '-Wpedantic', '-Wextra', '-Wnull-dereference', '-Wshadow', '-Wconversion', '-Wstrict-prototypes', '-Wmissing-prototypes', '-Wcast-qual', '-Wstrict-overflow=5', '-Wunreachable-code', '-Wno-unused-parameter']
CFLAGS = ['-std=c89', '-fstrict-aliasing', '-fPIC'] + WARNINGS
CFLAGS = ['-std=c11', '-fstrict-aliasing', '-fPIC'] + WARNINGS
subdir('include')
subdir('src')
@@ -11,4 +11,4 @@ pkg = import('pkgconfig')
libs = 'cutils'
headers = 'include'
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.')
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)
| ((uint32_t) data[1] << 16)
| ((uint32_t) data[0] << 24);
}
#if __STDC_VERSION__ >= 201112L
struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
{
struct timespec diff;
if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
{
diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
} else {
diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
}
return diff;
}
struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
{
struct timespec res;
if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec)
{
res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1;
res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ;
} else {
res.tv_sec = ts_1->tv_sec + ts_2->tv_sec;
res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec;
}
return res;
};
max_uint_t timespec_ms(const struct timespec* ts)
{
return (max_uint_t)ts->tv_sec * 1000 + (max_uint_t)ts->tv_nsec/1000000;
}
#endif
▾Msrc/test.c
@@ -177,7 +177,6 @@ static void test3(void)
my_struct.a = 2;
my_struct.b = 2;
printf("%d\n", my_structptr->a);
assert(my_structptr->a == my_struct.a);
assert(my_structptr->b == my_struct.b);
free(my_structptr);
@@ -188,11 +187,62 @@ static void test3(void)
}
static void test4(void)
{
struct timespec ts1 = {10, 10};
struct timespec ts2 = {10, 10};
struct timespec res;
res = timespec_add(&ts1, &ts2);
assert(res.tv_sec == 20 && res.tv_nsec == 20);
ts1.tv_sec = 10;
ts1.tv_nsec = 999999999;
ts2.tv_sec = 0;
ts2.tv_nsec = 1;
res = timespec_add(&ts1, &ts2);
assert(res.tv_sec == 11 && res.tv_nsec == 0);
ts1.tv_sec = 0;
ts1.tv_nsec = 1;
ts2.tv_sec = 10;
ts2.tv_nsec = 999999999;
res = timespec_add(&ts1, &ts2);
assert(res.tv_sec == 11 && res.tv_nsec == 0);
ts1.tv_sec = 0;
ts1.tv_nsec = 1;
ts2.tv_sec = 10;
ts2.tv_nsec = 999999999;
res = timespec_diff(&ts1, &ts2);
assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
ts1.tv_sec = 0;
ts1.tv_nsec = 999999999;
ts2.tv_sec = 10;
ts2.tv_nsec = 1;
res = timespec_diff(&ts1, &ts2);
assert(res.tv_sec == 9 && res.tv_nsec == 2);
ts1.tv_sec = 0;
ts1.tv_nsec = 1;
ts2.tv_sec = 10;
ts2.tv_nsec = 999999999;
res = timespec_diff(&ts1, &ts2);
assert(res.tv_sec == 10 && res.tv_nsec == 999999998);
}
int main(int argc, char** argv)
{
test1();
test2();
test3();
test4();
sleep_ms(1000);
return 0;
}