Index: sys/kern/subr_hash.c =================================================================== --- sys/kern/subr_hash.c +++ sys/kern/subr_hash.c @@ -104,16 +104,21 @@ #define NPRIMES (sizeof(primes) / sizeof(primes[0])) /* - * 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,28 @@ hashsize = primes[i]; } hashsize = primes[i - 1]; - hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); + + if (flags & HASH_NOWAIT) + m_flags = M_NOWAIT; + else + m_flags = 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)); +} Index: sys/sys/systm.h =================================================================== --- sys/sys/systm.h +++ sys/sys/systm.h @@ -189,6 +189,8 @@ #define HASH_WAITOK 0x00000002 void *phashinit(int count, struct malloc_type *type, u_long *nentries); +void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries, + int flags /* HASH_{NOWAIT,WAITOK} */); void g_waitidle(void); void panic(const char *, ...) __dead2 __printflike(1, 2);