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
25 $Id: list.c 7905 2007-05-14 14:11:33Z twisti $
36 #include "mm/memory.h"
38 #include "threads/lock-common.h"
40 #include "toolbox/list.h"
43 /* list_create *****************************************************************
45 Allocates a new list and initializes the lock object.
47 *******************************************************************************/
49 list_t *list_create(s4 nodeoffset)
55 #if defined(ENABLE_THREADS)
56 lock_init_object_lock((java_objectheader *) l);
61 l->nodeoffset = nodeoffset;
68 /* list_create_dump ************************************************************
70 Allocates a new list on the dump memory.
72 ATTENTION: This list does NOT initialize the locking object!!!
74 *******************************************************************************/
76 list_t *list_create_dump(s4 nodeoffset)
84 l->nodeoffset = nodeoffset;
91 /* list_add_first **************************************************************
93 Adds the element as first element.
95 *******************************************************************************/
97 void list_add_first(list_t *l, void *element)
99 LOCK_MONITOR_ENTER(l);
101 list_add_first_unsynced(l, element);
103 LOCK_MONITOR_EXIT(l);
107 /* list_add_first_unsynced *****************************************************
109 Adds the element as first element, but WITHOUT LOCKING!
111 ATTENTION: Use this function with care!!!
113 *******************************************************************************/
115 void list_add_first_unsynced(list_t *l, void *element)
119 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
134 /* increase number of elements */
140 /* list_add_last ***************************************************************
142 Adds the element as last element.
144 *******************************************************************************/
146 void list_add_last(list_t *l, void *element)
148 LOCK_MONITOR_ENTER(l);
150 list_add_last_unsynced(l, element);
152 LOCK_MONITOR_EXIT(l);
156 /* list_add_last_unsynced ******************************************************
158 Adds the element as last element but does NO locking!
160 ATTENTION: Use this function with care!!!
162 *******************************************************************************/
164 void list_add_last_unsynced(list_t *l, void *element)
168 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
183 /* increase number of elements */
189 /* list_add_before *************************************************************
191 Adds the element newelement to the list l before element.
193 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
195 *******************************************************************************/
197 void list_add_before(list_t *l, void *element, void *newelement)
202 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
203 newln = (listnode_t *) (((u1 *) newelement) + l->nodeoffset);
205 LOCK_MONITOR_ENTER(l);
207 /* set the new links */
209 newln->prev = ln->prev;
213 newln->prev->next = newln;
217 /* set list's first and last if necessary */
225 /* increase number of elements */
229 LOCK_MONITOR_EXIT(l);
233 /* list_remove ***************************************************************
237 *******************************************************************************/
239 void list_remove(list_t *l, void *element)
241 LOCK_MONITOR_ENTER(l);
243 list_remove_unsynced(l, element);
245 LOCK_MONITOR_EXIT(l);
249 /* list_remove_unsynced ********************************************************
251 Removes the element but does NO locking!
253 ATTENTION: Use this function with care!!!
255 *******************************************************************************/
257 void list_remove_unsynced(list_t *l, void *element)
261 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
264 ln->next->prev = ln->prev;
269 ln->prev->next = ln->next;
276 /* decrease number of elements */
282 /* list_first ******************************************************************
284 Returns the first element of the list.
286 *******************************************************************************/
288 void *list_first(list_t *l)
292 LOCK_MONITOR_ENTER(l);
294 el = list_first_unsynced(l);
296 LOCK_MONITOR_EXIT(l);
302 /* list_first_unsynced *********************************************************
304 Returns the first element of the list, but does NO locking!
306 ATTENTION: Use this function with care!!!
308 *******************************************************************************/
310 void *list_first_unsynced(list_t *l)
314 if (l->first == NULL)
317 el = ((u1 *) l->first) - l->nodeoffset;
323 /* list_last *******************************************************************
325 Returns the last element of the list.
327 *******************************************************************************/
329 void *list_last(list_t *l)
333 LOCK_MONITOR_ENTER(l);
335 el = list_last_unsynced(l);
337 LOCK_MONITOR_EXIT(l);
343 /* list_last_unsynced **********************************************************
345 Returns the last element of the list, but does NO locking!
347 ATTENTION: Use this function with care!!!
349 *******************************************************************************/
351 void *list_last_unsynced(list_t *l)
358 el = ((u1 *) l->last) - l->nodeoffset;
364 /* list_next *******************************************************************
366 Returns the next element of element from the list.
368 *******************************************************************************/
370 void *list_next(list_t *l, void *element)
374 LOCK_MONITOR_ENTER(l);
376 el = list_next_unsynced(l, element);
378 LOCK_MONITOR_EXIT(l);
384 /* list_next_unsynced **********************************************************
386 Returns the next element of element from the list, but does NO
389 ATTENTION: Use this function with care!!!
391 *******************************************************************************/
393 void *list_next_unsynced(list_t *l, void *element)
398 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
400 if (ln->next == NULL)
403 el = ((u1 *) ln->next) - l->nodeoffset;
409 /* list_prev *******************************************************************
411 Returns the previous element of element from the list.
413 *******************************************************************************/
415 void *list_prev(list_t *l, void *element)
419 LOCK_MONITOR_ENTER(l);
421 el = list_prev_unsynced(l, element);
423 LOCK_MONITOR_EXIT(l);
429 /* list_prev_unsynced **********************************************************
431 Returns the previous element of element from the list, but does NO
434 ATTENTION: Use this function with care!!!
436 *******************************************************************************/
438 void *list_prev_unsynced(list_t *l, void *element)
443 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
445 if (ln->prev == NULL)
448 el = ((u1 *) ln->prev) - l->nodeoffset;
455 * These are local overrides for various environment variables in Emacs.
456 * Please do not remove this and leave it at the end of the file, where
457 * Emacs will automagically detect them.
458 * ---------------------------------------------------------------------
461 * indent-tabs-mode: t