xref: /freebsd/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompat.h (revision b487b1f502899530951bd3923b3927c067d13ffb)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef	_SYS_CCOMPAT_H
29 #define	_SYS_CCOMPAT_H
30 
31 struct hlist_node {
32 	struct hlist_node *next, **pprev;
33 };
34 
35 struct hlist_head {
36 	struct hlist_node *first;
37 };
38 
39 typedef struct {
40 	volatile int counter;
41 } atomic_t;
42 
43 #define	hlist_for_each(p, head)                                      \
44 	for (p = (head)->first; p; p = (p)->next)
45 
46 #define	hlist_entry(ptr, type, field)   container_of(ptr, type, field)
47 
48 #define	container_of(ptr, type, member)                         \
49 /* CSTYLED */                                                   \
50 ({                                                              \
51 	const __typeof(((type *)0)->member) *__p = (ptr);       \
52 	(type *)((uintptr_t)__p - offsetof(type, member));      \
53 })
54 
55 static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)56 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
57 {
58 	n->next = h->first;
59 	if (h->first != NULL)
60 		h->first->pprev = &n->next;
61 	WRITE_ONCE(h->first, n);
62 	n->pprev = &h->first;
63 }
64 
65 static inline void
hlist_del(struct hlist_node * n)66 hlist_del(struct hlist_node *n)
67 {
68 	WRITE_ONCE(*(n->pprev), n->next);
69 	if (n->next != NULL)
70 		n->next->pprev = n->pprev;
71 }
72 	/* BEGIN CSTYLED */
73 #define	HLIST_HEAD_INIT { }
74 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
75 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
76 
77 #define	INIT_HLIST_NODE(node)					\
78 	do {																\
79 		(node)->next = NULL;											\
80 		(node)->pprev = NULL;											\
81 	} while (0)
82 
83 /* END CSTYLED */
84 static inline int
atomic_read(const atomic_t * v)85 atomic_read(const atomic_t *v)
86 {
87 	return (READ_ONCE(v->counter));
88 }
89 
90 static inline int
atomic_inc(atomic_t * v)91 atomic_inc(atomic_t *v)
92 {
93 	return (atomic_fetchadd_int(&v->counter, 1) + 1);
94 }
95 
96 static inline int
atomic_dec(atomic_t * v)97 atomic_dec(atomic_t *v)
98 {
99 	return (atomic_fetchadd_int(&v->counter, -1) - 1);
100 }
101 #endif
102