Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash table in Linux kernel

Tags:

Does the Linux kernel have a generic hash table implementation for use in kernel code? I know that linked lists, red-black trees, and radix trees are available, but haven't found reference to a generic hash table implementation, although I know hash tables are heavily used in the core kernel.

like image 819
David B. Avatar asked Mar 30 '11 16:03

David B.


People also ask

What is hash table Linux?

A hash table is a data structure that stores one or more key and value pairs. A hash table can store keys of any type. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. Ideally, the hash function will assign each key to a unique bucket.

What is hash table with example?

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

What is a hash table used for?

A hash table is a data structure that you can use to store data in key-value format with direct access to its items in constant time. Hash tables are said to be associative, which means that for each key, data occurs at most once.

What does hash table mean?

Hash tables are a type of data structure in which the address/ index value of the data element is generated from a hash function. This enables very fast data access as the index value behaves as a key for the data value.


1 Answers

At the risk of looking like a reputation whore, let me summarize the answers I've acquired so far.


Kernel 3.7+

A generic implementation was introduced by Sasha Levin in 2012 and merged for the 3.7 kernel.


Older Kernels

The kernel (as of 2.6.38) does not include a generic hash table implementation, but does include some pieces:

  • hlist_*/HLIST_* in list.h are single-pointer-head doubly-linked list structs and macros useful for hash buckets. (answer below from adobriyan)
  • hash.h includes hashing routines for ints, longs, and pointers. This article by Chuck Lever studies the performance of these routines.
  • See pid_hash in pid.c for an example constructed from these primitives.

uthash is a generic hash table for C implemented as macros defined in a single header file. This solution may be appropriate for many third-party kernel modules (e.g., device drivers). However, reliance on uthash might impede mainlining of a module.

like image 182
David B. Avatar answered Sep 20 '22 21:09

David B.