2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A *
2N/A * MODULE: dapl_llist.c
2N/A *
2N/A * PURPOSE: Manage doubly linked lists within the DAPL Reference Implementation
2N/A *
2N/A * A link list head points to the first member of a linked list, but
2N/A * is itself not a member of the list.
2N/A *
2N/A * +---------------------------------------------+
2N/A * | entry entry entry |
2N/A * HEAD -> | +-------+ +-------+ +-------+ |
2N/A * +--> | flink | --> | flink | --> | flink | >--+
2N/A * | data | | data | | data |
2N/A * +--< | blink | <-- | blink | <-- | blink | <--|
2N/A * | +-------+ +-------+ +-------+ |
2N/A * | |
2N/A * +---------------------------------------------+
2N/A *
2N/A * Note: Each of the remove functions takes an assertion failure if
2N/A * an element cannot be removed from the list.
2N/A *
2N/A * $Id: dapl_llist.c,v 1.9 2003/06/13 12:21:11 sjs2 Exp $
2N/A */
2N/A
2N/A#include "dapl.h"
2N/A
2N/A/*
2N/A * dapl_llist_init_head()
2N/A *
2N/A * Purpose: initialize a linked list head
2N/A */
2N/Avoid
2N/Adapl_llist_init_head(DAPL_LLIST_HEAD *head)
2N/A{
2N/A *head = NULL;
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_init_entry()
2N/A *
2N/A * Purpose: initialize a linked list entry
2N/A */
2N/Avoid
2N/Adapl_llist_init_entry(DAPL_LLIST_ENTRY *entry)
2N/A{
2N/A entry->blink = NULL;
2N/A entry->flink = NULL;
2N/A entry->data = 0;
2N/A entry->list_head = NULL;
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_is_empty()
2N/A *
2N/A * Purpose: returns TRUE if the linked list is empty
2N/A */
2N/ADAT_BOOLEAN
2N/Adapl_llist_is_empty(DAPL_LLIST_HEAD *head)
2N/A{
2N/A return (*head == NULL);
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_add_head()
2N/A *
2N/A * Purpose: Add an entry to the head of a linked list
2N/A */
2N/Avoid
2N/Adapl_llist_add_head(DAPL_LLIST_HEAD *head,
2N/A DAPL_LLIST_ENTRY *entry,
2N/A void *data)
2N/A{
2N/A DAPL_LLIST_ENTRY *first;
2N/A
2N/A /* deal with empty list */
2N/A if (dapl_llist_is_empty(head)) {
2N/A entry->flink = entry;
2N/A entry->blink = entry;
2N/A } else {
2N/A first = *head;
2N/A entry->flink = first;
2N/A entry->blink = first->blink;
2N/A first->blink->flink = entry;
2N/A first->blink = entry;
2N/A }
2N/A
2N/A *head = entry;
2N/A entry->data = data;
2N/A entry->list_head = head;
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_add_tail()
2N/A *
2N/A * Purpose: Add an entry to the tail of a linked list
2N/A */
2N/Avoid
2N/Adapl_llist_add_tail(DAPL_LLIST_HEAD *head,
2N/A DAPL_LLIST_ENTRY *entry,
2N/A void *data)
2N/A{
2N/A DAPL_LLIST_ENTRY *last;
2N/A
2N/A /* deal with empty list */
2N/A if (dapl_llist_is_empty(head)) {
2N/A *head = entry;
2N/A entry->flink = entry;
2N/A entry->blink = entry;
2N/A } else {
2N/A last = (*head)->blink;
2N/A entry->flink = last->flink;
2N/A entry->blink = last;
2N/A last->flink->blink = entry;
2N/A last->flink = entry;
2N/A }
2N/A entry->data = data;
2N/A entry->list_head = head;
2N/A}
2N/A
2N/A
2N/A/*
2N/A * dapl_llist_add_entry()
2N/A *
2N/A * Purpose: Add an entry before an element in the list
2N/A */
2N/Avoid
2N/Adapl_llist_add_entry(DAPL_LLIST_HEAD * head,
2N/A DAPL_LLIST_ENTRY * entry,
2N/A DAPL_LLIST_ENTRY * new_entry,
2N/A void * data)
2N/A{
2N/A DAPL_LLIST_ENTRY *last;
2N/A
2N/A /* deal with empty list */
2N/A if (dapl_llist_is_empty(head)) {
2N/A *head = entry;
2N/A entry->flink = entry;
2N/A entry->blink = entry;
2N/A } else {
2N/A last = entry->blink;
2N/A entry->blink = new_entry;
2N/A last->flink = new_entry;
2N/A
2N/A new_entry->flink = entry;
2N/A new_entry->blink = last;
2N/A
2N/A }
2N/A new_entry->data = data;
2N/A new_entry->list_head = head;
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_remove_head()
2N/A *
2N/A * Purpose: Remove the first entry of a linked list
2N/A */
2N/Avoid *
2N/Adapl_llist_remove_head(DAPL_LLIST_HEAD *head)
2N/A{
2N/A DAPL_LLIST_ENTRY *first;
2N/A
2N/A dapl_os_assert(!dapl_llist_is_empty(head));
2N/A first = *head;
2N/A *head = first->flink;
2N/A
2N/A first->flink->blink = first->blink;
2N/A first->blink->flink = first->flink;
2N/A
2N/A if (first->flink == first) {
2N/A *head = NULL;
2N/A }
2N/A /* clean up the links for good measure */
2N/A first->flink = NULL;
2N/A first->blink = NULL;
2N/A first->list_head = NULL;
2N/A return (first->data);
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_remove_tail()
2N/A *
2N/A * Purpose: Remove the last entry of a linked list
2N/A */
2N/Avoid *
2N/Adapl_llist_remove_tail(DAPL_LLIST_HEAD *head)
2N/A{
2N/A DAPL_LLIST_ENTRY *last;
2N/A
2N/A dapl_os_assert(!dapl_llist_is_empty(head));
2N/A last = (*head)->blink;
2N/A
2N/A last->blink->flink = last->flink;
2N/A last->flink->blink = last->blink;
2N/A
2N/A if (last->flink == last) {
2N/A *head = NULL;
2N/A }
2N/A /* clean up the links for good measure */
2N/A last->flink = NULL;
2N/A last->blink = NULL;
2N/A last->list_head = NULL;
2N/A
2N/A return (last->data);
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_remove_entry()
2N/A *
2N/A * Purpose: Remove the specified entry from a linked list
2N/A */
2N/Avoid *
2N/Adapl_llist_remove_entry(DAPL_LLIST_HEAD *head, DAPL_LLIST_ENTRY *entry)
2N/A{
2N/A DAPL_LLIST_ENTRY *first;
2N/A
2N/A dapl_os_assert(!dapl_llist_is_empty(head));
2N/A first = *head;
2N/A
2N/A /* if it's the first entry, pull it off */
2N/A if (first == entry) {
2N/A (*head) = first->flink;
2N/A /* if it was the only entry, kill the list */
2N/A if (first->flink == first) {
2N/A (*head) = NULL;
2N/A }
2N/A }
2N/A#ifdef VERIFY_LINKED_LIST
2N/A else {
2N/A DAPL_LLIST_ENTRY *try_entry;
2N/A
2N/A try_entry = first->flink;
2N/A for (;;) {
2N/A if (try_entry == first) {
2N/A /*
2N/A * not finding the element on the list
2N/A * is a BAD thing
2N/A */
2N/A dapl_os_assert(0);
2N/A break;
2N/A }
2N/A if (try_entry == entry) {
2N/A break;
2N/A }
2N/A try_entry = try_entry->flink;
2N/A }
2N/A }
2N/A#endif /* VERIFY_LINKED_LIST */
2N/A
2N/A dapl_os_assert(entry->list_head == head);
2N/A entry->list_head = NULL;
2N/A
2N/A entry->flink->blink = entry->blink;
2N/A entry->blink->flink = entry->flink;
2N/A entry->flink = NULL;
2N/A entry->blink = NULL;
2N/A
2N/A return (entry->data);
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_peek_head
2N/A */
2N/A
2N/Avoid *
2N/Adapl_llist_peek_head(DAPL_LLIST_HEAD *head)
2N/A{
2N/A DAPL_LLIST_ENTRY *first;
2N/A
2N/A dapl_os_assert(!dapl_llist_is_empty(head));
2N/A first = *head;
2N/A return (first->data);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * dapl_llist_next_entry
2N/A *
2N/A * Obtain the next entry in the list, return NULL when we get to the
2N/A * head
2N/A */
2N/A
2N/Avoid *
2N/Adapl_llist_next_entry(IN DAPL_LLIST_HEAD *head,
2N/A IN DAPL_LLIST_ENTRY *cur_ent)
2N/A{
2N/A DAPL_LLIST_ENTRY *next;
2N/A
2N/A dapl_os_assert(!dapl_llist_is_empty(head));
2N/A if (cur_ent == NULL) {
2N/A next = *head;
2N/A } else {
2N/A next = cur_ent->flink;
2N/A if (next == *head) {
2N/A return (NULL);
2N/A }
2N/A }
2N/A return (next->data);
2N/A}
2N/A
2N/A/*
2N/A * dapl_llist_debug_print_list()
2N/A *
2N/A * Purpose: Prints the linked list for debugging
2N/A */
2N/Avoid
2N/Adapl_llist_debug_print_list(DAPL_LLIST_HEAD *head)
2N/A{
2N/A DAPL_LLIST_ENTRY * entry;
2N/A DAPL_LLIST_ENTRY * first;
2N/A first = *head;
2N/A if (!first) {
2N/A dapl_dbg_log(DAPL_DBG_TYPE_RTN, "EMPTY_LIST\n");
2N/A return;
2N/A }
2N/A dapl_dbg_log(DAPL_DBG_TYPE_RTN, "HEAD %p\n", *head);
2N/A dapl_dbg_log(DAPL_DBG_TYPE_RTN, "Entry %p %p %p %p\n",
2N/A first,
2N/A first->flink,
2N/A first->blink,
2N/A first->data);
2N/A entry = first->flink;
2N/A while (entry != first) {
2N/A dapl_dbg_log(DAPL_DBG_TYPE_RTN, "Entry %p %p %p %p\n",
2N/A entry,
2N/A entry->flink,
2N/A entry->blink,
2N/A entry->data);
2N/A entry = entry->flink;
2N/A }
2N/A}