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 5049 2006-06-23 12:07:26Z twisti $
40 #include "mm/memory.h"
42 #if defined(ENABLE_THREADS)
43 # include "threads/native/threads.h"
46 #include "toolbox/list.h"
47 #include "vm/builtin.h"
50 /* list_create *****************************************************************
52 Allocates a new list and initializes the lock object.
54 *******************************************************************************/
56 list *list_create(s4 nodeoffset)
62 #if defined(ENABLE_THREADS)
63 lock_init_object_lock((java_objectheader *) l);
68 l->nodeoffset = nodeoffset;
74 void list_add_first(list *l, void *element)
78 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
80 BUILTIN_MONITOR_ENTER(l);
95 BUILTIN_MONITOR_EXIT(l);
99 /* list_add_last ***************************************************************
101 Adds the element as last element.
103 *******************************************************************************/
105 void list_add_last(list *l, void *element)
109 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
111 BUILTIN_MONITOR_ENTER(l);
126 BUILTIN_MONITOR_EXIT(l);
130 /* list_add_list_unsynced ******************************************************
132 Adds the element as last element but does NO locking!
134 ATTENTION: This function is used during bootstrap. DON'T USE IT!!!
136 *******************************************************************************/
138 void list_add_last_unsynced(list *l, void *element)
142 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
159 /* list_add_before *************************************************************
161 Adds the element newelement to the list l before element.
163 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
165 *******************************************************************************/
167 void list_add_before(list *l, void *element, void *newelement)
172 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
173 newln = (listnode *) (((u1 *) newelement) + l->nodeoffset);
175 BUILTIN_MONITOR_ENTER(l);
177 /* set the new links */
179 newln->prev = ln->prev;
183 newln->prev->next = newln;
187 /* set list's first and last if necessary */
195 BUILTIN_MONITOR_EXIT(l);
199 void list_remove(list *l, void *element)
203 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
205 BUILTIN_MONITOR_ENTER(l);
208 ln->next->prev = ln->prev;
213 ln->prev->next = ln->next;
220 BUILTIN_MONITOR_EXIT(l);
224 void *list_first(list *l)
228 BUILTIN_MONITOR_ENTER(l);
230 if (l->first == NULL)
233 el = ((u1 *) l->first) - l->nodeoffset;
235 BUILTIN_MONITOR_EXIT(l);
241 void *list_last(list *l)
245 BUILTIN_MONITOR_ENTER(l);
250 el = ((u1 *) l->last) - l->nodeoffset;
252 BUILTIN_MONITOR_EXIT(l);
258 void *list_next(list *l, void *element)
263 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
265 BUILTIN_MONITOR_ENTER(l);
267 if (ln->next == NULL)
270 el = ((u1 *) ln->next) - l->nodeoffset;
272 BUILTIN_MONITOR_EXIT(l);
278 void *list_prev(list *l, void *element)
283 ln = (listnode *) (((u1 *) element) + l->nodeoffset);
285 BUILTIN_MONITOR_ENTER(l);
287 if (ln->prev == NULL)
290 el = ((u1 *) ln->prev) - l->nodeoffset;
292 BUILTIN_MONITOR_EXIT(l);
299 * These are local overrides for various environment variables in Emacs.
300 * Please do not remove this and leave it at the end of the file, where
301 * Emacs will automagically detect them.
302 * ---------------------------------------------------------------------
305 * indent-tabs-mode: t