zfs/include/os/freebsd/spl/sys/ccompat.h

111 lines
3.1 KiB
C

/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _SYS_CCOMPAT_H
#define _SYS_CCOMPAT_H
struct hlist_node {
struct hlist_node *next, **pprev;
};
struct hlist_head {
struct hlist_node *first;
};
typedef struct {
volatile int counter;
} atomic_t;
#define hlist_for_each(p, head) \
for (p = (head)->first; p; p = (p)->next)
#define hlist_entry(ptr, type, field) container_of(ptr, type, field)
#define container_of(ptr, type, member) \
/* CSTYLED */ \
({ \
const __typeof(((type *)0)->member) *__p = (ptr); \
(type *)((uintptr_t)__p - offsetof(type, member)); \
})
static inline void
hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
n->next = h->first;
if (h->first != NULL)
h->first->pprev = &n->next;
WRITE_ONCE(h->first, n);
n->pprev = &h->first;
}
static inline void
hlist_del(struct hlist_node *n)
{
WRITE_ONCE(*(n->pprev), n->next);
if (n->next != NULL)
n->next->pprev = n->pprev;
}
/* BEGIN CSTYLED */
#define READ_ONCE(x) ({ \
__typeof(x) __var = ({ \
barrier(); \
ACCESS_ONCE(x); \
}); \
barrier(); \
__var; \
})
#define HLIST_HEAD_INIT { }
#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
#define INIT_HLIST_HEAD(head) (head)->first = NULL
#define INIT_HLIST_NODE(node) \
do { \
(node)->next = NULL; \
(node)->pprev = NULL; \
} while (0)
/* END CSTYLED */
static inline int
atomic_read(const atomic_t *v)
{
return (READ_ONCE(v->counter));
}
static inline int
atomic_inc(atomic_t *v)
{
return (atomic_fetchadd_int(&v->counter, 1) + 1);
}
static inline int
atomic_dec(atomic_t *v)
{
return (atomic_fetchadd_int(&v->counter, -1) - 1);
}
#endif