summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/kern/subr_hash.c')
-rw-r--r--sys/kern/subr_hash.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/sys/kern/subr_hash.c b/sys/kern/subr_hash.c
index 915f3f7..4d9249f 100644
--- a/sys/kern/subr_hash.c
+++ b/sys/kern/subr_hash.c
@@ -104,16 +104,21 @@ static const int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531,
#define NPRIMES nitems(primes)
/*
- * General routine to allocate a prime number sized hash table.
+ * General routine to allocate a prime number sized hash table with control of
+ * memory flags.
*/
void *
-phashinit(int elements, struct malloc_type *type, u_long *nentries)
+phashinit_flags(int elements, struct malloc_type *type, u_long *nentries, int flags)
{
long hashsize;
LIST_HEAD(generic, generic) *hashtbl;
- int i;
+ int i, m_flags;
KASSERT(elements > 0, ("%s: bad elements", __func__));
+ /* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */
+ KASSERT((flags & HASH_WAITOK) ^ (flags & HASH_NOWAIT),
+ ("Bad flags (0x%x) passed to phashinit_flags", flags));
+
for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
i++;
if (i == NPRIMES)
@@ -121,9 +126,25 @@ phashinit(int elements, struct malloc_type *type, u_long *nentries)
hashsize = primes[i];
}
hashsize = primes[i - 1];
- hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
+
+ m_flags = (flags & HASH_NOWAIT) ? M_NOWAIT : M_WAITOK;
+ hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, m_flags);
+ if (hashtbl == NULL)
+ return (NULL);
+
for (i = 0; i < hashsize; i++)
LIST_INIT(&hashtbl[i]);
*nentries = hashsize;
return (hashtbl);
}
+
+/*
+ * Allocate and initialize a prime number sized hash table with default flag:
+ * may sleep.
+ */
+void *
+phashinit(int elements, struct malloc_type *type, u_long *nentries)
+{
+
+ return (phashinit_flags(elements, type, nentries, HASH_WAITOK));
+}
OpenPOWER on IntegriCloud