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 7915 2007-05-18 14:22:19Z twisti $
36 #include "mm/memory.h"
38 #include "threads/lock-common.h"
40 #include "toolbox/list.h"
43 /* list_create *****************************************************************
45 Allocates a new list and initializes the lock object.
47 *******************************************************************************/
49 list_t *list_create(s4 nodeoffset)
55 LOCK_INIT_OBJECT_LOCK(l);
59 l->nodeoffset = nodeoffset;
66 /* list_create_dump ************************************************************
68 Allocates a new list on the dump memory.
70 ATTENTION: This list does NOT initialize the locking object!!!
72 *******************************************************************************/
74 list_t *list_create_dump(s4 nodeoffset)
82 l->nodeoffset = nodeoffset;
89 /* list_add_first **************************************************************
91 Adds the element as first element.
93 *******************************************************************************/
95 void list_add_first(list_t *l, void *element)
97 LOCK_MONITOR_ENTER(l);
99 list_add_first_unsynced(l, element);
101 LOCK_MONITOR_EXIT(l);
105 /* list_add_first_unsynced *****************************************************
107 Adds the element as first element, but WITHOUT LOCKING!
109 ATTENTION: Use this function with care!!!
111 *******************************************************************************/
113 void list_add_first_unsynced(list_t *l, void *element)
117 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
132 /* increase number of elements */
138 /* list_add_last ***************************************************************
140 Adds the element as last element.
142 *******************************************************************************/
144 void list_add_last(list_t *l, void *element)
146 LOCK_MONITOR_ENTER(l);
148 list_add_last_unsynced(l, element);
150 LOCK_MONITOR_EXIT(l);
154 /* list_add_last_unsynced ******************************************************
156 Adds the element as last element but does NO locking!
158 ATTENTION: Use this function with care!!!
160 *******************************************************************************/
162 void list_add_last_unsynced(list_t *l, void *element)
166 ln = (listnode_t *) (((u1 *) 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 *) (((u1 *) element) + l->nodeoffset);
201 newln = (listnode_t *) (((u1 *) newelement) + l->nodeoffset);
203 LOCK_MONITOR_ENTER(l);
205 /* set the new links */
207 newln->prev = ln->prev;
211 newln->prev->next = newln;
215 /* set list's first and last if necessary */
223 /* increase number of elements */
227 LOCK_MONITOR_EXIT(l);
231 /* list_remove ***************************************************************
235 *******************************************************************************/
237 void list_remove(list_t *l, void *element)
239 LOCK_MONITOR_ENTER(l);
241 list_remove_unsynced(l, element);
243 LOCK_MONITOR_EXIT(l);
247 /* list_remove_unsynced ********************************************************
249 Removes the element but does NO locking!
251 ATTENTION: Use this function with care!!!
253 *******************************************************************************/
255 void list_remove_unsynced(list_t *l, void *element)
259 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
262 ln->next->prev = ln->prev;
267 ln->prev->next = ln->next;
274 /* decrease number of elements */
280 /* list_first ******************************************************************
282 Returns the first element of the list.
284 *******************************************************************************/
286 void *list_first(list_t *l)
290 LOCK_MONITOR_ENTER(l);
292 el = list_first_unsynced(l);
294 LOCK_MONITOR_EXIT(l);
300 /* list_first_unsynced *********************************************************
302 Returns the first element of the list, but does NO locking!
304 ATTENTION: Use this function with care!!!
306 *******************************************************************************/
308 void *list_first_unsynced(list_t *l)
312 if (l->first == NULL)
315 el = ((u1 *) l->first) - l->nodeoffset;
321 /* list_last *******************************************************************
323 Returns the last element of the list.
325 *******************************************************************************/
327 void *list_last(list_t *l)
331 LOCK_MONITOR_ENTER(l);
333 el = list_last_unsynced(l);
335 LOCK_MONITOR_EXIT(l);
341 /* list_last_unsynced **********************************************************
343 Returns the last element of the list, but does NO locking!
345 ATTENTION: Use this function with care!!!
347 *******************************************************************************/
349 void *list_last_unsynced(list_t *l)
356 el = ((u1 *) l->last) - l->nodeoffset;
362 /* list_next *******************************************************************
364 Returns the next element of element from the list.
366 *******************************************************************************/
368 void *list_next(list_t *l, void *element)
372 LOCK_MONITOR_ENTER(l);
374 el = list_next_unsynced(l, element);
376 LOCK_MONITOR_EXIT(l);
382 /* list_next_unsynced **********************************************************
384 Returns the next element of element from the list, but does NO
387 ATTENTION: Use this function with care!!!
389 *******************************************************************************/
391 void *list_next_unsynced(list_t *l, void *element)
396 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
398 if (ln->next == NULL)
401 el = ((u1 *) ln->next) - l->nodeoffset;
407 /* list_prev *******************************************************************
409 Returns the previous element of element from the list.
411 *******************************************************************************/
413 void *list_prev(list_t *l, void *element)
417 LOCK_MONITOR_ENTER(l);
419 el = list_prev_unsynced(l, element);
421 LOCK_MONITOR_EXIT(l);
427 /* list_prev_unsynced **********************************************************
429 Returns the previous element of element from the list, but does NO
432 ATTENTION: Use this function with care!!!
434 *******************************************************************************/
436 void *list_prev_unsynced(list_t *l, void *element)
441 ln = (listnode_t *) (((u1 *) element) + l->nodeoffset);
443 if (ln->prev == NULL)
446 el = ((u1 *) ln->prev) - l->nodeoffset;
453 * These are local overrides for various environment variables in Emacs.
454 * Please do not remove this and leave it at the end of the file, where
455 * Emacs will automagically detect them.
456 * ---------------------------------------------------------------------
459 * indent-tabs-mode: t