1 /* Generic single linked list to keep various information
2 Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with files compiled with
23 GCC to produce an executable, this does not cause the resulting executable
24 to be covered by the GNU General Public License. This exception does not
25 however invalidate any other reasons why the executable file might be
26 covered by the GNU General Public License. */
28 #ifndef __GNU_OBJC_LIST_H
29 #define __GNU_OBJC_LIST_H
31 #include <objc/objc-mem.h>
35 struct objc_list *tail;
38 /* Return a cons cell produced from (head . tail) */
40 static __inline__ struct objc_list*
41 list_cons(void* head, struct objc_list* tail)
43 struct objc_list* cell;
46 cell = GC_MALLOC_UNCOLLECTABLE(sizeof(struct objc_list));
48 cell = malloc(sizeof(struct objc_list));
55 /* Return the length of a list, list_length(NULL) returns zero */
58 list_length(struct objc_list* list)
69 /* Return the Nth element of LIST, where N count from zero. If N
70 larger than the list length, NULL is returned */
72 static __inline__ void*
73 list_nth(int index, struct objc_list* list)
84 /* Remove the element at the head by replacing it by its successor */
86 static __inline__ void
87 list_remove_head(struct objc_list** list)
90 struct objc_list* tail = (*list)->tail; /* fetch next */
91 *(*list) = *tail; /* copy next to list head */
92 objc_free(tail); /* free next */
94 else { /* only one element in list */
101 /* Remove the element with `car' set to ELEMENT */
103 static __inline__ void
104 list_remove_elem(struct objc_list** list, void* elem)
107 if ((*list)->head == elem)
108 list_remove_head(list);
109 list = &((*list)->tail);
113 /* Map FUNCTION over all elements in LIST */
115 static __inline__ void
116 list_mapcar(struct objc_list* list, void(*function)(void*))
119 (*function)(list->head);
124 /* Return element that has ELEM as car */
126 static __inline__ struct objc_list**
127 list_find(struct objc_list** list, void* elem)
130 if ((*list)->head == elem)
132 list = &((*list)->tail);
137 /* Free list (backwards recursive) */
139 static void list_free(struct objc_list *list)
142 list_free(list->tail);
146 #endif /* __GNU_OBJC_LIST_H */