xref: /aosp_15_r20/external/trace-cmd/lib/trace-cmd/include/private/trace-hash.h (revision 58e6ee5f017f6a8912852c892d18457e4bafb554)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <[email protected]>
4  *
5  */
6 #ifndef _TRACE_HASH_H
7 #define _TRACE_HASH_H
8 
9 struct trace_hash_item {
10 	struct trace_hash_item	*next;
11 	struct trace_hash_item	*prev;
12 	unsigned long long	key;
13 };
14 
15 struct trace_hash {
16 	struct trace_hash_item	**buckets;
17 	int			nr_buckets;
18 	int			power;
19 };
20 
21 int trace_hash_init(struct trace_hash *hash, int buckets);
22 void trace_hash_free(struct trace_hash *hash);
23 int trace_hash_add(struct trace_hash *hash, struct trace_hash_item *item);
24 int trace_hash_empty(struct trace_hash *hash);
25 
trace_hash_del(struct trace_hash_item * item)26 static inline void trace_hash_del(struct trace_hash_item *item)
27 {
28 	struct trace_hash_item *prev = item->prev;
29 
30 	prev->next = item->next;
31 	if (item->next)
32 		item->next->prev = prev;
33 }
34 
35 #define trace_hash_for_each_bucket(bucket, hash)			\
36 	for (bucket = (hash)->buckets;					\
37 	     (bucket) < (hash)->buckets + (hash)->nr_buckets; (bucket)++)
38 
39 #define trace_hash_for_each_item(item, bucket)				\
40 	for ((item = *(bucket)); item; item = (item)->next)
41 
42 #define trace_hash_for_each_item_safe(item, n, bucket)		\
43 	for ((item = *(bucket)), n = item ? item->next : NULL; item; \
44 	     item = n, n = item ? (item)->next : NULL)
45 
46 #define trace_hash_while_item(item, bucket)	\
47 	while ((item = *(bucket)))
48 
49 typedef int (*trace_hash_func)(struct trace_hash_item *item, void *data);
50 
51 struct trace_hash_item *
52 trace_hash_find(struct trace_hash *hash, unsigned long long key,
53 		trace_hash_func match, void *data);
54 
55 #endif /* _TRACE_HASH_H */
56