libnl  1.1.4
Modules | Data Structures
Object

Modules

 Object API
 

Data Structures

struct  nl_derived_object
 

Object Creation/Deletion

struct nl_object * nl_object_alloc (struct nl_object_ops *ops)
 Allocate a new object of kind specified by the operations handle. More...
 
struct nl_object * nl_object_alloc_name (const char *kind)
 Allocate a new object of kind specified by the name. More...
 
struct nl_object * nl_object_clone (struct nl_object *obj)
 Allocate a new object and copy all data from an existing object. More...
 
void nl_object_free (struct nl_object *obj)
 Free a cacheable object. More...
 

Reference Management

void nl_object_get (struct nl_object *obj)
 Acquire a reference on a object. More...
 
void nl_object_put (struct nl_object *obj)
 Release a reference from an object. More...
 
int nl_object_shared (struct nl_object *obj)
 Check whether this object is used by multiple users. More...
 

Marks

void nl_object_mark (struct nl_object *obj)
 Add mark to object. More...
 
void nl_object_unmark (struct nl_object *obj)
 Remove mark from object. More...
 
int nl_object_is_marked (struct nl_object *obj)
 Return true if object is marked. More...
 

Utillities

void nl_object_dump (struct nl_object *obj, struct nl_dump_params *params)
 Dump this object according to the specified parameters. More...
 
int nl_object_identical (struct nl_object *a, struct nl_object *b)
 Check if the identifiers of two objects are identical. More...
 
uint32_t nl_object_diff (struct nl_object *a, struct nl_object *b)
 Compute bitmask representing difference in attribute values. More...
 
int nl_object_match_filter (struct nl_object *obj, struct nl_object *filter)
 Match a filter against an object. More...
 
char * nl_object_attrs2str (struct nl_object *obj, uint32_t attrs, char *buf, size_t len)
 Convert bitmask of attributes to a character string. More...
 
char * nl_object_attr_list (struct nl_object *obj, char *buf, size_t len)
 Return list of attributes present in an object. More...
 

Attributes

int nl_object_get_refcnt (struct nl_object *obj)
 
struct nl_cache * nl_object_get_cache (struct nl_object *obj)
 

Detailed Description

Function Documentation

◆ nl_object_alloc()

struct nl_object* nl_object_alloc ( struct nl_object_ops ops)
Parameters
opscache operations handle
Returns
The new object or NULL

Definition at line 42 of file object.c.

References nl_object_ops::oo_constructor, and nl_object_ops::oo_size.

Referenced by nl_object_alloc_name().

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  nl_errno(ENOMEM);
52  return NULL;
53  }
54 
55  new->ce_refcnt = 1;
56  nl_init_list_head(&new->ce_list);
57 
58  new->ce_ops = ops;
59  if (ops->oo_constructor)
60  ops->oo_constructor(new);
61 
62  NL_DBG(4, "Allocated new object %p\n", new);
63 
64  return new;
65 }
size_t oo_size
Size of object including its header.
Definition: object-api.h:264
void(* oo_constructor)(struct nl_object *)
Constructor function.
Definition: object-api.h:275

◆ nl_object_alloc_name()

struct nl_object* nl_object_alloc_name ( const char *  kind)
Parameters
kindname of object type
Returns
The new object or nULL

Definition at line 72 of file object.c.

References nl_cache_ops_lookup(), and nl_object_alloc().

73 {
74  struct nl_cache_ops *ops;
75 
76  ops = nl_cache_ops_lookup(kind);
77  if (!ops) {
78  nl_error(ENOENT, "Unable to lookup cache kind \"%s\"", kind);
79  return NULL;
80  }
81 
82  return nl_object_alloc(ops->co_obj_ops);
83 }
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:42
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
Definition: cache_mngt.c:68
Cache Operations.
Definition: cache-api.h:163

◆ nl_object_clone()

struct nl_object* nl_object_clone ( struct nl_object *  obj)
Parameters
objobject to inherite data from
Returns
The new object or NULL.

Definition at line 95 of file object.c.

Referenced by nl_cache_add().

