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 7784 2007-04-20 13:51:41Z twisti $
36 #include "mm/memory.h"
38 #if defined(ENABLE_THREADS)
39 # include "threads/native/lock.h"
41 # include "threads/none/lock.h"
44 #include "toolbox/list.h"
47 /* list_create *****************************************************************
49 Allocates a new list and initializes the lock object.
51 *******************************************************************************/
53 list_t *list_create(s4 nodeoffset)
59 #if defined(ENABLE_THREADS)
60 lock_init_object_lock((java_objectheader *) l);
65 l->nodeoffset = nodeoffset;
72 /* list_create_dump ************************************************************
74 Allocates a new list on the dump memory.
76 ATTENTION: This list does NOT initialize the locking object!!!
78 *******************************************************************************/
80 list_t *list_create_dump(s4 nodeoffset)
88 l->nodeoffset = nodeoffset;
95 /* list_add_first **************************************************************
97 Adds the element as first element.
99 *******************************************************************************/
101 void list_add_first(list_t *l, void *element)
105 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
107 LOCK_MONITOR_ENTER(l);
122 /* increase number of elements */
126 LOCK_MONITOR_EXIT(l);
130 /* list_add_last ***************************************************************
132 Adds the element as last element.
134 *******************************************************************************/
136 void list_add_last(list_t *l, void *element)
138 LOCK_MONITOR_ENTER(l);
140 list_add_last_unsynced(l, element);
142 LOCK_MONITOR_EXIT(l);
146 /* list_add_last_unsynced ******************************************************
148 Adds the element as last element but does NO locking!
150 ATTENTION: Use this function with care!!!
152 *******************************************************************************/
154 void list_add_last_unsynced(list_t *l, void *element)
158 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
173 /* increase number of elements */
179 /* list_add_before *************************************************************
181 Adds the element newelement to the list l before element.
183 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
185 *******************************************************************************/
187 void list_add_before(list_t *l, void *element, void *newelement)
192 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
193 newln = (listnode_t *) (((u1 *) newelement) + l->nodeoffset);
195 LOCK_MONITOR_ENTER(l);
197 /* set the new links */
199 newln->prev = ln->prev;
203 newln->prev->next = newln;
207 /* set list's first and last if necessary */
215 /* increase number of elements */
219 LOCK_MONITOR_EXIT(l);
223 /* list_remove ***************************************************************
227 *******************************************************************************/
229 void list_remove(list_t *l, void *element)
231 LOCK_MONITOR_ENTER(l);
233 list_remove_unsynced(l, element);
235 LOCK_MONITOR_EXIT(l);
239 /* list_remove_unsynced ********************************************************
241 Removes the element but does NO locking!
243 ATTENTION: Use this function with care!!!
245 *******************************************************************************/
247 void list_remove_unsynced(list_t *l, void *element)
251 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
254 ln->next->prev = ln->prev;
259 ln->prev->next = ln->next;
266 /* decrease number of elements */
272 /* list_first ******************************************************************
274 Returns the first element of the list.
276 *******************************************************************************/
278 void *list_first(list_t *l)
282 LOCK_MONITOR_ENTER(l);
284 el = list_first_unsynced(l);
286 LOCK_MONITOR_EXIT(l);
292 /* list_first_unsynced *********************************************************
294 Returns the first element of the list, but does NO locking!
296 ATTENTION: Use this function with care!!!
298 *******************************************************************************/
300 void *list_first_unsynced(list_t *l)
304 if (l->first == NULL)
307 el = ((u1 *) l->first) - l->nodeoffset;
313 /* list_last *******************************************************************
315 Returns the last element of the list.
317 *******************************************************************************/
319 void *list_last(list_t *l)
323 LOCK_MONITOR_ENTER(l);
325 el = list_last_unsynced(l);
327 LOCK_MONITOR_EXIT(l);
333 /* list_last_unsynced **********************************************************
335 Returns the last element of the list, but does NO locking!
337 ATTENTION: Use this function with care!!!
339 *******************************************************************************/
341 void *list_last_unsynced(list_t *l)
348 el = ((u1 *) l->last) - l->nodeoffset;
354 /* list_next *******************************************************************
356 Returns the next element of element from the list.
358 *******************************************************************************/
360 void *list_next(list_t *l, void *element)
364 LOCK_MONITOR_ENTER(l);
366 el = list_next_unsynced(l, element);
368 LOCK_MONITOR_EXIT(l);
374 /* list_next_unsynced **********************************************************
376 Returns the next element of element from the list, but does NO
379 ATTENTION: Use this function with care!!!
381 *******************************************************************************/
383 void *list_next_unsynced(list_t *l, void *element)
388 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
390 if (ln->next == NULL)
393 el = ((u1 *) ln->next) - l->nodeoffset;
399 /* list_prev *******************************************************************
401 Returns the previous element of element from the list.
403 *******************************************************************************/
405 void *list_prev(list_t *l, void *element)
409 LOCK_MONITOR_ENTER(l);
411 el = list_prev_unsynced(l, element);
413 LOCK_MONITOR_EXIT(l);
419 /* list_prev_unsynced **********************************************************
421 Returns the previous element of element from the list, but does NO
424 ATTENTION: Use this function with care!!!
426 *******************************************************************************/
428 void *list_prev_unsynced(list_t *l, void *element)
433 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
435 if (ln->prev == NULL)
438 el = ((u1 *) ln->prev) - l->nodeoffset;
445 * These are local overrides for various environment variables in Emacs.
446 * Please do not remove this and leave it at the end of the file, where
447 * Emacs will automagically detect them.
448 * ---------------------------------------------------------------------
451 * indent-tabs-mode: t