]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/refcount.h
Fix build with DRM and INVARIANTS enabled.
[FreeBSD/FreeBSD.git] / sys / sys / refcount.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #ifndef __SYS_REFCOUNT_H__
31 #define __SYS_REFCOUNT_H__
32
33 #include <machine/atomic.h>
34
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #else
38 #include <stdbool.h>
39 #define KASSERT(exp, msg)       /* */
40 #endif
41
42 #define REFCOUNT_SATURATED(val)         (((val) & (1U << 31)) != 0)
43 #define REFCOUNT_SATURATION_VALUE       (3U << 30)
44
45 /*
46  * Attempt to handle reference count overflow and underflow.  Force the counter
47  * to stay at the saturation value so that a counter overflow cannot trigger
48  * destruction of the containing object and instead leads to a less harmful
49  * memory leak.
50  */
51 static __inline void
52 _refcount_update_saturated(volatile u_int *count)
53 {
54 #ifdef INVARIANTS
55         panic("refcount %p wraparound", count);
56 #else
57         atomic_store_int(count, REFCOUNT_SATURATION_VALUE);
58 #endif
59 }
60
61 static __inline void
62 refcount_init(volatile u_int *count, u_int value)
63 {
64         KASSERT(!REFCOUNT_SATURATED(value),
65             ("invalid initial refcount value %u", value));
66         *count = value;
67 }
68
69 static __inline void
70 refcount_acquire(volatile u_int *count)
71 {
72         u_int old;
73
74         old = atomic_fetchadd_int(count, 1);
75         if (__predict_false(REFCOUNT_SATURATED(old)))
76                 _refcount_update_saturated(count);
77 }
78
79 static __inline __result_use_check bool
80 refcount_acquire_checked(volatile u_int *count)
81 {
82         u_int lcount;
83
84         for (lcount = *count;;) {
85                 if (__predict_false(REFCOUNT_SATURATED(lcount + 1)))
86                         return (false);
87                 if (__predict_true(atomic_fcmpset_int(count, &lcount,
88                     lcount + 1) == 1))
89                         return (true);
90         }
91 }
92
93 static __inline bool
94 refcount_release(volatile u_int *count)
95 {
96         u_int old;
97
98         atomic_thread_fence_rel();
99         old = atomic_fetchadd_int(count, -1);
100         if (__predict_false(old == 0 || REFCOUNT_SATURATED(old))) {
101                 /*
102                  * Avoid multiple destructor invocations if underflow occurred.
103                  * This is not perfect since the memory backing the containing
104                  * object may already have been reallocated.
105                  */
106                 _refcount_update_saturated(count);
107                 return (false);
108         }
109         if (old > 1)
110                 return (false);
111
112         /*
113          * Last reference.  Signal the user to call the destructor.
114          *
115          * Ensure that the destructor sees all updates.  The fence_rel
116          * at the start of the function synchronizes with this fence.
117          */
118         atomic_thread_fence_acq();
119         return (true);
120 }
121
122 /*
123  * This functions returns non-zero if the refcount was
124  * incremented. Else zero is returned.
125  */
126 static __inline __result_use_check bool
127 refcount_acquire_if_not_zero(volatile u_int *count)
128 {
129         u_int old;
130
131         old = *count;
132         for (;;) {
133                 if (old == 0)
134                         return (false);
135                 if (__predict_false(REFCOUNT_SATURATED(old)))
136                         return (true);
137                 if (atomic_fcmpset_int(count, &old, old + 1))
138                         return (true);
139         }
140 }
141
142 static __inline __result_use_check bool
143 refcount_release_if_not_last(volatile u_int *count)
144 {
145         u_int old;
146
147         old = *count;
148         for (;;) {
149                 if (old == 1)
150                         return (false);
151                 if (__predict_false(REFCOUNT_SATURATED(old)))
152                         return (true);
153                 if (atomic_fcmpset_int(count, &old, old - 1))
154                         return (true);
155         }
156 }
157
158 #endif  /* ! __SYS_REFCOUNT_H__ */