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/mutex.hpp"
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 l->mutex = Mutex_new();
54 l->nodeoffset = nodeoffset;
61 /* list_free *******************************************************************
65 *******************************************************************************/
67 void list_free(list_t *l)
71 Mutex_delete(l->mutex);
77 /* list_create_dump ************************************************************
79 Allocates a new list on the dump memory.
81 ATTENTION: This list does NOT initialize the locking object!!!
83 *******************************************************************************/
85 list_t *list_create_dump(int nodeoffset)
89 l = DumpMemory_allocate(sizeof(list_t));
94 l->nodeoffset = nodeoffset;
101 /* list_lock *******************************************************************
105 *******************************************************************************/
107 void list_lock(list_t *l)
109 Mutex_lock(l->mutex);
113 /* list_unlock *****************************************************************
117 *******************************************************************************/
119 void list_unlock(list_t *l)
121 Mutex_unlock(l->mutex);
125 /* list_add_first **************************************************************
127 Adds the element as first element.
129 *******************************************************************************/
131 void list_add_first(list_t *l, void *element)
135 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
150 /* Increase number of elements. */
156 /* list_add_last ***************************************************************
158 Adds the element as last element.
160 *******************************************************************************/
162 void list_add_last(list_t *l, void *element)
166 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
181 /* Increase number of elements. */
187 /* list_add_before *************************************************************
189 Adds the element newelement to the list l before element.
191 [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
193 *******************************************************************************/
195 void list_add_before(list_t *l, void *element, void *newelement)
200 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
201 newln = (listnode_t *) (((uint8_t *) newelement) + l->nodeoffset);
203 /* Set the new links. */
205 newln->prev = ln->prev;
209 newln->prev->next = newln;
213 /* set list's first and last if necessary */
221 /* Increase number of elements. */
227 /* list_remove ***************************************************************
231 *******************************************************************************/
233 void list_remove(list_t *l, void *element)
237 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
240 ln->next->prev = ln->prev;
245 ln->prev->next = ln->next;
252 /* Decrease number of elements. */
258 /* list_first ******************************************************************
260 Returns the first element of the list.
262 *******************************************************************************/
264 void *list_first(list_t *l)
268 if (l->first == NULL)
271 el = ((uint8_t *) l->first) - l->nodeoffset;
277 /* list_last *******************************************************************
279 Returns the last element of the list.
281 *******************************************************************************/
283 void *list_last(list_t *l)
290 el = ((uint8_t *) l->last) - l->nodeoffset;
296 /* list_next *******************************************************************
298 Returns the next element of element from the list.
300 *******************************************************************************/
302 void *list_next(list_t *l, void *element)
307 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
309 if (ln->next == NULL)
312 el = ((uint8_t *) ln->next) - l->nodeoffset;
318 /* list_prev *******************************************************************
320 Returns the previous element of element from the list.
322 *******************************************************************************/
324 void *list_prev(list_t *l, void *element)
329 ln = (listnode_t *) (((uint8_t *) element) + l->nodeoffset);
331 if (ln->prev == NULL)
334 el = ((uint8_t *) ln->prev) - l->nodeoffset;
341 * These are local overrides for various environment variables in Emacs.
342 * Please do not remove this and leave it at the end of the file, where
343 * Emacs will automagically detect them.
344 * ---------------------------------------------------------------------
347 * indent-tabs-mode: t