2 * lib/object.c Generic Cacheable Object
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-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup object Object
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>
24 static inline struct nl_object_ops *obj_ops(struct nl_object *obj)
33 * @name Object Creation/Deletion
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
42 struct nl_object *nl_object_alloc(struct nl_object_ops *ops)
44 struct nl_object *new;
46 if (ops->oo_size < sizeof(*new))
49 new = calloc(1, ops->oo_size);
56 nl_init_list_head(&new->ce_list);
59 if (ops->oo_constructor)
60 ops->oo_constructor(new);
62 NL_DBG(4, "Allocated new object %p\n", new);
68 * Allocate a new object of kind specified by the name
69 * @arg kind name of object type
70 * @return The new object or nULL
72 struct nl_object *nl_object_alloc_name(const char *kind)
74 struct nl_cache_ops *ops;
76 ops = nl_cache_ops_lookup(kind);
78 nl_error(ENOENT, "Unable to lookup cache kind \"%s\"", kind);
82 return nl_object_alloc(ops->co_obj_ops);
85 struct nl_derived_object {
91 * Allocate a new object and copy all data from an existing object
92 * @arg obj object to inherite data from
93 * @return The new object or NULL.
95 struct nl_object *nl_object_clone(struct nl_object *obj)
97 struct nl_object *new;
98 struct nl_object_ops *ops = obj_ops(obj);
99 int doff = offsetof(struct nl_derived_object, data);
102 new = nl_object_alloc(ops);
106 size = ops->oo_size - doff;
110 new->ce_ops = obj->ce_ops;
111 new->ce_msgtype = obj->ce_msgtype;
114 memcpy((void *)new + doff, (void *)obj + doff, size);
117 if (ops->oo_clone(new, obj) < 0) {
121 } else if (size && ops->oo_free_data)
128 * Free a cacheable object
129 * @arg obj object to free
131 * @return 0 or a negative error code.
133 void nl_object_free(struct nl_object *obj)
135 struct nl_object_ops *ops = obj_ops(obj);
137 if (obj->ce_refcnt > 0)
138 NL_DBG(1, "Warning: Freeing object in use...\n");
141 nl_cache_remove(obj);
143 if (ops->oo_free_data)
144 ops->oo_free_data(obj);
148 NL_DBG(4, "Freed object %p\n", obj);
154 * @name Reference Management
159 * Acquire a reference on a object
160 * @arg obj object to acquire reference from
162 void nl_object_get(struct nl_object *obj)
165 NL_DBG(4, "New reference to object %p, total %d\n",
166 obj, obj->ce_refcnt);
170 * Release a reference from an object
171 * @arg obj object to release reference from
173 void nl_object_put(struct nl_object *obj)
179 NL_DBG(4, "Returned object reference %p, %d remaining\n",
180 obj, obj->ce_refcnt);
182 if (obj->ce_refcnt < 0)
185 if (obj->ce_refcnt <= 0)
190 * Check whether this object is used by multiple users
191 * @arg obj object to check
192 * @return true or false
194 int nl_object_shared(struct nl_object *obj)
196 return obj->ce_refcnt > 1;
208 * @arg obj Object to mark
210 void nl_object_mark(struct nl_object *obj)
212 obj->ce_flags |= NL_OBJ_MARK;
216 * Remove mark from object
217 * @arg obj Object to unmark
219 void nl_object_unmark(struct nl_object *obj)
221 obj->ce_flags &= ~NL_OBJ_MARK;
225 * Return true if object is marked
226 * @arg obj Object to check
227 * @return true if object is marked, otherwise false
229 int nl_object_is_marked(struct nl_object *obj)
231 return (obj->ce_flags & NL_OBJ_MARK);
242 * Dump this object according to the specified parameters
243 * @arg obj object to dump
244 * @arg params dumping parameters
246 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
248 dump_from_ops(obj, params);
252 * Check if the identifiers of two objects are identical
254 * @arg b another object of same type
256 * @return true if both objects have equal identifiers, otherwise false.
258 int nl_object_identical(struct nl_object *a, struct nl_object *b)
260 struct nl_object_ops *ops = obj_ops(a);
263 /* Both objects must be of same type */
264 if (ops != obj_ops(b))
267 req_attrs = ops->oo_id_attrs;
269 /* Both objects must provide all required attributes to uniquely
270 * identify an object */
271 if ((a->ce_mask & req_attrs) != req_attrs ||
272 (b->ce_mask & req_attrs) != req_attrs)
275 /* Can't judge unless we can compare */
276 if (ops->oo_compare == NULL)
279 return !(ops->oo_compare(a, b, req_attrs, 0));
283 * Compute bitmask representing difference in attribute values
285 * @arg b another object of same type
287 * The bitmask returned is specific to an object type, each bit set represents
288 * an attribute which mismatches in either of the two objects. Unavailability
289 * of an attribute in one object and presence in the other is regarded a
292 * @return Bitmask describing differences or 0 if they are completely identical.
294 uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
296 struct nl_object_ops *ops = obj_ops(a);
298 if (ops != obj_ops(b) || ops->oo_compare == NULL)
301 return ops->oo_compare(a, b, ~0, 0);
305 * Match a filter against an object
306 * @arg obj object to check
307 * @arg filter object of same type acting as filter
309 * @return 1 if the object matches the filter or 0
310 * if no filter procedure is available or if the
311 * filter does not match.
313 int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
315 struct nl_object_ops *ops = obj_ops(obj);
317 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
320 return !(ops->oo_compare(obj, filter, filter->ce_mask,
321 LOOSE_FLAG_COMPARISON));
325 * Convert bitmask of attributes to a character string
326 * @arg obj object of same type as attribute bitmask
327 * @arg attrs bitmask of attribute types
328 * @arg buf destination buffer
329 * @arg len length of destination buffer
331 * Converts the bitmask of attribute types into a list of attribute
332 * names separated by comas.
334 * @return destination buffer.
336 char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
337 char *buf, size_t len)
339 struct nl_object_ops *ops = obj_ops(obj);
341 if (ops->oo_attrs2str != NULL)
342 return ops->oo_attrs2str(attrs, buf, len);
350 * Return list of attributes present in an object
352 * @arg buf destination buffer
353 * @arg len length of destination buffer
355 * @return destination buffer.
357 char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
359 return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
369 int nl_object_get_refcnt(struct nl_object *obj)
371 return obj->ce_refcnt;
374 struct nl_cache *nl_object_get_cache(struct nl_object *obj)
376 return obj->ce_cache;
379 inline void *nl_object_priv(struct nl_object *obj)