# Re: \[RFC PATCH net-next 1/4\] rhashtable: add length helper for rhashtable and rhltable

- From: Eric Dumazet \<hidden\>
- Date: 2023-06-09 15:22:37 
- List: netdev
- Page: https://ratatoskr.run/netdev/2023/06/11263317
- Thread: https://ratatoskr.run/netdev/2023/06/11263311/t.md

```text
On Fri, Jun 9, 2023 at 5:13 PM Pedro Tammela [ref] wrote:
>
> Instead of having users open code the rhashtable length like:
>    atomic_read(&ht->nelems)
>
> Provide a helper for both flavours of rhashtables.
>
> Signed-off-by: Pedro Tammela <redacted>
> ---
>  include/linux/rhashtable.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
> index 5b5357c0bd8c..aac803491916 100644
> --- a/include/linux/rhashtable.h
> +++ b/include/linux/rhashtable.h
> @@ -1283,4 +1283,20 @@ static inline void rhltable_destroy(struct rhltable *hlt)
>         return rhltable_free_and_destroy(hlt, NULL, NULL);
>  }
>
> +/**
> + * rhashtable_len - hash table length
> + * @ht: the hash table
> + *
> + * Returns the number of elements in the hash table
> + */
> +static inline int rhashtable_len(struct rhashtable *ht)
> +{
> +       return atomic_read(&ht->nelems);
> +}
> +
> +static inline int rhltable_len(struct rhltable *hlt)
> +{
> +       return rhashtable_len(&hlt->ht);
> +}
> +

If we want/need these, please add 'const' qualifiers to both

static inline int rhltable_len(const struct rhltable *hlt)
...
```
