crypto: atmel-sha - remove unnecessary static in atmel_sha_remove()
[linux-stable.git] / include / linux / refcount.h
blob591792c8e5b0cc12be5806aa349d6a373f41cde4
1 #ifndef _LINUX_REFCOUNT_H
2 #define _LINUX_REFCOUNT_H
4 #include <linux/atomic.h>
5 #include <linux/mutex.h>
6 #include <linux/spinlock.h>
7 #include <linux/kernel.h>
9 /**
10 * refcount_t - variant of atomic_t specialized for reference counts
11 * @refs: atomic_t counter field
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.
17 typedef struct refcount_struct {
18 atomic_t refs;
19 } refcount_t;
21 #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
23 /**
24 * refcount_set - set a refcount's value
25 * @r: the refcount
26 * @n: value to which the refcount will be set
28 static inline void refcount_set(refcount_t *r, unsigned int n)
30 atomic_set(&r->refs, n);
33 /**
34 * refcount_read - get a refcount's value
35 * @r: the refcount
37 * Return: the refcount's value
39 static inline unsigned int refcount_read(const refcount_t *r)
41 return atomic_read(&r->refs);
44 #ifdef CONFIG_REFCOUNT_FULL
45 extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
46 extern void refcount_add(unsigned int i, refcount_t *r);
48 extern __must_check bool refcount_inc_not_zero(refcount_t *r);
49 extern void refcount_inc(refcount_t *r);
51 extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r);
53 extern __must_check bool refcount_dec_and_test(refcount_t *r);
54 extern void refcount_dec(refcount_t *r);
55 #else
56 static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r)
58 return atomic_add_unless(&r->refs, i, 0);
61 static inline void refcount_add(unsigned int i, refcount_t *r)
63 atomic_add(i, &r->refs);
66 static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
68 return atomic_add_unless(&r->refs, 1, 0);
71 static inline void refcount_inc(refcount_t *r)
73 atomic_inc(&r->refs);
76 static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r)
78 return atomic_sub_and_test(i, &r->refs);
81 static inline __must_check bool refcount_dec_and_test(refcount_t *r)
83 return atomic_dec_and_test(&r->refs);
86 static inline void refcount_dec(refcount_t *r)
88 atomic_dec(&r->refs);
90 #endif /* CONFIG_REFCOUNT_FULL */
92 extern __must_check bool refcount_dec_if_one(refcount_t *r);
93 extern __must_check bool refcount_dec_not_one(refcount_t *r);
94 extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
95 extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
97 #endif /* _LINUX_REFCOUNT_H */