From: Christian Würdig Date: Wed, 6 Sep 2006 08:40:54 +0000 (+0000) Subject: added plist constructor with foreign obstack X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=9e0551a238d67f7c2428d0474cd93b03c92e0414;p=libfirm added plist constructor with foreign obstack made code more firm style [r8176] --- diff --git a/ir/adt/plist.c b/ir/adt/plist.c index 4cbe15082..9c4261dcf 100644 --- a/ir/adt/plist.c +++ b/ir/adt/plist.c @@ -21,25 +21,26 @@ * @param list the list for which to allocate the element. * @return the newly allocated, uninitialized element. */ -static plist_element_t* allocate_element(plist_t* list) { - plist_element_t* newElement; +static plist_element_t *allocate_element(plist_t* list) { + plist_element_t *new_element; if (list->first_free_element != NULL) { - newElement = list->first_free_element; - list->first_free_element = newElement->next; - newElement->next = NULL; + new_element = list->first_free_element; + list->first_free_element = new_element->next; + new_element->next = NULL; } else { - newElement = obstack_alloc(&list->obst, sizeof(*newElement)); + new_element = obstack_alloc(list->obst, sizeof(*new_element)); } - return newElement; + return new_element; } plist_t* plist_new(void) { plist_t* list = xmalloc(sizeof(*list)); - obstack_init(&list->obst); + obstack_init(list->obst); + list->foreign_obstack = 0; list->first_element = NULL; list->last_element = NULL; list->first_free_element = NULL; @@ -48,14 +49,29 @@ plist_t* plist_new(void) { return list; } -void plist_free(plist_t* list) { - obstack_free(&list->obst, NULL); +plist_t *plist_obstack_new(struct obstack *obst) { + plist_t *list = obstack_alloc(obst, sizeof(*list)); + list->obst = obst; + list->foreign_obstack = 1; list->first_element = NULL; list->last_element = NULL; list->first_free_element = NULL; list->element_count = 0; - xfree(list); + + return list; +} + +void plist_free(plist_t *list) { + list->first_element = NULL; + list->last_element = NULL; + list->first_free_element = NULL; + list->element_count = 0; + + if (! list->foreign_obstack) { + obstack_free(list->obst, NULL); + xfree(list); + } } void plist_insert_back(plist_t* list, void* value) { @@ -128,22 +144,22 @@ void plist_insert_after(plist_t* list, plist_element_t* element, void* value) { ++list->element_count; } -void plist_erase(plist_t* list, plist_element_t* element) { - plist_element_t* nextElement = element->next; - plist_element_t* prevElement = element->prev; +void plist_erase(plist_t *list, plist_element_t *element) { + plist_element_t *next_element = element->next; + plist_element_t *prev_element = element->prev; - if (nextElement != NULL) { - nextElement->prev = prevElement; + if (next_element != NULL) { + next_element->prev = prev_element; } else { - list->last_element = prevElement; + list->last_element = prev_element; } - if (prevElement != NULL) { - prevElement->next = nextElement; + if (prev_element != NULL) { + prev_element->next = next_element; } else { - list->first_element = nextElement; + list->first_element = next_element; } --list->element_count; @@ -154,18 +170,18 @@ void plist_erase(plist_t* list, plist_element_t* element) { list->first_free_element = element; } -void plist_clear(plist_t* list) { - plist_element_t* currentElement = list->first_element; +void plist_clear(plist_t *list) { + plist_element_t *curr_element = list->first_element; - while (currentElement != NULL) { - currentElement->prev = NULL; - currentElement = currentElement->next; + while (curr_element != NULL) { + curr_element->prev = NULL; + curr_element = curr_element->next; } - currentElement = list->last_element; + curr_element = list->last_element; - if (currentElement != NULL) { - currentElement->next = list->first_free_element; + if (curr_element != NULL) { + curr_element->next = list->first_free_element; } list->first_free_element = list->first_element; diff --git a/ir/adt/plist.h b/ir/adt/plist.h index 94a109d28..0c9f0ab62 100644 --- a/ir/adt/plist.h +++ b/ir/adt/plist.h @@ -26,19 +26,26 @@ struct _plist { /** * The obstack used for all allocations. */ - struct obstack obst; + struct obstack *obst; + + /* Set to 1 if plist uses a foreign obstack */ + unsigned foreign_obstack : 1; + /** * First element in the list. */ plist_element_t *first_element; + /** * Last element in the list. */ plist_element_t *last_element; + /** * Current number of elements in the list. */ int element_count; + /** * First element in the free list. * Please note that the free list is a single linked list and all back @@ -51,23 +58,31 @@ struct _plist { * An element in the pointer list. */ struct _plist_element { - plist_element_t* next; - plist_element_t* prev; - void* data; + plist_element_t *next; + plist_element_t *prev; + void *data; }; /** * Creates a new pointer list and initializes it. * @return The newly created pointer list. */ -plist_t* plist_new(void); +plist_t *plist_new(void); + +/** + * Creates a new pointer list and initializes it. + * Uses the given obstack instead of creating one. + * @param obst The obstack to use + * @return The newly created pointer list. + */ +plist_t *plist_obstack_new(struct obstack *obst); /** * Frees the passed pointer list. * After a call to this function all references to the list and any of * its elements are invalid. */ -void plist_free(plist_t* list); +void plist_free(plist_t *list); /** * Returns the number of elements in a pointer list. @@ -82,14 +97,14 @@ void plist_free(plist_t* list); * @param list the pointer list to append the new element to. * @param value the element value to append. */ -void plist_insert_back(plist_t* list, void* value); +void plist_insert_back(plist_t *list, void *value); /** * Inserts an element at the front of a pointer list. * @param list the pointer list to prepend the new element to. * @param value the element value to prepend. */ -void plist_insert_front(plist_t* list, void* value); +void plist_insert_front(plist_t *list, void *value); /** * Inserts an element into a pointer list before the specified element, @@ -99,7 +114,7 @@ void plist_insert_front(plist_t* list, void* value); * be inserted. This element must be a part of @p list. * @param value the element value to insert. */ -void plist_insert_before(plist_t* list, plist_element_t* element, void* value); +void plist_insert_before(plist_t *list, plist_element_t *element, void *value); /** * Inserts an element into a pointer list after the specified element, @@ -109,21 +124,21 @@ void plist_insert_before(plist_t* list, plist_element_t* element, void* value); * be inserted. This element must be a part of @p list. * @param value the element value to insert. */ -void plist_insert_after(plist_t* list, plist_element_t* element, void* value); +void plist_insert_after(plist_t *list, plist_element_t *element, void *value); /** * Erases the specified element from the pointer list. - * @param list the pointer list from which the lement should be erased. + * @param list the pointer list from which the element should be erased. * @param element the list element to erase. This element must be a part * of @p list. */ -void plist_erase(plist_t* list, plist_element_t* element); +void plist_erase(plist_t *list, plist_element_t *element); /** * Erases all elements from the specified pointer list. - * @param list the pointer list that should be cleard. + * @param list the pointer list that should be cleared. */ -void plist_clear(plist_t* list); +void plist_clear(plist_t *list); /** * Returns the first element of a pointer list. @@ -143,7 +158,7 @@ void plist_clear(plist_t* list); /** * Checks whether a pointer list element has a successor or not. - * @param element the list element that should be queried for existance + * @param element the list element that should be queried for existence * of a successor. * @return TRUE if @p element has a successor, otherwise FALSE. */ @@ -152,7 +167,7 @@ void plist_clear(plist_t* list); /** * Checks whether a pointer list element has a predecessor or not. - * @param element the list element that should be queried for existance + * @param element the list element that should be queried for existence * of a predecessor. * @return TRUE if @p element has a successor, otherwise FALSE. */