initial commit with v2.6.9
[linux-2.6.9-moxart.git] / lib / kref.c
blob2218b7ae7db6fa089ba423e6f01956a338ccc564
1 /*
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.
14 #include <linux/kref.h>
15 #include <linux/module.h>
17 /**
18 * kref_init - initialize object.
19 * @kref: object in question.
21 void kref_init(struct kref *kref)
23 atomic_set(&kref->refcount,1);
26 /**
27 * kref_get - increment refcount for object.
28 * @kref: object.
30 void kref_get(struct kref *kref)
32 WARN_ON(!atomic_read(&kref->refcount));
33 atomic_inc(&kref->refcount);
36 /**
37 * kref_put - decrement refcount for object.
38 * @kref: object.
39 * @release: pointer to the function that will clean up the object when the
40 * last reference to the object is released.
41 * This pointer is required, and it is not acceptable to pass kfree
42 * in as this function.
44 * Decrement the refcount, and if 0, call release().
46 void kref_put(struct kref *kref, void (*release) (struct kref *kref))
48 WARN_ON(release == NULL);
49 WARN_ON(release == (void (*)(struct kref *))kfree);
51 if (atomic_dec_and_test(&kref->refcount))
52 release(kref);
55 EXPORT_SYMBOL(kref_init);
56 EXPORT_SYMBOL(kref_get);
57 EXPORT_SYMBOL(kref_put);