Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Zwinkau
libfirm
Commits
f75cc683
Commit
f75cc683
authored
Aug 30, 2006
by
Christian Würdig
Browse files
made code more firm style
added iterator macro [r8144]
parent
7cfc603a
Changes
2
Hide whitespace changes
Inline
Side-by-side
ir/adt/plist.c
View file @
f75cc683
...
@@ -5,7 +5,8 @@
...
@@ -5,7 +5,8 @@
* This list uses an obstack and a free-list to efficiently manage its
* This list uses an obstack and a free-list to efficiently manage its
* elements.
* elements.
* @author Kimon Hoffmann
* @author Kimon Hoffmann
* @date 14.07.2005
* @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains
* @note Until now the code is entirely untested so it probably contains
* plenty of errors.
* plenty of errors.
*/
*/
...
@@ -20,128 +21,155 @@
...
@@ -20,128 +21,155 @@
* @param list the list for which to allocate the element.
* @param list the list for which to allocate the element.
* @return the newly allocated, uninitialized element.
* @return the newly allocated, uninitialized element.
*/
*/
static
PListElement
*
allocate_element
(
PList
*
list
)
{
static
plist_element_t
*
allocate_element
(
plist_t
*
list
)
{
PListElement
*
newElement
;
plist_element_t
*
newElement
;
if
(
list
->
firstFreeElement
!=
NULL
)
{
newElement
=
list
->
firstFreeElement
;
if
(
list
->
first_free_element
!=
NULL
)
{
list
->
firstFreeElement
=
newElement
->
next
;
newElement
=
list
->
first_free_element
;
newElement
->
next
=
NULL
;
list
->
first_free_element
=
newElement
->
next
;
}
else
{
newElement
->
next
=
NULL
;
}
else
{
newElement
=
obstack_alloc
(
&
list
->
obst
,
sizeof
(
*
newElement
));
newElement
=
obstack_alloc
(
&
list
->
obst
,
sizeof
(
*
newElement
));
}
}
return
newElement
;
return
newElement
;
}
}
PList
*
plist_new
(
void
)
{
plist_t
*
plist_new
(
void
)
{
PList
*
list
=
xmalloc
(
sizeof
(
*
list
));
plist_t
*
list
=
xmalloc
(
sizeof
(
*
list
));
obstack_init
(
&
list
->
obst
);
obstack_init
(
&
list
->
obst
);
list
->
firstElement
=
NULL
;
list
->
first_element
=
NULL
;
list
->
lastElement
=
NULL
;
list
->
last_element
=
NULL
;
list
->
firstFreeElement
=
NULL
;
list
->
first_free_element
=
NULL
;
list
->
elementCount
=
0
;
list
->
element_count
=
0
;
return
list
;
return
list
;
}
}
void
plist_free
(
PLis
t
*
list
)
{
void
plist_free
(
plist_
t
*
list
)
{
obstack_free
(
&
list
->
obst
,
NULL
);
obstack_free
(
&
list
->
obst
,
NULL
);
list
->
firstElement
=
NULL
;
list
->
lastElement
=
NULL
;
list
->
first_element
=
NULL
;
list
->
firstFreeElement
=
NULL
;
list
->
last_element
=
NULL
;
list
->
elementCount
=
0
;
list
->
first_free_element
=
NULL
;
list
->
element_count
=
0
;
xfree
(
list
);
xfree
(
list
);
}
}
void
plist_insert_back
(
PList
*
list
,
void
*
value
)
{
void
plist_insert_back
(
plist_t
*
list
,
void
*
value
)
{
if
(
list
->
lastElement
!=
NULL
)
{
if
(
list
->
last_element
!=
NULL
)
{
plist_insert_after
(
list
,
list
->
lastElement
,
value
);
plist_insert_after
(
list
,
list
->
last_element
,
value
);
}
else
{
}
PListElement
*
newElement
=
allocate_element
(
list
);
else
{
newElement
->
data
=
value
;
plist_element_t
*
newElement
=
allocate_element
(
list
);
newElement
->
prev
=
NULL
;
newElement
->
next
=
NULL
;
newElement
->
data
=
value
;
list
->
firstElement
=
list
->
lastElement
=
newElement
;
newElement
->
prev
=
NULL
;
list
->
elementCount
=
1
;
newElement
->
next
=
NULL
;
list
->
first_element
=
list
->
last_element
=
newElement
;
list
->
element_count
=
1
;
}
}
}
}
void
plist_insert_front
(
PList
*
list
,
void
*
value
)
{
void
plist_insert_front
(
plist_t
*
list
,
void
*
value
)
{
if
(
list
->
firstElement
!=
NULL
)
{
if
(
list
->
first_element
!=
NULL
)
{
plist_insert_before
(
list
,
list
->
firstElement
,
value
);
plist_insert_before
(
list
,
list
->
first_element
,
value
);
}
else
{
}
PListElement
*
newElement
=
allocate_element
(
list
);
else
{
newElement
->
data
=
value
;
plist_element_t
*
newElement
=
allocate_element
(
list
);
newElement
->
prev
=
NULL
;
newElement
->
next
=
NULL
;
newElement
->
data
=
value
;
list
->
firstElement
=
list
->
lastElement
=
newElement
;
newElement
->
prev
=
NULL
;
list
->
elementCount
=
1
;
newElement
->
next
=
NULL
;
list
->
first_element
=
list
->
last_element
=
newElement
;
list
->
element_count
=
1
;
}
}
}
}
void
plist_insert_before
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
)
{
void
plist_insert_before
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
)
{
PL
ist
E
lement
*
prevElement
;
pl
ist
_e
lement
_t
*
prevElement
;
PL
ist
E
lement
*
newElement
=
allocate_element
(
list
);
pl
ist
_e
lement
_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
data
=
value
;
newElement
->
next
=
element
;
newElement
->
next
=
element
;
prevElement
=
element
->
prev
;
prevElement
=
element
->
prev
;
newElement
->
prev
=
prevElement
;
newElement
->
prev
=
prevElement
;
if
(
prevElement
!=
NULL
)
{
if
(
prevElement
!=
NULL
)
{
prevElement
->
next
=
newElement
;
prevElement
->
next
=
newElement
;
}
else
{
list
->
firstElement
=
newElement
;
}
}
element
->
prev
=
newElement
;
else
{
++
list
->
elementCount
;
list
->
first_element
=
newElement
;
}
element
->
prev
=
newElement
;
++
list
->
element_count
;
}
}
void
plist_insert_after
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
)
{
void
plist_insert_after
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
)
{
PL
ist
E
lement
*
nextElement
;
pl
ist
_e
lement
_t
*
nextElement
;
PL
ist
E
lement
*
newElement
=
allocate_element
(
list
);
pl
ist
_e
lement
_t
*
newElement
=
allocate_element
(
list
);
newElement
->
data
=
value
;
newElement
->
data
=
value
;
newElement
->
prev
=
element
;
newElement
->
prev
=
element
;
nextElement
=
element
->
next
;
nextElement
=
element
->
next
;
newElement
->
next
=
nextElement
;
newElement
->
next
=
nextElement
;
if
(
nextElement
!=
NULL
)
{
if
(
nextElement
!=
NULL
)
{
nextElement
->
prev
=
newElement
;
nextElement
->
prev
=
newElement
;
}
else
{
list
->
lastElement
=
newElement
;
}
}
else
{
list
->
last_element
=
newElement
;
}
element
->
next
=
newElement
;
element
->
next
=
newElement
;
++
list
->
element
C
ount
;
++
list
->
element
_c
ount
;
}
}
void
plist_erase
(
PList
*
list
,
PListElement
*
element
)
{
void
plist_erase
(
plist_t
*
list
,
plist_element_t
*
element
)
{
PListElement
*
nextElement
=
element
->
next
;
plist_element_t
*
nextElement
=
element
->
next
;
PListElement
*
prevElement
=
element
->
prev
;
plist_element_t
*
prevElement
=
element
->
prev
;
if
(
nextElement
!=
NULL
)
{
if
(
nextElement
!=
NULL
)
{
nextElement
->
prev
=
prevElement
;
nextElement
->
prev
=
prevElement
;
}
else
{
list
->
lastElement
=
prevElement
;
}
}
else
{
list
->
last_element
=
prevElement
;
}
if
(
prevElement
!=
NULL
)
{
if
(
prevElement
!=
NULL
)
{
prevElement
->
next
=
nextElement
;
prevElement
->
next
=
nextElement
;
}
else
{
list
->
firstElement
=
nextElement
;
}
}
--
list
->
elementCount
;
else
{
list
->
first_element
=
nextElement
;
}
--
list
->
element_count
;
/* Clean the element and prepend it to the free list */
/* Clean the element and prepend it to the free list */
element
->
prev
=
NULL
;
/* The allocation code expects prev to be NULL */
element
->
prev
=
NULL
;
/* The allocation code expects prev to be NULL */
element
->
next
=
list
->
first
F
ree
E
lement
;
element
->
next
=
list
->
first
_f
ree
_e
lement
;
list
->
first
F
ree
E
lement
=
element
;
list
->
first
_f
ree
_e
lement
=
element
;
}
}
void
plist_clear
(
PList
*
list
)
{
void
plist_clear
(
plist_t
*
list
)
{
PListElement
*
currentElement
=
list
->
firstElement
;
plist_element_t
*
currentElement
=
list
->
first_element
;
while
(
currentElement
!=
NULL
)
{
while
(
currentElement
!=
NULL
)
{
currentElement
->
prev
=
NULL
;
currentElement
->
prev
=
NULL
;
currentElement
=
currentElement
->
next
;
currentElement
=
currentElement
->
next
;
}
}
currentElement
=
list
->
lastElement
;
currentElement
=
list
->
last_element
;
if
(
currentElement
!=
NULL
)
{
if
(
currentElement
!=
NULL
)
{
currentElement
->
next
=
list
->
first
F
ree
E
lement
;
currentElement
->
next
=
list
->
first
_f
ree
_e
lement
;
}
}
list
->
firstFreeElement
=
list
->
firstElement
;
list
->
firstElement
=
list
->
lastElement
=
0
;
list
->
first_free_element
=
list
->
first_element
;
list
->
elementCount
=
0
;
list
->
first_element
=
0
;
list
->
last_element
=
0
;
list
->
element_count
=
0
;
}
}
ir/adt/plist.h
View file @
f75cc683
...
@@ -5,7 +5,8 @@
...
@@ -5,7 +5,8 @@
* This list uses an obstack and a free-list to efficiently manage its
* This list uses an obstack and a free-list to efficiently manage its
* elements.
* elements.
* @author Kimon Hoffmann
* @author Kimon Hoffmann
* @date 14.07.2005
* @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains
* @note Until now the code is entirely untested so it probably contains
* plenty of errors.
* plenty of errors.
*/
*/
...
@@ -15,13 +16,13 @@
...
@@ -15,13 +16,13 @@
#include <stddef.h>
#include <stddef.h>
#include "obst.h"
#include "obst.h"
typedef
struct
PL
ist
E
lement
PL
ist
E
lement
;
typedef
struct
_pl
ist
_e
lement
pl
ist
_e
lement
_t
;
typedef
struct
PL
ist
PLis
t
;
typedef
struct
_pl
ist
plist_
t
;
/**
/**
* The
P
list data type.
* The
p
list data type.
*/
*/
struct
PL
ist
{
struct
_pl
ist
{
/**
/**
* The obstack used for all allocations.
* The obstack used for all allocations.
*/
*/
...
@@ -29,29 +30,29 @@ struct PList {
...
@@ -29,29 +30,29 @@ struct PList {
/**
/**
* First element in the list.
* First element in the list.
*/
*/
PL
ist
E
lement
*
first
E
lement
;
pl
ist
_e
lement
_t
*
first
_e
lement
;
/**
/**
* Last element in the list.
* Last element in the list.
*/
*/
PL
ist
E
lement
*
last
E
lement
;
pl
ist
_e
lement
_t
*
last
_e
lement
;
/**
/**
* Current number of elements in the list.
* Current number of elements in the list.
*/
*/
int
element
C
ount
;
int
element
_c
ount
;
/**
/**
* First element in the free list.
* First element in the free list.
* Please note that the free list is a single linked list and all back
* Please note that the free list is a single linked list and all back
* references are invalid.
* references are invalid.
*/
*/
PL
ist
E
lement
*
first
F
ree
E
lement
;
pl
ist
_e
lement
_t
*
first
_f
ree
_e
lement
;
};
};
/**
/**
* An element in the pointer list.
* An element in the pointer list.
*/
*/
struct
PL
ist
E
lement
{
struct
_pl
ist
_e
lement
{
PL
ist
E
lement
*
next
;
pl
ist
_e
lement
_t
*
next
;
PL
ist
E
lement
*
prev
;
pl
ist
_e
lement
_t
*
prev
;
void
*
data
;
void
*
data
;
};
};
...
@@ -59,14 +60,14 @@ struct PListElement {
...
@@ -59,14 +60,14 @@ struct PListElement {
* Creates a new pointer list and initializes it.
* Creates a new pointer list and initializes it.
* @return The newly created pointer list.
* @return The newly created pointer list.
*/
*/
PLis
t
*
plist_new
(
void
);
plist_
t
*
plist_new
(
void
);
/**
/**
* Frees the passed pointer list.
* Frees the passed pointer list.
* After a call to this function all references to the list and any of
* After a call to this function all references to the list and any of
* its elements are invalid.
* its elements are invalid.
*/
*/
void
plist_free
(
PLis
t
*
list
);
void
plist_free
(
plist_
t
*
list
);
/**
/**
* Returns the number of elements in a pointer list.
* Returns the number of elements in a pointer list.
...
@@ -74,21 +75,21 @@ void plist_free(PList* list);
...
@@ -74,21 +75,21 @@ void plist_free(PList* list);
* @return The number of elements in a pointer list.
* @return The number of elements in a pointer list.
*/
*/
#define plist_count(list) \
#define plist_count(list) \
((list)->element
C
ount)
((list)->element
_c
ount)
/**
/**
* Inserts an element at the back of a pointer list.
* Inserts an element at the back of a pointer list.
* @param list the pointer list to append the new element to.
* @param list the pointer list to append the new element to.
* @param value the element value to append.
* @param value the element value to append.
*/
*/
void
plist_insert_back
(
PLis
t
*
list
,
void
*
value
);
void
plist_insert_back
(
plist_
t
*
list
,
void
*
value
);
/**
/**
* Inserts an element at the front of a pointer list.
* Inserts an element at the front of a pointer list.
* @param list the pointer list to prepend the new element to.
* @param list the pointer list to prepend the new element to.
* @param value the element value to prepend.
* @param value the element value to prepend.
*/
*/
void
plist_insert_front
(
PLis
t
*
list
,
void
*
value
);
void
plist_insert_front
(
plist_
t
*
list
,
void
*
value
);
/**
/**
* Inserts an element into a pointer list before the specified element,
* Inserts an element into a pointer list before the specified element,
...
@@ -98,7 +99,7 @@ void plist_insert_front(PList* list, void* value);
...
@@ -98,7 +99,7 @@ void plist_insert_front(PList* list, void* value);
* be inserted. This element must be a part of @p list.
* be inserted. This element must be a part of @p list.
* @param value the element value to insert.
* @param value the element value to insert.
*/
*/
void
plist_insert_before
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
);
void
plist_insert_before
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
);
/**
/**
* Inserts an element into a pointer list after the specified element,
* Inserts an element into a pointer list after the specified element,
...
@@ -108,7 +109,7 @@ void plist_insert_before(PList* list, PListElement* element, void* value);
...
@@ -108,7 +109,7 @@ void plist_insert_before(PList* list, PListElement* element, void* value);
* be inserted. This element must be a part of @p list.
* be inserted. This element must be a part of @p list.
* @param value the element value to insert.
* @param value the element value to insert.
*/
*/
void
plist_insert_after
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
,
void
*
value
);
void
plist_insert_after
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
,
void
*
value
);
/**
/**
* Erases the specified element from the pointer list.
* Erases the specified element from the pointer list.
...
@@ -116,13 +117,13 @@ void plist_insert_after(PList* list, PListElement* element, void* value);
...
@@ -116,13 +117,13 @@ void plist_insert_after(PList* list, PListElement* element, void* value);
* @param element the list element to erase. This element must be a part
* @param element the list element to erase. This element must be a part
* of @p list.
* of @p list.
*/
*/
void
plist_erase
(
PLis
t
*
list
,
PL
ist
E
lement
*
element
);
void
plist_erase
(
plist_
t
*
list
,
pl
ist
_e
lement
_t
*
element
);
/**
/**
* Erases all elements from the specified pointer list.
* 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 cleard.
*/
*/
void
plist_clear
(
PLis
t
*
list
);
void
plist_clear
(
plist_
t
*
list
);
/**
/**
* Returns the first element of a pointer list.
* Returns the first element of a pointer list.
...
@@ -130,7 +131,7 @@ void plist_clear(PList* list);
...
@@ -130,7 +131,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty
* @return a pointer to the element or NULL if the list is empty
*/
*/
#define plist_first(list) \
#define plist_first(list) \
((list)->first
E
lement)
((list)->first
_e
lement)
/**
/**
* Returns the last element of a pointer list.
* Returns the last element of a pointer list.
...
@@ -138,7 +139,7 @@ void plist_clear(PList* list);
...
@@ -138,7 +139,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty
* @return a pointer to the element or NULL if the list is empty
*/
*/
#define plist_last(list) \
#define plist_last(list) \
((list)->last
E
lement)
((list)->last
_e
lement)
/**
/**
* Checks whether a pointer list element has a successor or not.
* Checks whether a pointer list element has a successor or not.
...
@@ -184,4 +185,10 @@ void plist_clear(PList* list);
...
@@ -184,4 +185,10 @@ void plist_clear(PList* list);
#define plist_element_get_value(element) \
#define plist_element_get_value(element) \
((element)->data)
((element)->data)
/**
* Convenience macro to iterate over a plist.
*/
#define foreach_plist(list, el) \
for (el = plist_first(list); el; el = plist_element_get_next(el))
#endif
/*_PLIST_H_*/
#endif
/*_PLIST_H_*/
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment