1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* lib/object.c Generic Cacheable Object
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup cache
* @defgroup object Cacheable Object
* @{
*/
#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/object.h>
#include <netlink/utils.h>
static inline struct nl_cache_ops *obj_ops(struct nl_object *obj)
{
if (!obj->ce_ops)
BUG();
return obj->ce_ops;
}
/**
* @name Object Creation/Deletion
* @{
*/
/**
* Allocate a cacheable object
* @arg size size of object
* @return The new object or NULL.
*/
struct nl_object *nl_object_alloc(size_t size)
{
struct nl_object *new;
if (size < sizeof(*new))
BUG();
new = calloc(1, size);
if (!new) {
nl_errno(ENOMEM);
return NULL;
}
new->ce_refcnt = 1;
nl_init_list_head(&new->ce_list);
return new;
}
/**
* Allocate a new object of kind specified by the operations handle
* @arg ops cache operations handle
* @return The new object or NULL
*/
struct nl_object *nl_object_alloc_from_ops(struct nl_cache_ops *ops)
{
struct nl_object *new;
new = nl_object_alloc(ops->co_size);
if (new) {
new->ce_ops = ops;
if (ops->co_constructor)
ops->co_constructor(new);
}
return new;
}
/**
* Allocate a new object of kind specified by the name
* @arg kind name of object type
* @return The new object or nULL
*/
struct nl_object *nl_object_alloc_name(const char *kind)
{
struct nl_cache_ops *ops;
ops = nl_cache_mngt_lookup(kind);
if (!ops) {
nl_error(ENOENT, "Unable to lookup cache kind \"%s\"", kind);
return NULL;
}
return nl_object_alloc_from_ops(ops);
}
struct nl_derived_object {
NLHDR_COMMON
char data;
};
/**
* Allocate a new object and copy all data from an existing object
* @arg obj object to inherite data from
* @return The new object or NULL.
*/
struct nl_object *nl_object_clone(struct nl_object *obj)
{
struct nl_object *new;
struct nl_cache_ops *ops = obj_ops(obj);
int doff = offsetof(struct nl_derived_object, data);
int size;
new = nl_object_alloc(ops->co_size);
if (!new)
return NULL;
size = ops->co_size - doff;
if (size < 0)
BUG();
new->ce_cache = obj->ce_cache;
new->ce_ops = obj->ce_ops;
new->ce_msgtype = obj->ce_msgtype;
if (ops->co_free_data) {
new->ce_dataref = obj;
nl_object_get(obj);
}
if (size)
memcpy((void *)new + doff, (void *)obj + doff, size);
return new;
}
/**
* Free a cacheable object
* @arg obj object to free
*
* @return 0 or a negative error code.
*/
void nl_object_free(struct nl_object *obj)
{
struct nl_cache_ops *ops = obj_ops(obj);
if (obj->ce_refcnt > 0)
NL_DBG(1, "Warning: Freeing object in used...\n");
if (obj->ce_dataref)
nl_object_put(obj->ce_dataref);
else if (ops->co_free_data)
ops->co_free_data(obj);
free(obj);
}
/** @} */
/**
* @name Reference Management
* @{
*/
/**
* Acquire a reference on a object
* @arg obj object to acquire reference from
*/
void nl_object_get(struct nl_object *obj)
{
obj->ce_refcnt++;
}
/**
* Release a reference from an object
* @arg obj object to release reference from
*/
void nl_object_put(struct nl_object *obj)
{
if (!obj)
return;
obj->ce_refcnt--;
if (obj->ce_refcnt < 0)
BUG();
if (obj->ce_refcnt <= 0)
nl_object_free(obj);
}
/**
* Check whether this object is used by multiple users
* @arg obj object to check
* @return true or false
*/
int nl_object_shared(struct nl_object *obj)
{
return obj->ce_refcnt > 1;
}
/** @} */
/**
* @name Utillities
* @{
*/
/**
* Dump this object according to the specified parameters
* @arg obj object to dump
* @arg params dumping parameters
*/
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
{
dump_from_ops(obj, params);
}
/**
* Match a filter against an object
* @arg obj object to check
* @arg filter filter object
*
* @return 0 if the object matches the filter or non-zero
* if no filter procedure is available or if the
* filter does not match.
*/
int nl_object_match(struct nl_object *obj, struct nl_object *filter)
{
struct nl_cache_ops *ops = obj->ce_ops;
if (ops == filter->ce_ops &&
ops->co_filter && !ops->co_filter(obj, filter))
return 1;
else
return 0;
}
/** @} */
/**
* @name Access Functions
* @{
*/
/**
* Get reference count of object
* @arg obj object handle
*/
int nl_object_get_refcnt(struct nl_object *obj)
{
return obj->ce_refcnt;
}
/**
* Get cache operations of object
* @arg obj object handle
*/
struct nl_cache_ops *nl_object_get_ops(struct nl_object *obj)
{
return obj->ce_ops;
}
/**
* Get cache this object is in
* @arg obj object handle
* @return cache handle or NULL if object is not associated to a cache
*/
struct nl_cache *nl_object_get_cache(struct nl_object *obj)
{
return obj->ce_cache;
}
/**
* Get the private data of object
* @arg obj object handle
*/
inline void *nl_object_priv(struct nl_object *obj)
{
return obj;
}
/** @} */
/** @} */
|