From 2f30ecd05f7e5b9f78f256f75677530c501e5a6d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 27 Apr 2011 22:15:35 -0700 Subject: [PATCH] * lread.c (hash_string): Use size_t, not int, for hash computation. Normally we prefer signed values; but hashing is special, because it's better to use unsigned division on hash table sizes so that the remainder is nonnegative. Also, size_t is the natural width for hashing into memory. The previous code used 'int', which doesn't retain enough info to hash well into very large tables. (oblookup, oblookup_last_bucket_number, Funintern): Likewise. --- src/ChangeLog | 8 ++++++++ src/lread.c | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 52b7f323cd3..2bda5ffa46f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,13 @@ 2011-04-28 Paul Eggert + * lread.c (hash_string): Use size_t, not int, for hash computation. + Normally we prefer signed values; but hashing is special, because + it's better to use unsigned division on hash table sizes so that + the remainder is nonnegative. Also, size_t is the natural width + for hashing into memory. The previous code used 'int', which doesn't + retain enough info to hash well into very large tables. + (oblookup, oblookup_last_bucket_number, Funintern): Likewise. + * dbusbind.c: Don't possibly lose pointer info when converting. (xd_remove_watch, Fdbus_init_bus, xd_read_queued_messages): Use XPNTR rather than XHASH, so that the high-order bits of diff --git a/src/lread.c b/src/lread.c index 7ffc98b254f..2c8c3acd56a 100644 --- a/src/lread.c +++ b/src/lread.c @@ -3611,9 +3611,9 @@ static Lisp_Object initial_obarray; /* oblookup stores the bucket number here, for the sake of Funintern. */ -static int oblookup_last_bucket_number; +static size_t oblookup_last_bucket_number; -static int hash_string (const char *ptr, int len); +static size_t hash_string (const char *ptr, size_t len); /* Get an error if OBARRAY is not an obarray. If it is one, return it. */ @@ -3755,7 +3755,7 @@ OBARRAY defaults to the value of the variable `obarray'. */) (Lisp_Object name, Lisp_Object obarray) { register Lisp_Object string, tem; - int hash; + size_t hash; if (NILP (obarray)) obarray = Vobarray; obarray = check_obarray (obarray); @@ -3824,8 +3824,8 @@ OBARRAY defaults to the value of the variable `obarray'. */) Lisp_Object oblookup (Lisp_Object obarray, register const char *ptr, EMACS_INT size, EMACS_INT size_byte) { - int hash; - int obsize; + size_t hash; + size_t obsize; register Lisp_Object tail; Lisp_Object bucket, tem; @@ -3858,21 +3858,21 @@ oblookup (Lisp_Object obarray, register const char *ptr, EMACS_INT size, EMACS_I return tem; } -static int -hash_string (const char *ptr, int len) +static size_t +hash_string (const char *ptr, size_t len) { register const char *p = ptr; register const char *end = p + len; register unsigned char c; - register int hash = 0; + register size_t hash = 0; while (p != end) { c = *p++; if (c >= 0140) c -= 40; - hash = ((hash<<3) + (hash>>28) + c); + hash = (hash << 3) + (hash >> (CHAR_BIT * sizeof hash - 4)) + c; } - return hash & 07777777777; + return hash; } void -- 2.39.2