1 /* src/toolbox/list.c - double linked list
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
29 $Id: list.c 5123 2006-07-12 21:45:34Z twisti $
40 #include "mm/memory.h"
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/lock.h"
44 # include "threads/native/threads.h"
46 # include "threads/none/lock.h"
49 #include "toolbox/list.h"
52 /* list_create *****************************************************************
54 Allocates a new list and initializes the lock object.
56 *******************************************************************************/
58 list *list_create(s4 nodeoffset)
64 #if defined(ENABLE_THREADS)
65 lock_init_object_lock((java_objectheader *) l);
70 l->nodeoffset = nodeoffset;
76 void list_add_first(list *l, void *element)
80 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
82 LOCK_MONITOR_ENTER(l);
101 /* list_add_last ***************************************************************
103 Adds the element as last element.
105 *******************************************************************************/
107 void list_add_last(list *l, void *element)
111 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
113 LOCK_MONITOR_ENTER(l);
128 LOCK_MONITOR_EXIT(l);
132 /* list_add_list_unsynced ******************************************************
134 Adds the element as last element but does NO locking!
136 ATTENTION: This function is used during bootstrap. DON'T USE IT!!!
138 *******************************************************************************/
140 void list_add_last_unsynced(list *l, void *element)
144 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
161 /* list_add_before *************************************************************
163 Adds the element newelement to the list l before element.
165 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
167 *******************************************************************************/
169 void list_add_before(list *l, void *element, void *newelement)
174 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
175 newln = (listnode *) (((u1 *) newelement) + l->nodeoffset);
177 LOCK_MONITOR_ENTER(l);
179 /* set the new links */
181 newln->prev = ln->prev;
185 newln->prev->next = newln;
189 /* set list's first and last if necessary */
197 LOCK_MONITOR_EXIT(l);
201 void list_remove(list *l, void *element)
205 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
207 LOCK_MONITOR_ENTER(l);
210 ln->next->prev = ln->prev;
215 ln->prev->next = ln->next;
222 LOCK_MONITOR_EXIT(l);
226 void *list_first(list *l)
230 LOCK_MONITOR_ENTER(l);
232 if (l->first == NULL)
235 el = ((u1 *) l->first) - l->nodeoffset;
237 LOCK_MONITOR_EXIT(l);
243 void *list_last(list *l)
247 LOCK_MONITOR_ENTER(l);
252 el = ((u1 *) l->last) - l->nodeoffset;
254 LOCK_MONITOR_EXIT(l);
260 void *list_next(list *l, void *element)
265 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
267 LOCK_MONITOR_ENTER(l);
269 if (ln->next == NULL)
272 el = ((u1 *) ln->next) - l->nodeoffset;
274 LOCK_MONITOR_EXIT(l);
280 void *list_prev(list *l, void *element)
285 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
287 LOCK_MONITOR_ENTER(l);
289 if (ln->prev == NULL)
292 el = ((u1 *) ln->prev) - l->nodeoffset;
294 LOCK_MONITOR_EXIT(l);
301 * These are local overrides for various environment variables in Emacs.
302 * Please do not remove this and leave it at the end of the file, where
303 * Emacs will automagically detect them.
304 * ---------------------------------------------------------------------
307 * indent-tabs-mode: t