1*3f2dd94aSFrançois Tigeot /*
2*3f2dd94aSFrançois Tigeot * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
3*3f2dd94aSFrançois Tigeot * All rights reserved.
4*3f2dd94aSFrançois Tigeot *
5*3f2dd94aSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
6*3f2dd94aSFrançois Tigeot * modification, are permitted provided that the following conditions
7*3f2dd94aSFrançois Tigeot * are met:
8*3f2dd94aSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
9*3f2dd94aSFrançois Tigeot * notice unmodified, this list of conditions, and the following
10*3f2dd94aSFrançois Tigeot * disclaimer.
11*3f2dd94aSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
12*3f2dd94aSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
13*3f2dd94aSFrançois Tigeot * documentation and/or other materials provided with the distribution.
14*3f2dd94aSFrançois Tigeot *
15*3f2dd94aSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*3f2dd94aSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*3f2dd94aSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*3f2dd94aSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*3f2dd94aSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*3f2dd94aSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*3f2dd94aSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*3f2dd94aSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*3f2dd94aSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*3f2dd94aSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*3f2dd94aSFrançois Tigeot */
26*3f2dd94aSFrançois Tigeot
27*3f2dd94aSFrançois Tigeot #ifndef _LINUX_REFCOUNT_H_
28*3f2dd94aSFrançois Tigeot #define _LINUX_REFCOUNT_H_
29*3f2dd94aSFrançois Tigeot
30*3f2dd94aSFrançois Tigeot #include <linux/atomic.h>
31*3f2dd94aSFrançois Tigeot #include <linux/mutex.h>
32*3f2dd94aSFrançois Tigeot #include <linux/spinlock.h>
33*3f2dd94aSFrançois Tigeot #include <linux/kernel.h>
34*3f2dd94aSFrançois Tigeot
35*3f2dd94aSFrançois Tigeot typedef struct refcount_struct {
36*3f2dd94aSFrançois Tigeot atomic_t refs;
37*3f2dd94aSFrançois Tigeot } refcount_t;
38*3f2dd94aSFrançois Tigeot
39*3f2dd94aSFrançois Tigeot #define REFCOUNT_SATURATED (INT_MIN / 2)
40*3f2dd94aSFrançois Tigeot
41*3f2dd94aSFrançois Tigeot static inline void
refcount_set(refcount_t * r,unsigned int n)42*3f2dd94aSFrançois Tigeot refcount_set(refcount_t *r, unsigned int n)
43*3f2dd94aSFrançois Tigeot {
44*3f2dd94aSFrançois Tigeot atomic_set(&r->refs, n);
45*3f2dd94aSFrançois Tigeot }
46*3f2dd94aSFrançois Tigeot
47*3f2dd94aSFrançois Tigeot static inline bool
refcount_dec_and_test(refcount_t * r)48*3f2dd94aSFrançois Tigeot refcount_dec_and_test(refcount_t *r)
49*3f2dd94aSFrançois Tigeot {
50*3f2dd94aSFrançois Tigeot return atomic_dec_and_test(&r->refs);
51*3f2dd94aSFrançois Tigeot }
52*3f2dd94aSFrançois Tigeot
53*3f2dd94aSFrançois Tigeot static inline void
refcount_inc(refcount_t * r)54*3f2dd94aSFrançois Tigeot refcount_inc(refcount_t *r)
55*3f2dd94aSFrançois Tigeot {
56*3f2dd94aSFrançois Tigeot int old_value = atomic_fetchadd_int(&r->refs.counter, 1);
57*3f2dd94aSFrançois Tigeot
58*3f2dd94aSFrançois Tigeot if (old_value <= 0)
59*3f2dd94aSFrançois Tigeot refcount_set(r, REFCOUNT_SATURATED);
60*3f2dd94aSFrançois Tigeot }
61*3f2dd94aSFrançois Tigeot
62*3f2dd94aSFrançois Tigeot #endif /* _LINUX_REFCOUNT_H_ */
63