ppp: add a more descriptive title for ppp-multilink
[openwrt.git] / package / libnl-tiny / src / object.c
1 /*
2 * lib/object.c Generic Cacheable Object
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 cache
14 * @defgroup object Object
15 * @{
16 */
17
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/cache.h>
21 #include <netlink/object.h>
22 #include <netlink/utils.h>
23
24 static inline struct nl_object_ops *obj_ops(struct nl_object *obj)
25 {
26 if (!obj->ce_ops)
27 BUG();
28
29 return obj->ce_ops;
30 }
31
32 /**
33 * @name Object Creation/Deletion
34 * @{
35 */
36
37 /**
38 * Allocate a new object of kind specified by the operations handle
39 * @arg ops cache operations handle
40 * @return The new object or NULL
41 */
42 struct nl_object *nl_object_alloc(struct nl_object_ops *ops)
43 {
44 struct nl_object *new;
45
46 if (ops->oo_size < sizeof(*new))
47 BUG();
48
49 new = calloc(1, ops->oo_size);
50 if (!new)
51 return NULL;
52
53 new->ce_refcnt = 1;
54 nl_init_list_head(&new->ce_list);
55
56 new->ce_ops = ops;
57 if (ops->oo_constructor)
58 ops->oo_constructor(new);
59
60 NL_DBG(4, "Allocated new object %p\n", new);
61
62 return new;
63 }
64
65 #ifdef disabled
66 /**
67 * Allocate a new object of kind specified by the name
68 * @arg kind name of object type
69 * @return The new object or nULL
70 */
71 int nl_object_alloc_name(const char *kind, struct nl_object **result)
72 {
73 struct nl_cache_ops *ops;
74
75 ops = nl_cache_ops_lookup(kind);
76 if (!ops)
77 return -NLE_OPNOTSUPP;
78
79 if (!(*result = nl_object_alloc(ops->co_obj_ops)))
80 return -NLE_NOMEM;
81
82 return 0;
83 }
84 #endif
85
86 struct nl_derived_object {
87 NLHDR_COMMON
88 char data;
89 };
90
91 /**
92 * Allocate a new object and copy all data from an existing object
93 * @arg obj object to inherite data from
94 * @return The new object or NULL.
95 */
96 struct nl_object *nl_object_clone(struct nl_object *obj)
97 {
98 struct nl_object *new;
99 struct nl_object_ops *ops = obj_ops(obj);
100 int doff = offsetof(struct nl_derived_object, data);
101 int size;
102
103 new = nl_object_alloc(ops);
104 if (!new)
105 return NULL;
106
107 size = ops->oo_size - doff;
108 if (size < 0)
109 BUG();
110
111 new->ce_ops = obj->ce_ops;
112 new->ce_msgtype = obj->ce_msgtype;
113
114 if (size)
115 memcpy((void *)new + doff, (void *)obj + doff, size);
116
117 if (ops->oo_clone) {
118 if (ops->oo_clone(new, obj) < 0) {
119 nl_object_free(new);
120 return NULL;
121 }
122 } else if (size && ops->oo_free_data)
123 BUG();
124
125 return new;
126 }
127
128 /**
129 * Free a cacheable object
130 * @arg obj object to free
131 *
132 * @return 0 or a negative error code.
133 */
134 void nl_object_free(struct nl_object *obj)
135 {
136 struct nl_object_ops *ops = obj_ops(obj);
137
138 if (obj->ce_refcnt > 0)
139 NL_DBG(1, "Warning: Freeing object in use...\n");
140
141 if (obj->ce_cache)
142 nl_cache_remove(obj);
143
144 if (ops->oo_free_data)
145 ops->oo_free_data(obj);
146
147 free(obj);
148
149 NL_DBG(4, "Freed object %p\n", obj);
150 }
151
152 /** @} */
153
154 /**
155 * @name Reference Management
156 * @{
157 */
158
159 /**
160 * Acquire a reference on a object
161 * @arg obj object to acquire reference from
162 */
163 void nl_object_get(struct nl_object *obj)
164 {
165 obj->ce_refcnt++;
166 NL_DBG(4, "New reference to object %p, total %d\n",
167 obj, obj->ce_refcnt);
168 }
169
170 /**
171 * Release a reference from an object
172 * @arg obj object to release reference from
173 */
174 void nl_object_put(struct nl_object *obj)
175 {
176 if (!obj)
177 return;
178
179 obj->ce_refcnt--;
180 NL_DBG(4, "Returned object reference %p, %d remaining\n",
181 obj, obj->ce_refcnt);
182
183 if (obj->ce_refcnt < 0)
184 BUG();
185
186 if (obj->ce_refcnt <= 0)
187 nl_object_free(obj);
188 }
189
190 /** @} */
191
192 /**
193 * @name Utillities
194 * @{
195 */
196
197 #ifdef disabled
198 /**
199 * Dump this object according to the specified parameters
200 * @arg obj object to dump
201 * @arg params dumping parameters
202 */
203 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
204 {
205 dump_from_ops(obj, params);
206 }
207
208 /**
209 * Check if the identifiers of two objects are identical
210 * @arg a an object
211 * @arg b another object of same type
212 *
213 * @return true if both objects have equal identifiers, otherwise false.
214 */
215 int nl_object_identical(struct nl_object *a, struct nl_object *b)
216 {
217 struct nl_object_ops *ops = obj_ops(a);
218 int req_attrs;
219
220 /* Both objects must be of same type */
221 if (ops != obj_ops(b))
222 return 0;
223
224 req_attrs = ops->oo_id_attrs;
225
226 /* Both objects must provide all required attributes to uniquely
227 * identify an object */
228 if ((a->ce_mask & req_attrs) != req_attrs ||
229 (b->ce_mask & req_attrs) != req_attrs)
230 return 0;
231
232 /* Can't judge unless we can compare */
233 if (ops->oo_compare == NULL)
234 return 0;
235
236 return !(ops->oo_compare(a, b, req_attrs, 0));
237 }
238 #endif
239
240 /**
241 * Compute bitmask representing difference in attribute values
242 * @arg a an object
243 * @arg b another object of same type
244 *
245 * The bitmask returned is specific to an object type, each bit set represents
246 * an attribute which mismatches in either of the two objects. Unavailability
247 * of an attribute in one object and presence in the other is regarded a
248 * mismatch as well.
249 *
250 * @return Bitmask describing differences or 0 if they are completely identical.
251 */
252 uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
253 {
254 struct nl_object_ops *ops = obj_ops(a);
255
256 if (ops != obj_ops(b) || ops->oo_compare == NULL)
257 return UINT_MAX;
258
259 return ops->oo_compare(a, b, ~0, 0);
260 }
261
262 /**
263 * Match a filter against an object
264 * @arg obj object to check
265 * @arg filter object of same type acting as filter
266 *
267 * @return 1 if the object matches the filter or 0
268 * if no filter procedure is available or if the
269 * filter does not match.
270 */
271 int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
272 {
273 struct nl_object_ops *ops = obj_ops(obj);
274
275 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
276 return 0;
277
278 return !(ops->oo_compare(obj, filter, filter->ce_mask,
279 LOOSE_COMPARISON));
280 }
281
282 /**
283 * Convert bitmask of attributes to a character string
284 * @arg obj object of same type as attribute bitmask
285 * @arg attrs bitmask of attribute types
286 * @arg buf destination buffer
287 * @arg len length of destination buffer
288 *
289 * Converts the bitmask of attribute types into a list of attribute
290 * names separated by comas.
291 *
292 * @return destination buffer.
293 */
294 char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
295 char *buf, size_t len)
296 {
297 struct nl_object_ops *ops = obj_ops(obj);
298
299 if (ops->oo_attrs2str != NULL)
300 return ops->oo_attrs2str(attrs, buf, len);
301 else {
302 memset(buf, 0, len);
303 return buf;
304 }
305 }
306
307 /** @} */
308
309 /** @} */
This page took 0.058952 seconds and 5 git commands to generate.