2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Simple, non circular, double linked pointer list.
23 * @note Created because the properties of the standard circular list were not
24 * very well suited for the interference graph implementation.
25 * This list uses an obstack and a free-list to efficiently manage its
27 * @author Kimon Hoffmann
36 * Helper macro that returns a new uninitialized list element by either
37 * fetching one from the free-list or allocating a new one on the lists
39 * @param list the list for which to allocate the element.
40 * @return the newly allocated, uninitialized element.
42 static plist_element_t *allocate_element(plist_t* list) {
43 plist_element_t *new_element;
45 if (list->first_free_element != NULL) {
46 new_element = list->first_free_element;
47 list->first_free_element = new_element->next;
48 new_element->next = NULL;
51 new_element = OALLOC(list->obst, plist_element_t);
57 plist_t *plist_new(void) {
58 plist_t *list = xmalloc(sizeof(*list) + sizeof(*list->obst));
60 list->obst = (struct obstack *)&list[1];
61 list->foreign_obstack = 0;
62 list->first_element = NULL;
63 list->last_element = NULL;
64 list->first_free_element = NULL;
65 list->element_count = 0;
67 obstack_init(list->obst);
71 plist_t *plist_obstack_new(struct obstack *obst) {
72 plist_t *list = OALLOC(obst, plist_t);
75 list->foreign_obstack = 1;
76 list->first_element = NULL;
77 list->last_element = NULL;
78 list->first_free_element = NULL;
79 list->element_count = 0;
84 void plist_free(plist_t *list) {
85 list->first_element = NULL;
86 list->last_element = NULL;
87 list->first_free_element = NULL;
88 list->element_count = 0;
90 if (! list->foreign_obstack) {
91 obstack_free(list->obst, NULL);
96 void plist_insert_back(plist_t *list, void *value) {
97 if (list->last_element != NULL) {
98 plist_insert_after(list, list->last_element, value);
101 plist_element_t *newElement = allocate_element(list);
103 newElement->data = value;
104 newElement->prev = NULL;
105 newElement->next = NULL;
106 list->first_element = list->last_element = newElement;
107 list->element_count = 1;
111 void plist_insert_front(plist_t *list, void *value) {
112 if (list->first_element != NULL) {
113 plist_insert_before(list, list->first_element, value);
116 plist_element_t *newElement = allocate_element(list);
118 newElement->data = value;
119 newElement->prev = NULL;
120 newElement->next = NULL;
121 list->first_element = list->last_element = newElement;
122 list->element_count = 1;
126 void plist_insert_before(plist_t *list, plist_element_t *element, void *value) {
127 plist_element_t *prevElement;
128 plist_element_t *newElement = allocate_element(list);
130 newElement->data = value;
131 newElement->next = element;
132 prevElement = element->prev;
133 newElement->prev = prevElement;
135 if (prevElement != NULL) {
136 prevElement->next = newElement;
139 list->first_element = newElement;
142 element->prev = newElement;
143 ++list->element_count;
146 void plist_insert_after(plist_t* list, plist_element_t* element, void* value) {
147 plist_element_t *nextElement;
148 plist_element_t *newElement = allocate_element(list);
150 newElement->data = value;
151 newElement->prev = element;
152 nextElement = element->next;
153 newElement->next = nextElement;
155 if (nextElement != NULL) {
156 nextElement->prev = newElement;
159 list->last_element = newElement;
162 element->next = newElement;
163 ++list->element_count;
166 int plist_has_value(plist_t *list, void *value) {
167 plist_element_t *iter;
169 for (iter = plist_first(list); iter; iter = plist_element_get_next(iter)) {
170 if (plist_element_get_value(iter) == value)
177 plist_element_t *plist_find_value(plist_t *list, void *value) {
178 plist_element_t *iter;
180 for (iter = plist_first(list); iter; iter = plist_element_get_next(iter)) {
181 if (plist_element_get_value(iter) == value)
188 void plist_erase(plist_t *list, plist_element_t *element) {
189 plist_element_t *next_element = element->next;
190 plist_element_t *prev_element = element->prev;
192 if (next_element != NULL) {
193 next_element->prev = prev_element;
196 list->last_element = prev_element;
199 if (prev_element != NULL) {
200 prev_element->next = next_element;
203 list->first_element = next_element;
206 --list->element_count;
208 /* Clean the element and prepend it to the free list */
209 element->prev = NULL; /* The allocation code expects prev to be NULL */
210 element->next = list->first_free_element;
211 list->first_free_element = element;
214 void plist_clear(plist_t *list) {
215 plist_element_t *curr_element = list->first_element;
217 while (curr_element != NULL) {
218 curr_element->prev = NULL;
219 curr_element = curr_element->next;
222 curr_element = list->last_element;
224 if (curr_element != NULL) {
225 curr_element->next = list->first_free_element;
228 list->first_free_element = list->first_element;
229 list->first_element = 0;
230 list->last_element = 0;
231 list->element_count = 0;