libnl  3.2.7
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  */
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 /**
66  * Allocate new object of kind specified by the name
67  * @arg kind name of object type
68  * @arg result Result pointer
69  *
70  * @return 0 on success or a negative error code.
71  */
72 int nl_object_alloc_name(const char *kind, struct nl_object **result)
73 {
74  struct nl_cache_ops *ops;
75 
76  ops = nl_cache_ops_lookup(kind);
77  if (!ops)
78  return -NLE_OPNOTSUPP;
79 
80  if (!(*result = nl_object_alloc(ops->co_obj_ops)))
81  return -NLE_NOMEM;
82 
83  return 0;
84 }
85 
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  new->ce_mask = obj->ce_mask;
114 
115  if (size)
116  memcpy((void *)new + doff, (void *)obj + doff, size);
117 
118  if (ops->oo_clone) {
119  if (ops->oo_clone(new, obj) < 0) {
120  nl_object_free(new);
121  return NULL;
122  }
123  } else if (size && ops->oo_free_data)
124  BUG();
125 
126  return new;
127 }
128 
129 /**
130  * Free a cacheable object
131  * @arg obj object to free
132  *
133  * @return 0 or a negative error code.
134  */
135 void nl_object_free(struct nl_object *obj)
136 {
137  struct nl_object_ops *ops = obj_ops(obj);
138 
139  if (obj->ce_refcnt > 0)
140  NL_DBG(1, "Warning: Freeing object in use...\n");
141 
142  if (obj->ce_cache)
143  nl_cache_remove(obj);
144 
145  if (ops->oo_free_data)
146  ops->oo_free_data(obj);
147 
148  free(obj);
149 
150  NL_DBG(4, "Freed object %p\n", obj);
151 }
152 
153 /** @} */
154 
155 /**
156  * @name Reference Management
157  * @{
158  */
159 
160 /**
161  * Acquire a reference on a object
162  * @arg obj object to acquire reference from
163  */
164 void nl_object_get(struct nl_object *obj)
165 {
166  obj->ce_refcnt++;
167  NL_DBG(4, "New reference to object %p, total %d\n",
168  obj, obj->ce_refcnt);
169 }
170 
171 /**
172  * Release a reference from an object
173  * @arg obj object to release reference from
174  */
175 void nl_object_put(struct nl_object *obj)
176 {
177  if (!obj)
178  return;
179 
180  obj->ce_refcnt--;
181  NL_DBG(4, "Returned object reference %p, %d remaining\n",
182  obj, obj->ce_refcnt);
183 
184  if (obj->ce_refcnt < 0)
185  BUG();
186 
187  if (obj->ce_refcnt <= 0)
188  nl_object_free(obj);
189 }
190 
191 /**
192  * Check whether this object is used by multiple users
193  * @arg obj object to check
194  * @return true or false
195  */
196 int nl_object_shared(struct nl_object *obj)
197 {
198  return obj->ce_refcnt > 1;
199 }
200 
201 /** @} */
202 
203 /**
204  * @name Marks
205  * @{
206  */
207 
208 /**
209  * Add mark to object
210  * @arg obj Object to mark
211  */
212 void nl_object_mark(struct nl_object *obj)
213 {
214  obj->ce_flags |= NL_OBJ_MARK;
215 }
216 
217 /**
218  * Remove mark from object
219  * @arg obj Object to unmark
220  */
221 void nl_object_unmark(struct nl_object *obj)
222 {
223  obj->ce_flags &= ~NL_OBJ_MARK;
224 }
225 
226 /**
227  * Return true if object is marked
228  * @arg obj Object to check
229  * @return true if object is marked, otherwise false
230  */
232 {
233  return (obj->ce_flags & NL_OBJ_MARK);
234 }
235 
236 /** @} */
237 
238 /**
239  * @name Utillities
240  * @{
241  */
242 
243 /**
244  * Dump this object according to the specified parameters
245  * @arg obj object to dump
246  * @arg params dumping parameters
247  */
248 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
249 {
250  dump_from_ops(obj, params);
251 }
252 
253 void nl_object_dump_buf(struct nl_object *obj, char *buf, size_t len)
254 {
255  struct nl_dump_params dp = {
256  .dp_buf = buf,
257  .dp_buflen = len,
258  };
259 
260  return nl_object_dump(obj, &dp);
261 }
262 
263 /**
264  * Check if the identifiers of two objects are identical
265  * @arg a an object
266  * @arg b another object of same type
267  *
268  * @return true if both objects have equal identifiers, otherwise false.
269  */
270 int nl_object_identical(struct nl_object *a, struct nl_object *b)
271 {
272  struct nl_object_ops *ops = obj_ops(a);
273  int req_attrs;
274 
275  /* Both objects must be of same type */
276  if (ops != obj_ops(b))
277  return 0;
278 
279  req_attrs = ops->oo_id_attrs;
280  if (req_attrs == ~0)
281  req_attrs = a->ce_mask & b->ce_mask;
282 
283  /* Both objects must provide all required attributes to uniquely
284  * identify an object */
285  if ((a->ce_mask & req_attrs) != req_attrs ||
286  (b->ce_mask & req_attrs) != req_attrs)
287  return 0;
288 
289  /* Can't judge unless we can compare */
290  if (ops->oo_compare == NULL)
291  return 0;
292 
293  return !(ops->oo_compare(a, b, req_attrs, 0));
294 }
295 
296 /**
297  * Compute bitmask representing difference in attribute values
298  * @arg a an object
299  * @arg b another object of same type
300  *
301  * The bitmask returned is specific to an object type, each bit set represents
302  * an attribute which mismatches in either of the two objects. Unavailability
303  * of an attribute in one object and presence in the other is regarded a
304  * mismatch as well.
305  *
306  * @return Bitmask describing differences or 0 if they are completely identical.
307  */
308 uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
309 {
310  struct nl_object_ops *ops = obj_ops(a);
311 
312  if (ops != obj_ops(b) || ops->oo_compare == NULL)
313  return UINT_MAX;
314 
315  return ops->oo_compare(a, b, ~0, 0);
316 }
317 
318 /**
319  * Match a filter against an object
320  * @arg obj object to check
321  * @arg filter object of same type acting as filter
322  *
323  * @return 1 if the object matches the filter or 0
324  * if no filter procedure is available or if the
325  * filter does not match.
326  */
327 int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
328 {
329  struct nl_object_ops *ops = obj_ops(obj);
330 
331  if (ops != obj_ops(filter) || ops->oo_compare == NULL)
332  return 0;
333 
334  return !(ops->oo_compare(obj, filter, filter->ce_mask,
335  LOOSE_COMPARISON));
336 }
337 
338 /**
339  * Convert bitmask of attributes to a character string
340  * @arg obj object of same type as attribute bitmask
341  * @arg attrs bitmask of attribute types
342  * @arg buf destination buffer
343  * @arg len length of destination buffer
344  *
345  * Converts the bitmask of attribute types into a list of attribute
346  * names separated by comas.
347  *
348  * @return destination buffer.
349  */
350 char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
351  char *buf, size_t len)
352 {
353  struct nl_object_ops *ops = obj_ops(obj);
354 
355  if (ops->oo_attrs2str != NULL)
356  return ops->oo_attrs2str(attrs, buf, len);
357  else {
358  memset(buf, 0, len);
359  return buf;
360  }
361 }
362 
363 /**
364  * Return list of attributes present in an object
365  * @arg obj an object
366  * @arg buf destination buffer
367  * @arg len length of destination buffer
368  *
369  * @return destination buffer.
370  */
371 char *nl_object_attr_list(struct nl_object *obj, char *buf, size_t len)
372 {
373  return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
374 }
375 
376 /** @} */
377 
378 /**
379  * @name Attributes
380  * @{
381  */
382 
383 int nl_object_get_refcnt(struct nl_object *obj)
384 {
385  return obj->ce_refcnt;
386 }
387 
388 struct nl_cache *nl_object_get_cache(struct nl_object *obj)
389 {
390  return obj->ce_cache;
391 }
392 
393 /** @} */
394 
395 /** @} */