96 {
97  struct nl_object *new;
98  struct nl_object_ops *ops = obj_ops(obj);
99  int doff = offsetof(struct nl_derived_object, data);
100  int size;
101 
102  new = nl_object_alloc(ops);
103  if (!new)
104  return NULL;
105 
106  size = ops->oo_size - doff;
107  if (size < 0)
108  BUG();
109 
110  new->ce_ops = obj->ce_ops;
111  new->ce_msgtype = obj->ce_msgtype;
112 
113  if (size)
114  memcpy((void *)new + doff, (void *)obj + doff, size);
115 
116  if (ops->oo_clone) {
117  if (ops->oo_clone(new, obj) < 0) {
118  nl_object_free(new);
119  return NULL;
120  }
121  } else if (size && ops->oo_free_data)
122  BUG();
123 
124  return new;
125 }
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:42
size_t oo_size
Size of object including its header.
Definition: object-api.h:264
void nl_object_free(struct nl_object *obj)
Free a cacheable object.
Definition: object.c:133
Object Operations.
Definition: object-api.h:254
int(* oo_clone)(struct nl_object *, struct nl_object *)
Cloning function.
Definition: object-api.h:296
void(* oo_free_data)(struct nl_object *)
Destructor function.
Definition: object-api.h:284

◆ nl_object_free()

void nl_object_free ( struct nl_object *  obj)
Parameters
objobject to free
Returns
0 or a negative error code.

Definition at line 133 of file object.c.

Referenced by nl_object_put().

134 {
135  struct nl_object_ops *ops;
136 
137  if (!obj)
138  return;
139 
140  ops = obj_ops(obj);
141 
142  if (obj->ce_refcnt > 0)
143  NL_DBG(1, "Warning: Freeing object in use...\n");
144 
145  if (obj->ce_cache)
146  nl_cache_remove(obj);
147 
148  if (ops->oo_free_data)
149  ops->oo_free_data(obj);
150 
151  free(obj);
152 
153  NL_DBG(4, "Freed object %p\n", obj);
154 }
void nl_cache_remove(struct nl_object *obj)
Removes an object from a cache.
Definition: cache.c:374
Object Operations.
Definition: object-api.h:254
void(* oo_free_data)(struct nl_object *)
Destructor function.
Definition: object-api.h:284

◆ nl_object_get()

void nl_object_get ( struct nl_object *  obj)
Parameters
objobject to acquire reference from

Definition at line 167 of file object.c.

Referenced by genl_ctrl_search(), genl_ctrl_search_by_name(), nl_cache_add(), nl_cache_move(), nl_cache_search(), rtnl_neigh_get(), rtnl_qdisc_get(), and rtnl_qdisc_get_by_parent().

168 {
169  obj->ce_refcnt++;
170  NL_DBG(4, "New reference to object %p, total %d\n",
171  obj, obj->ce_refcnt);
172 }

◆ nl_object_put()

void nl_object_put ( struct nl_object *  obj)
Parameters
objobject to release reference from

Definition at line 178 of file object.c.

References nl_object_free().

Referenced by nl_cache_remove().

179 {
180  if (!obj)
181  return;
182 
183  obj->ce_refcnt--;
184  NL_DBG(4, "Returned object reference %p, %d remaining\n",
185  obj, obj->ce_refcnt);
186 
187  if (obj->ce_refcnt < 0)
188  BUG();
189 
190  if (obj->ce_refcnt <= 0)
191  nl_object_free(obj);
192 }
void nl_object_free(struct nl_object *obj)
Free a cacheable object.
Definition: object.c:133

◆ nl_object_shared()

int nl_object_shared ( struct nl_object *  obj)
Parameters
objobject to check
Returns
true or false

Definition at line 199 of file object.c.

200 {
201  return obj->ce_refcnt > 1;
202 }

◆ nl_object_mark()

void nl_object_mark ( struct nl_object *  obj)
Parameters
objObject to mark

Definition at line 215 of file object.c.

Referenced by nl_cache_mark_all().

216 {
217  obj->ce_flags |= NL_OBJ_MARK;
218 }

◆ nl_object_unmark()

void nl_object_unmark ( struct nl_object *  obj)
Parameters
objObject to unmark

Definition at line 224 of file object.c.

225 {
226  obj->ce_flags &= ~NL_OBJ_MARK;
227 }

◆ nl_object_is_marked()

int nl_object_is_marked ( struct nl_object *  obj)
Parameters
objObject to check
Returns
true if object is marked, otherwise false

Definition at line 234 of file object.c.

235 {
236  return (obj->ce_flags & NL_OBJ_MARK);
237 }

◆ nl_object_dump()

void nl_object_dump ( struct nl_object *  obj,
struct nl_dump_params params 
)
Parameters
objobject to dump
paramsdumping parameters

Definition at line 251 of file object.c.

