diff options
-rw-r--r-- | include/linux/kref.h | 80 | ||||
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/kref.c | 97 |
3 files changed, 76 insertions, 103 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h index d4a62ab2ee5..1cbae9f2ef7 100644 --- a/include/linux/kref.h +++ b/include/linux/kref.h | |||
@@ -16,15 +16,85 @@ | |||
16 | #define _KREF_H_ | 16 | #define _KREF_H_ |
17 | 17 | ||
18 | #include <linux/types.h> | 18 | #include <linux/types.h> |
19 | #include <linux/slab.h> | ||
19 | 20 | ||
20 | struct kref { | 21 | struct kref { |
21 | atomic_t refcount; | 22 | atomic_t refcount; |
22 | }; | 23 | }; |
23 | 24 | ||
24 | void kref_init(struct kref *kref); | 25 | /** |
25 | void kref_get(struct kref *kref); | 26 | * kref_init - initialize object. |
26 | int kref_put(struct kref *kref, void (*release) (struct kref *kref)); | 27 | * @kref: object in question. |
27 | int kref_sub(struct kref *kref, unsigned int count, | 28 | */ |
28 | void (*release) (struct kref *kref)); | 29 | static inline void kref_init(struct kref *kref) |
30 | { | ||
31 | atomic_set(&kref->refcount, 1); | ||
32 | smp_mb(); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * kref_get - increment refcount for object. | ||
37 | * @kref: object. | ||
38 | */ | ||
39 | static inline void kref_get(struct kref *kref) | ||
40 | { | ||
41 | WARN_ON(!atomic_read(&kref->refcount)); | ||
42 | atomic_inc(&kref->refcount); | ||
43 | smp_mb__after_atomic_inc(); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * kref_put - decrement refcount for object. | ||
48 | * @kref: object. | ||
49 | * @release: pointer to the function that will clean up the object when the | ||
50 | * last reference to the object is released. | ||
51 | * This pointer is required, and it is not acceptable to pass kfree | ||
52 | * in as this function. | ||
53 | * | ||
54 | * Decrement the refcount, and if 0, call release(). | ||
55 | * 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 | * memory. Only use the return value if you want to see if the kref is now | ||
58 | * gone, not present. | ||
59 | */ | ||
60 | static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)) | ||
61 | { | ||
62 | WARN_ON(release == NULL); | ||
63 | WARN_ON(release == (void (*)(struct kref *))kfree); | ||
64 | |||
65 | if (atomic_dec_and_test(&kref->refcount)) { | ||
66 | release(kref); | ||
67 | return 1; | ||
68 | } | ||
69 | return 0; | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * kref_sub - subtract a number of refcounts for object. | ||
75 | * @kref: object. | ||
76 | * @count: Number of recounts to subtract. | ||
77 | * @release: pointer to the function that will clean up the object when the | ||
78 | * last reference to the object is released. | ||
79 | * This pointer is required, and it is not acceptable to pass kfree | ||
80 | * in as this function. | ||
81 | * | ||
82 | * Subtract @count from the refcount, and if 0, call release(). | ||
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 | ||
85 | * memory. Only use the return value if you want to see if the kref is now | ||
86 | * gone, not present. | ||
87 | */ | ||
88 | static inline int kref_sub(struct kref *kref, unsigned int count, | ||
89 | void (*release)(struct kref *kref)) | ||
90 | { | ||
91 | WARN_ON(release == NULL); | ||
92 | WARN_ON(release == (void (*)(struct kref *))kfree); | ||
29 | 93 | ||
94 | if (atomic_sub_and_test((int) count, &kref->refcount)) { | ||
95 | release(kref); | ||
96 | return 1; | ||
97 | } | ||
98 | return 0; | ||
99 | } | ||
30 | #endif /* _KREF_H_ */ | 100 | #endif /* _KREF_H_ */ |
diff --git a/lib/Makefile b/lib/Makefile index a4da283f5dc..6f195ff6a1a 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -17,7 +17,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \ | |||
17 | lib-$(CONFIG_MMU) += ioremap.o | 17 | lib-$(CONFIG_MMU) += ioremap.o |
18 | lib-$(CONFIG_SMP) += cpumask.o | 18 | lib-$(CONFIG_SMP) += cpumask.o |
19 | 19 | ||
20 | lib-y += kobject.o kref.o klist.o | 20 | lib-y += kobject.o klist.o |
21 | 21 | ||
22 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ | 22 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ |
23 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ | 23 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ |
diff --git a/lib/kref.c b/lib/kref.c deleted file mode 100644 index 3efb882b11d..00000000000 --- a/lib/kref.c +++ /dev/null | |||
@@ -1,97 +0,0 @@ | |||
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 | #include <linux/slab.h> | ||
17 | |||
18 | /** | ||
19 | * kref_init - initialize object. | ||
20 | * @kref: object in question. | ||
21 | */ | ||
22 | void kref_init(struct kref *kref) | ||
23 | { | ||
24 | atomic_set(&kref->refcount, 1); | ||
25 | smp_mb(); | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * kref_get - increment refcount for object. | ||
30 | * @kref: object. | ||
31 | */ | ||
32 | void kref_get(struct kref *kref) | ||
33 | { | ||
34 | WARN_ON(!atomic_read(&kref->refcount)); | ||
35 | atomic_inc(&kref->refcount); | ||
36 | smp_mb__after_atomic_inc(); | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * kref_put - decrement refcount for object. | ||
41 | * @kref: object. | ||
42 | * @release: pointer to the function that will clean up the object when the | ||
43 | * last reference to the object is released. | ||
44 | * This pointer is required, and it is not acceptable to pass kfree | ||
45 | * in as this function. | ||
46 | * | ||
47 | * Decrement the refcount, and if 0, call release(). | ||
48 | * Return 1 if the object was removed, otherwise return 0. Beware, if this | ||
49 | * function returns 0, you still can not count on the kref from remaining in | ||
50 | * memory. Only use the return value if you want to see if the kref is now | ||
51 | * gone, not present. | ||
52 | */ | ||
53 | int kref_put(struct kref *kref, void (*release)(struct kref *kref)) | ||
54 | { | ||
55 | WARN_ON(release == NULL); | ||
56 | WARN_ON(release == (void (*)(struct kref *))kfree); | ||
57 | |||
58 | if (atomic_dec_and_test(&kref->refcount)) { | ||
59 | release(kref); | ||
60 | return 1; | ||
61 | } | ||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | |||
66 | /** | ||
67 | * kref_sub - subtract a number of refcounts for object. | ||
68 | * @kref: object. | ||
69 | * @count: Number of recounts to subtract. | ||
70 | * @release: pointer to the function that will clean up the object when the | ||
71 | * last reference to the object is released. | ||
72 | * This pointer is required, and it is not acceptable to pass kfree | ||
73 | * in as this function. | ||
74 | * | ||
75 | * Subtract @count from the refcount, and if 0, call release(). | ||
76 | * Return 1 if the object was removed, otherwise return 0. Beware, if this | ||
77 | * function returns 0, you still can not count on the kref from remaining in | ||
78 | * memory. Only use the return value if you want to see if the kref is now | ||
79 | * gone, not present. | ||
80 | */ | ||
81 | int kref_sub(struct kref *kref, unsigned int count, | ||
82 | void (*release)(struct kref *kref)) | ||
83 | { | ||
84 | WARN_ON(release == NULL); | ||
85 | WARN_ON(release == (void (*)(struct kref *))kfree); | ||
86 | |||
87 | if (atomic_sub_and_test((int) count, &kref->refcount)) { | ||
88 | release(kref); | ||
89 | return 1; | ||
90 | } | ||
91 | return 0; | ||
92 | } | ||
93 | |||
94 | EXPORT_SYMBOL(kref_init); | ||
95 | EXPORT_SYMBOL(kref_get); | ||
96 | EXPORT_SYMBOL(kref_put); | ||
97 | EXPORT_SYMBOL(kref_sub); | ||