ath9k: add a patch from linux-wireless@ for reducing driver size
[openwrt.git] / package / libnl-tiny / src / cache_mngt.c
1 /*
2 * lib/cache_mngt.c Cache Management
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12 /**
13 * @ingroup core
14 * @defgroup cache_mngt Caching
15 * @{
16 */
17
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/cache.h>
21 #include <netlink/utils.h>
22
23 static struct nl_cache_ops *cache_ops;
24
25 /**
26 * @name Cache Operations Sets
27 * @{
28 */
29
30 /**
31 * Lookup the set cache operations of a certain cache type
32 * @arg name name of the cache type
33 *
34 * @return The cache operations or NULL if no operations
35 * have been registered under the specified name.
36 */
37 struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
38 {
39 struct nl_cache_ops *ops;
40
41 for (ops = cache_ops; ops; ops = ops->co_next)
42 if (!strcmp(ops->co_name, name))
43 return ops;
44
45 return NULL;
46 }
47
48 /**
49 * Associate a message type to a set of cache operations
50 * @arg protocol netlink protocol
51 * @arg msgtype netlink message type
52 *
53 * Associates the specified netlink message type with
54 * a registered set of cache operations.
55 *
56 * @return The cache operations or NULL if no association
57 * could be made.
58 */
59 struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
60 {
61 int i;
62 struct nl_cache_ops *ops;
63
64 for (ops = cache_ops; ops; ops = ops->co_next) {
65 if (ops->co_protocol != protocol)
66 continue;
67
68 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
69 if (ops->co_msgtypes[i].mt_id == msgtype)
70 return ops;
71 }
72
73 return NULL;
74 }
75
76 #ifdef disabled
77
78 /**
79 * Lookup message type cache association
80 * @arg ops cache operations
81 * @arg msgtype netlink message type
82 *
83 * Searches for a matching message type association ing the specified
84 * cache operations.
85 *
86 * @return A message type association or NULL.
87 */
88 struct nl_msgtype *nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
89 {
90 int i;
91
92 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
93 if (ops->co_msgtypes[i].mt_id == msgtype)
94 return &ops->co_msgtypes[i];
95
96 return NULL;
97 }
98
99 static struct nl_cache_ops *cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
100 {
101 struct nl_cache_ops *ops;
102
103 for (ops = cache_ops; ops; ops = ops->co_next)
104 if (ops->co_obj_ops == obj_ops)
105 return ops;
106
107 return NULL;
108
109 }
110
111 /**
112 * Call a function for each registered cache operation
113 * @arg cb Callback function to be called
114 * @arg arg User specific argument.
115 */
116 void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
117 {
118 struct nl_cache_ops *ops;
119
120 for (ops = cache_ops; ops; ops = ops->co_next)
121 cb(ops, arg);
122 }
123 #endif
124
125 /**
126 * Register a set of cache operations
127 * @arg ops cache operations
128 *
129 * Called by users of caches to announce the avaibility of
130 * a certain cache type.
131 *
132 * @return 0 on success or a negative error code.
133 */
134 int nl_cache_mngt_register(struct nl_cache_ops *ops)
135 {
136 if (!ops->co_name || !ops->co_obj_ops)
137 return -NLE_INVAL;
138
139 if (nl_cache_ops_lookup(ops->co_name))
140 return -NLE_EXIST;
141
142 ops->co_next = cache_ops;
143 cache_ops = ops;
144
145 NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
146
147 return 0;
148 }
149
150 /**
151 * Unregister a set of cache operations
152 * @arg ops cache operations
153 *
154 * Called by users of caches to announce a set of
155 * cache operations is no longer available. The
156 * specified cache operations must have been registered
157 * previously using nl_cache_mngt_register()
158 *
159 * @return 0 on success or a negative error code
160 */
161 int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
162 {
163 struct nl_cache_ops *t, **tp;
164
165 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
166 if (t == ops)
167 break;
168
169 if (!t)
170 return -NLE_NOCACHE;
171
172 NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
173
174 *tp = t->co_next;
175 return 0;
176 }
177
178 /** @} */
179
180 /**
181 * @name Global Cache Provisioning/Requiring
182 * @{
183 */
184 #ifdef disabled
185 /**
186 * Provide a cache for global use
187 * @arg cache cache to provide
188 *
189 * Offers the specified cache to be used by other modules.
190 * Only one cache per type may be shared at a time,
191 * a previsouly provided caches will be overwritten.
192 */
193 void nl_cache_mngt_provide(struct nl_cache *cache)
194 {
195 struct nl_cache_ops *ops;
196
197 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
198 if (!ops)
199 BUG();
200 else
201 ops->co_major_cache = cache;
202 }
203
204 /**
205 * Unprovide a cache for global use
206 * @arg cache cache to unprovide
207 *
208 * Cancels the offer to use a cache globally. The
209 * cache will no longer be returned via lookups but
210 * may still be in use.
211 */
212 void nl_cache_mngt_unprovide(struct nl_cache *cache)
213 {
214 struct nl_cache_ops *ops;
215
216 ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
217 if (!ops)
218 BUG();
219 else if (ops->co_major_cache == cache)
220 ops->co_major_cache = NULL;
221 }
222
223 /**
224 * Demand the use of a global cache
225 * @arg name name of the required object type
226 *
227 * Trys to find a cache of the specified type for global
228 * use.
229 *
230 * @return A cache provided by another subsystem of the
231 * specified type marked to be available.
232 */
233 struct nl_cache *nl_cache_mngt_require(const char *name)
234 {
235 struct nl_cache_ops *ops;
236
237 ops = nl_cache_ops_lookup(name);
238 if (!ops || !ops->co_major_cache) {
239 fprintf(stderr, "Application BUG: Your application must "
240 "call nl_cache_mngt_provide() and\nprovide a valid "
241 "%s cache to be used for internal lookups.\nSee the "
242 " API documentation for more details.\n", name);
243
244 return NULL;
245 }
246
247 return ops->co_major_cache;
248 }
249
250 #endif
251 /** @} */
252
253 /** @} */
This page took 0.058391 seconds and 5 git commands to generate.