From 975893b229072aa1b5565cc1a73987fa83ed5b21 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 23 Feb 2019 13:47:52 -0800 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20assume=20timersub=20and=20gettim?= =?utf8?q?eofday?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit POSIX does not specify timersub, and marks gettimeofday as obsolescent. Avoid porting problems by using timespec.h functions instead. * src/editfns.c: Include systime.h instead of sys/time.h. (EXTRA_CONTEXT_FIELDS): Replace start and max_secs with time_limit. All uses changed. This removes the need to call gettimeofday or timersub. * src/term.c (timeval_to_Time): Remove. Replace all uses with ... (current_Time): ... this new function, removing the need to call gettimeofday. --- src/editfns.c | 27 ++++++++++++++++----------- src/term.c | 20 +++++++------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/editfns.c b/src/editfns.c index 8f21f8a677e..b349bd59a25 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -20,7 +20,6 @@ along with GNU Emacs. If not, see . */ #include #include -#include #include #ifdef HAVE_PWD_H @@ -48,6 +47,7 @@ along with GNU Emacs. If not, see . */ #include "composite.h" #include "intervals.h" #include "ptr-bounds.h" +#include "systime.h" #include "character.h" #include "buffer.h" #include "window.h" @@ -1935,8 +1935,7 @@ static unsigned short rbc_quitcounter; or inserted. */ \ unsigned char *deletions; \ unsigned char *insertions; \ - struct timeval start; \ - double max_secs; \ + struct timespec time_limit; \ unsigned int early_abort_tests; #define NOTE_DELETE(ctx, xoff) set_bit ((ctx)->deletions, (xoff)) @@ -2037,6 +2036,17 @@ nil. */) else CHECK_FIXNUM (max_costs); + struct timespec time_limit = make_timespec (0, -1); + if (!NILP (max_secs)) + { + struct timespec + tlim = timespec_add (current_timespec (), + lisp_time_argument (max_secs)), + tmax = make_timespec (TYPE_MAXIMUM (time_t), TIMESPEC_HZ - 1); + if (timespec_cmp (tlim, tmax) < 0) + time_limit = tlim; + } + /* Micro-optimization: Casting to size_t generates much better code. */ ptrdiff_t del_bytes = (size_t) size_a / CHAR_BIT + 1; @@ -2054,13 +2064,12 @@ nil. */) .bdiag = buffer + diags + size_b + 1, .heuristic = true, .too_expensive = XFIXNUM (max_costs), - .max_secs = FLOATP (max_secs) ? XFLOAT_DATA (max_secs) : -1.0, + .time_limit = time_limit, .early_abort_tests = 0 }; memclear (ctx.deletions, del_bytes); memclear (ctx.insertions, ins_bytes); - gettimeofday (&ctx.start, NULL); /* compareseq requires indices to be zero-based. We add BEGV back later. */ bool early_abort = compareseq (0, size_a, 0, size_b, false, &ctx); @@ -2213,13 +2222,9 @@ buffer_chars_equal (struct context *ctx, static bool compareseq_early_abort (struct context *ctx) { - if (ctx->max_secs < 0.0) + if (ctx->time_limit.tv_nsec < 0) return false; - - struct timeval now, diff; - gettimeofday (&now, NULL); - timersub (&now, &ctx->start, &diff); - return diff.tv_sec + diff.tv_usec / 1000000.0 > ctx->max_secs; + return timespec_cmp (ctx->time_limit, current_timespec ()) < 0; } diff --git a/src/term.c b/src/term.c index 7255f561e2a..60ee8619484 100644 --- a/src/term.c +++ b/src/term.c @@ -2435,15 +2435,14 @@ term_mouse_movement (struct frame *frame, Gpm_Event *event) return 0; } -/* Return the Time that corresponds to T. Wrap around on overflow. */ +/* Return the current time, as a Time value. Wrap around on overflow. */ static Time -timeval_to_Time (struct timeval const *t) +current_Time (void) { - Time s_1000, ms; - - s_1000 = t->tv_sec; + struct timespec now = current_timespec (); + Time s_1000 = now.tv_sec; s_1000 *= 1000; - ms = t->tv_usec / 1000; + Time ms = now.tv_nsec / 1000000; return s_1000 + ms; } @@ -2465,8 +2464,6 @@ term_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window, enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y, Time *timeptr) { - struct timeval now; - *fp = SELECTED_FRAME (); (*fp)->mouse_moved = 0; @@ -2475,8 +2472,7 @@ term_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window, XSETINT (*x, last_mouse_x); XSETINT (*y, last_mouse_y); - gettimeofday(&now, 0); - *timeptr = timeval_to_Time (&now); + *timeptr = current_Time (); } /* Prepare a mouse-event in *RESULT for placement in the input queue. @@ -2488,7 +2484,6 @@ static Lisp_Object term_mouse_click (struct input_event *result, Gpm_Event *event, struct frame *f) { - struct timeval now; int i, j; result->kind = GPM_CLICK_EVENT; @@ -2499,8 +2494,7 @@ term_mouse_click (struct input_event *result, Gpm_Event *event, break; } } - gettimeofday(&now, 0); - result->timestamp = timeval_to_Time (&now); + result->timestamp = current_Time (); if (event->type & GPM_UP) result->modifiers = up_modifier; -- 2.39.5