2010-08-26 18:50:56 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-11 21:16:13 +00:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2010-08-26 18:50:56 +00:00
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic doubly-linked list implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/list.h>
|
|
|
|
#include <sys/list_impl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <sys/debug.h>
|
|
|
|
|
|
|
|
#define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
|
|
|
|
#define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
|
2020-04-23 22:53:14 +00:00
|
|
|
#define list_empty(a) ((a)->list_head.next == &(a)->list_head)
|
2010-08-26 18:50:56 +00:00
|
|
|
|
|
|
|
#define list_insert_after_node(list, node, object) { \
|
|
|
|
list_node_t *lnew = list_d2l(list, object); \
|
2020-04-23 22:53:14 +00:00
|
|
|
lnew->prev = (node); \
|
|
|
|
lnew->next = (node)->next; \
|
|
|
|
(node)->next->prev = lnew; \
|
|
|
|
(node)->next = lnew; \
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define list_insert_before_node(list, node, object) { \
|
|
|
|
list_node_t *lnew = list_d2l(list, object); \
|
2020-04-23 22:53:14 +00:00
|
|
|
lnew->next = (node); \
|
|
|
|
lnew->prev = (node)->prev; \
|
|
|
|
(node)->prev->next = lnew; \
|
|
|
|
(node)->prev = lnew; \
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define list_remove_node(node) \
|
2020-04-23 22:53:14 +00:00
|
|
|
(node)->prev->next = (node)->next; \
|
|
|
|
(node)->next->prev = (node)->prev; \
|
|
|
|
(node)->next = (node)->prev = NULL
|
2010-08-26 18:50:56 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
list_create(list_t *list, size_t size, size_t offset)
|
|
|
|
{
|
|
|
|
ASSERT(list);
|
|
|
|
ASSERT(size > 0);
|
|
|
|
ASSERT(size >= offset + sizeof (list_node_t));
|
|
|
|
|
2024-01-26 22:46:42 +00:00
|
|
|
(void) size;
|
|
|
|
|
2010-08-26 18:50:56 +00:00
|
|
|
list->list_offset = offset;
|
2020-04-23 22:53:14 +00:00
|
|
|
list->list_head.next = list->list_head.prev = &list->list_head;
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_destroy(list_t *list)
|
|
|
|
{
|
|
|
|
list_node_t *node = &list->list_head;
|
|
|
|
|
|
|
|
ASSERT(list);
|
2020-04-23 22:53:14 +00:00
|
|
|
ASSERT(list->list_head.next == node);
|
|
|
|
ASSERT(list->list_head.prev == node);
|
2010-08-26 18:50:56 +00:00
|
|
|
|
2020-04-23 22:53:14 +00:00
|
|
|
node->next = node->prev = NULL;
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_insert_after(list_t *list, void *object, void *nobject)
|
|
|
|
{
|
|
|
|
if (object == NULL) {
|
|
|
|
list_insert_head(list, nobject);
|
|
|
|
} else {
|
|
|
|
list_node_t *lold = list_d2l(list, object);
|
|
|
|
list_insert_after_node(list, lold, nobject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_insert_before(list_t *list, void *object, void *nobject)
|
|
|
|
{
|
|
|
|
if (object == NULL) {
|
|
|
|
list_insert_tail(list, nobject);
|
|
|
|
} else {
|
|
|
|
list_node_t *lold = list_d2l(list, object);
|
|
|
|
list_insert_before_node(list, lold, nobject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_insert_head(list_t *list, void *object)
|
|
|
|
{
|
|
|
|
list_node_t *lold = &list->list_head;
|
|
|
|
list_insert_after_node(list, lold, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_insert_tail(list_t *list, void *object)
|
|
|
|
{
|
|
|
|
list_node_t *lold = &list->list_head;
|
|
|
|
list_insert_before_node(list, lold, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_remove(list_t *list, void *object)
|
|
|
|
{
|
|
|
|
list_node_t *lold = list_d2l(list, object);
|
|
|
|
ASSERT(!list_empty(list));
|
2020-04-23 22:53:14 +00:00
|
|
|
ASSERT(lold->next != NULL);
|
2010-08-26 18:50:56 +00:00
|
|
|
list_remove_node(lold);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_remove_head(list_t *list)
|
|
|
|
{
|
2020-04-23 22:53:14 +00:00
|
|
|
list_node_t *head = list->list_head.next;
|
2010-08-26 18:50:56 +00:00
|
|
|
if (head == &list->list_head)
|
|
|
|
return (NULL);
|
|
|
|
list_remove_node(head);
|
|
|
|
return (list_object(list, head));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_remove_tail(list_t *list)
|
|
|
|
{
|
2020-04-23 22:53:14 +00:00
|
|
|
list_node_t *tail = list->list_head.prev;
|
2010-08-26 18:50:56 +00:00
|
|
|
if (tail == &list->list_head)
|
|
|
|
return (NULL);
|
|
|
|
list_remove_node(tail);
|
|
|
|
return (list_object(list, tail));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_head(list_t *list)
|
|
|
|
{
|
|
|
|
if (list_empty(list))
|
|
|
|
return (NULL);
|
2020-04-23 22:53:14 +00:00
|
|
|
return (list_object(list, list->list_head.next));
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_tail(list_t *list)
|
|
|
|
{
|
|
|
|
if (list_empty(list))
|
|
|
|
return (NULL);
|
2020-04-23 22:53:14 +00:00
|
|
|
return (list_object(list, list->list_head.prev));
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_next(list_t *list, void *object)
|
|
|
|
{
|
|
|
|
list_node_t *node = list_d2l(list, object);
|
|
|
|
|
2020-04-23 22:53:14 +00:00
|
|
|
if (node->next != &list->list_head)
|
|
|
|
return (list_object(list, node->next));
|
2010-08-26 18:50:56 +00:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
list_prev(list_t *list, void *object)
|
|
|
|
{
|
|
|
|
list_node_t *node = list_d2l(list, object);
|
|
|
|
|
2020-04-23 22:53:14 +00:00
|
|
|
if (node->prev != &list->list_head)
|
|
|
|
return (list_object(list, node->prev));
|
2010-08-26 18:50:56 +00:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert src list after dst list. Empty src list thereafter.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
list_move_tail(list_t *dst, list_t *src)
|
|
|
|
{
|
|
|
|
list_node_t *dstnode = &dst->list_head;
|
|
|
|
list_node_t *srcnode = &src->list_head;
|
|
|
|
|
|
|
|
ASSERT(dst->list_offset == src->list_offset);
|
|
|
|
|
|
|
|
if (list_empty(src))
|
|
|
|
return;
|
|
|
|
|
2020-04-23 22:53:14 +00:00
|
|
|
dstnode->prev->next = srcnode->next;
|
|
|
|
srcnode->next->prev = dstnode->prev;
|
|
|
|
dstnode->prev = srcnode->prev;
|
|
|
|
srcnode->prev->next = dstnode;
|
2010-08-26 18:50:56 +00:00
|
|
|
|
|
|
|
/* empty src list */
|
2020-04-23 22:53:14 +00:00
|
|
|
srcnode->next = srcnode->prev = srcnode;
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_link_replace(list_node_t *lold, list_node_t *lnew)
|
|
|
|
{
|
|
|
|
ASSERT(list_link_active(lold));
|
|
|
|
ASSERT(!list_link_active(lnew));
|
|
|
|
|
2020-04-23 22:53:14 +00:00
|
|
|
lnew->next = lold->next;
|
|
|
|
lnew->prev = lold->prev;
|
|
|
|
lold->prev->next = lnew;
|
|
|
|
lold->next->prev = lnew;
|
|
|
|
lold->next = lold->prev = NULL;
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_link_init(list_node_t *ln)
|
|
|
|
{
|
2020-04-23 22:53:14 +00:00
|
|
|
ln->next = NULL;
|
|
|
|
ln->prev = NULL;
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
list_link_active(list_node_t *ln)
|
|
|
|
{
|
2020-07-14 18:04:35 +00:00
|
|
|
EQUIV(ln->next == NULL, ln->prev == NULL);
|
2020-04-23 22:53:14 +00:00
|
|
|
return (ln->next != NULL);
|
2010-08-26 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
list_is_empty(list_t *list)
|
|
|
|
{
|
|
|
|
return (list_empty(list));
|
|
|
|
}
|