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 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 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 READ_ONCE(x) ({ \ 74 __typeof(x) __var = ({ \ 75 barrier(); \ 76 ACCESS_ONCE(x); \ 77 }); \ 78 barrier(); \ 79 __var; \ 80 }) 81 82 #define HLIST_HEAD_INIT { } 83 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 84 #define INIT_HLIST_HEAD(head) (head)->first = NULL 85 86 #define INIT_HLIST_NODE(node) \ 87 do { \ 88 (node)->next = NULL; \ 89 (node)->pprev = NULL; \ 90 } while (0) 91 92 /* END CSTYLED */ 93 static inline int 94 atomic_read(const atomic_t *v) 95 { 96 return (READ_ONCE(v->counter)); 97 } 98 99 static inline int 100 atomic_inc(atomic_t *v) 101 { 102 return (atomic_fetchadd_int(&v->counter, 1) + 1); 103 } 104 105 static inline int 106 atomic_dec(atomic_t *v) 107 { 108 return (atomic_fetchadd_int(&v->counter, -1) - 1); 109 } 110 #endif 111