diff options
Diffstat (limited to 'include/linux/refcount.h')
-rw-r--r-- | include/linux/refcount.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 0023fee4bbbc..b34aa649d204 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h | |||
@@ -6,17 +6,36 @@ | |||
6 | #include <linux/spinlock.h> | 6 | #include <linux/spinlock.h> |
7 | #include <linux/kernel.h> | 7 | #include <linux/kernel.h> |
8 | 8 | ||
9 | /** | ||
10 | * refcount_t - variant of atomic_t specialized for reference counts | ||
11 | * @refs: atomic_t counter field | ||
12 | * | ||
13 | * The counter saturates at UINT_MAX and will not move once | ||
14 | * there. This avoids wrapping the counter and causing 'spurious' | ||
15 | * use-after-free bugs. | ||
16 | */ | ||
9 | typedef struct refcount_struct { | 17 | typedef struct refcount_struct { |
10 | atomic_t refs; | 18 | atomic_t refs; |
11 | } refcount_t; | 19 | } refcount_t; |
12 | 20 | ||
13 | #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } | 21 | #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } |
14 | 22 | ||
23 | /** | ||
24 | * refcount_set - set a refcount's value | ||
25 | * @r: the refcount | ||
26 | * @n: value to which the refcount will be set | ||
27 | */ | ||
15 | static inline void refcount_set(refcount_t *r, unsigned int n) | 28 | static inline void refcount_set(refcount_t *r, unsigned int n) |
16 | { | 29 | { |
17 | atomic_set(&r->refs, n); | 30 | atomic_set(&r->refs, n); |
18 | } | 31 | } |
19 | 32 | ||
33 | /** | ||
34 | * refcount_read - get a refcount's value | ||
35 | * @r: the refcount | ||
36 | * | ||
37 | * Return: the refcount's value | ||
38 | */ | ||
20 | static inline unsigned int refcount_read(const refcount_t *r) | 39 | static inline unsigned int refcount_read(const refcount_t *r) |
21 | { | 40 | { |
22 | return atomic_read(&r->refs); | 41 | return atomic_read(&r->refs); |