\doxysection{hashtable.\+h}
\label{hashtable_8h_source}\index{include/osmocom/core/hashtable.h@{include/osmocom/core/hashtable.h}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{1 \textcolor{comment}{/* SPDX-\/License-\/Identifier: GPL-\/2.0 */}}
\DoxyCodeLine{2 \textcolor{comment}{/*}}
\DoxyCodeLine{3 \textcolor{comment}{ * Statically sized hash table implementation}}
\DoxyCodeLine{4 \textcolor{comment}{ * (C) 2012  Sasha Levin <levinsasha928@gmail.com>}}
\DoxyCodeLine{5 \textcolor{comment}{ */}}
\DoxyCodeLine{6 }
\DoxyCodeLine{7 \textcolor{preprocessor}{\#pragma once}}
\DoxyCodeLine{8 }
\DoxyCodeLine{9 \textcolor{preprocessor}{\#include <osmocom/core/linuxlist.h>}}
\DoxyCodeLine{10 \textcolor{preprocessor}{\#include <osmocom/core/hash.h>}}
\DoxyCodeLine{11 }
\DoxyCodeLine{12 \textcolor{preprocessor}{\#define DEFINE\_HASHTABLE(name, bits)                                            \(\backslash\)}}
\DoxyCodeLine{13 \textcolor{preprocessor}{        struct hlist\_head name[1 << (bits)] =                                   \(\backslash\)}}
\DoxyCodeLine{14 \textcolor{preprocessor}{                        \{ [0 ... ((1 << (bits)) -\/ 1)] = HLIST\_HEAD\_INIT \}}}
\DoxyCodeLine{15 }
\DoxyCodeLine{16 \textcolor{preprocessor}{\#define DECLARE\_HASHTABLE(name, bits)                                           \(\backslash\)}}
\DoxyCodeLine{17 \textcolor{preprocessor}{        struct hlist\_head name[1 << (bits)]}}
\DoxyCodeLine{18 }
\DoxyCodeLine{19 \textcolor{preprocessor}{\#define HASH\_SIZE(name) (ARRAY\_SIZE(name))}}
\DoxyCodeLine{20 \textcolor{preprocessor}{\#define HASH\_BITS(name) ilog2(HASH\_SIZE(name))}}
\DoxyCodeLine{21 }
\DoxyCodeLine{22 \textcolor{comment}{/* Use hash\_32 when possible to allow for fast 32bit hashing in 64bit kernels. */}}
\DoxyCodeLine{23 \textcolor{preprocessor}{\#define hash\_min(val, bits)                                                     \(\backslash\)}}
\DoxyCodeLine{24 \textcolor{preprocessor}{        (sizeof(val) <= 4 ? hash\_32(val, bits) : hash\_long(val, bits))}}
\DoxyCodeLine{25 }
\DoxyCodeLine{26 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} \_\_hash\_init(\textcolor{keyword}{struct} hlist\_head *ht, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} sz)}
\DoxyCodeLine{27 \{}
\DoxyCodeLine{28         \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{29 }
\DoxyCodeLine{30         \textcolor{keywordflow}{for} (i = 0; i < sz; i++)}
\DoxyCodeLine{31                 INIT\_HLIST\_HEAD(\&ht[i]);}
\DoxyCodeLine{32 \}}
\DoxyCodeLine{33 }
\DoxyCodeLine{44 \textcolor{preprocessor}{\#define hash\_init(hashtable) \_\_hash\_init(hashtable, HASH\_SIZE(hashtable))}}
\DoxyCodeLine{45 }
\DoxyCodeLine{52 \textcolor{preprocessor}{\#define hash\_add(hashtable, node, key)                                          \(\backslash\)}}
\DoxyCodeLine{53 \textcolor{preprocessor}{        hlist\_add\_head(node, \&hashtable[hash\_min(key, HASH\_BITS(hashtable))])}}
\DoxyCodeLine{54 }
\DoxyCodeLine{59 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{bool} hash\_hashed(\textcolor{keyword}{struct} hlist\_node *node)}
\DoxyCodeLine{60 \{}
\DoxyCodeLine{61         \textcolor{keywordflow}{return} !hlist\_unhashed(node);}
\DoxyCodeLine{62 \}}
\DoxyCodeLine{63 }
\DoxyCodeLine{64 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{bool} \_\_hash\_empty(\textcolor{keyword}{struct} hlist\_head *ht, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} sz)}
\DoxyCodeLine{65 \{}
\DoxyCodeLine{66         \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{67 }
\DoxyCodeLine{68         \textcolor{keywordflow}{for} (i = 0; i < sz; i++)}
\DoxyCodeLine{69                 \textcolor{keywordflow}{if} (!hlist\_empty(\&ht[i]))}
\DoxyCodeLine{70                         \textcolor{keywordflow}{return} \textcolor{keyword}{false};}
\DoxyCodeLine{71 }
\DoxyCodeLine{72         \textcolor{keywordflow}{return} \textcolor{keyword}{true};}
\DoxyCodeLine{73 \}}
\DoxyCodeLine{74 }
\DoxyCodeLine{82 \textcolor{preprocessor}{\#define hash\_empty(hashtable) \_\_hash\_empty(hashtable, HASH\_SIZE(hashtable))}}
\DoxyCodeLine{83 }
\DoxyCodeLine{88 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} hash\_del(\textcolor{keyword}{struct} hlist\_node *node)}
\DoxyCodeLine{89 \{}
\DoxyCodeLine{90         hlist\_del\_init(node);}
\DoxyCodeLine{91 \}}
\DoxyCodeLine{92 }
\DoxyCodeLine{100 \textcolor{preprocessor}{\#define hash\_for\_each(name, bkt, obj, member)                           \(\backslash\)}}
\DoxyCodeLine{101 \textcolor{preprocessor}{        for ((bkt) = 0, obj = NULL; obj == NULL \&\& (bkt) < HASH\_SIZE(name);\(\backslash\)}}
\DoxyCodeLine{102 \textcolor{preprocessor}{                        (bkt)++)\(\backslash\)}}
\DoxyCodeLine{103 \textcolor{preprocessor}{                hlist\_for\_each\_entry(obj, \&name[bkt], member)}}
\DoxyCodeLine{104 }
\DoxyCodeLine{114 \textcolor{preprocessor}{\#define hash\_for\_each\_safe(name, bkt, tmp, obj, member)                 \(\backslash\)}}
\DoxyCodeLine{115 \textcolor{preprocessor}{        for ((bkt) = 0, obj = NULL; obj == NULL \&\& (bkt) < HASH\_SIZE(name);\(\backslash\)}}
\DoxyCodeLine{116 \textcolor{preprocessor}{                        (bkt)++)\(\backslash\)}}
\DoxyCodeLine{117 \textcolor{preprocessor}{                hlist\_for\_each\_entry\_safe(obj, tmp, \&name[bkt], member)}}
\DoxyCodeLine{118 }
\DoxyCodeLine{127 \textcolor{preprocessor}{\#define hash\_for\_each\_possible(name, obj, member, key)                  \(\backslash\)}}
\DoxyCodeLine{128 \textcolor{preprocessor}{        hlist\_for\_each\_entry(obj, \&name[hash\_min(key, HASH\_BITS(name))], member)}}
\DoxyCodeLine{129 }
\DoxyCodeLine{139 \textcolor{preprocessor}{\#define hash\_for\_each\_possible\_safe(name, obj, tmp, member, key)        \(\backslash\)}}
\DoxyCodeLine{140 \textcolor{preprocessor}{        hlist\_for\_each\_entry\_safe(obj, tmp,\(\backslash\)}}
\DoxyCodeLine{141 \textcolor{preprocessor}{                \&name[hash\_min(key, HASH\_BITS(name))], member)}}

\end{DoxyCode}
