From: Matthew Dillon Date: Sun, 20 Dec 2009 08:13:38 +0000 (-0800) Subject: kernel - add hashinit_ext() and phashinit_ext() X-Git-Tag: v2.7.1~272 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/c02197ac4398205f5d55cbb345e14c21e70d8c3b kernel - add hashinit_ext() and phashinit_ext() * Add hashinit_ext() and phashinit_ext(), extended versions of hashinit() and phashinit() which take a structure size and zero the array instead of assuming it is only an array of LIST_HEAD's. --- diff --git a/sys/kern/kern_subr.c b/sys/kern/kern_subr.c index 5968f852cc..a8a2b8be51 100644 --- a/sys/kern/kern_subr.c +++ b/sys/kern/kern_subr.c @@ -268,6 +268,28 @@ hashinit(int elements, struct malloc_type *type, u_long *hashmask) return (hashtbl); } +/* + * This is a newer version which allocates a hash table of structures. + * + * The returned array will be zero'd. The caller is responsible for + * initializing the structures. + */ +void * +hashinit_ext(int elements, size_t size, struct malloc_type *type, + u_long *hashmask) +{ + long hashsize; + void *hashtbl; + + if (elements <= 0) + panic("hashinit: bad elements"); + for (hashsize = 2; hashsize < elements; hashsize <<= 1) + continue; + hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO); + *hashmask = hashsize - 1; + return (hashtbl); +} + static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039, 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653, 7159, 7673, 8191, 12281, 16381, 24571, 32749 }; @@ -299,6 +321,35 @@ phashinit(int elements, struct malloc_type *type, u_long *nentries) return (hashtbl); } +/* + * This is a newer version which allocates a hash table of structures + * in a prime-number size. + * + * The returned array will be zero'd. The caller is responsible for + * initializing the structures. + */ +void * +phashinit_ext(int elements, size_t size, struct malloc_type *type, + u_long *nentries) +{ + long hashsize; + void *hashtbl; + int i; + + if (elements <= 0) + panic("phashinit: bad elements"); + for (i = 1, hashsize = primes[1]; hashsize <= elements;) { + i++; + if (i == NPRIMES) + break; + hashsize = primes[i]; + } + hashsize = primes[i - 1]; + hashtbl = kmalloc((size_t)hashsize * size, type, M_WAITOK | M_ZERO); + *nentries = hashsize; + return (hashtbl); +} + /* * Copyin an iovec. If the iovec array fits, use the preallocated small * iovec structure. If it is too big, dynamically allocate an iovec array diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 4b04fc46d7..a9fddd49fa 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -151,6 +151,10 @@ int seltrue (cdev_t dev, int which); int ureadc (int, struct uio *); void *hashinit (int count, struct malloc_type *type, u_long *hashmask); void *phashinit (int count, struct malloc_type *type, u_long *nentries); +void *hashinit_ext (int count, size_t size, + struct malloc_type *type, u_long *hashmask); +void *phashinit_ext (int count, size_t size, + struct malloc_type *type, u_long *nentries); int cpu_sanitize_frame (struct trapframe *); int cpu_sanitize_tls (struct savetls *);