xref: /titanic_52/usr/src/cmd/syslogd/list.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
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 (c) 1996-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <pthread.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <assert.h>
33 #include <poll.h>
34 #include <stdio.h>
35 #include "llt.h"
36 void
37 ll_init(llh_t *head)
38 {
39 	head->back = &head->front;
40 	head->front = NULL;
41 }
42 void
43 ll_enqueue(llh_t *head, ll_t *data)
44 {
45 	data->n = NULL;
46 	*head->back = data;
47 	head->back = &data->n;
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 ll_t *
67 ll_peek(llh_t *head)
68 {
69 	return (head->front);
70 }
71 ll_t *
72 ll_dequeue(llh_t *head)
73 {
74 	ll_t *ptr;
75 	ptr = head->front;
76 	if (ptr && ((head->front = ptr->n) == NULL))
77 		head->back = &head->front;
78 	return (ptr);
79 }
80 ll_t *
81 ll_traverse(llh_t *ptr, int (*func)(void *, void *), void *user)
82 {
83 	ll_t *t;
84 	ll_t **prev = &ptr->front;
85 
86 	t = ptr->front;
87 	while (t) {
88 		switch (func(t, user)) {
89 		case 1:
90 			return (NULL);
91 		case 0:
92 			prev = &(t->n);
93 			t = t->n;
94 			break;
95 		case -1:
96 			if ((*prev = t->n) == NULL)
97 				ptr->back = prev;
98 			return (t);
99 		}
100 	}
101 	return (NULL);
102 }
103 /* Make sure the list isn't corrupt and returns number of list items */
104 int
105 ll_check(llh_t *head)
106 {
107 	int i = 0;
108 	ll_t *ptr = head->front;
109 	ll_t **prev = &head->front;
110 
111 	while (ptr) {
112 		i++;
113 		prev = &ptr->n;
114 		ptr = ptr->n;
115 	}
116 	assert(head->back == prev);
117 	return (i);
118 }
119