diff --git a/include/dap_common.h b/include/dap_common.h index e3212ca1ba55ce7b1950941e254fdb97c3bcbb70..ca885dc8f8725906a4e0155197fc71f520a133a2 100755 --- a/include/dap_common.h +++ b/include/dap_common.h @@ -352,6 +352,7 @@ void dap_set_log_tag_width(size_t width); char *dap_itoa(int i); int dap_time_to_str_rfc822(char * out, size_t out_size_max, time_t t); +int timespec_diff(struct timespec *a_start, struct timespec *a_stop, struct timespec *a_result); int get_select_breaker(void); int send_select_break(void); diff --git a/include/dap_strfuncs.h b/include/dap_strfuncs.h index 1786c84e0e98444e282d4dbc4cdab36fb0fee63a..a50775d36763a1cc49a4a41fc2694dfad682ec75 100755 --- a/include/dap_strfuncs.h +++ b/include/dap_strfuncs.h @@ -74,7 +74,10 @@ char* dap_strdown(const char *a_str, ssize_t a_len); char* dap_strreverse(char *a_string); #ifdef _WIN32 -char *strndup(char *str, unsigned long len); +#ifdef HAVE_STRNDUP +#define strndup(s, l) _strndup(s, l) +#endif +char *_strndup(char *str, unsigned long len); #endif #define DAP_USEC_PER_SEC 1000000 diff --git a/src/dap_common.c b/src/dap_common.c index 56bc9fd1627628f4a7886b6f95df5d4209e8eafa..1f6988152caa1b07ac2dbbd197d553f66156a342 100755 --- a/src/dap_common.c +++ b/src/dap_common.c @@ -370,6 +370,32 @@ int dap_time_to_str_rfc822(char * out, size_t out_size_max, time_t t) return ret; } +/** + * @brief Calculate diff of two struct timespec + * @param[in] a_start - first time + * @param[in] a_stop - second time + * @param[out] a_result - diff time, may be NULL + * @return diff time in millisecond + */ +int timespec_diff(struct timespec *a_start, struct timespec *a_stop, struct timespec *a_result) +{ + if(!a_start || !a_stop) + return 0; + if(!a_result) { + struct timespec l_time_tmp = { 0 }; + a_result = &l_time_tmp; + } + if((a_stop->tv_nsec - a_start->tv_nsec) < 0) { + a_result->tv_sec = a_stop->tv_sec - a_start->tv_sec - 1; + a_result->tv_nsec = a_stop->tv_nsec - a_start->tv_nsec + 1000000000; + } else { + a_result->tv_sec = a_stop->tv_sec - a_start->tv_sec; + a_result->tv_nsec = a_stop->tv_nsec - a_start->tv_nsec; + } + + return (a_result->tv_sec * 1000 + a_result->tv_nsec / 1000000); +} + #define BREAK_LATENCY 1 static int breaker_set[2] = { -1, -1 }; diff --git a/src/dap_strfuncs.c b/src/dap_strfuncs.c index eb7b677239fa8d3fa411a2f0fbae19a95d4c0953..29ef7430d33710fd19f1b532537f86625f9e4df6 100755 --- a/src/dap_strfuncs.c +++ b/src/dap_strfuncs.c @@ -690,7 +690,7 @@ char *strptime( char *buff, const char *fmt, struct tm *tm ) { return buff + len; } -char *strndup(char *str, unsigned long len) { +char *_strndup(char *str, unsigned long len) { char *buf = (char*)memchr(str, '\0', len); if (buf) len = buf - str;