#ifndef _SYS_PSLIST_H
#define _SYS_PSLIST_H
#include <sys/param.h>
#include <sys/atomic.h>
struct pslist_head;
struct pslist_entry;
struct pslist_head {
struct pslist_entry *plh_first;
};
struct pslist_entry {
struct pslist_entry **ple_prevp;
struct pslist_entry *ple_next;
};
#ifdef _KERNEL
#define _PSLIST_ASSERT KASSERT
#else
#include <assert.h>
#define _PSLIST_ASSERT assert
#endif
#define _PSLIST_POISON ((void *)1ul)
static __inline void
pslist_init(struct pslist_head *head)
{
head->plh_first = NULL;
}
static __inline void
pslist_destroy(struct pslist_head *head __diagused)
{
_PSLIST_ASSERT(head->plh_first == NULL);
}
static __inline void
pslist_entry_init(struct pslist_entry *entry)
{
entry->ple_next = NULL;
entry->ple_prevp = NULL;
}
static __inline void
pslist_entry_destroy(struct pslist_entry *entry)
{
_PSLIST_ASSERT(entry->ple_prevp == NULL);
atomic_store_relaxed(&entry->ple_next, _PSLIST_POISON);
}
static __inline void
pslist_writer_insert_head(struct pslist_head *head, struct pslist_entry *new)
{
_PSLIST_ASSERT(head->plh_first == NULL ||
head->plh_first->ple_prevp == &head->plh_first);
_PSLIST_ASSERT(new->ple_next == NULL);
_PSLIST_ASSERT(new->ple_prevp == NULL);
new->ple_prevp = &head->plh_first;
new->ple_next = head->plh_first;
if (head->plh_first != NULL)
head->plh_first->ple_prevp = &new->ple_next;
atomic_store_release(&head->plh_first, new);
}
static __inline void
pslist_writer_insert_before(struct pslist_entry *entry,
struct pslist_entry *new)
{
_PSLIST_ASSERT(entry->ple_next != _PSLIST_POISON);
_PSLIST_ASSERT(entry->ple_prevp != NULL);
_PSLIST_ASSERT(*entry->ple_prevp == entry);
_PSLIST_ASSERT(new->ple_next == NULL);
_PSLIST_ASSERT(new->ple_prevp == NULL);
new->ple_prevp = entry->ple_prevp;
new->ple_next = entry;
atomic_store_release(entry->ple_prevp, new);
entry->ple_prevp = &new->ple_next;
}
static __inline void
pslist_writer_insert_after(struct pslist_entry *entry,
struct pslist_entry *new)
{
_PSLIST_ASSERT(entry->ple_next != _PSLIST_POISON);
_PSLIST_ASSERT(entry->ple_prevp != NULL);
_PSLIST_ASSERT(*entry->ple_prevp == entry);
_PSLIST_ASSERT(new->ple_next == NULL);
_PSLIST_ASSERT(new->ple_prevp == NULL);
new->ple_prevp = &entry->ple_next;
new->ple_next = entry->ple_next;
if (new->ple_next != NULL)
new->ple_next->ple_prevp = &new->ple_next;
atomic_store_release(&entry->ple_next, new);
}
static __inline void
pslist_writer_remove(struct pslist_entry *entry)
{
_PSLIST_ASSERT(entry->ple_next != _PSLIST_POISON);
_PSLIST_ASSERT(entry->ple_prevp != NULL);
_PSLIST_ASSERT(*entry->ple_prevp == entry);
if (entry->ple_next != NULL)
entry->ple_next->ple_prevp = entry->ple_prevp;
atomic_store_relaxed(entry->ple_prevp, entry->ple_next);
entry->ple_prevp = NULL;
}
static __inline struct pslist_entry *
pslist_writer_first(const struct pslist_head *head)
{
return head->plh_first;
}
static __inline struct pslist_entry *
pslist_writer_next(const struct pslist_entry *entry)
{
_PSLIST_ASSERT(entry->ple_next != _PSLIST_POISON);
return entry->ple_next;
}
static __inline void *
_pslist_writer_first_container(const struct pslist_head *head,
const ptrdiff_t offset)
{
struct pslist_entry *first = head->plh_first;
return (first == NULL ? NULL : (char *)first - offset);
}
static __inline void *
_pslist_writer_next_container(const struct pslist_entry *entry,
const ptrdiff_t offset)
{
struct pslist_entry *next = entry->ple_next;
_PSLIST_ASSERT(next != _PSLIST_POISON);
return (next == NULL ? NULL : (char *)next - offset);
}
static __inline struct pslist_entry *
pslist_reader_first(const struct pslist_head *head)
{
return atomic_load_consume(&head->plh_first);
}
static __inline struct pslist_entry *
pslist_reader_next(const struct pslist_entry *entry)
{
struct pslist_entry *next = atomic_load_consume(&entry->ple_next);
_PSLIST_ASSERT(next != _PSLIST_POISON);
return next;
}
static __inline void *
_pslist_reader_first_container(const struct pslist_head *head,
const ptrdiff_t offset)
{
struct pslist_entry *first = pslist_reader_first(head);
if (first == NULL)
return NULL;
return (char *)first - offset;
}
static __inline void *
_pslist_reader_next_container(const struct pslist_entry *entry,
const ptrdiff_t offset)
{
struct pslist_entry *next = pslist_reader_next(entry);
if (next == NULL)
return NULL;
return (char *)next - offset;
}
#if defined(__COVERITY__) || defined(__LGTM_BOT__)
#define _PSLIST_VALIDATE_PTRS(P, Q) 0
#define _PSLIST_VALIDATE_CONTAINER(P, T, F) 0
#else
#define _PSLIST_VALIDATE_PTRS(P, Q) \
(0 * sizeof((P) - (Q)) * sizeof(*(P)) * sizeof(*(Q)))
#define _PSLIST_VALIDATE_CONTAINER(P, T, F) \
(0 * sizeof((P) - &((T *)(((char *)(P)) - offsetof(T, F)))->F))
#endif
#define PSLIST_INITIALIZER { .plh_first = NULL }
#define PSLIST_ENTRY_INITIALIZER { .ple_next = NULL, .ple_prevp = NULL }
#define PSLIST_INIT(H) pslist_init((H))
#define PSLIST_DESTROY(H) pslist_destroy((H))
#define PSLIST_ENTRY_INIT(E, F) pslist_entry_init(&(E)->F)
#define PSLIST_ENTRY_DESTROY(E, F) pslist_entry_destroy(&(E)->F)
#define PSLIST_WRITER_INSERT_HEAD(H, V, F) \
pslist_writer_insert_head((H), &(V)->F)
#define PSLIST_WRITER_INSERT_BEFORE(E, N, F) \
pslist_writer_insert_before(&(E)->F + _PSLIST_VALIDATE_PTRS(E, N), \
&(N)->F)
#define PSLIST_WRITER_INSERT_AFTER(E, N, F) \
pslist_writer_insert_after(&(E)->F + _PSLIST_VALIDATE_PTRS(E, N), \
&(N)->F)
#define PSLIST_WRITER_REMOVE(E, F) \
pslist_writer_remove(&(E)->F)
#define PSLIST_WRITER_FIRST(H, T, F) \
((T *)(_pslist_writer_first_container((H), offsetof(T, F))) + \
_PSLIST_VALIDATE_CONTAINER(pslist_writer_first(H), T, F))
#define PSLIST_WRITER_NEXT(V, T, F) \
((T *)(_pslist_writer_next_container(&(V)->F, offsetof(T, F))) + \
_PSLIST_VALIDATE_CONTAINER(pslist_writer_next(&(V)->F), T, F))
#define PSLIST_WRITER_FOREACH(V, H, T, F) \
for ((V) = PSLIST_WRITER_FIRST((H), T, F); \
(V) != NULL; \
(V) = PSLIST_WRITER_NEXT((V), T, F))
#define PSLIST_READER_FIRST(H, T, F) \
((T *)(_pslist_reader_first_container((H), offsetof(T, F))) + \
_PSLIST_VALIDATE_CONTAINER(pslist_reader_first(H), T, F))
#define PSLIST_READER_NEXT(V, T, F) \
((T *)(_pslist_reader_next_container(&(V)->F, offsetof(T, F))) + \
_PSLIST_VALIDATE_CONTAINER(pslist_reader_next(&(V)->F), T, F))
#define PSLIST_READER_FOREACH(V, H, T, F) \
for ((V) = PSLIST_READER_FIRST((H), T, F); \
(V) != NULL; \
(V) = PSLIST_READER_NEXT((V), T, F))
#endif