Don’t assume timersub and gettimeofday

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.
This commit is contained in:
Paul Eggert 2019-02-23 13:47:52 -08:00
parent e96923c188
commit 975893b229
2 changed files with 23 additions and 24 deletions

View file

@ -20,7 +20,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#ifdef HAVE_PWD_H
@ -48,6 +47,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#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;
}

View file

@ -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;