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 7246 2007-01-29 18:49:05Z 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 *list_create(s4 nodeoffset)
59 #if defined(ENABLE_THREADS)
60 lock_init_object_lock((java_objectheader *) l);
65 l->nodeoffset = nodeoffset;
71 /* list_create_dump ************************************************************
73 Allocates a new list on the dump memory.
75 ATTENTION: This list does NOT initialize the locking object!!!
77 *******************************************************************************/
79 list *list_create_dump(s4 nodeoffset)
87 l->nodeoffset = nodeoffset;
93 void list_add_first(list *l, void *element)
97 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
99 LOCK_MONITOR_ENTER(l);
114 LOCK_MONITOR_EXIT(l);
118 /* list_add_last ***************************************************************
120 Adds the element as last element.
122 *******************************************************************************/
124 void list_add_last(list *l, void *element)
126 LOCK_MONITOR_ENTER(l);
128 list_add_last_unsynced(l, element);
130 LOCK_MONITOR_EXIT(l);
134 /* list_add_list_unsynced ******************************************************
136 Adds the element as last element but does NO locking!
138 ATTENTION: Use this function with care!!!
140 *******************************************************************************/
142 void list_add_last_unsynced(list *l, void *element)
146 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
163 /* list_add_before *************************************************************
165 Adds the element newelement to the list l before element.
167 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
169 *******************************************************************************/
171 void list_add_before(list *l, void *element, void *newelement)
176 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
177 newln = (listnode *) (((u1 *) newelement) + l->nodeoffset);
179 LOCK_MONITOR_ENTER(l);
181 /* set the new links */
183 newln->prev = ln->prev;
187 newln->prev->next = newln;
191 /* set list's first and last if necessary */
199 LOCK_MONITOR_EXIT(l);
203 void list_remove(list *l, void *element)
207 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
209 LOCK_MONITOR_ENTER(l);
212 ln->next->prev = ln->prev;
217 ln->prev->next = ln->next;
224 LOCK_MONITOR_EXIT(l);
228 /* list_first ******************************************************************
230 Returns the first element of the list.
232 *******************************************************************************/
234 void *list_first(list *l)
238 LOCK_MONITOR_ENTER(l);
240 el = list_first_unsynced(l);
242 LOCK_MONITOR_EXIT(l);
248 /* list_first_unsynced *********************************************************
250 Returns the first element of the list, but does NO locking!
252 ATTENTION: Use this function with care!!!
254 *******************************************************************************/
256 void *list_first_unsynced(list *l)
260 if (l->first == NULL)
263 el = ((u1 *) l->first) - l->nodeoffset;
269 /* list_last *******************************************************************
271 Returns the last element of the list.
273 *******************************************************************************/
275 void *list_last(list *l)
279 LOCK_MONITOR_ENTER(l);
281 el = list_last_unsynced(l);
283 LOCK_MONITOR_EXIT(l);
289 /* list_last_unsynced **********************************************************
291 Returns the last element of the list, but does NO locking!
293 ATTENTION: Use this function with care!!!
295 *******************************************************************************/
297 void *list_last_unsynced(list *l)
304 el = ((u1 *) l->last) - l->nodeoffset;
310 /* list_next *******************************************************************
312 Returns the next element of element from the list.
314 *******************************************************************************/
316 void *list_next(list *l, void *element)
320 LOCK_MONITOR_ENTER(l);
322 el = list_next_unsynced(l, element);
324 LOCK_MONITOR_EXIT(l);
330 /* list_next_unsynced **********************************************************
332 Returns the next element of element from the list, but does NO
335 ATTENTION: Use this function with care!!!
337 *******************************************************************************/
339 void *list_next_unsynced(list *l, void *element)
344 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
346 if (ln->next == NULL)
349 el = ((u1 *) ln->next) - l->nodeoffset;
355 /* list_prev *******************************************************************
357 Returns the previous element of element from the list.
359 *******************************************************************************/
361 void *list_prev(list *l, void *element)
365 LOCK_MONITOR_ENTER(l);
367 el = list_prev_unsynced(l, element);
369 LOCK_MONITOR_EXIT(l);
375 /* list_prev_unsynced **********************************************************
377 Returns the previous element of element from the list, but does NO
380 ATTENTION: Use this function with care!!!
382 *******************************************************************************/
384 void *list_prev_unsynced(list *l, void *element)
389 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
391 if (ln->prev == NULL)
394 el = ((u1 *) ln->prev) - l->nodeoffset;
401 * These are local overrides for various environment variables in Emacs.
402 * Please do not remove this and leave it at the end of the file, where
403 * Emacs will automagically detect them.
404 * ---------------------------------------------------------------------
407 * indent-tabs-mode: t