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