2 * lib/cache_mngt.c Cache Management
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
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cache_mngt Caching
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/cache.h>
21 #include <netlink/utils.h>
23 static struct nl_cache_ops
*cache_ops
;
26 * @name Cache Operations Sets
31 * Lookup the set cache operations of a certain cache type
32 * @arg name name of the cache type
34 * @return The cache operations or NULL if no operations
35 * have been registered under the specified name.
37 struct nl_cache_ops
*nl_cache_ops_lookup(const char *name
)
39 struct nl_cache_ops
*ops
;
41 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
)
42 if (!strcmp(ops
->co_name
, name
))
49 * Associate a message type to a set of cache operations
50 * @arg protocol netlink protocol
51 * @arg msgtype netlink message type
53 * Associates the specified netlink message type with
54 * a registered set of cache operations.
56 * @return The cache operations or NULL if no association
59 struct nl_cache_ops
*nl_cache_ops_associate(int protocol
, int msgtype
)
62 struct nl_cache_ops
*ops
;
64 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
) {
65 if (ops
->co_protocol
!= protocol
)
68 for (i
= 0; ops
->co_msgtypes
[i
].mt_id
>= 0; i
++)
69 if (ops
->co_msgtypes
[i
].mt_id
== msgtype
)
79 * Lookup message type cache association
80 * @arg ops cache operations
81 * @arg msgtype netlink message type
83 * Searches for a matching message type association ing the specified
86 * @return A message type association or NULL.
88 struct nl_msgtype
*nl_msgtype_lookup(struct nl_cache_ops
*ops
, int msgtype
)
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
];
99 static struct nl_cache_ops
*cache_ops_lookup_for_obj(struct nl_object_ops
*obj_ops
)
101 struct nl_cache_ops
*ops
;
103 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
)
104 if (ops
->co_obj_ops
== obj_ops
)
112 * Call a function for each registered cache operation
113 * @arg cb Callback function to be called
114 * @arg arg User specific argument.
116 void nl_cache_ops_foreach(void (*cb
)(struct nl_cache_ops
*, void *), void *arg
)
118 struct nl_cache_ops
*ops
;
120 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
)
126 * Register a set of cache operations
127 * @arg ops cache operations
129 * Called by users of caches to announce the avaibility of
130 * a certain cache type.
132 * @return 0 on success or a negative error code.
134 int nl_cache_mngt_register(struct nl_cache_ops
*ops
)
136 if (!ops
->co_name
|| !ops
->co_obj_ops
)
139 if (nl_cache_ops_lookup(ops
->co_name
))
142 ops
->co_next
= cache_ops
;
145 NL_DBG(1, "Registered cache operations %s\n", ops
->co_name
);
151 * Unregister a set of cache operations
152 * @arg ops cache operations
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()
159 * @return 0 on success or a negative error code
161 int nl_cache_mngt_unregister(struct nl_cache_ops
*ops
)
163 struct nl_cache_ops
*t
, **tp
;
165 for (tp
= &cache_ops
; (t
=*tp
) != NULL
; tp
= &t
->co_next
)
172 NL_DBG(1, "Unregistered cache operations %s\n", ops
->co_name
);
181 * @name Global Cache Provisioning/Requiring
186 * Provide a cache for global use
187 * @arg cache cache to provide
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.
193 void nl_cache_mngt_provide(struct nl_cache
*cache
)
195 struct nl_cache_ops
*ops
;
197 ops
= cache_ops_lookup_for_obj(cache
->c_ops
->co_obj_ops
);
201 ops
->co_major_cache
= cache
;
205 * Unprovide a cache for global use
206 * @arg cache cache to unprovide
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.
212 void nl_cache_mngt_unprovide(struct nl_cache
*cache
)
214 struct nl_cache_ops
*ops
;
216 ops
= cache_ops_lookup_for_obj(cache
->c_ops
->co_obj_ops
);
219 else if (ops
->co_major_cache
== cache
)
220 ops
->co_major_cache
= NULL
;
224 * Demand the use of a global cache
225 * @arg name name of the required object type
227 * Trys to find a cache of the specified type for global
230 * @return A cache provided by another subsystem of the
231 * specified type marked to be available.
233 struct nl_cache
*nl_cache_mngt_require(const char *name
)
235 struct nl_cache_ops
*ops
;
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
);
247 return ops
->co_major_cache
;
This page took 0.062917 seconds and 5 git commands to generate.