blob: eb239c4f1f29f905ddbbdff0e4eea96b87d9a2b8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
* 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
* or http://www.opensolaris.org/os/licensing.
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <pthread.h>
#include <malloc.h>
#include <memory.h>
#include <assert.h>
#include <poll.h>
#include <stdio.h>
#include "llt.h"
void
ll_init(llh_t *head)
{
head->back = &head->front;
head->front = NULL;
}
void
ll_enqueue(llh_t *head, ll_t *data)
{
data->n = NULL;
*head->back = data;
head->back = &data->n;
}
/*
* apply the function func to every element of the ll in sequence. Can
* be used to free up the element, so "n" is computed before func is
* called on it.
*/
void
ll_mapf(llh_t *head, void (*func)(void *))
{
ll_t *t = head->front;
ll_t *n;
while (t) {
n = t->n;
func(t);
t = n;
}
}
ll_t *
ll_peek(llh_t *head)
{
return (head->front);
}
ll_t *
ll_dequeue(llh_t *head)
{
ll_t *ptr;
ptr = head->front;
if (ptr && ((head->front = ptr->n) == NULL))
head->back = &head->front;
return (ptr);
}
ll_t *
ll_traverse(llh_t *ptr, int (*func)(void *, void *), void *user)
{
ll_t *t;
ll_t **prev = &ptr->front;
t = ptr->front;
while (t) {
switch (func(t, user)) {
case 1:
return (NULL);
case 0:
prev = &(t->n);
t = t->n;
break;
case -1:
if ((*prev = t->n) == NULL)
ptr->back = prev;
return (t);
}
}
return (NULL);
}
/* Make sure the list isn't corrupt and returns number of list items */
int
ll_check(llh_t *head)
{
int i = 0;
ll_t *ptr = head->front;
#ifndef NDEBUG
ll_t **prev = &head->front;
#endif
while (ptr) {
i++;
#ifndef NDEBUG
prev = &ptr->n;
#endif
ptr = ptr->n;
}
assert(head->back == prev);
return (i);
}
|