1 /* src/toolbox/list.c - double linked list
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "mm/memory.h"
36 #include "threads/lock-common.h"
38 #include "toolbox/list.h"
41 /* list_create *****************************************************************
43 Allocates a new list and initializes the lock object.
45 *******************************************************************************/
47 list_t *list_create(s4 nodeoffset)
53 LOCK_INIT_OBJECT_LOCK(l);
57 l->nodeoffset = nodeoffset;
64 /* list_create_dump ************************************************************
66 Allocates a new list on the dump memory.
68 ATTENTION: This list does NOT initialize the locking object!!!
70 *******************************************************************************/
72 list_t *list_create_dump(s4 nodeoffset)
80 l->nodeoffset = nodeoffset;
87 /* list_add_first **************************************************************
89 Adds the element as first element.
91 *******************************************************************************/
93 void list_add_first(list_t *l, void *element)
95 LOCK_MONITOR_ENTER(l);
97 list_add_first_unsynced(l, element);
103 /* list_add_first_unsynced *****************************************************
105 Adds the element as first element, but WITHOUT LOCKING!
107 ATTENTION: Use this function with care!!!
109 *******************************************************************************/
111 void list_add_first_unsynced(list_t *l, void *element)
115 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
130 /* increase number of elements */
136 /* list_add_last ***************************************************************
138 Adds the element as last element.
140 *******************************************************************************/
142 void list_add_last(list_t *l, void *element)
144 LOCK_MONITOR_ENTER(l);
146 list_add_last_unsynced(l, element);
148 LOCK_MONITOR_EXIT(l);
152 /* list_add_last_unsynced ******************************************************
154 Adds the element as last element but does NO locking!
156 ATTENTION: Use this function with care!!!
158 *******************************************************************************/
160 void list_add_last_unsynced(list_t *l, void *element)
164 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
179 /* increase number of elements */
185 /* list_add_before *************************************************************
187 Adds the element newelement to the list l before element.
189 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
191 *******************************************************************************/
193 void list_add_before(list_t *l, void *element, void *newelement)
198 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
199 newln = (listnode_t *) (((u1 *) newelement) + l->nodeoffset);
201 LOCK_MONITOR_ENTER(l);
203 /* set the new links */
205 newln->prev = ln->prev;
209 newln->prev->next = newln;
213 /* set list's first and last if necessary */
221 /* increase number of elements */
225 LOCK_MONITOR_EXIT(l);
229 /* list_remove ***************************************************************
233 *******************************************************************************/
235 void list_remove(list_t *l, void *element)
237 LOCK_MONITOR_ENTER(l);
239 list_remove_unsynced(l, element);
241 LOCK_MONITOR_EXIT(l);
245 /* list_remove_unsynced ********************************************************
247 Removes the element but does NO locking!
249 ATTENTION: Use this function with care!!!
251 *******************************************************************************/
253 void list_remove_unsynced(list_t *l, void *element)
257 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
260 ln->next->prev = ln->prev;
265 ln->prev->next = ln->next;
272 /* decrease number of elements */
278 /* list_first ******************************************************************
280 Returns the first element of the list.
282 *******************************************************************************/
284 void *list_first(list_t *l)
288 LOCK_MONITOR_ENTER(l);
290 el = list_first_unsynced(l);
292 LOCK_MONITOR_EXIT(l);
298 /* list_first_unsynced *********************************************************
300 Returns the first element of the list, but does NO locking!
302 ATTENTION: Use this function with care!!!
304 *******************************************************************************/
306 void *list_first_unsynced(list_t *l)
310 if (l->first == NULL)
313 el = ((u1 *) l->first) - l->nodeoffset;
319 /* list_last *******************************************************************
321 Returns the last element of the list.
323 *******************************************************************************/
325 void *list_last(list_t *l)
329 LOCK_MONITOR_ENTER(l);
331 el = list_last_unsynced(l);
333 LOCK_MONITOR_EXIT(l);
339 /* list_last_unsynced **********************************************************
341 Returns the last element of the list, but does NO locking!
343 ATTENTION: Use this function with care!!!
345 *******************************************************************************/
347 void *list_last_unsynced(list_t *l)
354 el = ((u1 *) l->last) - l->nodeoffset;
360 /* list_next *******************************************************************
362 Returns the next element of element from the list.
364 *******************************************************************************/
366 void *list_next(list_t *l, void *element)
370 LOCK_MONITOR_ENTER(l);
372 el = list_next_unsynced(l, element);
374 LOCK_MONITOR_EXIT(l);
380 /* list_next_unsynced **********************************************************
382 Returns the next element of element from the list, but does NO
385 ATTENTION: Use this function with care!!!
387 *******************************************************************************/
389 void *list_next_unsynced(list_t *l, void *element)
394 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
396 if (ln->next == NULL)
399 el = ((u1 *) ln->next) - l->nodeoffset;
405 /* list_prev *******************************************************************
407 Returns the previous element of element from the list.
409 *******************************************************************************/
411 void *list_prev(list_t *l, void *element)
415 LOCK_MONITOR_ENTER(l);
417 el = list_prev_unsynced(l, element);
419 LOCK_MONITOR_EXIT(l);
425 /* list_prev_unsynced **********************************************************
427 Returns the previous element of element from the list, but does NO
430 ATTENTION: Use this function with care!!!
432 *******************************************************************************/
434 void *list_prev_unsynced(list_t *l, void *element)
439 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
441 if (ln->prev == NULL)
444 el = ((u1 *) ln->prev) - l->nodeoffset;
451 * These are local overrides for various environment variables in Emacs.
452 * Please do not remove this and leave it at the end of the file, where
453 * Emacs will automagically detect them.
454 * ---------------------------------------------------------------------
457 * indent-tabs-mode: t