1 /* SPDX-License-Identifier: GPL-2.0 */
2 #define __BTREE_TP(pfx, type, sfx)	pfx ## type ## sfx
3 #define _BTREE_TP(pfx, type, sfx)	__BTREE_TP(pfx, type, sfx)
4 #define BTREE_TP(pfx)			_BTREE_TP(pfx, BTREE_TYPE_SUFFIX,)
5 #define BTREE_FN(name)			BTREE_TP(btree_ ## name)
6 #define BTREE_TYPE_HEAD			BTREE_TP(struct btree_head)
7 #define VISITOR_FN			BTREE_TP(visitor)
8 #define VISITOR_FN_T			_BTREE_TP(visitor, BTREE_TYPE_SUFFIX, _t)
9 
10 BTREE_TYPE_HEAD {
11 	struct btree_head h;
12 };
13 
BTREE_FN(init_mempool)14 static inline void BTREE_FN(init_mempool)(BTREE_TYPE_HEAD *head,
15 					  mempool_t *mempool)
16 {
17 	btree_init_mempool(&head->h, mempool);
18 }
19 
BTREE_FN(init)20 static inline int BTREE_FN(init)(BTREE_TYPE_HEAD *head)
21 {
22 	return btree_init(&head->h);
23 }
24 
BTREE_FN(destroy)25 static inline void BTREE_FN(destroy)(BTREE_TYPE_HEAD *head)
26 {
27 	btree_destroy(&head->h);
28 }
29 
BTREE_FN(merge)30 static inline int BTREE_FN(merge)(BTREE_TYPE_HEAD *target,
31 				  BTREE_TYPE_HEAD *victim,
32 				  gfp_t gfp)
33 {
34 	return btree_merge(&target->h, &victim->h, BTREE_TYPE_GEO, gfp);
35 }
36 
37 #if (BITS_PER_LONG > BTREE_TYPE_BITS)
BTREE_FN(lookup)38 static inline void *BTREE_FN(lookup)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key)
39 {
40 	unsigned long _key = key;
41 	return btree_lookup(&head->h, BTREE_TYPE_GEO, &_key);
42 }
43 
BTREE_FN(insert)44 static inline int BTREE_FN(insert)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key,
45 				   void *val, gfp_t gfp)
46 {
47 	unsigned long _key = key;
48 	return btree_insert(&head->h, BTREE_TYPE_GEO, &_key, val, gfp);
49 }
50 
BTREE_FN(update)51 static inline int BTREE_FN(update)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key,
52 		void *val)
53 {
54 	unsigned long _key = key;
55 	return btree_update(&head->h, BTREE_TYPE_GEO, &_key, val);
56 }
57 
BTREE_FN(remove)58 static inline void *BTREE_FN(remove)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key)
59 {
60 	unsigned long _key = key;
61 	return btree_remove(&head->h, BTREE_TYPE_GEO, &_key);
62 }
63 
BTREE_FN(last)64 static inline void *BTREE_FN(last)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE *key)
65 {
66 	unsigned long _key;
67 	void *val = btree_last(&head->h, BTREE_TYPE_GEO, &_key);
68 	if (val)
69 		*key = _key;
70 	return val;
71 }
72 
BTREE_FN(get_prev)73 static inline void *BTREE_FN(get_prev)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE *key)
74 {
75 	unsigned long _key = *key;
76 	void *val = btree_get_prev(&head->h, BTREE_TYPE_GEO, &_key);
77 	if (val)
78 		*key = _key;
79 	return val;
80 }
81 #else
BTREE_FN(lookup)82 static inline void *BTREE_FN(lookup)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key)
83 {
84 	return btree_lookup(&head->h, BTREE_TYPE_GEO, (unsigned long *)&key);
85 }
86 
BTREE_FN(insert)87 static inline int BTREE_FN(insert)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key,
88 			   void *val, gfp_t gfp)
89 {
90 	return btree_insert(&head->h, BTREE_TYPE_GEO, (unsigned long *)&key,
91 			    val, gfp);
92 }
93 
BTREE_FN(update)94 static inline int BTREE_FN(update)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key,
95 		void *val)
96 {
97 	return btree_update(&head->h, BTREE_TYPE_GEO, (unsigned long *)&key, val);
98 }
99 
BTREE_FN(remove)100 static inline void *BTREE_FN(remove)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE key)
101 {
102 	return btree_remove(&head->h, BTREE_TYPE_GEO, (unsigned long *)&key);
103 }
104 
BTREE_FN(last)105 static inline void *BTREE_FN(last)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE *key)
106 {
107 	return btree_last(&head->h, BTREE_TYPE_GEO, (unsigned long *)key);
108 }
109 
BTREE_FN(get_prev)110 static inline void *BTREE_FN(get_prev)(BTREE_TYPE_HEAD *head, BTREE_KEYTYPE *key)
111 {
112 	return btree_get_prev(&head->h, BTREE_TYPE_GEO, (unsigned long *)key);
113 }
114 #endif
115 
116 void VISITOR_FN(void *elem, unsigned long opaque, unsigned long *key,
117 		size_t index, void *__func);
118 
119 typedef void (*VISITOR_FN_T)(void *elem, unsigned long opaque,
120 			     BTREE_KEYTYPE key, size_t index);
121 
BTREE_FN(visitor)122 static inline size_t BTREE_FN(visitor)(BTREE_TYPE_HEAD *head,
123 				       unsigned long opaque,
124 				       VISITOR_FN_T func2)
125 {
126 	return btree_visitor(&head->h, BTREE_TYPE_GEO, opaque,
127 			     visitorl, func2);
128 }
129 
BTREE_FN(grim_visitor)130 static inline size_t BTREE_FN(grim_visitor)(BTREE_TYPE_HEAD *head,
131 					    unsigned long opaque,
132 					    VISITOR_FN_T func2)
133 {
134 	return btree_grim_visitor(&head->h, BTREE_TYPE_GEO, opaque,
135 				  visitorl, func2);
136 }
137 
138 #undef VISITOR_FN
139 #undef VISITOR_FN_T
140 #undef __BTREE_TP
141 #undef _BTREE_TP
142 #undef BTREE_TP
143 #undef BTREE_FN
144 #undef BTREE_TYPE_HEAD
145 #undef BTREE_TYPE_SUFFIX
146 #undef BTREE_TYPE_GEO
147 #undef BTREE_KEYTYPE
148 #undef BTREE_TYPE_BITS
149