252 {
253  dump_from_ops(obj, params);
254 }

◆ nl_object_identical()

int nl_object_identical ( struct nl_object *  a,
struct nl_object *  b 
)
Parameters
aan object
banother object of same type
Returns
true if both objects have equal identifiers, otherwise false.

Definition at line 263 of file object.c.

Referenced by nl_cache_search().

264 {
265  struct nl_object_ops *ops = obj_ops(a);
266  int req_attrs;
267 
268  /* Both objects must be of same type */
269  if (ops != obj_ops(b))
270  return 0;
271 
272  req_attrs = ops->oo_id_attrs;
273 
274  /* Both objects must provide all required attributes to uniquely
275  * identify an object */
276  if ((a->ce_mask & req_attrs) != req_attrs ||
277  (b->ce_mask & req_attrs) != req_attrs)
278  return 0;
279 
280  /* Can't judge unless we can compare */
281  if (ops->oo_compare == NULL)
282  return 0;
283 
284  return !(ops->oo_compare(a, b, req_attrs, 0));
285 }
int(* oo_compare)(struct nl_object *, struct nl_object *, uint32_t, int)
Comparison function.
Definition: object-api.h:321
Object Operations.
Definition: object-api.h:254

◆ nl_object_diff()

uint32_t nl_object_diff ( struct nl_object *  a,
struct nl_object *  b 
)
Parameters
aan object
banother object of same type

The bitmask returned is specific to an object type, each bit set represents an attribute which mismatches in either of the two objects. Unavailability of an attribute in one object and presence in the other is regarded a mismatch as well.

Returns
Bitmask describing differences or 0 if they are completely identical.

Definition at line 299 of file object.c.

300 {
301  struct nl_object_ops *ops = obj_ops(a);
302 
303  if (ops != obj_ops(b) || ops->oo_compare == NULL)
304  return UINT_MAX;
305 
306  return ops->oo_compare(a, b, ~0, 0);
307 }
int(* oo_compare)(struct nl_object *, struct nl_object *, uint32_t, int)
Comparison function.
Definition: object-api.h:321
Object Operations.
Definition: object-api.h:254

◆ nl_object_match_filter()

int nl_object_match_filter ( struct nl_object *  obj,
struct nl_object *  filter 
)
Parameters
objobject to check
filterobject of same type acting as filter
Returns
1 if the object matches the filter or 0 if no filter procedure is available or if the filter does not match.

Definition at line 318 of file object.c.

Referenced by nl_cache_dump_filter(), nl_cache_foreach_filter(), nl_cache_nitems_filter(), and nl_cache_subset().

319 {
320  struct nl_object_ops *ops = obj_ops(obj);
321 
322  if (ops != obj_ops(filter) || ops->oo_compare == NULL)
323  return 0;
324 
325  return !(ops->oo_compare(obj, filter, filter->ce_mask,
326  LOOSE_FLAG_COMPARISON));
327 }
int(* oo_compare)(struct nl_object *, struct nl_object *, uint32_t, int)
Comparison function.
Definition: object-api.h:321
Object Operations.
Definition: object-api.h:254

◆ nl_object_attrs2str()

char* nl_object_attrs2str ( struct nl_object *  obj,
uint32_t  attrs,
char *  buf,
size_t  len 
)
Parameters
objobject of same type as attribute bitmask
attrsbitmask of attribute types
bufdestination buffer
lenlength of destination buffer

Converts the bitmask of attribute types into a list of attribute names separated by comas.

Returns
destination buffer.

Definition at line 341 of file object.c.

Referenced by nl_object_attr_list().

343 {
344  struct nl_object_ops *ops = obj_ops(obj);
345 
346  if (ops->oo_attrs2str != NULL)
347  return ops->oo_attrs2str(attrs, buf, len);
348  else {
349  memset(buf, 0, len);
350  return buf;
351  }
352 }
Object Operations.
Definition: object-api.h:254

◆ nl_object_attr_list()

char* nl_object_attr_list ( struct nl_object *  obj,
char *  buf,
size_t  len 
)
Parameters
objan object
bufdestination buffer
lenlength of destination buffer
Returns
destination buffer.

Definition at line 362 of file object.c.

References nl_object_attrs2str().

363 {
364  return nl_object_attrs2str(obj, obj->ce_mask, buf, len);
365 }
char * nl_object_attrs2str(struct nl_object *obj, uint32_t attrs, char *buf, size_t len)
Convert bitmask of attributes to a character string.
Definition: object.c:341