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-2008 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);
54 nl_init_list_head(&new->ce_list);
57 if (ops->oo_constructor)
58 ops->oo_constructor(new);
60 NL_DBG(4, "Allocated new object %p\n", new);
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
71 int nl_object_alloc_name(const char *kind, struct nl_object **result)
73 struct nl_cache_ops *ops;
75 ops = nl_cache_ops_lookup(kind);
77 return -NLE_OPNOTSUPP;
79 if (!(*result = nl_object_alloc(ops->co_obj_ops)))
86 struct nl_derived_object {
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.
96 struct nl_object *nl_object_clone(struct nl_object *obj)
98 struct nl_object *new;
99 struct nl_object_ops *ops = obj_ops(obj);
100 int doff = offsetof(struct nl_derived_object, data);
103 new = nl_object_alloc(ops);
107 size = ops->oo_size - doff;
111 new->ce_ops = obj->ce_ops;
112 new->ce_msgtype = obj->ce_msgtype;
115 memcpy((void *)new + doff, (void *)obj + doff, size);
118 if (ops->oo_clone(new, obj) < 0) {
122 } else if (size && ops->oo_free_data)
129 * Free a cacheable object
130 * @arg obj object to free
132 * @return 0 or a negative error code.
134 void nl_object_free(struct nl_object *obj)
136 struct nl_object_ops *ops = obj_ops(obj);
138 if (obj->ce_refcnt > 0)
139 NL_DBG(1, "Warning: Freeing object in use...\n");
142 nl_cache_remove(obj);
144 if (ops->oo_free_data)
145 ops->oo_free_data(obj);
149 NL_DBG(4, "Freed object %p\n", obj);
155 * @name Reference Management
160 * Acquire a reference on a object
161 * @arg obj object to acquire reference from
163 void nl_object_get(struct nl_object *obj)
166 NL_DBG(4, "New reference to object %p, total %d\n",
167 obj, obj->ce_refcnt);
171 * Release a reference from an object
172 * @arg obj object to release reference from
174 void nl_object_put(struct nl_object *obj)
180 NL_DBG(4, "Returned object reference %p, %d remaining\n",
181 obj, obj->ce_refcnt);
183 if (obj->ce_refcnt < 0)
186 if (obj->ce_refcnt <= 0)
199 * Dump this object according to the specified parameters
200 * @arg obj object to dump
201 * @arg params dumping parameters
203 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
205 dump_from_ops(obj, params);
209 * Check if the identifiers of two objects are identical
211 * @arg b another object of same type
213 * @return true if both objects have equal identifiers, otherwise false.
215 int nl_object_identical(struct nl_object *a, struct nl_object *b)
217 struct nl_object_ops *ops = obj_ops(a);
220 /* Both objects must be of same type */
221 if (ops != obj_ops(b))
224 req_attrs = ops->oo_id_attrs;
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)
232 /* Can't judge unless we can compare */
233 if (ops->oo_compare == NULL)
236 return !(ops->oo_compare(a, b, req_attrs, 0));
241 * Compute bitmask representing difference in attribute values
243 * @arg b another object of same type
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
250 * @return Bitmask describing differences or 0 if they are completely identical.
252 uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
254 struct nl_object_ops *ops = obj_ops(a);
256 if (ops != obj_ops(b) || ops->oo_compare == NULL)
259 return ops->oo_compare(a, b, ~0, 0);
263 * Match a filter against an object
264 * @arg obj object to check
265 * @arg filter object of same type acting as filter
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.
271 int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
273 struct nl_object_ops *ops = obj_ops(obj);
275 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
278 return !(ops->oo_compare(obj, filter, filter->ce_mask,
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
289 * Converts the bitmask of attribute types into a list of attribute
290 * names separated by comas.
292 * @return destination buffer.
294 char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
295 char *buf, size_t len)
297 struct nl_object_ops *ops = obj_ops(obj);
299 if (ops->oo_attrs2str != NULL)
300 return ops->oo_attrs2str(attrs, buf, len);