xref: /illumos-gate/usr/src/cmd/syslogd/list.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 (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.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <pthread.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <assert.h>
30 #include <poll.h>
31 #include <stdio.h>
32 #include "llt.h"
33 
34 void
35 ll_init(llh_t *head)
36 {
37 	head->back = &head->front;
38 	head->front = NULL;
39 }
40 
41 void
42 ll_enqueue(llh_t *head, ll_t *data)
43 {
44 	data->n = NULL;
45 	*head->back = data;
46 	head->back = &data->n;
47 }
48 
49 /*
50  * apply the function func to every element of the ll in sequence.  Can
51  * be used to free up the element, so "n" is computed before func is
52  * called on it.
53  */
54 void
55 ll_mapf(llh_t *head, void (*func)(void *))
56 {
57 	ll_t *t = head->front;
58 	ll_t *n;
59 
60 	while (t) {
61 		n = t->n;
62 		func(t);
63 		t = n;
64 	}
65 }
66 
67 ll_t *
68 ll_peek(llh_t *head)
69 {
70 	return (head->front);
71 }
72 
73 ll_t *
74 ll_dequeue(llh_t *head)
75 {
76 	ll_t *ptr;
77 	ptr = head->front;
78 	if (ptr && ((head->front = ptr->n) == NULL))
79 		head->back = &head->front;
80 	return (ptr);
81 }
82 
83 ll_t *
84 ll_traverse(llh_t *ptr, int (*func)(void *, void *), void *user)
85 {
86 	ll_t *t;
87 	ll_t **prev = &ptr->front;
88 
89 	t = ptr->front;
90 	while (t) {
91 		switch (func(t, user)) {
92 		case 1:
93 			return (NULL);
94 		case 0:
95 			prev = &(t->n);
96 			t = t->n;
97 			break;
98 		case -1:
99 			if ((*prev = t->n) == NULL)
100 				ptr->back = prev;
101 			return (t);
102 		}
103 	}
104 	return (NULL);
105 }
106 
107 /* Make sure the list isn't corrupt and returns number of list items */
108 int
109 ll_check(llh_t *head)
110 {
111 	int i = 0;
112 	ll_t *ptr = head->front;
113 #ifndef NDEBUG
114 	ll_t **prev = &head->front;
115 #endif
116 
117 	while (ptr) {
118 		i++;
119 #ifndef NDEBUG
120 		prev = &ptr->n;
121 #endif
122 		ptr = ptr->n;
123 	}
124 	assert(head->back == prev);
125 	return (i);
126 }
127