summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authoranton <anton@cvs.openbsd.org>2018-04-29 17:26:32 +0000
committeranton <anton@cvs.openbsd.org>2018-04-29 17:26:32 +0000
commit00c3e3eff96c21ed0a1da1236708cd5ceb9326a2 (patch)
tree190c4c1929265e97322c2c0a9c9c67f14dc04dc8 /sys/kern
parent6e8b4c725a0dfa64bf09a5a64a9924697366be73 (diff)
In hash{free,init}(), there's no need to calculate the size of the hash table if
the given number of elements already is a power of 2. ok visa@, "seems like a good plan" deraadt@
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_subr.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/sys/kern/kern_subr.c b/sys/kern/kern_subr.c
index d46a9c410ce..1c333451def 100644
--- a/sys/kern/kern_subr.c
+++ b/sys/kern/kern_subr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_subr.c,v 1.49 2017/02/14 10:31:15 mpi Exp $ */
+/* $OpenBSD: kern_subr.c,v 1.50 2018/04/29 17:26:31 anton Exp $ */
/* $NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $ */
/*
@@ -163,8 +163,11 @@ hashinit(int elements, int type, int flags, u_long *hashmask)
if (elements <= 0)
panic("hashinit: bad cnt");
- for (hashsize = 1; hashsize < elements; hashsize <<= 1)
- continue;
+ if ((elements & (elements - 1)) == 0)
+ hashsize = elements;
+ else
+ for (hashsize = 1; hashsize < elements; hashsize <<= 1)
+ continue;
hashtbl = mallocarray(hashsize, sizeof(*hashtbl), type, flags);
if (hashtbl == NULL)
return NULL;
@@ -182,8 +185,11 @@ hashfree(void *hash, int elements, int type)
if (elements <= 0)
panic("hashfree: bad cnt");
- for (hashsize = 1; hashsize < elements; hashsize <<= 1)
- continue;
+ if ((elements & (elements - 1)) == 0)
+ hashsize = elements;
+ else
+ for (hashsize = 1; hashsize < elements; hashsize <<= 1)
+ continue;
free(hashtbl, type, sizeof(*hashtbl) * hashsize);
}