Blame include/list.h

Packit 4a16fb
/* Doubly linked list macros compatible with Linux kernel's version
Packit 4a16fb
 * Copyright (c) 2015 by Takashi Iwai <tiwai@suse.de>
Packit 4a16fb
 *
Packit 4a16fb
 * This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 * it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 * published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 * the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 * This program is distributed in the hope that it will be useful,
Packit 4a16fb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 * GNU Lesser General Public License for more details.
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#ifndef _LIST_H
Packit 4a16fb
#define _LIST_H
Packit 4a16fb
Packit 4a16fb
#include <stddef.h>
Packit 4a16fb
Packit 4a16fb
struct list_head {
Packit 4a16fb
	struct list_head *next;
Packit 4a16fb
	struct list_head *prev;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
/* one-shot definition of a list head */
Packit 4a16fb
#define LIST_HEAD(x) \
Packit 4a16fb
	struct list_head x = { &x, &x }
Packit 4a16fb
Packit 4a16fb
/* initialize a list head explicitly */
Packit 4a16fb
static inline void INIT_LIST_HEAD(struct list_head *p)
Packit 4a16fb
{
Packit 4a16fb
	p->next = p->prev = p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#define list_entry_offset(p, type, offset) \
Packit 4a16fb
	((type *)((char *)(p) - (offset)))
Packit 4a16fb
Packit 4a16fb
/* list_entry - retrieve the original struct from list_head
Packit 4a16fb
 * @p: list_head pointer
Packit 4a16fb
 * @type: struct type
Packit 4a16fb
 * @member: struct field member containing the list_head
Packit 4a16fb
 */
Packit 4a16fb
#define list_entry(p, type, member) \
Packit 4a16fb
	list_entry_offset(p, type, offsetof(type, member))
Packit 4a16fb
Packit 4a16fb
/* list_for_each - iterate over the linked list
Packit 4a16fb
 * @p: iterator, a list_head pointer variable
Packit 4a16fb
 * @list: list_head pointer containing the list
Packit 4a16fb
 */
Packit 4a16fb
#define list_for_each(p, list) \
Packit 4a16fb
	for (p = (list)->next; p != (list); p = p->next)
Packit 4a16fb
Packit 4a16fb
/* list_for_each_safe - iterate over the linked list, safe to delete
Packit 4a16fb
 * @p: iterator, a list_head pointer variable
Packit 4a16fb
 * @s: a temporary variable to keep the next, a list_head pointer, too
Packit 4a16fb
 * @list: list_head pointer containing the list
Packit 4a16fb
 */
Packit 4a16fb
#define list_for_each_safe(p, s, list) \
Packit 4a16fb
	for (p = (list)->next; s = p->next, p != (list); p = s)
Packit 4a16fb
Packit 4a16fb
/* list_add - prepend a list entry at the head
Packit 4a16fb
 * @p: the new list entry to add
Packit 4a16fb
 * @list: the list head
Packit 4a16fb
 */
Packit 4a16fb
static inline void list_add(struct list_head *p, struct list_head *list)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *first = list->next;
Packit 4a16fb
Packit 4a16fb
	p->next = first;
Packit 4a16fb
	first->prev = p;
Packit 4a16fb
	list->next = p;
Packit 4a16fb
	p->prev = list;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* list_add_tail - append a list entry at the tail
Packit 4a16fb
 * @p: the new list entry to add
Packit 4a16fb
 * @list: the list head
Packit 4a16fb
 */
Packit 4a16fb
static inline void list_add_tail(struct list_head *p, struct list_head *list)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *last = list->prev;
Packit 4a16fb
Packit 4a16fb
	last->next = p;
Packit 4a16fb
	p->prev = last;
Packit 4a16fb
	p->next = list;
Packit 4a16fb
	list->prev = p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* list_insert - insert a new list entry between two known consecutive entries
Packit 4a16fb
 * @p: the new entry to be inserted between prev and next
Packit 4a16fb
 * @prev: the left-side entry
Packit 4a16fb
 * @next: the right-side entry
Packit 4a16fb
 */
Packit 4a16fb
static inline void list_insert(struct list_head *p,
Packit 4a16fb
			       struct list_head *prev,
Packit 4a16fb
			       struct list_head *next)
Packit 4a16fb
{
Packit 4a16fb
	next->prev = p;
Packit 4a16fb
	p->next = next;
Packit 4a16fb
	p->prev = prev;
Packit 4a16fb
	prev->next = p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* list_del - delete the given list entry */
Packit 4a16fb
static inline void list_del(struct list_head *p)
Packit 4a16fb
{
Packit 4a16fb
	p->prev->next = p->next;
Packit 4a16fb
	p->next->prev = p->prev;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* list_empty - returns 1 if the given list is empty */
Packit 4a16fb
static inline int list_empty(const struct list_head *p)
Packit 4a16fb
{
Packit 4a16fb
	return p->next == p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#endif /* _LIST_H */