[scripts] env: colorize git output
[openwrt.git] / package / libnl-tiny / src / 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 */
42 struct nl_object *nl_object_alloc(struct nl_object_ops *ops)
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 #ifdef disabled
66 /**
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
70 */
71 int nl_object_alloc_name(const char *kind, struct nl_object **result)
72 {
73 struct nl_cache_ops *ops;
74
75 ops = nl_cache_ops_lookup(kind);
76 if (!ops)
77 return -NLE_OPNOTSUPP;
78
79 if (!(*result = nl_object_alloc(ops->co_obj_ops)))
80 return -NLE_NOMEM;
81
82 return 0;
83 }
84 #endif
85
86 struct nl_derived_object {
87 NLHDR_COMMON
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
114 if (size)
115 memcpy((void *)new + doff, (void *)obj + doff, size);
116
117 if (ops->oo_clone) {
118 if (ops->oo_clone(new, obj) < 0) {
119 nl_object_free(new);
120 return NULL;
121 }
122 } else if (size && ops->oo_free_data)
123 BUG();
124
125 return new;
126 }
127
128 /**
129 * Free a cacheable object
130 * @arg obj object to free
131 *
132 * @return 0 or a negative error code.
133 */
134 void nl_object_free(struct nl_object *obj)
135 {
136 struct nl_object_ops *ops = obj_ops(obj);
137
138 if (obj->ce_refcnt > 0)
139 NL_DBG(1, "Warning: Freeing object in use...\n");
140
141 if (obj->ce_cache)
142 nl_cache_remove(obj);
143
144 if (ops->oo_free_data)
145 ops->oo_free_data(obj);
146
147 free(obj);
148
149 NL_DBG(4, "Freed object %p\n", obj);
150 }
151
152 /** @} */
153
154 /**
155 * @name Reference Management
156 * @{
157 */
158
159 /** @} */
160
161 /**
162 * @name Utillities
163 * @{
164 */
165
166 #ifdef disabled
167 /**
168 * Dump this object according to the specified parameters
169 * @arg obj object to dump
170 * @arg params dumping parameters
171 */
172 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
173 {
174 dump_from_ops(obj, params);
175 }
176
177 /**
178 * Check if the identifiers of two objects are identical
179 * @arg a an object
180 * @arg b another object of same type
181 *
182 * @return true if both objects have equal identifiers, otherwise false.
183 */
184 int nl_object_identical(struct nl_object *a, struct nl_object *b)
185 {
186 struct nl_object_ops *ops = obj_ops(a);
187 int req_attrs;
188
189 /* Both objects must be of same type */
190 if (ops != obj_ops(b))
191 return 0;
192
193 req_attrs = ops->oo_id_attrs;
194
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)
199 return 0;
200
201 /* Can't judge unless we can compare */
202 if (ops->oo_compare == NULL)
203 return 0;
204
205 return !(ops->oo_compare(a, b, req_attrs, 0));
206 }
207
208 /**
209 * Compute bitmask representing difference in attribute values
210 * @arg a an object
211 * @arg b another object of same type
212 *
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
216 * mismatch as well.
217 *
218 * @return Bitmask describing differences or 0 if they are completely identical.
219 */
220 uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
221 {
222 struct nl_object_ops *ops = obj_ops(a);
223
224 if (ops != obj_ops(b) || ops->oo_compare == NULL)
225 return UINT_MAX;
226
227 return ops->oo_compare(a, b, ~0, 0);
228 }
229
230 /**
231 * Match a filter against an object
232 * @arg obj object to check
233 * @arg filter object of same type acting as filter
234 *
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.
238 */
239 int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
240 {
241 struct nl_object_ops *ops = obj_ops(obj);
242
243 if (ops != obj_ops(filter) || ops->oo_compare == NULL)
244 return 0;
245
246 return !(ops->oo_compare(obj, filter, filter->ce_mask,
247 LOOSE_COMPARISON));
248 }
249
250 /**
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
256 *
257 * Converts the bitmask of attribute types into a list of attribute
258 * names separated by comas.
259 *
260 * @return destination buffer.
261 */
262 char *nl_object_attrs2str(struct nl_object *obj, uint32_t attrs,
263 char *buf, size_t len)
264 {
265 struct nl_object_ops *ops = obj_ops(obj);
266
267 if (ops->oo_attrs2str != NULL)
268 return ops->oo_attrs2str(attrs, buf, len);
269 else {
270 memset(buf, 0, len);
271 return buf;
272 }
273 }
274
275 #endif
276
277 /** @} */
278
279 /** @} */
This page took 0.083437 seconds and 5 git commands to generate.