1252867Sdelphij/* $Id: list.h,v 1.6 2006/10/31 06:25:28 gmm Exp $ */ 2252867Sdelphij/*- 3252867Sdelphij * Copyright (C) 2005-2011 HighPoint Technologies, Inc. 4252867Sdelphij * All rights reserved. 5252867Sdelphij * 6252867Sdelphij * Redistribution and use in source and binary forms, with or without 7252867Sdelphij * modification, are permitted provided that the following conditions 8252867Sdelphij * are met: 9252867Sdelphij * 1. Redistributions of source code must retain the above copyright 10252867Sdelphij * notice, this list of conditions and the following disclaimer. 11252867Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 12252867Sdelphij * notice, this list of conditions and the following disclaimer in the 13252867Sdelphij * documentation and/or other materials provided with the distribution. 14252867Sdelphij * 15252867Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16252867Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17252867Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18252867Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19252867Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20252867Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21252867Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22252867Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23252867Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24252867Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25252867Sdelphij * SUCH DAMAGE. 26252867Sdelphij * 27252867Sdelphij * $FreeBSD$ 28252867Sdelphij */ 29252867Sdelphij#include <dev/hptnr/hptnr_config.h> 30252867Sdelphij#ifndef _HPT_LIST_H_ 31252867Sdelphij#define _HPT_LIST_H_ 32252867Sdelphij 33252867Sdelphij#ifndef _LINUX_LIST_H 34252867Sdelphij 35252867Sdelphij#ifndef HPT_INLINE 36252867Sdelphij#define HPT_INLINE __inline 37252867Sdelphij#endif 38252867Sdelphij 39252867Sdelphijstruct list_head { 40252867Sdelphij struct list_head *next, *prev; 41252867Sdelphij}; 42252867Sdelphij 43252867Sdelphij#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0) 44252867Sdelphij 45252867Sdelphijstatic HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next) 46252867Sdelphij{ 47252867Sdelphij next->prev = _new; 48252867Sdelphij _new->next = next; 49252867Sdelphij _new->prev = prev; 50252867Sdelphij prev->next = _new; 51252867Sdelphij} 52252867Sdelphij 53252867Sdelphijstatic HPT_INLINE void list_add(struct list_head *_new, struct list_head *head) 54252867Sdelphij{ 55252867Sdelphij __list_add(_new, head, head->next); 56252867Sdelphij} 57252867Sdelphij 58252867Sdelphijstatic HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head) 59252867Sdelphij{ 60252867Sdelphij __list_add(_new, head->prev, head); 61252867Sdelphij} 62252867Sdelphij 63252867Sdelphijstatic HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next) 64252867Sdelphij{ 65252867Sdelphij next->prev = prev; 66252867Sdelphij prev->next = next; 67252867Sdelphij} 68252867Sdelphij 69252867Sdelphijstatic HPT_INLINE void list_del(struct list_head *entry) 70252867Sdelphij{ 71252867Sdelphij __list_del(entry->prev, entry->next); 72252867Sdelphij} 73252867Sdelphij 74252867Sdelphijstatic HPT_INLINE void list_del_init(struct list_head *entry) 75252867Sdelphij{ 76252867Sdelphij __list_del(entry->prev, entry->next); 77252867Sdelphij INIT_LIST_HEAD(entry); 78252867Sdelphij} 79252867Sdelphij 80252867Sdelphijstatic HPT_INLINE int list_empty(struct list_head *head) 81252867Sdelphij{ 82252867Sdelphij HPT_ASSERT(!(head->next==head && head->prev!=head)); 83252867Sdelphij return head->next == head; 84252867Sdelphij} 85252867Sdelphij 86252867Sdelphijstatic HPT_INLINE void __list_splice(struct list_head *list, 87252867Sdelphij struct list_head *head) 88252867Sdelphij{ 89252867Sdelphij struct list_head *first = list->next; 90252867Sdelphij struct list_head *last = list->prev; 91252867Sdelphij struct list_head *at = head->next; 92252867Sdelphij 93252867Sdelphij first->prev = head; 94252867Sdelphij head->next = first; 95252867Sdelphij 96252867Sdelphij last->next = at; 97252867Sdelphij at->prev = last; 98252867Sdelphij} 99252867Sdelphij 100252867Sdelphijstatic HPT_INLINE void list_splice(struct list_head *list, struct list_head *head) 101252867Sdelphij{ 102252867Sdelphij if (!list_empty(list)) 103252867Sdelphij __list_splice(list, head); 104252867Sdelphij} 105252867Sdelphij 106252867Sdelphijstatic HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head) 107252867Sdelphij{ 108252867Sdelphij if (!list_empty(list)) { 109252867Sdelphij __list_splice(list, head); 110252867Sdelphij INIT_LIST_HEAD(list); 111252867Sdelphij } 112252867Sdelphij} 113252867Sdelphij 114252867Sdelphij#define list_entry(ptr, type, member) \ 115252867Sdelphij ((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member))) 116252867Sdelphij 117252867Sdelphij#define list_for_each(pos, head) \ 118252867Sdelphij for (pos = (head)->next; pos != (head); pos = pos->next) 119252867Sdelphij 120252867Sdelphij#define list_for_each_safe(pos, n, head) \ 121252867Sdelphij for (pos = (head)->next, n = pos->next; pos != (head); \ 122252867Sdelphij pos = n, n = pos->next) 123252867Sdelphij 124252867Sdelphij#define get_first_item(attached, type, member) \ 125252867Sdelphij ((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member))) 126252867Sdelphij 127252867Sdelphij#endif 128252867Sdelphij 129252867Sdelphij#endif 130