diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-07-25 17:44:23 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-07-25 17:44:23 -0400 |
commit | 9550beda61ea74a2a9738e9c10423fa817b7b897 (patch) | |
tree | 8975561b90ab99f1349ffd1eae06173e1e4e7f71 /src | |
parent | 4dadadd0015b6efb31869cca52d6bae36762c36e (diff) | |
parent | 7b1d46f39d9a21b77931b857d28a2a2fe7b284d0 (diff) | |
download | rneovim-9550beda61ea74a2a9738e9c10423fa817b7b897.tar.gz rneovim-9550beda61ea74a2a9738e9c10423fa817b7b897.tar.bz2 rneovim-9550beda61ea74a2a9738e9c10423fa817b7b897.zip |
Merge pull request #975 from aktau/remove-gettimeofday
remove gettimeofday() usage
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 12 | ||||
-rw-r--r-- | src/nvim/sha256.c | 21 | ||||
-rw-r--r-- | src/nvim/term.c | 28 | ||||
-rw-r--r-- | src/nvim/term.h | 2 |
4 files changed, 26 insertions, 37 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 77f327c124..069715ffee 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6490,6 +6490,7 @@ static struct fst { {"settabvar", 3, 3, f_settabvar}, {"settabwinvar", 4, 4, f_settabwinvar}, {"setwinvar", 3, 3, f_setwinvar}, + {"sha256", 1, 1, f_sha256}, {"shellescape", 1, 2, f_shellescape}, {"shiftwidth", 0, 0, f_shiftwidth}, {"simplify", 1, 1, f_simplify}, @@ -13102,6 +13103,17 @@ static void setwinvar(typval_T *argvars, typval_T *rettv, int off) } } +/// f_sha256 - sha256({string}) function +static void f_sha256(typval_T *argvars, typval_T *rettv) +{ + char_u *p = get_tv_string(&argvars[0]); + const char_u *hash = sha256_bytes(p, (int) STRLEN(p) , NULL, 0); + + // make a copy of the hash (sha256_bytes returns a static buffer) + rettv->vval.v_string = (char_u *) xstrdup((char *) hash); + rettv->v_type = VAR_STRING; +} + /* * "shellescape({string})" function */ diff --git a/src/nvim/sha256.c b/src/nvim/sha256.c index 9877a82fa1..b11ee2293c 100644 --- a/src/nvim/sha256.c +++ b/src/nvim/sha256.c @@ -15,10 +15,10 @@ #include <inttypes.h> #include <string.h> +#include "nvim/os/time.h" #include "nvim/vim.h" #include "nvim/sha256.h" - #ifdef INCLUDE_GENERATED_DECLARATIONS # include "sha256.c.generated.h" #endif @@ -350,21 +350,6 @@ int sha256_self_test(void) return failures > 0 ? FAIL : OK; } -static unsigned int get_some_time(void) -{ -#ifdef HAVE_GETTIMEOFDAY - struct timeval tv; - - // Using usec makes it less predictable. - gettimeofday(&tv, NULL); - return (unsigned int) (tv.tv_sec + tv.tv_usec); - -#else // ifdef HAVE_GETTIMEOFDAY - return (unsigned int) time(NULL); - -#endif // ifdef HAVE_GETTIMEOFDAY -} - /// Fill "header[header_len]" with random_data. /// Also "salt[salt_len]" when "salt" is not NULL. /// @@ -378,11 +363,11 @@ void sha2_seed(char_u *header, int header_len, char_u *salt, int salt_len) char_u sha256sum[32]; context_sha256_T ctx; - srand(get_some_time()); + srand((unsigned int) os_hrtime()); int i; for (i = 0; i < (int) sizeof(random_data) - 1; i++) { - random_data[i] = (char_u) ((get_some_time() ^ rand()) & 0xff); + random_data[i] = (char_u) ((os_hrtime() ^ rand()) & 0xff); } sha256_start(&ctx); sha256_update(&ctx, (char_u *) random_data, sizeof(random_data)); diff --git a/src/nvim/term.c b/src/nvim/term.c index 4ed438b282..f4bc2df2fb 100644 --- a/src/nvim/term.c +++ b/src/nvim/term.c @@ -3179,14 +3179,6 @@ int check_termcode(int max_offset, char_u *buf, int bufsize, int *buflen) static int held_button = MOUSE_RELEASE; static int orig_num_clicks = 1; static int orig_mouse_code = 0x0; -# ifdef CHECK_DOUBLE_CLICK - static int orig_mouse_col = 0; - static int orig_mouse_row = 0; - static struct timeval orig_mouse_time = {0, 0}; - /* time of previous mouse click */ - struct timeval mouse_time; /* time of current mouse click */ - long timediff; /* elapsed time in msec */ -# endif int cpo_koffset; cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL); @@ -3955,17 +3947,17 @@ int check_termcode(int max_offset, char_u *buf, int bufsize, int *buflen) } else if (wheel_code == 0) { # ifdef CHECK_DOUBLE_CLICK { - /* - * Compute the time elapsed since the previous mouse click. - */ - gettimeofday(&mouse_time, NULL); - timediff = (mouse_time.tv_usec - - orig_mouse_time.tv_usec) / 1000; - if (timediff < 0) - --orig_mouse_time.tv_sec; - timediff += (mouse_time.tv_sec - - orig_mouse_time.tv_sec) * 1000; + static int orig_mouse_col = 0; + static int orig_mouse_row = 0; + + static uint64_t orig_mouse_time = 0; // time of previous mouse click + uint64_t mouse_time = os_hrtime(); // time of current mouse click + + // compute the time elapsed since the previous mouse click and + // convert it from ns to ms because p_mouset is stored as ms + long timediff = (long) (mouse_time - orig_mouse_time) / 1E6; orig_mouse_time = mouse_time; + if (mouse_code == orig_mouse_code && timediff < p_mouset && orig_num_clicks != 4 diff --git a/src/nvim/term.h b/src/nvim/term.h index 52c3efcac5..17154b8c26 100644 --- a/src/nvim/term.h +++ b/src/nvim/term.h @@ -52,7 +52,7 @@ * 128 = 16384 columns, now it's reduced to 10000. */ #define MOUSE_COLOFF 10000 -#if defined(UNIX) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H) +#if defined(UNIX) # define CHECK_DOUBLE_CLICK 1 /* Checking for double clicks ourselves. */ #endif |