| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * kref.c - library routines for handling generic reference counted objects | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | 
|  | 5 | * Copyright (C) 2004 IBM Corp. | 
|  | 6 | * | 
|  | 7 | * based on lib/kobject.c which was: | 
|  | 8 | * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org> | 
|  | 9 | * | 
|  | 10 | * This file is released under the GPLv2. | 
|  | 11 | * | 
|  | 12 | */ | 
|  | 13 |  | 
|  | 14 | #include <linux/kref.h> | 
|  | 15 | #include <linux/module.h> | 
|  | 16 |  | 
|  | 17 | /** | 
| Evgeniy Polyakov | 41ca28a | 2007-12-10 23:03:43 +0300 | [diff] [blame] | 18 | * kref_set - initialize object and set refcount to requested number. | 
|  | 19 | * @kref: object in question. | 
|  | 20 | * @num: initial reference counter | 
|  | 21 | */ | 
|  | 22 | void kref_set(struct kref *kref, int num) | 
|  | 23 | { | 
|  | 24 | atomic_set(&kref->refcount, num); | 
|  | 25 | smp_mb(); | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * kref_init - initialize object. | 
|  | 30 | * @kref: object in question. | 
|  | 31 | */ | 
|  | 32 | void kref_init(struct kref *kref) | 
|  | 33 | { | 
| Evgeniy Polyakov | 41ca28a | 2007-12-10 23:03:43 +0300 | [diff] [blame] | 34 | kref_set(kref, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
|  | 37 | /** | 
|  | 38 | * kref_get - increment refcount for object. | 
|  | 39 | * @kref: object. | 
|  | 40 | */ | 
|  | 41 | void kref_get(struct kref *kref) | 
|  | 42 | { | 
|  | 43 | WARN_ON(!atomic_read(&kref->refcount)); | 
|  | 44 | atomic_inc(&kref->refcount); | 
| Oliver Neukum | 1b0b3b9 | 2007-04-02 14:47:59 +0200 | [diff] [blame] | 45 | smp_mb__after_atomic_inc(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
|  | 48 | /** | 
|  | 49 | * kref_put - decrement refcount for object. | 
|  | 50 | * @kref: object. | 
|  | 51 | * @release: pointer to the function that will clean up the object when the | 
|  | 52 | *	     last reference to the object is released. | 
|  | 53 | *	     This pointer is required, and it is not acceptable to pass kfree | 
|  | 54 | *	     in as this function. | 
|  | 55 | * | 
|  | 56 | * Decrement the refcount, and if 0, call release(). | 
|  | 57 | * Return 1 if the object was removed, otherwise return 0.  Beware, if this | 
|  | 58 | * function returns 0, you still can not count on the kref from remaining in | 
|  | 59 | * memory.  Only use the return value if you want to see if the kref is now | 
|  | 60 | * gone, not present. | 
|  | 61 | */ | 
|  | 62 | int kref_put(struct kref *kref, void (*release)(struct kref *kref)) | 
|  | 63 | { | 
|  | 64 | WARN_ON(release == NULL); | 
|  | 65 | WARN_ON(release == (void (*)(struct kref *))kfree); | 
|  | 66 |  | 
| Venkatesh Pallipadi | f334b60 | 2006-12-19 13:01:29 -0800 | [diff] [blame] | 67 | if (atomic_dec_and_test(&kref->refcount)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | release(kref); | 
|  | 69 | return 1; | 
|  | 70 | } | 
|  | 71 | return 0; | 
|  | 72 | } | 
|  | 73 |  | 
| Evgeniy Polyakov | 41ca28a | 2007-12-10 23:03:43 +0300 | [diff] [blame] | 74 | EXPORT_SYMBOL(kref_set); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | EXPORT_SYMBOL(kref_init); | 
|  | 76 | EXPORT_SYMBOL(kref_get); | 
|  | 77 | EXPORT_SYMBOL(kref_put); |