Blame src/list.h

Packit Service db8df9
/*
Packit Service db8df9
 * Intel(R) Enclosure LED Utilities
Packit Service db8df9
 * Copyright (C) 2009-2018 Intel Corporation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is free software; you can redistribute it and/or modify it
Packit Service db8df9
 * under the terms and conditions of the GNU General Public License,
Packit Service db8df9
 * version 2, as published by the Free Software Foundation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit Service db8df9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit Service db8df9
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit Service db8df9
 * more details.
Packit Service db8df9
 *
Packit Service db8df9
 * You should have received a copy of the GNU General Public License along with
Packit Service db8df9
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit Service db8df9
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit Service db8df9
 *
Packit Service db8df9
 */
Packit Service db8df9
Packit Service db8df9
#ifndef _LIST_H_INCLUDED_
Packit Service db8df9
#define _LIST_H_INCLUDED_
Packit Service db8df9
Packit Service db8df9
#include <stdlib.h>
Packit Service db8df9
Packit Service db8df9
struct node {
Packit Service db8df9
	struct node *next, *prev;
Packit Service db8df9
	struct list *list;
Packit Service db8df9
	void *item;
Packit Service db8df9
};
Packit Service db8df9
Packit Service db8df9
typedef void (*item_free_t)(void *);
Packit Service db8df9
Packit Service db8df9
struct list {
Packit Service db8df9
	struct node *head, *tail;
Packit Service db8df9
	item_free_t item_free;
Packit Service db8df9
};
Packit Service db8df9
Packit Service db8df9
#define __list_for_each_node(__list, __node, __start_fn, __iter_fn) \
Packit Service db8df9
	for (struct node *__n = __start_fn(__list), *__next; \
Packit Service db8df9
	     __n && (__node = __n) && ((__next = __iter_fn(__n)) || (!__next)); \
Packit Service db8df9
	     __n = __next)
Packit Service db8df9
Packit Service db8df9
#define list_for_each_node(__list, __node) \
Packit Service db8df9
	__list_for_each_node(__list, __node, list_head, list_next)
Packit Service db8df9
Packit Service db8df9
#define list_for_each_node_reverse(__list, __node) \
Packit Service db8df9
	__list_for_each_node(__list, __node, list_tail, list_prev)
Packit Service db8df9
Packit Service db8df9
#define __list_for_each(__list, __item, __start_fn, __iter_fn) \
Packit Service db8df9
	for (struct node *__node = __start_fn(__list); \
Packit Service db8df9
	     __node && ((__item = __node->item) || (!__node->item)); \
Packit Service db8df9
	     __node = __iter_fn(__node))
Packit Service db8df9
Packit Service db8df9
#define list_for_each(__list, __item) \
Packit Service db8df9
	__list_for_each(__list, __item, list_head, list_next)
Packit Service db8df9
Packit Service db8df9
#define list_for_each_reverse(__list, __item) \
Packit Service db8df9
	__list_for_each(__list, __item, list_tail, list_prev)
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Initializes a list object.
Packit Service db8df9
 *
Packit Service db8df9
 * Initializes a list object to reflect an empty state.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 * @param[in]      item_free_fn   custom callback for deallocating list items.
Packit Service db8df9
 *                                If NULL, free() will be used.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_init(struct list *list, item_free_t item_free_fn)
