Path: blob/main/sys/contrib/openzfs/lib/libspl/list.c
48375 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2008 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526/*27* Generic doubly-linked list implementation28*/2930#include <sys/list.h>31#include <sys/list_impl.h>32#include <sys/types.h>33#include <sys/sysmacros.h>34#include <sys/debug.h>3536#define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))37#define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))38#define list_empty(a) ((a)->list_head.next == &(a)->list_head)3940#define list_insert_after_node(list, node, object) { \41list_node_t *lnew = list_d2l(list, object); \42lnew->prev = (node); \43lnew->next = (node)->next; \44(node)->next->prev = lnew; \45(node)->next = lnew; \46}4748#define list_insert_before_node(list, node, object) { \49list_node_t *lnew = list_d2l(list, object); \50lnew->next = (node); \51lnew->prev = (node)->prev; \52(node)->prev->next = lnew; \53(node)->prev = lnew; \54}5556#define list_remove_node(node) \57(node)->prev->next = (node)->next; \58(node)->next->prev = (node)->prev; \59(node)->next = (node)->prev = NULL6061void62list_create(list_t *list, size_t size, size_t offset)63{64ASSERT(list);65ASSERT(size > 0);66ASSERT(size >= offset + sizeof (list_node_t));6768(void) size;6970list->list_offset = offset;71list->list_head.next = list->list_head.prev = &list->list_head;72}7374void75list_destroy(list_t *list)76{77list_node_t *node = &list->list_head;7879ASSERT(list);80ASSERT(list->list_head.next == node);81ASSERT(list->list_head.prev == node);8283node->next = node->prev = NULL;84}8586void87list_insert_after(list_t *list, void *object, void *nobject)88{89if (object == NULL) {90list_insert_head(list, nobject);91} else {92list_node_t *lold = list_d2l(list, object);93list_insert_after_node(list, lold, nobject);94}95}9697void98list_insert_before(list_t *list, void *object, void *nobject)99{100if (object == NULL) {101list_insert_tail(list, nobject);102} else {103list_node_t *lold = list_d2l(list, object);104list_insert_before_node(list, lold, nobject);105}106}107108void109list_insert_head(list_t *list, void *object)110{111list_node_t *lold = &list->list_head;112list_insert_after_node(list, lold, object);113}114115void116list_insert_tail(list_t *list, void *object)117{118list_node_t *lold = &list->list_head;119list_insert_before_node(list, lold, object);120}121122void123list_remove(list_t *list, void *object)124{125list_node_t *lold = list_d2l(list, object);126ASSERT(!list_empty(list));127ASSERT(lold->next != NULL);128list_remove_node(lold);129}130131void *132list_remove_head(list_t *list)133{134list_node_t *head = list->list_head.next;135if (head == &list->list_head)136return (NULL);137list_remove_node(head);138return (list_object(list, head));139}140141void *142list_remove_tail(list_t *list)143{144list_node_t *tail = list->list_head.prev;145if (tail == &list->list_head)146return (NULL);147list_remove_node(tail);148return (list_object(list, tail));149}150151void *152list_head(list_t *list)153{154if (list_empty(list))155return (NULL);156return (list_object(list, list->list_head.next));157}158159void *160list_tail(list_t *list)161{162if (list_empty(list))163return (NULL);164return (list_object(list, list->list_head.prev));165}166167void *168list_next(list_t *list, void *object)169{170list_node_t *node = list_d2l(list, object);171172if (node->next != &list->list_head)173return (list_object(list, node->next));174175return (NULL);176}177178void *179list_prev(list_t *list, void *object)180{181list_node_t *node = list_d2l(list, object);182183if (node->prev != &list->list_head)184return (list_object(list, node->prev));185186return (NULL);187}188189/*190* Insert src list after dst list. Empty src list thereafter.191*/192void193list_move_tail(list_t *dst, list_t *src)194{195list_node_t *dstnode = &dst->list_head;196list_node_t *srcnode = &src->list_head;197198ASSERT(dst->list_offset == src->list_offset);199200if (list_empty(src))201return;202203dstnode->prev->next = srcnode->next;204srcnode->next->prev = dstnode->prev;205dstnode->prev = srcnode->prev;206srcnode->prev->next = dstnode;207208/* empty src list */209srcnode->next = srcnode->prev = srcnode;210}211212void213list_link_replace(list_node_t *lold, list_node_t *lnew)214{215ASSERT(list_link_active(lold));216ASSERT(!list_link_active(lnew));217218lnew->next = lold->next;219lnew->prev = lold->prev;220lold->prev->next = lnew;221lold->next->prev = lnew;222lold->next = lold->prev = NULL;223}224225void226list_link_init(list_node_t *ln)227{228ln->next = NULL;229ln->prev = NULL;230}231232int233list_link_active(list_node_t *ln)234{235EQUIV(ln->next == NULL, ln->prev == NULL);236return (ln->next != NULL);237}238239int240list_is_empty(list_t *list)241{242return (list_empty(list));243}244245246