author | ihse |
Sat, 03 Mar 2018 08:21:47 +0100 | |
branch | ihse-warnings-cflags-branch |
changeset 56230 | 489867818774 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
/* $XConsortium: list.c /main/4 1996/10/14 15:03:56 swick $ */ |
|
25 |
/** ------------------------------------------------------------------------ |
|
26 |
This file contains routines for manipulating generic lists. |
|
27 |
Lists are implemented with a "harness". In other words, each |
|
28 |
node in the list consists of two pointers, one to the data item |
|
29 |
and one to the next node in the list. The head of the list is |
|
30 |
the same struct as each node, but the "item" ptr is used to point |
|
31 |
to the current member of the list (used by the first_in_list and |
|
32 |
next_in_list functions). |
|
33 |
||
34 |
This file is available under and governed by the GNU General Public |
|
35 |
License version 2 only, as published by the Free Software Foundation. |
|
36 |
However, the following notice accompanied the original version of this |
|
37 |
file: |
|
38 |
||
39 |
Copyright (c) 1994 Hewlett-Packard Co. |
|
40 |
Copyright (c) 1996 X Consortium |
|
41 |
||
42 |
Permission is hereby granted, free of charge, to any person obtaining |
|
43 |
a copy of this software and associated documentation files (the |
|
44 |
"Software"), to deal in the Software without restriction, including |
|
45 |
without limitation the rights to use, copy, modify, merge, publish, |
|
46 |
distribute, sublicense, and sell copies of the Software, and to |
|
47 |
permit persons to whom the Software is furnished to do so, subject to |
|
48 |
the following conditions: |
|
49 |
||
50 |
The above copyright notice and this permission notice shall be included |
|
51 |
in all copies or substantial portions of the Software. |
|
52 |
||
53 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
54 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
55 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
56 |
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR |
|
57 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
58 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
59 |
OTHER DEALINGS IN THE SOFTWARE. |
|
60 |
||
61 |
Except as contained in this notice, the name of the X Consortium shall |
|
62 |
not be used in advertising or otherwise to promote the sale, use or |
|
63 |
other dealings in this Software without prior written authorization |
|
64 |
from the X Consortium. |
|
65 |
||
66 |
----------------------------------------------------------------------- **/ |
|
67 |
||
68 |
#include <stdio.h> |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
69 |
#include <stdlib.h> |
2 | 70 |
#include "list.h" |
71 |
||
72 |
||
73 |
/** ------------------------------------------------------------------------ |
|
74 |
Sets the pointers of the specified list to NULL. |
|
75 |
--------------------------------------------------------------------- **/ |
|
76 |
void zero_list(list_ptr lp) |
|
77 |
{ |
|
78 |
lp->next = NULL; |
|
79 |
lp->ptr.item = NULL; |
|
80 |
} |
|
81 |
||
82 |
||
83 |
/** ------------------------------------------------------------------------ |
|
84 |
Adds item to the list pointed to by lp. Finds the end of the |
|
85 |
list, then mallocs a new list node onto the end of the list. |
|
86 |
The item pointer in the new node is set to "item" passed in, |
|
87 |
and the next pointer in the new node is set to NULL. |
|
88 |
Returns 1 if successful, 0 if the malloc failed. |
|
89 |
-------------------------------------------------------------------- **/ |
|
90 |
int32_t add_to_list(list_ptr lp, void *item) |
|
91 |
{ |
|
92 |
while (lp->next) { |
|
93 |
lp = lp->next; |
|
94 |
} |
|
95 |
if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) { |
|
96 |
||
97 |
return 0; |
|
98 |
} |
|
99 |
lp->next->ptr.item = item; |
|
100 |
lp->next->next = NULL; |
|
101 |
||
102 |
return 1; |
|
103 |
} |
|
104 |
||
105 |
||
106 |
/** ------------------------------------------------------------------------ |
|
107 |
Creates a new list and sets its pointers to NULL. |
|
108 |
Returns a pointer to the new list. |
|
109 |
-------------------------------------------------------------------- **/ |
|
56230 | 110 |
list_ptr new_list (void) |
2 | 111 |
{ |
112 |
list_ptr lp; |
|
113 |
||
114 |
if (lp = (list_ptr) malloc( sizeof( list_item))) { |
|
115 |
lp->next = NULL; |
|
116 |
lp->ptr.item = NULL; |
|
117 |
} |
|
118 |
||
119 |
return lp; |
|
120 |
} |
|
121 |
||
122 |
||
123 |
/** ------------------------------------------------------------------------ |
|
124 |
Creates a new list head, pointing to the same list as the one |
|
125 |
passed in. If start_at_curr is TRUE, the new list's first item |
|
126 |
is the "current" item (as set by calls to first/next_in_list()). |
|
127 |
If start_at_curr is FALSE, the first item in the new list is the |
|
128 |
same as the first item in the old list. In either case, the |
|
129 |
curr pointer in the new list is the same as in the old list. |
|
130 |
Returns a pointer to the new list head. |
|
131 |
-------------------------------------------------------------------- **/ |
|
132 |
list_ptr dup_list_head(list_ptr lp, int32_t start_at_curr) |
|
133 |
{ |
|
134 |
list_ptr new_list; |
|
135 |
||
136 |
if ((new_list = (list_ptr) malloc( sizeof( list_item))) == NULL) { |
|
137 |
||
138 |
return (list_ptr)NULL; |
|
139 |
} |
|
140 |
new_list->next = start_at_curr ? lp->ptr.curr : lp->next; |
|
141 |
new_list->ptr.curr = lp->ptr.curr; |
|
142 |
||
143 |
return new_list; |
|
144 |
} |
|
145 |
||
146 |
||
147 |
/** ------------------------------------------------------------------------ |
|
148 |
Returns the number of items in the list. |
|
149 |
-------------------------------------------------------------------- **/ |
|
150 |
uint32_t list_length(list_ptr lp) |
|
151 |
{ |
|
152 |
uint32_t count = 0; |
|
153 |
||
154 |
while (lp->next) { |
|
155 |
count++; |
|
156 |
lp = lp->next; |
|
157 |
} |
|
158 |
||
159 |
return count; |
|
160 |
} |
|
161 |
||
162 |
||
163 |
/** ------------------------------------------------------------------------ |
|
164 |
Scans thru list, looking for a node whose ptr.item is equal to |
|
165 |
the "item" passed in. "Equal" here means the same address - no |
|
166 |
attempt is made to match equivalent values stored in different |
|
167 |
locations. If a match is found, that node is deleted from the |
|
168 |
list. Storage for the node is freed, but not for the item itself. |
|
169 |
Returns a pointer to the item, so the caller can free it if it |
|
170 |
so desires. If a match is not found, returns NULL. |
|
171 |
-------------------------------------------------------------------- **/ |
|
172 |
void *delete_from_list(list_ptr lp, void *item) |
|
173 |
{ |
|
174 |
list_ptr new_next; |
|
175 |
||
176 |
while (lp->next) { |
|
177 |
if (lp->next->ptr.item == item) { |
|
178 |
new_next = lp->next->next; |
|
179 |
free (lp->next); |
|
180 |
lp->next = new_next; |
|
181 |
||
182 |
return item; |
|
183 |
} |
|
184 |
lp = lp->next; |
|
185 |
} |
|
186 |
||
187 |
return NULL; |
|
188 |
} |
|
189 |
||
190 |
||
191 |
/** ------------------------------------------------------------------------ |
|
192 |
Deletes each node in the list *except the head*. This allows |
|
193 |
the deletion of lists where the head is not malloced or created |
|
194 |
with new_list(). If free_items is true, each item pointed to |
|
195 |
from the node is freed, in addition to the node itself. |
|
196 |
-------------------------------------------------------------------- **/ |
|
197 |
void delete_list(list_ptr lp, int32_t free_items) |
|
198 |
{ |
|
199 |
list_ptr del_node; |
|
200 |
void *item; |
|
201 |
||
202 |
while (lp->next) { |
|
203 |
del_node = lp->next; |
|
204 |
item = del_node->ptr.item; |
|
205 |
lp->next = del_node->next; |
|
206 |
free (del_node); |
|
207 |
if (free_items) { |
|
208 |
free( item); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
void delete_list_destroying(list_ptr lp, void destructor(void *item)) |
|
214 |
{ |
|
215 |
list_ptr del_node; |
|
216 |
void *item; |
|
217 |
||
218 |
while (lp->next) { |
|
219 |
del_node = lp->next; |
|
220 |
item = del_node->ptr.item; |
|
221 |
lp->next = del_node->next; |
|
222 |
free( del_node); |
|
223 |
if (destructor) { |
|
224 |
destructor( item); |
|
225 |
} |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
||
230 |
/** ------------------------------------------------------------------------ |
|
231 |
Returns a ptr to the first *item* (not list node) in the list. |
|
232 |
Sets the list head node's curr ptr to the first node in the list. |
|
233 |
Returns NULL if the list is empty. |
|
234 |
-------------------------------------------------------------------- **/ |
|
235 |
void * first_in_list(list_ptr lp) |
|
236 |
{ |
|
237 |
if (! lp) { |
|
238 |
||
239 |
return NULL; |
|
240 |
} |
|
241 |
lp->ptr.curr = lp->next; |
|
242 |
||
243 |
return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL; |
|
244 |
} |
|
245 |
||
246 |
/** ------------------------------------------------------------------------ |
|
247 |
Returns a ptr to the next *item* (not list node) in the list. |
|
248 |
Sets the list head node's curr ptr to the next node in the list. |
|
249 |
first_in_list must have been called prior. |
|
250 |
Returns NULL if no next item. |
|
251 |
-------------------------------------------------------------------- **/ |
|
252 |
void * next_in_list(list_ptr lp) |
|
253 |
{ |
|
254 |
if (! lp) { |
|
255 |
||
256 |
return NULL; |
|
257 |
} |
|
258 |
if (lp->ptr.curr) { |
|
259 |
lp->ptr.curr = lp->ptr.curr->next; |
|
260 |
} |
|
261 |
||
262 |
return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL; |
|
263 |
} |
|
264 |
||
265 |
int32_t list_is_empty(list_ptr lp) |
|
266 |
{ |
|
267 |
return (lp == NULL || lp->next == NULL); |
|
268 |
} |