Replace list.h with its own version

We copied include/list.h from Linux kernel, and it's of course in
GPLv2.  This has raised concerns to many people, as it's not clear
whether such a code is considered to be completely trivial, thus it
might be seen as a derivative work, which takes effect in GPL, as
suggested by Clemens.

For clearing the situation, this patch replaces the existing list.h
implementation from a new version.  The API is kept to be compatible,
but the codes were written from full scratch under LGPL, to be aligned
with other alsa-lib codes.

Reported-by: Clemens Lang <clemens.lang@bmw-carit.de>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2015-07-27 12:32:37 +02:00
parent 77b6be6387
commit d0e13f8774

View file

@ -1,174 +1,102 @@
/* Doubly linked list macros compatible with Linux kernel's version
* Copyright (c) 2015 by Takashi Iwai <tiwai@suse.de>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
#ifndef _LIST_H #ifndef _LIST_H
#define _LIST_H #define _LIST_H
/* #include <stddef.h>
* This code was taken from the Linux 2.4.0 kernel. [jaroslav]
*/
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
#ifndef LIST_HEAD_IS_DEFINED
struct list_head { struct list_head {
struct list_head *next, *prev; struct list_head *next;
struct list_head *prev;
}; };
#endif
#define LIST_HEAD_INIT(name) { &(name), &(name) } /* one-shot definition of a list head */
#define LIST_HEAD(x) \
struct list_head x = { &x, &x }
#define LIST_HEAD(name) \ /* initialize a list head explicitly */
struct list_head name = LIST_HEAD_INIT(name) static inline void INIT_LIST_HEAD(struct list_head *p)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static __inline__ void __list_add(struct list_head * _new,
struct list_head * prev,
struct list_head * next)
{ {
next->prev = _new; p->next = p->prev = p;
_new->next = next;
_new->prev = prev;
prev->next = _new;
} }
/** #define list_entry_offset(p, type, offset) \
* list_add - add a new entry ((type *)((char *)(p) - (offset)))
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static __inline__ void list_add(struct list_head *_new, struct list_head *head)
{
__list_add(_new, head, head->next);
}
/** /* list_entry - retrieve the original struct from list_head
* list_add_tail - add a new entry * @p: list_head pointer
* @new: new entry to be added * @type: struct type
* @head: list head to add it before * @member: struct field member containing the list_head
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/ */
static __inline__ void list_add_tail(struct list_head *_new, struct list_head *head) #define list_entry(p, type, member) \
{ list_entry_offset(p, type, offsetof(type, member))
__list_add(_new, head->prev, head);
}
/* /* list_for_each - iterate over the linked list
* Delete a list entry by making the prev/next entries * @p: iterator, a list_head pointer variable
* point to each other. * @list: list_head pointer containing the list
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/ */
static __inline__ void __list_del(struct list_head * prev, #define list_for_each(p, list) \
struct list_head * next) for (p = (list)->next; p != (list); p = p->next)
{
next->prev = prev;
prev->next = next;
}
/** /* list_for_each_safe - iterate over the linked list, safe to delete
* list_del - deletes entry from list. * @p: iterator, a list_head pointer variable
* @entry: the element to delete from the list. * @s: a temporary variable to keep the next, a list_head pointer, too
* Note: list_empty on entry does not return true after this, the entry is in an undefined state. * @list: list_head pointer containing the list
*/ */
static __inline__ void list_del(struct list_head *entry) #define list_for_each_safe(p, s, list) \
{ for (p = (list)->next; s = p->next, p != (list); p = s)
__list_del(entry->prev, entry->next);
}
/** /* list_add - prepend a list entry at the head
* list_del_init - deletes entry from list and reinitialize it. * @p: the new list entry to add
* @entry: the element to delete from the list.n * @list: the list head
*/ */
static __inline__ void list_del_init(struct list_head *entry) static inline void list_add(struct list_head *p, struct list_head *list)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static __inline__ int list_empty(struct list_head *head)
{
return head->next == head;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static __inline__ void list_splice(struct list_head *list, struct list_head *head)
{ {
struct list_head *first = list->next; struct list_head *first = list->next;
if (first != list) { p->next = first;
struct list_head *last = list->prev; first->prev = p;
struct list_head *at = head->next; list->next = p;
p->prev = list;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
} }
/** /* list_add_tail - append a list entry at the tail
* list_for_each - iterate over a list * @p: the new list entry to add
* @pos: the &struct list_head to use as a loop counter. * @list: the list head
* @head: the head for your list.
*/ */
#define list_for_each(pos, head) \ static inline void list_add_tail(struct list_head *p, struct list_head *list)
for (pos = (head)->next ; pos != (head); pos = pos->next) {
struct list_head *last = list->prev;
/** last->next = p;
* list_for_each_safe - iterate over a list safely (actual pointer can be invalidated) p->prev = last;
* @pos: the &struct list_head to use as a loop counter. p->next = list;
* @next: the &struct list_head to use to save next. list->prev = p;
* @head: the head for your list. }
*/
#define list_for_each_safe(pos, npos, head) \
for (pos = (head)->next, npos = pos->next ; pos != (head); pos = npos, npos = pos->next)
/** /* list_del - delete the given list entry */
* list_entry - get the struct for this entry static inline void list_del(struct list_head *p)
* @ptr: the &struct list_head pointer. {
* @type: the type of the struct this is embedded in. p->prev->next = p->next;
* @member: the name of the list_struct within the struct. p->next->prev = p->prev;
*/ }
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/** /* list_empty - returns 1 if the given list is empty */
* list_entry - get the struct for this entry static inline int list_empty(const struct list_head *p)
* @ptr: the &struct list_head pointer. {
* @type: the type of the struct this is embedded in. return p->next == p;
* @offset: offset of entry inside a struct }
*/
#define list_entry_offset(ptr, type, offset) \
((type *)((char *)(ptr)-(offset)))
#endif /* _LIST_H */ #endif /* _LIST_H */