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
168 * Dump this object according to the specified parameters
169 * @arg obj object to dump
170 * @arg params dumping parameters
172 void nl_object_dump(struct nl_object
*obj
, struct nl_dump_params
*params
)
174 dump_from_ops(obj
, params
);
178 * Check if the identifiers of two objects are identical
180 * @arg b another object of same type
182 * @return true if both objects have equal identifiers, otherwise false.
184 int nl_object_identical(struct nl_object
*a
, struct nl_object
*b
)
186 struct nl_object_ops
*ops
= obj_ops(a
);
189 /* Both objects must be of same type */
190 if (ops
!= obj_ops(b
))
193 req_attrs
= ops
->oo_id_attrs
;
195 /* Both objects must provide all required attributes to uniquely
196 * identify an object */
197 if ((a
->ce_mask
& req_attrs
) != req_attrs
||
198 (b
->ce_mask
& req_attrs
) != req_attrs
)
201 /* Can't judge unless we can compare */
202 if (ops
->oo_compare
== NULL
)
205 return !(ops
->oo_compare(a
, b
, req_attrs
, 0));
209 * Compute bitmask representing difference in attribute values
211 * @arg b another object of same type
213 * The bitmask returned is specific to an object type, each bit set represents
214 * an attribute which mismatches in either of the two objects. Unavailability
215 * of an attribute in one object and presence in the other is regarded a
218 * @return Bitmask describing differences or 0 if they are completely identical.
220 uint32_t nl_object_diff(struct nl_object
*a
, struct nl_object
*b
)
222 struct nl_object_ops
*ops
= obj_ops(a
);
224 if (ops
!= obj_ops(b
) || ops
->oo_compare
== NULL
)
227 return ops
->oo_compare(a
, b
, ~0, 0);
231 * Match a filter against an object
232 * @arg obj object to check
233 * @arg filter object of same type acting as filter
235 * @return 1 if the object matches the filter or 0
236 * if no filter procedure is available or if the
237 * filter does not match.
239 int nl_object_match_filter(struct nl_object
*obj
, struct nl_object
*filter
)
241 struct nl_object_ops
*ops
= obj_ops(obj
);
243 if (ops
!= obj_ops(filter
) || ops
->oo_compare
== NULL
)
246 return !(ops
->oo_compare(obj
, filter
, filter
->ce_mask
,
251 * Convert bitmask of attributes to a character string
252 * @arg obj object of same type as attribute bitmask
253 * @arg attrs bitmask of attribute types
254 * @arg buf destination buffer
255 * @arg len length of destination buffer
257 * Converts the bitmask of attribute types into a list of attribute
258 * names separated by comas.
260 * @return destination buffer.
262 char *nl_object_attrs2str(struct nl_object
*obj
, uint32_t attrs
,
263 char *buf
, size_t len
)
265 struct nl_object_ops
*ops
= obj_ops(obj
);
267 if (ops
->oo_attrs2str
!= NULL
)
268 return ops
->oo_attrs2str(attrs
, buf
, len
);
This page took 0.048592 seconds and 5 git commands to generate.