HASH(9)
π HASH(9) β general kernel hashing functions π
okay bestie, welcome to the kernel's little hash factory β¨ this one lives in the Kernel Developer's Manual, and it's genuinely iconic for anyone doing lookup tables and pathname hashing in kernel land. let's get into it π«Ά
πΈ NAME πΈ
hash, hash32, hash32_buf, hash32_str, hash32_strn, hash32_stre, hash32_strne, jenkins_hash, jenkins_hash32, murmur3_32_hash, murmur3_32_hash32 β the general kernel hashing functions, all here to serve cunt and produce 32-bit hash values π
β¨ SYNOPSIS β¨
#include <sys/hash.h>
uint32_t
hash32_buf(const void *buf, size_t len, uint32_t hash);
uint32_t
hash32_str(const void *buf, uint32_t hash);
uint32_t
hash32_strn(const void *buf, size_t len, uint32_t hash);
uint32_t
hash32_stre(const void *buf, int end, const char **ep, uint32_t hash);
uint32_t
hash32_strne(const void *buf, size_t len, int end, const char **ep,
uint32_t hash);
uint32_t
jenkins_hash(const void *buf, size_t len, uint32_t hash);
uint32_t
jenkins_hash32(const uint32_t *buf, size_t count, uint32_t hash);
uint32_t
murmur3_32_hash(const void *buf, size_t len, uint32_t hash);
uint32_t
murmur3_32_hash32(const uint32_t *buf, size_t count, uint32_t hash);
π¦ DESCRIPTION π¦
so the hash32() functions exist to give you a consistent, general interface to a decent hashing algorithm inside the kernel β lowkey the whole point π these functions work on ASCII strings terminated by NUL, and also on blocks of memory. versatile queen.
a len argument is the length of the buffer in bytes. a count argument is the length of the buffer in 32-bit words. don't mix them up bestie, i'm begging π
hash32_buf()β the general buffer hashing function.bufis where the data lives,lenis the length in bytes. andhashis either an existing hash you're extending, or you pass the initial valueHASHINITto start a brand new hash. fresh start energy β¨hash32_str()β hashes a NUL terminated string passed inbuf, with the initial hash value given inhash. simple, clean, ate.hash32_strn()β literallyhash32_str()but it also takes alenargument, which is the maximal length of the expected string. boundaries, we love them πhash32_stre()andhash32_strne()β helper functions the kernel uses to hash pathname components. they have an extra termination condition: they stop when they find a character given byendin the string being hashed. and if the argumentepis notNULL, it gets set to the point in the buffer where the hash function terminated hashing. very "here's exactly where i stopped" of them πjenkins_hash()β same semantics ashash32_buf(), but gives you a more advanced hashing algorithm with better distribution. glow-up fr fr π₯jenkins_hash32()β uses the same hashing algorithm asjenkins_hash(), but works only onuint32_tsized arrays, which makes it simpler and faster. it accepts an array ofuint32_tvalues as its first argument and the size of this array as the second argument. efficient icon πmurmur3_32_hash()andmurmur3_32_hash32()β similar tojenkins_hash()andjenkins_hash32(), but they implement the 32-bit version of MurmurHash3. different algorithm, same slay π
π RETURN VALUES π
the hash32() functions return a 32 bit hash value of the buffer or string. that's it, that's the tea β
π EXAMPLES π
like, here's a whole little sample so you can see it in the wild:
LIST_HEAD(head, cache) *hashtbl = NULL;
u_long mask = 0;
void
sample_init(void)
{
hashtbl = hashinit(numwanted, type, flags, &mask);
}
void
sample_use(char *str, int len)
{
uint32_t hash;
hash = hash32_str(str, HASHINIT);
hash = hash32_buf(&len, sizeof(len), hash);
hashtbl[hash & mask] = len;
}
π§ SEE ALSO π§
go read those too, iykyk π
π LIMITATIONS π
bestie, listen. the hash32() functions are only 32 bit functions. they will prove to give poor 64 bit performance, especially for the top 32 bits. at the current time, this is not seen as a great limitation, because these hash values are usually used to index into an array. but should these hash values be used for other means, this limitation should be revisited. noted, respected, moving on π«‘
π HISTORY π
the hash functions first appeared in NetBSD 1.6. the current implementation of hash32 functions was first committed to OpenBSD 3.2, and later imported to FreeBSD 6.1. the jenkins_hash functions were added in FreeBSD 10.0. the murmur3_32_hash functions were added in FreeBSD 10.1. a whole timeline, honestly β
π AUTHORS π
- the
hash32functions were written by Tobias Weingartner. - the
jenkins_hashfunctions were written by Bob Jenkins. - the
murmur3_32_hashfunctions were written by Dag-Erling SmΓΈrgrav<des@FreeBSD.org>.
absolute legends, all of them π
okay that's the whole page bestie, no cap π this edition comes from Debian, dated June 30, 2015, and it's the HASH(9) page from the Kernel Developer's Manual β mwah, go hash something responsibly π
β¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2001 Tobias Weingartner
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms