xref: /titanic_41/usr/src/uts/common/io/1394/targets/av1394/av1394_list.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Yet another list implementation
31  *    This is a multipurpose double-linked list. It requires that the first
32  *    two structure members of each item are the 'next' and 'prev' pointers.
33  *    This works for mblk's and other data types utilized by av1394.
34  *
35  *    Locking is provided by the caller.
36  */
37 
38 #include <sys/1394/targets/av1394/av1394_impl.h>
39 
40 #define	ITEM(i)	((av1394_list_item_t *)(i))
41 
42 /*
43  * av1394_list_init()
44  *    Initializes the list
45  */
46 void
av1394_list_init(av1394_list_t * lp)47 av1394_list_init(av1394_list_t *lp)
48 {
49 	lp->l_head = lp->l_tail = NULL;
50 	lp->l_cnt = 0;
51 }
52 
53 /*
54  * av1394_list_head()
55  *    Returns pointer to the first item in the list (but does not remove it)
56  */
57 void *
av1394_list_head(av1394_list_t * lp)58 av1394_list_head(av1394_list_t *lp)
59 {
60 	return (lp->l_head);
61 }
62 
63 
64 /*
65  * av1394_list_put_tail()
66  *    Adds item to the end of the list
67  */
68 void
av1394_list_put_tail(av1394_list_t * lp,void * item)69 av1394_list_put_tail(av1394_list_t *lp, void *item)
70 {
71 	ITEM(item)->i_next = NULL;
72 	ITEM(item)->i_prev = lp->l_tail;
73 	if (lp->l_tail == NULL) {
74 		ASSERT(lp->l_head == 0);
75 		ASSERT(lp->l_cnt == 0);
76 		lp->l_head = lp->l_tail = item;
77 	} else {
78 		lp->l_tail->i_next = item;
79 		lp->l_tail = item;
80 	}
81 	lp->l_cnt++;
82 }
83 
84 /*
85  * av1394_list_put_head()
86  *    Inserts item in the front of the list
87  */
88 void
av1394_list_put_head(av1394_list_t * lp,void * item)89 av1394_list_put_head(av1394_list_t *lp, void *item)
90 {
91 	ITEM(item)->i_next = lp->l_head;
92 	ITEM(item)->i_prev = NULL;
93 	if (lp->l_head == NULL) {
94 		ASSERT(lp->l_tail == 0);
95 		ASSERT(lp->l_cnt == 0);
96 		lp->l_head = lp->l_tail = item;
97 	} else {
98 		lp->l_head->i_prev = item;
99 		lp->l_head = item;
100 	}
101 	lp->l_cnt++;
102 }
103 
104 /*
105  * av1394_list_get_head()
106  *    Removes and returns an item from the front of the list
107  */
108 void *
av1394_list_get_head(av1394_list_t * lp)109 av1394_list_get_head(av1394_list_t *lp)
110 {
111 	av1394_list_item_t	*item;
112 
113 	item = lp->l_head;
114 	if (item != NULL) {
115 		lp->l_head = item->i_next;
116 		if (item == lp->l_tail) {
117 			ASSERT(lp->l_cnt == 1);
118 			ASSERT(lp->l_head == NULL);
119 			lp->l_tail = NULL;
120 			lp->l_cnt = 0;
121 		} else {
122 			ASSERT(lp->l_cnt > 1);
123 			item->i_next->i_prev = item->i_prev;
124 			lp->l_cnt--;
125 		}
126 		item->i_next = item->i_prev = NULL;
127 	}
128 	return (item);
129 }
130