|
Packit |
5af8b3 |
/*
|
|
Packit |
5af8b3 |
* Copyright © 2010 Intel Corporation
|
|
Packit |
5af8b3 |
* Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
Packit |
5af8b3 |
* copy of this software and associated documentation files (the "Software"),
|
|
Packit |
5af8b3 |
* to deal in the Software without restriction, including without limitation
|
|
Packit |
5af8b3 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
Packit |
5af8b3 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
Packit |
5af8b3 |
* Software is furnished to do so, subject to the following conditions:
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* The above copyright notice and this permission notice (including the next
|
|
Packit |
5af8b3 |
* paragraph) shall be included in all copies or substantial portions of the
|
|
Packit |
5af8b3 |
* Software.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
5af8b3 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
5af8b3 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
Packit |
5af8b3 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
5af8b3 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit |
5af8b3 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
Packit |
5af8b3 |
* IN THE SOFTWARE.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
#ifndef _GLVND_LIST_H_
|
|
Packit |
5af8b3 |
#define _GLVND_LIST_H_
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
#include <stddef.h> /* offsetof() */
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* @file Classic doubly-link circular list implementation.
|
|
Packit |
5af8b3 |
* For real usage examples of the linked list, see the file test/list.c
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* We need to keep a list of struct foo in the parent struct bar, i.e. what
|
|
Packit |
5af8b3 |
* we want is something like this.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct bar {
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* We need one list head in bar and a list element in all list_of_foos (both are of
|
|
Packit |
5af8b3 |
* data type 'struct glvnd_list').
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct bar {
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* struct glvnd_list list_of_foos;
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct foo {
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* struct glvnd_list entry;
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Now we initialize the list head:
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct bar bar;
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* glvnd_list_init(&bar.list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Then we create the first element and add it to this list:
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct foo *foo = malloc(...);
|
|
Packit |
5af8b3 |
* ....
|
|
Packit |
5af8b3 |
* glvnd_list_add(&foo->entry, &bar.list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Repeat the above for each element you want to add to the list. Deleting
|
|
Packit |
5af8b3 |
* works with the element itself.
|
|
Packit |
5af8b3 |
* glvnd_list_del(&foo->entry);
|
|
Packit |
5af8b3 |
* free(foo);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Note: calling glvnd_list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
|
|
Packit |
5af8b3 |
* list again.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Looping through the list requires a 'struct foo' as iterator and the
|
|
Packit |
5af8b3 |
* name of the field the subnodes use.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct foo *iterator;
|
|
Packit |
5af8b3 |
* glvnd_list_for_each_entry(iterator, &bar.list_of_foos, entry) {
|
|
Packit |
5af8b3 |
* if (iterator->something == ...)
|
|
Packit |
5af8b3 |
* ...
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Note: You must not call glvnd_list_del() on the iterator if you continue the
|
|
Packit |
5af8b3 |
* loop. You need to run the safe for-each loop instead:
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct foo *iterator, *next;
|
|
Packit |
5af8b3 |
* glvnd_list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
|
|
Packit |
5af8b3 |
* if (...)
|
|
Packit |
5af8b3 |
* glvnd_list_del(&iterator->entry);
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* The linkage struct for list nodes. This struct must be part of your
|
|
Packit |
5af8b3 |
* to-be-linked struct. struct glvnd_list is required for both the head of the
|
|
Packit |
5af8b3 |
* list and for each list node.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Position and name of the struct glvnd_list field is irrelevant.
|
|
Packit |
5af8b3 |
* There are no requirements that elements of a list are of the same type.
|
|
Packit |
5af8b3 |
* There are no requirements for a list head, any struct glvnd_list can be a list
|
|
Packit |
5af8b3 |
* head.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
struct glvnd_list {
|
|
Packit |
5af8b3 |
struct glvnd_list *next, *prev;
|
|
Packit |
5af8b3 |
};
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Initialize the list as an empty list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* glvnd_list_init(&bar->list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param The list to initialized.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
glvnd_list_init(struct glvnd_list *list)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
list->next = list->prev = list;
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
__glvnd_list_add(struct glvnd_list *entry,
|
|
Packit |
5af8b3 |
struct glvnd_list *prev, struct glvnd_list *next)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
next->prev = entry;
|
|
Packit |
5af8b3 |
entry->next = next;
|
|
Packit |
5af8b3 |
entry->prev = prev;
|
|
Packit |
5af8b3 |
prev->next = entry;
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Insert a new element after the given list head. The new element does not
|
|
Packit |
5af8b3 |
* need to be initialised as empty list.
|
|
Packit |
5af8b3 |
* The list changes from:
|
|
Packit |
5af8b3 |
* head → some element → ...
|
|
Packit |
5af8b3 |
* to
|
|
Packit |
5af8b3 |
* head → new element → older element → ...
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *newfoo = malloc(...);
|
|
Packit |
5af8b3 |
* glvnd_list_add(&newfoo->entry, &bar->list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry The new element to prepend to the list.
|
|
Packit |
5af8b3 |
* @param head The existing list.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
glvnd_list_add(struct glvnd_list *entry, struct glvnd_list *head)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
__glvnd_list_add(entry, head, head->next);
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Append a new element to the end of the list given with this list head.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* The list changes from:
|
|
Packit |
5af8b3 |
* head → some element → ... → lastelement
|
|
Packit |
5af8b3 |
* to
|
|
Packit |
5af8b3 |
* head → some element → ... → lastelement → new element
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *newfoo = malloc(...);
|
|
Packit |
5af8b3 |
* glvnd_list_append(&newfoo->entry, &bar->list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry The new element to prepend to the list.
|
|
Packit |
5af8b3 |
* @param head The existing list.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
glvnd_list_append(struct glvnd_list *entry, struct glvnd_list *head)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
__glvnd_list_add(entry, head->prev, head);
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
__glvnd_list_del(struct glvnd_list *prev, struct glvnd_list *next)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
next->prev = prev;
|
|
Packit |
5af8b3 |
prev->next = next;
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Remove the element from the list it is in. Using this function will reset
|
|
Packit |
5af8b3 |
* the pointers to/from this element so it is removed from the list. It does
|
|
Packit |
5af8b3 |
* NOT free the element itself or manipulate it otherwise.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Using glvnd_list_del on a pure list head (like in the example at the top of
|
|
Packit |
5af8b3 |
* this file) will NOT remove the first element from
|
|
Packit |
5af8b3 |
* the list but rather reset the list as empty list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* glvnd_list_del(&foo->entry);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry The element to remove.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
static inline void
|
|
Packit |
5af8b3 |
glvnd_list_del(struct glvnd_list *entry)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
__glvnd_list_del(entry->prev, entry->next);
|
|
Packit |
5af8b3 |
glvnd_list_init(entry);
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Check if the list is empty.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* glvnd_list_is_empty(&bar->list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @return True if the list contains one or more elements or False otherwise.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
static inline int
|
|
Packit |
5af8b3 |
glvnd_list_is_empty(struct glvnd_list *head)
|
|
Packit |
5af8b3 |
{
|
|
Packit |
5af8b3 |
return head->next == head;
|
|
Packit |
5af8b3 |
}
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Returns a pointer to the container of this list element.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo* f;
|
|
Packit |
5af8b3 |
* f = glvnd_container_of(&foo->entry, struct foo, entry);
|
|
Packit |
5af8b3 |
* assert(f == foo);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param ptr Pointer to the struct glvnd_list.
|
|
Packit |
5af8b3 |
* @param type Data type of the list element.
|
|
Packit |
5af8b3 |
* @param member Member name of the struct glvnd_list field in the list element.
|
|
Packit |
5af8b3 |
* @return A pointer to the data struct containing the list head.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#ifndef glvnd_container_of
|
|
Packit |
5af8b3 |
#define glvnd_container_of(ptr, type, member) \
|
|
Packit |
5af8b3 |
(type *)((char *)(ptr) - offsetof(type, member))
|
|
Packit |
5af8b3 |
#endif
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Alias of glvnd_container_of
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_list_entry(ptr, type, member) \
|
|
Packit |
5af8b3 |
glvnd_container_of(ptr, type, member)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Retrieve the first list entry for the given list pointer.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *first;
|
|
Packit |
5af8b3 |
* first = glvnd_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param ptr The list head
|
|
Packit |
5af8b3 |
* @param type Data type of the list element to retrieve
|
|
Packit |
5af8b3 |
* @param member Member name of the struct glvnd_list field in the list element.
|
|
Packit |
5af8b3 |
* @return A pointer to the first list element.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_list_first_entry(ptr, type, member) \
|
|
Packit |
5af8b3 |
glvnd_list_entry((ptr)->next, type, member)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Retrieve the last list entry for the given listpointer.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *first;
|
|
Packit |
5af8b3 |
* first = glvnd_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param ptr The list head
|
|
Packit |
5af8b3 |
* @param type Data type of the list element to retrieve
|
|
Packit |
5af8b3 |
* @param member Member name of the struct glvnd_list field in the list element.
|
|
Packit |
5af8b3 |
* @return A pointer to the last list element.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_list_last_entry(ptr, type, member) \
|
|
Packit |
5af8b3 |
glvnd_list_entry((ptr)->prev, type, member)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
#ifdef HAVE_TYPEOF
|
|
Packit |
5af8b3 |
#define __glvnd_container_of(ptr, sample, member) \
|
|
Packit |
5af8b3 |
glvnd_container_of(ptr, typeof(*sample), member)
|
|
Packit |
5af8b3 |
#else
|
|
Packit |
5af8b3 |
/* This implementation of __glvnd_container_of has undefined behavior according
|
|
Packit |
5af8b3 |
* to the C standard, but it works in many cases. If your compiler doesn't
|
|
Packit |
5af8b3 |
* support typeof() and fails with this implementation, please try a newer
|
|
Packit |
5af8b3 |
* compiler.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define __glvnd_container_of(ptr, sample, member) \
|
|
Packit |
5af8b3 |
(void *)((char *)(ptr) \
|
|
Packit |
5af8b3 |
- ((char *)&(sample)->member - (char *)(sample)))
|
|
Packit |
5af8b3 |
#endif
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Loop through the list given by head and set pos to struct in the list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *iterator;
|
|
Packit |
5af8b3 |
* glvnd_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
|
|
Packit |
5af8b3 |
* [modify iterator]
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* This macro is not safe for node deletion. Use glvnd_list_for_each_entry_safe
|
|
Packit |
5af8b3 |
* instead.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param pos Iterator variable of the type of the list elements.
|
|
Packit |
5af8b3 |
* @param head List head
|
|
Packit |
5af8b3 |
* @param member Member name of the struct glvnd_list in the list elements.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_list_for_each_entry(pos, head, member) \
|
|
Packit |
5af8b3 |
for (pos = __glvnd_container_of((head)->next, pos, member); \
|
|
Packit |
5af8b3 |
&pos->member != (head); \
|
|
Packit |
5af8b3 |
pos = __glvnd_container_of(pos->member.next, pos, member))
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Loop through the list, keeping a backup pointer to the element. This
|
|
Packit |
5af8b3 |
* macro allows for the deletion of a list element while looping through the
|
|
Packit |
5af8b3 |
* list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* See glvnd_list_for_each_entry for more details.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_list_for_each_entry_safe(pos, tmp, head, member) \
|
|
Packit |
5af8b3 |
for (pos = __glvnd_container_of((head)->next, pos, member), \
|
|
Packit |
5af8b3 |
tmp = __glvnd_container_of(pos->member.next, pos, member); \
|
|
Packit |
5af8b3 |
&pos->member != (head); \
|
|
Packit |
5af8b3 |
pos = tmp, tmp = __glvnd_container_of(pos->member.next, tmp, member))
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/* NULL-Terminated List Interface
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* The interface below does _not_ use the struct glvnd_list as described above.
|
|
Packit |
5af8b3 |
* It is mainly for legacy structures that cannot easily be switched to
|
|
Packit |
5af8b3 |
* struct glvnd_list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* This interface is for structs like
|
|
Packit |
5af8b3 |
* struct foo {
|
|
Packit |
5af8b3 |
* [...]
|
|
Packit |
5af8b3 |
* struct foo *next;
|
|
Packit |
5af8b3 |
* [...]
|
|
Packit |
5af8b3 |
* };
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* The position and field name of "next" are arbitrary.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Init the element as null-terminated list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *list = malloc();
|
|
Packit |
5af8b3 |
* glvnd_nt_list_init(list, next);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param list The list element that will be the start of the list
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_init(_list, _member) \
|
|
Packit |
5af8b3 |
(_list)->_member = NULL
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Returns the next element in the list or NULL on termination.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *element = list;
|
|
Packit |
5af8b3 |
* while ((element = glvnd_nt_list_next(element, next)) { }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* This macro is not safe for node deletion. Use glvnd_nt_list_for_each_entry_safe
|
|
Packit |
5af8b3 |
* instead.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param list The list or current element.
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_next(_list, _member) \
|
|
Packit |
5af8b3 |
(_list)->_member
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Iterate through each element in the list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *iterator;
|
|
Packit |
5af8b3 |
* glvnd_nt_list_for_each_entry(iterator, list, next) {
|
|
Packit |
5af8b3 |
* [modify iterator]
|
|
Packit |
5af8b3 |
* }
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry Assigned to the current list element
|
|
Packit |
5af8b3 |
* @param list The list to iterate through.
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_for_each_entry(_entry, _list, _member) \
|
|
Packit |
5af8b3 |
for (_entry = _list; _entry; _entry = (_entry)->_member)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Iterate through each element in the list, keeping a backup pointer to the
|
|
Packit |
5af8b3 |
* element. This macro allows for the deletion of a list element while
|
|
Packit |
5af8b3 |
* looping through the list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* See glvnd_nt_list_for_each_entry for more details.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry Assigned to the current list element
|
|
Packit |
5af8b3 |
* @param tmp The pointer to the next element
|
|
Packit |
5af8b3 |
* @param list The list to iterate through.
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct.
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_for_each_entry_safe(_entry, _tmp, _list, _member) \
|
|
Packit |
5af8b3 |
for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
|
|
Packit |
5af8b3 |
_entry; \
|
|
Packit |
5af8b3 |
_entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Append the element to the end of the list. This macro may be used to
|
|
Packit |
5af8b3 |
* merge two lists.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *elem = malloc(...);
|
|
Packit |
5af8b3 |
* glvnd_nt_list_init(elem, next)
|
|
Packit |
5af8b3 |
* glvnd_nt_list_append(elem, list, struct foo, next);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Resulting list order:
|
|
Packit |
5af8b3 |
* list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry An entry (or list) to append to the list
|
|
Packit |
5af8b3 |
* @param list The list to append to. This list must be a valid list, not
|
|
Packit |
5af8b3 |
* NULL.
|
|
Packit |
5af8b3 |
* @param type The list type
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_append(_entry, _list, _type, _member) \
|
|
Packit |
5af8b3 |
do { \
|
|
Packit |
5af8b3 |
_type *__iterator = _list; \
|
|
Packit |
5af8b3 |
while (__iterator->_member) { __iterator = __iterator->_member;}\
|
|
Packit |
5af8b3 |
__iterator->_member = _entry; \
|
|
Packit |
5af8b3 |
} while (0)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Insert the element at the next position in the list. This macro may be
|
|
Packit |
5af8b3 |
* used to insert a list into a list.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* struct foo *elem = malloc(...);
|
|
Packit |
5af8b3 |
* glvnd_nt_list_init(elem, next)
|
|
Packit |
5af8b3 |
* glvnd_nt_list_insert(elem, list, struct foo, next);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Resulting list order:
|
|
Packit |
5af8b3 |
* list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry An entry (or list) to append to the list
|
|
Packit |
5af8b3 |
* @param list The list to insert to. This list must be a valid list, not
|
|
Packit |
5af8b3 |
* NULL.
|
|
Packit |
5af8b3 |
* @param type The list type
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to next struct
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_insert(_entry, _list, _type, _member) \
|
|
Packit |
5af8b3 |
do { \
|
|
Packit |
5af8b3 |
glvnd_nt_list_append((_list)->_member, _entry, _type, _member); \
|
|
Packit |
5af8b3 |
(_list)->_member = _entry; \
|
|
Packit |
5af8b3 |
} while (0)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
/**
|
|
Packit |
5af8b3 |
* Delete the entry from the list by iterating through the list and
|
|
Packit |
5af8b3 |
* removing any reference from the list to the entry.
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* Example:
|
|
Packit |
5af8b3 |
* struct foo *elem = <assign to right element>
|
|
Packit |
5af8b3 |
* glvnd_nt_list_del(elem, list, struct foo, next);
|
|
Packit |
5af8b3 |
*
|
|
Packit |
5af8b3 |
* @param entry The entry to delete from the list. entry is always
|
|
Packit |
5af8b3 |
* re-initialized as a null-terminated list.
|
|
Packit |
5af8b3 |
* @param list The list containing the entry, set to the new list without
|
|
Packit |
5af8b3 |
* the removed entry.
|
|
Packit |
5af8b3 |
* @param type The list type
|
|
Packit |
5af8b3 |
* @param member Member name of the field pointing to the next entry
|
|
Packit |
5af8b3 |
*/
|
|
Packit |
5af8b3 |
#define glvnd_nt_list_del(_entry, _list, _type, _member) \
|
|
Packit |
5af8b3 |
do { \
|
|
Packit |
5af8b3 |
_type *__e = _entry; \
|
|
Packit |
5af8b3 |
if (__e == NULL || _list == NULL) break; \
|
|
Packit |
5af8b3 |
if ((_list) == __e) { \
|
|
Packit |
5af8b3 |
_list = __e->_member; \
|
|
Packit |
5af8b3 |
} else { \
|
|
Packit |
5af8b3 |
_type *__prev = _list; \
|
|
Packit |
5af8b3 |
while (__prev->_member && __prev->_member != __e) \
|
|
Packit |
5af8b3 |
__prev = glvnd_nt_list_next(__prev, _member); \
|
|
Packit |
5af8b3 |
if (__prev->_member) \
|
|
Packit |
5af8b3 |
__prev->_member = __e->_member; \
|
|
Packit |
5af8b3 |
} \
|
|
Packit |
5af8b3 |
glvnd_nt_list_init(__e, _member); \
|
|
Packit |
5af8b3 |
} while(0)
|
|
Packit |
5af8b3 |
|
|
Packit |
5af8b3 |
#endif
|