* [PATCH] Make `hash_long' function work if bits parameter is 0.
@ 2002-12-06 9:33 Miles Bader
2002-12-06 16:37 ` Linus Torvalds
0 siblings, 1 reply; 5+ messages in thread
From: Miles Bader @ 2002-12-06 9:33 UTC (permalink / raw)
To: Linus Torvalds; +Cc: linux-kernel
If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
often is a nop).
This patch just handles that case explicitly.
diff -ruN -X../cludes ../orig/linux-2.5.50-uc0/include/linux/hash.h include/linux/hash.h
--- ../orig/linux-2.5.50-uc0/include/linux/hash.h 2002-09-18 09:59:18.000000000 +0900
+++ include/linux/hash.h 2002-12-06 13:22:00.000000000 +0900
@@ -27,6 +27,9 @@
{
unsigned long hash = val;
+ if (bits == 0)
+ return 0;
+
#if BITS_PER_LONG == 64
/* Sigh, gcc can't optimise this alone like it does for 32 bits. */
unsigned long n = hash;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Make `hash_long' function work if bits parameter is 0.
2002-12-06 9:33 [PATCH] Make `hash_long' function work if bits parameter is 0 Miles Bader
@ 2002-12-06 16:37 ` Linus Torvalds
2002-12-06 17:58 ` Miles Bader
2002-12-08 17:23 ` Rogier Wolff
0 siblings, 2 replies; 5+ messages in thread
From: Linus Torvalds @ 2002-12-06 16:37 UTC (permalink / raw)
To: Miles Bader; +Cc: linux-kernel
On Fri, 6 Dec 2002, Miles Bader wrote:
>
> If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> often is a nop).
I would much rather just add a comment saying that "bits" had better be in
a valid range. There are no valid uses for a 0-bit hash table that I can
see, and undefined behaviour for undefined operations is fine with me.
Linus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Make `hash_long' function work if bits parameter is 0.
2002-12-06 16:37 ` Linus Torvalds
@ 2002-12-06 17:58 ` Miles Bader
2002-12-06 18:16 ` Linus Torvalds
2002-12-08 17:23 ` Rogier Wolff
1 sibling, 1 reply; 5+ messages in thread
From: Miles Bader @ 2002-12-06 17:58 UTC (permalink / raw)
To: Linus Torvalds; +Cc: linux-kernel
On Fri, Dec 06, 2002 at 08:37:26AM -0800, Linus Torvalds wrote:
> > If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> > ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> > often is a nop).
>
> I would much rather just add a comment saying that "bits" had better be in
> a valid range. There are no valid uses for a 0-bit hash table that I can
> see, and undefined behaviour for undefined operations is fine with me.
The reason I sent the patch is because I ran into a case where the return
value _should_ be zero -- on a machine with very little memory (1MB), the
page wait-queue hash-table ends up having only one bucket (it has 256 pages,
and the code tries to make a wait-queue for every 256 pages....). The 0 is
returned by the `wait_table_bits' function in mm/page_alloc.c.
I suppose an alternative in this case is to special-case above calculation to
peg the minimum at 1.
-Miles
--
`...the Soviet Union was sliding in to an economic collapse so comprehensive
that in the end its factories produced not goods but bads: finished products
less valuable than the raw materials they were made from.' [The Economist]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Make `hash_long' function work if bits parameter is 0.
2002-12-06 17:58 ` Miles Bader
@ 2002-12-06 18:16 ` Linus Torvalds
0 siblings, 0 replies; 5+ messages in thread
From: Linus Torvalds @ 2002-12-06 18:16 UTC (permalink / raw)
To: Miles Bader; +Cc: linux-kernel
On Fri, 6 Dec 2002, Miles Bader wrote:
>
> The reason I sent the patch is because I ran into a case where the return
> value _should_ be zero -- on a machine with very little memory (1MB), the
> page wait-queue hash-table ends up having only one bucket (it has 256 pages,
> and the code tries to make a wait-queue for every 256 pages....). The 0 is
> returned by the `wait_table_bits' function in mm/page_alloc.c.
Ahh, ok. Fair enough, but I do have to say that I consider a hash of size
1 to be kind of useless, so even then I think the right approach is:
> I suppose an alternative in this case is to special-case above
> calculation to peg the minimum at 1.
Yup. It needs a special case _somewhere_, and I suspect it's clearer to
just make the special case be where the issue is.
Linus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Make `hash_long' function work if bits parameter is 0.
2002-12-06 16:37 ` Linus Torvalds
2002-12-06 17:58 ` Miles Bader
@ 2002-12-08 17:23 ` Rogier Wolff
1 sibling, 0 replies; 5+ messages in thread
From: Rogier Wolff @ 2002-12-08 17:23 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Miles Bader, linux-kernel
On Fri, Dec 06, 2002 at 08:37:26AM -0800, Linus Torvalds wrote:
> On Fri, 6 Dec 2002, Miles Bader wrote:
> >
> > If the bits parameter of hash_long (in <linux/hash.h>) is 0, then it
> > ends up right-shifting by BITS_PER_LONG, which is undefined in C (and
> > often is a nop).
>
> I would much rather just add a comment saying that "bits" had better be in
> a valid range. There are no valid uses for a 0-bit hash table that I can
> see, and undefined behaviour for undefined operations is fine with me.
Wouldn't it be nice to have a memory-for-speed tradeoff by being able
to set the bits-in-the-hash-table to zero?
Roger.
--
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* The Worlds Ecosystem is a stable system. Stable systems may experience *
* excursions from the stable situation. We are currently in such an *
* excursion: The stable situation does not include humans. ***************
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-12-08 17:15 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-06 9:33 [PATCH] Make `hash_long' function work if bits parameter is 0 Miles Bader
2002-12-06 16:37 ` Linus Torvalds
2002-12-06 17:58 ` Miles Bader
2002-12-06 18:16 ` Linus Torvalds
2002-12-08 17:23 ` Rogier Wolff
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox