HASH(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

πŸ’– 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. buf is where the data lives, len is the length in bytes. and hash is either an existing hash you're extending, or you pass the initial value HASHINIT to start a brand new hash. fresh start energy ✨
  • hash32_str() β€” hashes a NUL terminated string passed in buf, with the initial hash value given in hash. simple, clean, ate.
  • hash32_strn() β€” literally hash32_str() but it also takes a len argument, which is the maximal length of the expected string. boundaries, we love them πŸ’…
  • hash32_stre() and hash32_strne() β€” helper functions the kernel uses to hash pathname components. they have an extra termination condition: they stop when they find a character given by end in the string being hashed. and if the argument ep is not NULL, 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 as hash32_buf(), but gives you a more advanced hashing algorithm with better distribution. glow-up fr fr πŸ”₯
  • jenkins_hash32() β€” uses the same hashing algorithm as jenkins_hash(), but works only on uint32_t sized arrays, which makes it simpler and faster. it accepts an array of uint32_t values as its first argument and the size of this array as the second argument. efficient icon πŸ‘‘
  • murmur3_32_hash() and murmur3_32_hash32() β€” similar to jenkins_hash() and jenkins_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 hash32 functions were written by Tobias Weingartner.
  • the jenkins_hash functions were written by Bob Jenkins.
  • the murmur3_32_hash functions 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