added timespec functions & tests, changed default build options to c11
Minclude/cutils/cutils.h
| @@ -6,5 +6,4 @@ | |||
|---|---|---|---|
| 6 | 6 | #include <cutils/misc.h> | |
| 7 | 7 | #include <cutils/byte_array.h> | |
| 8 | 8 | ||
| 9 | - | ||
| 10 | 9 | #endif /* CUTILS_H */ | |
Minclude/cutils/misc.h
| @@ -13,7 +13,24 @@ | |||
|---|---|---|---|
| 13 | 13 | #include <stdint.h> | |
| 14 | 14 | #include <string.h> | |
| 15 | 15 | ||
| 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 | + | ||
| 16 | 27 | void sleep_ms(unsigned int milliseconds); | |
| 17 | 28 | uint32_t ntoh32(uint32_t const net); | |
| 18 | 29 | ||
| 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 | + | ||
| 19 | 36 | #endif /* MISC_H */ | |
Minclude/meson.build
| @@ -1,2 +1,2 @@ | |||
|---|---|---|---|
| 1 | 1 | 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 @@ | |||
|---|---|---|---|
| 1 | 1 | project('cutils', 'c') | |
| 2 | 2 | ||
| 3 | 3 | 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 | |
| 5 | 5 | ||
| 6 | 6 | subdir('include') | |
| 7 | 7 | subdir('src') | |
| @@ -11,4 +11,4 @@ pkg = import('pkgconfig') | |||
|---|---|---|---|
| 11 | 11 | libs = 'cutils' | |
| 12 | 12 | headers = 'include' | |
| 13 | 13 | ||
| 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) | |||
|---|---|---|---|
| 20 | 20 | | ((uint32_t) data[1] << 16) | |
| 21 | 21 | | ((uint32_t) data[0] << 24); | |
| 22 | 22 | } | |
| 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) | |||
|---|---|---|---|
| 177 | 177 | ||
| 178 | 178 | my_struct.a = 2; | |
| 179 | 179 | my_struct.b = 2; | |
| 180 | - | printf("%d\n", my_structptr->a); | |
| 181 | 180 | assert(my_structptr->a == my_struct.a); | |
| 182 | 181 | assert(my_structptr->b == my_struct.b); | |
| 183 | 182 | free(my_structptr); | |
| @@ -188,11 +187,62 @@ static void test3(void) | |||
|---|---|---|---|
| 188 | 187 | ||
| 189 | 188 | } | |
| 190 | 189 | ||
| 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 | + | ||
| 191 | 240 | int main(int argc, char** argv) | |
| 192 | 241 | { | |
| 193 | 242 | test1(); | |
| 194 | 243 | test2(); | |
| 195 | 244 | test3(); | |
| 245 | + | test4(); | |
| 196 | 246 | sleep_ms(1000); | |
| 197 | 247 | return 0; | |
| 198 | 248 | } | |