1 | #ifndef _LINUX_REFCOUNT_H
|
---|
2 | #define _LINUX_REFCOUNT_H
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * struct refcount_t - variant of atomic_t specialized for reference counts
|
---|
6 | * @refs: atomic_t counter field
|
---|
7 | *
|
---|
8 | * The counter saturates at UINT_MAX and will not move once
|
---|
9 | * there. This avoids wrapping the counter and causing 'spurious'
|
---|
10 | * use-after-free bugs.
|
---|
11 | */
|
---|
12 | typedef struct refcount_struct {
|
---|
13 | atomic_t refs;
|
---|
14 | } refcount_t;
|
---|
15 |
|
---|
16 | #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * refcount_read - get a refcount's value
|
---|
20 | * @r: the refcount
|
---|
21 | *
|
---|
22 | * Return: the refcount's value
|
---|
23 | */
|
---|
24 | static inline unsigned int refcount_read(const refcount_t *r)
|
---|
25 | {
|
---|
26 | return atomic_read(&r->refs);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * refcount_set - set a refcount's value
|
---|
31 | * @r: the refcount
|
---|
32 | * @n: value to which the refcount will be set
|
---|
33 | */
|
---|
34 | static inline void refcount_set(refcount_t *r, unsigned int n)
|
---|
35 | {
|
---|
36 | atomic_set(&r->refs, n);
|
---|
37 | }
|
---|
38 |
|
---|
39 | static inline void refcount_add(unsigned int i, refcount_t *r)
|
---|
40 | {
|
---|
41 | atomic_add(i, &r->refs);
|
---|
42 | }
|
---|
43 |
|
---|
44 | static inline void refcount_inc(refcount_t *r)
|
---|
45 | {
|
---|
46 | atomic_inc(&r->refs);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r)
|
---|
50 | {
|
---|
51 | return atomic_sub_and_test(i, &r->refs);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static inline __must_check bool refcount_dec_and_test(refcount_t *r)
|
---|
55 | {
|
---|
56 | return atomic_dec_and_test(&r->refs);
|
---|
57 | }
|
---|
58 |
|
---|
59 | static inline void refcount_dec(refcount_t *r)
|
---|
60 | {
|
---|
61 | atomic_dec(&r->refs);
|
---|
62 | }
|
---|
63 |
|
---|
64 | #endif /* _LINUX_REFCOUNT_H */
|
---|