Packit Service db8df9
{
Packit Service db8df9
	list->head = NULL;
Packit Service db8df9
	list->tail = NULL;
Packit Service db8df9
	if (item_free_fn)
Packit Service db8df9
		list->item_free = item_free_fn;
Packit Service db8df9
	else
Packit Service db8df9
		list->item_free = free;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Clears a list and frees the items it contains.
Packit Service db8df9
 *
Packit Service db8df9
 * This function releases the memory allocated for a list object. It also frees
Packit Service db8df9
 * the data items attached to list nodes. It does not free the list itself.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_erase(struct list *list)
Packit Service db8df9
{
Packit Service db8df9
	void __list_erase(struct list *list, item_free_t free_fn);
Packit Service db8df9
	__list_erase(list, list->item_free);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Clears a list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function removes and deallocates all nodes from the list. It does not
Packit Service db8df9
 * free the data items, to do that use list_erase().
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_clear(struct list *list)
Packit Service db8df9
{
Packit Service db8df9
	void __list_erase(struct list *list, item_free_t free_fn);
Packit Service db8df9
	__list_erase(list, NULL);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Removes an element from the list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function removes an element from the list. It only detaches the element
Packit Service db8df9
 * and does not release the memory allocated for the element. To free memory
Packit Service db8df9
 * allocated for an element use list_delete() instead.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      node           pointer to a node object.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_remove(struct node *node)
Packit Service db8df9
{
Packit Service db8df9
	void __list_remove(struct node *node, item_free_t free_fn);
Packit Service db8df9
	__list_remove(node, NULL);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Removes an element from the list and releases its memory.
Packit Service db8df9
 *
Packit Service db8df9
 * This function removes an element from the list and frees the memory allocated
Packit Service db8df9
 * for the list node and data item.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      node           pointer to a node object.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_delete(struct node *node)
Packit Service db8df9
{
Packit Service db8df9
	void __list_remove(struct node *node, item_free_t free_fn);
Packit Service db8df9
	__list_remove(node, node->list->item_free);
Packit Service db8df9
	free(node);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Inserts an element into the list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function puts an element after a given element.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to list object.
Packit Service db8df9
 * @param[in]      item           data item to be inserted into the list.
Packit Service db8df9
 * @param[in]      after          list node after which to insert the element.
Packit Service db8df9
 *                                If NULL, then insert at the head of the list.
Packit Service db8df9
 */
Packit Service db8df9
void list_insert(struct list *list, void *item, struct node *after);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Appends an element to the end of the list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function puts an element on tail of a list.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to list object.
Packit Service db8df9
 * @param[in]      item           data item to be inserted into the list.
Packit Service db8df9
 */
Packit Service db8df9
static inline void list_append(struct list *list, void *item)
Packit Service db8df9
{
Packit Service db8df9
	list_insert(list, item, list->tail);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reruns next element.
Packit Service db8df9
 *
Packit Service db8df9
 * This function returns next element relatively to the given element.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      node           pointer to a node object.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to an element if successful. The NULL pointer means
Packit Service db8df9
 *         that node is the last element on the list.
Packit Service db8df9
 */
Packit Service db8df9
static inline struct node *list_next(const struct node *node)
Packit Service db8df9
{
Packit Service db8df9
	return node->next;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Returns previous element.
Packit Service db8df9
 *
Packit Service db8df9
 * This function returns previous element relatively to the given element.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      node           pointer to a node object.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to an element if successful. The NULL pointer means
Packit Service db8df9
 *         that node is the first element on the list.
Packit Service db8df9
 */
Packit Service db8df9
static inline struct node *list_prev(const struct node *node)
Packit Service db8df9
{
Packit Service db8df9
	return node->prev;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Returns head of a list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function returns a head of a list.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to an element if successful. The NULL pointer means that
Packit Service db8df9
 *         there's no element on a list.
Packit Service db8df9
 */
Packit Service db8df9
static inline struct node *list_head(const struct list *list)
Packit Service db8df9
{
Packit Service db8df9
	return list->head;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Returns tail of a list.
Packit Service db8df9
 *
Packit Service db8df9
 * This function returns a tail of a list.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to an element if successful. The NULL pointer means that
Packit Service db8df9
 *         there's no element on a list.
Packit Service db8df9
 */
Packit Service db8df9
static inline struct node *list_tail(const struct list *list)
Packit Service db8df9
{
Packit Service db8df9
	return list->tail;
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Checks if a list is empty.
Packit Service db8df9
 *
Packit Service db8df9
 * This function checks if a list object has elements.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      list           pointer to a list object.
Packit Service db8df9
 *
Packit Service db8df9
 * @return 1 if list is empty, otherwise the function returns 0.
Packit Service db8df9
 */
Packit Service db8df9
static inline int list_is_empty(const struct list *list)
Packit Service db8df9
{
Packit Service db8df9
	return (list->head == NULL);
Packit Service db8df9
}
Packit Service db8df9
Packit Service db8df9
#endif				/* _LIST_H_INCLUDED_ */