1 /* src/toolbox/list.c - double linked list
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "mm/memory.h"
34 #include "threads/lock-common.h"
36 #include "toolbox/list.h"
39 /* list_create *****************************************************************
41 Allocates a new list and initializes the lock object.
43 *******************************************************************************/
45 list_t *list_create(int nodeoffset)
51 LOCK_INIT_OBJECT_LOCK(l);
55 l->nodeoffset = nodeoffset;
62 /* list_free *******************************************************************
66 *******************************************************************************/
68 void list_free(list_t *l)
76 /* list_create_dump ************************************************************
78 Allocates a new list on the dump memory.
80 ATTENTION: This list does NOT initialize the locking object!!!
82 *******************************************************************************/
84 list_t *list_create_dump(int nodeoffset)
92 l->nodeoffset = nodeoffset;
99 /* list_lock *******************************************************************
103 *******************************************************************************/
105 void list_lock(list_t *l)
107 LOCK_MONITOR_ENTER(l);
111 /* list_unlock *****************************************************************
115 *******************************************************************************/
117 void list_unlock(list_t *l)
119 LOCK_MONITOR_EXIT(l);
123 /* list_add_first **************************************************************
125 Adds the element as first element.
127 *******************************************************************************/
129 void list_add_first(list_t *l, void *element)
133 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
148 /* Increase number of elements. */
154 /* list_add_last ***************************************************************
156 Adds the element as last element.
158 *******************************************************************************/
160 void list_add_last(list_t *l, void *element)
164 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
179 /* Increase number of elements. */
185 /* list_add_before *************************************************************
187 Adds the element newelement to the list l before element.
189 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
191 *******************************************************************************/
193 void list_add_before(list_t *l, void *element, void *newelement)
198 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
199 newln = (listnode_t *) (((uint8_t *) newelement) + l->nodeoffset);
201 /* Set the new links. */
203 newln->prev = ln->prev;
207 newln->prev->next = newln;
211 /* set list's first and last if necessary */
219 /* Increase number of elements. */
225 /* list_remove ***************************************************************
229 *******************************************************************************/
231 void list_remove(list_t *l, void *element)
235 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
238 ln->next->prev = ln->prev;
243 ln->prev->next = ln->next;
250 /* Decrease number of elements. */
256 /* list_first ******************************************************************
258 Returns the first element of the list.
260 *******************************************************************************/
262 void *list_first(list_t *l)
266 if (l->first == NULL)
269 el = ((uint8_t *) l->first) - l->nodeoffset;
275 /* list_last *******************************************************************
277 Returns the last element of the list.
279 *******************************************************************************/
281 void *list_last(list_t *l)
288 el = ((uint8_t *) l->last) - l->nodeoffset;
294 /* list_next *******************************************************************
296 Returns the next element of element from the list.
298 *******************************************************************************/
300 void *list_next(list_t *l, void *element)
305 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
307 if (ln->next == NULL)
310 el = ((uint8_t *) ln->next) - l->nodeoffset;
316 /* list_prev *******************************************************************
318 Returns the previous element of element from the list.
320 *******************************************************************************/
322 void *list_prev(list_t *l, void *element)
327 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
329 if (ln->prev == NULL)
332 el = ((uint8_t *) ln->prev) - l->nodeoffset;
339 * These are local overrides for various environment variables in Emacs.
340 * Please do not remove this and leave it at the end of the file, where
341 * Emacs will automagically detect them.
342 * ---------------------------------------------------------------------
345 * indent-tabs-mode: t