* src/toolbox/list.c, src/toolbox/list.h (list_create_dump): New
[cacao.git] / src / toolbox / list.h
1 /* src/toolbox/list.h - synchronized linked list
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28
29    Changes: Christian Thalinger
30
31    $Id: list.h 5894 2006-11-02 12:54:15Z twisti $
32
33 */
34
35
36 #ifndef _LIST_H
37 #define _LIST_H
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include "vm/global.h"
43
44
45 /* ---------------------- interface description -----------------------------
46
47 The list management with this module works like this:
48         
49         - to be used in a list, a structure must have an element of type
50           'listnode'.
51           
52         - there needs to be a structure of type 'list'.
53         
54         - the function list_init(l, nodeoffset) initializes the structure.
55           nodeoffset is the offset of the 'listnode' from the start of the
56           structure in bytes.
57           
58         - The remaining functions provide inserting, removing and searching.
59           
60 This small example aims to demonstrate correct usage:
61
62
63
64         void bsp() {
65                 struct node {
66                         listnode linkage;
67                         int value;
68                         } a,b,c, *el;
69                         
70                 list l;
71                 
72                 a.value = 7;
73                 b.value = 9;
74                 c.value = 11;
75                 
76                 list_init (&l, OFFSET(struct node,linkage) );
77                 list_addlast (&l, a);
78                 list_addlast (&l, b);
79                 list_addlast (&l, c);
80                 
81                 e = list_first (&l);
82                 while (e) {
83                         printf ("Element: %d\n", e->value);
84                         e = list_next (&l,e);
85                         }
86         }
87         
88         
89         The output from this program should be:
90                 7
91                 9
92                 11
93
94
95
96 The reason for the usage of 'nodeoffset' is that this way, the same node can
97 part of different lists (there must be one 'listnode' element for every
98 distinct list).
99
100 */
101
102 /* listnode *******************************************************************/
103
104 typedef struct listnode listnode;
105
106 struct listnode {
107         listnode *next;
108         listnode *prev;
109 };
110
111
112 /* list ***********************************************************************/
113
114 typedef struct list list;
115
116 struct list {
117 #if defined(ENABLE_THREADS)
118         java_objectheader  lock;            /* threads lock object                */
119 #endif
120         listnode          *first;
121         listnode          *last;
122         s4                 nodeoffset;
123 };
124
125
126 /* function prototypes ********************************************************/
127
128 list *list_create(s4 nodeoffset);
129 list *list_create_dump(s4 nodeoffset);
130
131 void list_add_first(list *l, void *element);
132
133 void list_add_last(list *l, void *element);
134 void list_add_last_unsynced(list *l, void *element);
135
136 void list_add_before(list *l, void *element, void *newelement);
137
138 void list_remove(list *l, void *element);
139  
140 void *list_first(list *l);
141 void *list_first_unsynced(list *l);
142
143 void *list_last(list *l);
144 void *list_last_unsynced(list *l);
145
146 void *list_next(list *l, void *element);
147 void *list_next_unsynced(list *l, void *element);
148
149 void *list_prev(list *l, void *element);
150 void *list_prev_unsynced(list *l, void *element);
151
152 #endif /* _LIST_H */
153
154
155 /*
156  * These are local overrides for various environment variables in Emacs.
157  * Please do not remove this and leave it at the end of the file, where
158  * Emacs will automagically detect them.
159  * ---------------------------------------------------------------------
160  * Local variables:
161  * mode: c
162  * indent-tabs-mode: t
163  * c-basic-offset: 4
164  * tab-width: 4
165  * End:
166  */