2 * kref.c - library routines for handling generic reference counted objects
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Corp.
7 * based on lib/kobject.c which was:
8 * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
10 * This file is released under the GPLv2.
16 #include <linux/kref.h>
17 #include <linux/module.h>
20 * kref_init - initialize object.
21 * @kref: object in question.
22 * @release: pointer to a function that will clean up the object
23 * when the last reference to the object is released.
24 * This pointer is required.
26 void kref_init(struct kref
*kref
, void (*release
)(struct kref
*kref
))
28 WARN_ON(release
== NULL
);
29 atomic_set(&kref
->refcount
,1);
30 kref
->release
= release
;
34 * kref_get - increment refcount for object.
37 struct kref
*kref_get(struct kref
*kref
)
39 WARN_ON(!atomic_read(&kref
->refcount
));
40 atomic_inc(&kref
->refcount
);
45 * kref_put - decrement refcount for object.
48 * Decrement the refcount, and if 0, call kref->release().
50 void kref_put(struct kref
*kref
)
52 if (atomic_dec_and_test(&kref
->refcount
)) {
53 pr_debug("kref cleaning up\n");
58 EXPORT_SYMBOL(kref_init
);
59 EXPORT_SYMBOL(kref_get
);
60 EXPORT_SYMBOL(kref_put
);