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 7481 2007-03-08 13:12:21Z 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_last_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 /* list_remove ***************************************************************
207 *******************************************************************************/
209 void list_remove(list *l, void *element)
211 LOCK_MONITOR_ENTER(l);
213 list_remove_unsynced(l, element);
215 LOCK_MONITOR_EXIT(l);
219 /* list_remove_unsynced ********************************************************
221 Removes the element but does NO locking!
223 ATTENTION: Use this function with care!!!
225 *******************************************************************************/
227 void list_remove_unsynced(list *l, void *element)
231 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
234 ln->next->prev = ln->prev;
239 ln->prev->next = ln->next;
248 /* list_first ******************************************************************
250 Returns the first element of the list.
252 *******************************************************************************/
254 void *list_first(list *l)
258 LOCK_MONITOR_ENTER(l);
260 el = list_first_unsynced(l);
262 LOCK_MONITOR_EXIT(l);
268 /* list_first_unsynced *********************************************************
270 Returns the first element of the list, but does NO locking!
272 ATTENTION: Use this function with care!!!
274 *******************************************************************************/
276 void *list_first_unsynced(list *l)
280 if (l->first == NULL)
283 el = ((u1 *) l->first) - l->nodeoffset;
289 /* list_last *******************************************************************
291 Returns the last element of the list.
293 *******************************************************************************/
295 void *list_last(list *l)
299 LOCK_MONITOR_ENTER(l);
301 el = list_last_unsynced(l);
303 LOCK_MONITOR_EXIT(l);
309 /* list_last_unsynced **********************************************************
311 Returns the last element of the list, but does NO locking!
313 ATTENTION: Use this function with care!!!
315 *******************************************************************************/
317 void *list_last_unsynced(list *l)
324 el = ((u1 *) l->last) - l->nodeoffset;
330 /* list_next *******************************************************************
332 Returns the next element of element from the list.
334 *******************************************************************************/
336 void *list_next(list *l, void *element)
340 LOCK_MONITOR_ENTER(l);
342 el = list_next_unsynced(l, element);
344 LOCK_MONITOR_EXIT(l);
350 /* list_next_unsynced **********************************************************
352 Returns the next element of element from the list, but does NO
355 ATTENTION: Use this function with care!!!
357 *******************************************************************************/
359 void *list_next_unsynced(list *l, void *element)
364 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
366 if (ln->next == NULL)
369 el = ((u1 *) ln->next) - l->nodeoffset;
375 /* list_prev *******************************************************************
377 Returns the previous element of element from the list.
379 *******************************************************************************/
381 void *list_prev(list *l, void *element)
385 LOCK_MONITOR_ENTER(l);
387 el = list_prev_unsynced(l, element);
389 LOCK_MONITOR_EXIT(l);
395 /* list_prev_unsynced **********************************************************
397 Returns the previous element of element from the list, but does NO
400 ATTENTION: Use this function with care!!!
402 *******************************************************************************/
404 void *list_prev_unsynced(list *l, void *element)
409 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
411 if (ln->prev == NULL)
414 el = ((u1 *) ln->prev) - l->nodeoffset;
421 * These are local overrides for various environment variables in Emacs.
422 * Please do not remove this and leave it at the end of the file, where
423 * Emacs will automagically detect them.
424 * ---------------------------------------------------------------------
427 * indent-tabs-mode: t