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
)
77 * Lookup message type cache association
78 * @arg ops cache operations
79 * @arg msgtype netlink message type
81 * Searches for a matching message type association ing the specified
84 * @return A message type association or NULL.
86 struct nl_msgtype
*nl_msgtype_lookup(struct nl_cache_ops
*ops
, int msgtype
)
90 for (i
= 0; ops
->co_msgtypes
[i
].mt_id
>= 0; i
++)
91 if (ops
->co_msgtypes
[i
].mt_id
== msgtype
)
92 return &ops
->co_msgtypes
[i
];
97 static struct nl_cache_ops
*cache_ops_lookup_for_obj(struct nl_object_ops
*obj_ops
)
99 struct nl_cache_ops
*ops
;
101 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
)
102 if (ops
->co_obj_ops
== obj_ops
)
110 * Call a function for each registered cache operation
111 * @arg cb Callback function to be called
112 * @arg arg User specific argument.
114 void nl_cache_ops_foreach(void (*cb
)(struct nl_cache_ops
*, void *), void *arg
)
116 struct nl_cache_ops
*ops
;
118 for (ops
= cache_ops
; ops
; ops
= ops
->co_next
)
123 * Register a set of cache operations
124 * @arg ops cache operations
126 * Called by users of caches to announce the avaibility of
127 * a certain cache type.
129 * @return 0 on success or a negative error code.
131 int nl_cache_mngt_register(struct nl_cache_ops
*ops
)
133 if (!ops
->co_name
|| !ops
->co_obj_ops
)
136 if (nl_cache_ops_lookup(ops
->co_name
))
139 ops
->co_next
= cache_ops
;
142 NL_DBG(1, "Registered cache operations %s\n", ops
->co_name
);
148 * Unregister a set of cache operations
149 * @arg ops cache operations
151 * Called by users of caches to announce a set of
152 * cache operations is no longer available. The
153 * specified cache operations must have been registered
154 * previously using nl_cache_mngt_register()
156 * @return 0 on success or a negative error code
158 int nl_cache_mngt_unregister(struct nl_cache_ops
*ops
)
160 struct nl_cache_ops
*t
, **tp
;
162 for (tp
= &cache_ops
; (t
=*tp
) != NULL
; tp
= &t
->co_next
)
169 NL_DBG(1, "Unregistered cache operations %s\n", ops
->co_name
);
178 * @name Global Cache Provisioning/Requiring
183 * Provide a cache for global use
184 * @arg cache cache to provide
186 * Offers the specified cache to be used by other modules.
187 * Only one cache per type may be shared at a time,
188 * a previsouly provided caches will be overwritten.
190 void nl_cache_mngt_provide(struct nl_cache
*cache
)
192 struct nl_cache_ops
*ops
;
194 ops
= cache_ops_lookup_for_obj(cache
->c_ops
->co_obj_ops
);
198 ops
->co_major_cache
= cache
;
202 * Unprovide a cache for global use
203 * @arg cache cache to unprovide
205 * Cancels the offer to use a cache globally. The
206 * cache will no longer be returned via lookups but
207 * may still be in use.
209 void nl_cache_mngt_unprovide(struct nl_cache
*cache
)
211 struct nl_cache_ops
*ops
;
213 ops
= cache_ops_lookup_for_obj(cache
->c_ops
->co_obj_ops
);
216 else if (ops
->co_major_cache
== cache
)
217 ops
->co_major_cache
= NULL
;
221 * Demand the use of a global cache
222 * @arg name name of the required object type
224 * Trys to find a cache of the specified type for global
227 * @return A cache provided by another subsystem of the
228 * specified type marked to be available.
230 struct nl_cache
*nl_cache_mngt_require(const char *name
)
232 struct nl_cache_ops
*ops
;
234 ops
= nl_cache_ops_lookup(name
);
235 if (!ops
|| !ops
->co_major_cache
) {
236 fprintf(stderr
, "Application BUG: Your application must "
237 "call nl_cache_mngt_provide() and\nprovide a valid "
238 "%s cache to be used for internal lookups.\nSee the "
239 " API documentation for more details.\n", name
);
244 return ops
->co_major_cache
;
This page took 0.058977 seconds and 5 git commands to generate.