mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
fixed and improved linked list
This commit is contained in:
parent
e30e9292f2
commit
eebc1edf9e
2 changed files with 40 additions and 12 deletions
|
@ -12,8 +12,13 @@
|
|||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "hooks.h"
|
||||
|
||||
/* Forward declarations */
|
||||
struct list_elm;
|
||||
struct node;
|
||||
struct path;
|
||||
struct interface;
|
||||
|
||||
/** Static list initialization */
|
||||
#define LIST_INIT { \
|
||||
|
@ -24,27 +29,41 @@ struct list_elm;
|
|||
}
|
||||
|
||||
#define FOREACH(list, elm) \
|
||||
for ( struct list_elm *elm = (list)->head, ; \
|
||||
elm != (list)->tail; elm = elm->next )
|
||||
for ( struct list_elm *elm = (list)->head; \
|
||||
elm; elm = elm->next )
|
||||
|
||||
#define FOREACH_R(list, elm) \
|
||||
for ( struct list_elm *elm = (list)->tail; \
|
||||
elm; elm = elm->prev )
|
||||
|
||||
#define list_first(list) ((list)->head)
|
||||
#define list_last(list) ((list)->head)
|
||||
#define list_length(list) ((list)->count)
|
||||
|
||||
struct list {
|
||||
struct list_elm *head, tail;
|
||||
struct list_elm *head, *tail;
|
||||
int count;
|
||||
|
||||
pthread_mutex_lock_t lock;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
struct list_elm {
|
||||
void *data;
|
||||
union {
|
||||
void *ptr;
|
||||
struct node *node;
|
||||
struct path *path;
|
||||
struct interface *interface;
|
||||
hook_cb_t hook;
|
||||
};
|
||||
|
||||
struct list_elm *prev, next;
|
||||
struct list_elm *prev, *next;
|
||||
};
|
||||
|
||||
void list_init(struct list *l);
|
||||
|
||||
void list_destroy(struct list *l);
|
||||
|
||||
void list_push(struct list *l, void *d);
|
||||
void list_push(struct list *l, void *p);
|
||||
|
||||
struct list_elm * list_search(struct list *l, int (*cmp)(void *));
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include "list.h"
|
||||
|
||||
void list_init(struct list *l)
|
||||
|
@ -33,26 +34,34 @@ void list_destroy(struct list *l)
|
|||
pthread_mutex_destroy(&l->lock);
|
||||
}
|
||||
|
||||
void list_push(struct list *l, void *d)
|
||||
void list_push(struct list *l, void *p)
|
||||
{
|
||||
struct list_elm *e = alloc(sizeof(struct list_elm));
|
||||
|
||||
pthread_mutex_lock(&l->lock);
|
||||
|
||||
e->data = d;
|
||||
e->ptr = p;
|
||||
e->prev = l->tail;
|
||||
e->next = NULL;
|
||||
|
||||
l->tail->next = e;
|
||||
if (l->tail)
|
||||
l->tail->next = e;
|
||||
if (l->head)
|
||||
l->head->prev = e;
|
||||
else
|
||||
l->head = e;
|
||||
|
||||
l->tail = e;
|
||||
|
||||
l->count++;
|
||||
|
||||
pthread_mutex_unlock(&l->lock);
|
||||
}
|
||||
|
||||
struct list_elm * list_search(struct list *l, int (*cmp)(void *))
|
||||
{
|
||||
foreach(l, it) {
|
||||
if (!cmp(it->data))
|
||||
FOREACH(l, it) {
|
||||
if (!cmp(it->ptr))
|
||||
return it;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue