diff options
author | Peter Zijlstra <a.p.zijlstra@chello.nl> | 2011-12-10 05:43:43 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2011-12-13 11:41:43 -0500 |
commit | 47dbd7d90ad80edb67822f327241edcab8f3f46f (patch) | |
tree | 2e8880bfaf5da28b1bf2f265981e417d9afb4678 | |
parent | 4af679cd7cbb0a0d8774b5cdb34bffcaa4e86e52 (diff) |
kref: Implement kref_put in terms of kref_sub
Less lines of code is better.
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r-- | include/linux/kref.h | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h index 1cbae9f2ef77..fa9907a541e2 100644 --- a/include/linux/kref.h +++ b/include/linux/kref.h | |||
@@ -44,57 +44,49 @@ static inline void kref_get(struct kref *kref) | |||
44 | } | 44 | } |
45 | 45 | ||
46 | /** | 46 | /** |
47 | * kref_put - decrement refcount for object. | 47 | * kref_sub - subtract a number of refcounts for object. |
48 | * @kref: object. | 48 | * @kref: object. |
49 | * @count: Number of recounts to subtract. | ||
49 | * @release: pointer to the function that will clean up the object when the | 50 | * @release: pointer to the function that will clean up the object when the |
50 | * last reference to the object is released. | 51 | * last reference to the object is released. |
51 | * This pointer is required, and it is not acceptable to pass kfree | 52 | * This pointer is required, and it is not acceptable to pass kfree |
52 | * in as this function. | 53 | * in as this function. |
53 | * | 54 | * |
54 | * Decrement the refcount, and if 0, call release(). | 55 | * Subtract @count from the refcount, and if 0, call release(). |
55 | * Return 1 if the object was removed, otherwise return 0. Beware, if this | 56 | * Return 1 if the object was removed, otherwise return 0. Beware, if this |
56 | * function returns 0, you still can not count on the kref from remaining in | 57 | * function returns 0, you still can not count on the kref from remaining in |
57 | * memory. Only use the return value if you want to see if the kref is now | 58 | * memory. Only use the return value if you want to see if the kref is now |
58 | * gone, not present. | 59 | * gone, not present. |
59 | */ | 60 | */ |
60 | static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)) | 61 | static inline int kref_sub(struct kref *kref, unsigned int count, |
62 | void (*release)(struct kref *kref)) | ||
61 | { | 63 | { |
62 | WARN_ON(release == NULL); | 64 | WARN_ON(release == NULL); |
63 | WARN_ON(release == (void (*)(struct kref *))kfree); | 65 | WARN_ON(release == (void (*)(struct kref *))kfree); |
64 | 66 | ||
65 | if (atomic_dec_and_test(&kref->refcount)) { | 67 | if (atomic_sub_and_test((int) count, &kref->refcount)) { |
66 | release(kref); | 68 | release(kref); |
67 | return 1; | 69 | return 1; |
68 | } | 70 | } |
69 | return 0; | 71 | return 0; |
70 | } | 72 | } |
71 | 73 | ||
72 | |||
73 | /** | 74 | /** |
74 | * kref_sub - subtract a number of refcounts for object. | 75 | * kref_put - decrement refcount for object. |
75 | * @kref: object. | 76 | * @kref: object. |
76 | * @count: Number of recounts to subtract. | ||
77 | * @release: pointer to the function that will clean up the object when the | 77 | * @release: pointer to the function that will clean up the object when the |
78 | * last reference to the object is released. | 78 | * last reference to the object is released. |
79 | * This pointer is required, and it is not acceptable to pass kfree | 79 | * This pointer is required, and it is not acceptable to pass kfree |
80 | * in as this function. | 80 | * in as this function. |
81 | * | 81 | * |
82 | * Subtract @count from the refcount, and if 0, call release(). | 82 | * Decrement the refcount, and if 0, call release(). |
83 | * Return 1 if the object was removed, otherwise return 0. Beware, if this | 83 | * Return 1 if the object was removed, otherwise return 0. Beware, if this |
84 | * function returns 0, you still can not count on the kref from remaining in | 84 | * function returns 0, you still can not count on the kref from remaining in |
85 | * memory. Only use the return value if you want to see if the kref is now | 85 | * memory. Only use the return value if you want to see if the kref is now |
86 | * gone, not present. | 86 | * gone, not present. |
87 | */ | 87 | */ |
88 | static inline int kref_sub(struct kref *kref, unsigned int count, | 88 | static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)) |
89 | void (*release)(struct kref *kref)) | ||
90 | { | 89 | { |
91 | WARN_ON(release == NULL); | 90 | return kref_sub(kref, 1, release); |
92 | WARN_ON(release == (void (*)(struct kref *))kfree); | ||
93 | |||
94 | if (atomic_sub_and_test((int) count, &kref->refcount)) { | ||
95 | release(kref); | ||
96 | return 1; | ||
97 | } | ||
98 | return 0; | ||
99 | } | 91 | } |
100 | #endif /* _KREF_H_ */ | 92 | #endif /* _KREF_H_ */ |