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-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup object Cacheable 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_cache_ops *obj_ops(struct nl_object *obj)
33 * @name Object Creation/Deletion
38 * Allocate a cacheable object
39 * @arg size size of object
40 * @return The new object or NULL.
42 struct nl_object *nl_object_alloc(size_t size)
44 struct nl_object *new;
46 if (size < sizeof(*new))
49 new = calloc(1, size);
56 nl_init_list_head(&new->ce_list);
62 * Allocate a new object of kind specified by the operations handle
63 * @arg ops cache operations handle
64 * @return The new object or NULL
66 struct nl_object *nl_object_alloc_from_ops(struct nl_cache_ops *ops)
68 struct nl_object *new;
70 new = nl_object_alloc(ops->co_size);
73 if (ops->co_constructor)
74 ops->co_constructor(new);
81 * Allocate a new object of kind specified by the name
82 * @arg kind name of object type
83 * @return The new object or nULL
85 struct nl_object *nl_object_alloc_name(const char *kind)
87 struct nl_cache_ops *ops;
89 ops = nl_cache_mngt_lookup(kind);
91 nl_error(ENOENT, "Unable to lookup cache kind \"%s\"", kind);
95 return nl_object_alloc_from_ops(ops);
98 struct nl_derived_object {
104 * Allocate a new object and copy all data from an existing object
105 * @arg obj object to inherite data from
106 * @return The new object or NULL.
108 struct nl_object *nl_object_clone(struct nl_object *obj)
110 struct nl_object *new;
111 struct nl_cache_ops *ops = obj_ops(obj);
112 int doff = offsetof(struct nl_derived_object, data);
115 new = nl_object_alloc(ops->co_size);
119 size = ops->co_size - doff;
123 new->ce_cache = obj->ce_cache;
124 new->ce_ops = obj->ce_ops;
125 new->ce_msgtype = obj->ce_msgtype;
126 if (ops->co_free_data) {
127 new->ce_dataref = obj;
132 memcpy((void *)new + doff, (void *)obj + doff, size);
138 * Free a cacheable object
139 * @arg obj object to free
141 * @return 0 or a negative error code.
143 void nl_object_free(struct nl_object *obj)
145 struct nl_cache_ops *ops = obj_ops(obj);
147 if (obj->ce_refcnt > 0)
148 NL_DBG(1, "Warning: Freeing object in used...\n");
151 nl_object_put(obj->ce_dataref);
152 else if (ops->co_free_data)
153 ops->co_free_data(obj);
161 * @name Reference Management
166 * Acquire a reference on a object
167 * @arg obj object to acquire reference from
169 void nl_object_get(struct nl_object *obj)
175 * Release a reference from an object
176 * @arg obj object to release reference from
178 void nl_object_put(struct nl_object *obj)
185 if (obj->ce_refcnt < 0)
188 if (obj->ce_refcnt <= 0)
193 * Check whether this object is used by multiple users
194 * @arg obj object to check
195 * @return true or false
197 int nl_object_shared(struct nl_object *obj)
199 return obj->ce_refcnt > 1;
210 * Dump this object according to the specified parameters
211 * @arg obj object to dump
212 * @arg params dumping parameters
214 void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
216 dump_from_ops(obj, params);
220 * Match a filter against an object
221 * @arg obj object to check
222 * @arg filter filter object
224 * @return 0 if the object matches the filter or non-zero
225 * if no filter procedure is available or if the
226 * filter does not match.
228 int nl_object_match(struct nl_object *obj, struct nl_object *filter)
230 struct nl_cache_ops *ops = obj->ce_ops;
232 if (ops == filter->ce_ops &&
233 ops->co_filter && !ops->co_filter(obj, filter))
242 * @name Access Functions
247 * Get reference count of object
248 * @arg obj object handle
250 int nl_object_get_refcnt(struct nl_object *obj)
252 return obj->ce_refcnt;
256 * Get cache operations of object
257 * @arg obj object handle
259 struct nl_cache_ops *nl_object_get_ops(struct nl_object *obj)
265 * Get cache this object is in
266 * @arg obj object handle
267 * @return cache handle or NULL if object is not associated to a cache
269 struct nl_cache *nl_object_get_cache(struct nl_object *obj)
271 return obj->ce_cache;
275 * Get the private data of object
276 * @arg obj object handle
278 inline void *nl_object_priv(struct nl_object *obj)