1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/libdm/datastruct/list.h

104 lines
2.1 KiB
C
Raw Normal View History

2001-10-31 15:47:01 +03:00
/*
* Copyright (C) 2001 Sistina Software
*
* This file is released under the LGPL.
*/
2001-10-01 19:14:39 +04:00
#ifndef _LVM_LIST_H
#define _LVM_LIST_H
#include <assert.h>
2001-10-31 15:47:01 +03:00
struct list {
struct list *n, *p;
};
#define LIST_INIT(name) struct list name = { &(name), &(name) }
static inline void list_init(struct list *head)
{
2001-10-31 15:47:01 +03:00
head->n = head->p = head;
}
static inline void list_add(struct list *head, struct list *elem)
{
2001-10-31 15:47:01 +03:00
assert(head->n);
2001-10-31 15:47:01 +03:00
elem->n = head;
elem->p = head->p;
2001-10-31 15:47:01 +03:00
head->p->n = elem;
head->p = elem;
}
static inline void list_add_h(struct list *head, struct list *elem)
{
2001-10-31 15:47:01 +03:00
assert(head->n);
2001-10-31 15:47:01 +03:00
elem->n = head->n;
elem->p = head;
2001-10-31 15:47:01 +03:00
head->n->p = elem;
head->n = elem;
}
static inline void list_del(struct list *elem)
{
2001-10-31 15:47:01 +03:00
elem->n->p = elem->p;
elem->p->n = elem->n;
}
static inline int list_empty(struct list *head)
{
2001-10-31 15:47:01 +03:00
return head->n == head;
}
static inline int list_end(struct list *head, struct list *elem)
{
return elem->n == head;
}
2003-05-06 16:01:13 +04:00
static inline struct list *list_next(struct list *head, struct list *elem)
{
return (list_end(head, elem) ? NULL : elem->n);
}
2003-09-15 22:22:50 +04:00
#define list_item(v, t) \
((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->list))
#define list_struct_base(v, t, h) \
((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->h))
/* Given a known element in a known structure, locate another */
#define struct_field(v, t, e, f) \
(((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
/* Given a known element in a known structure, locate the list head */
#define list_head(v, t, e) struct_field(v, t, e, list)
2001-10-31 15:47:01 +03:00
#define list_iterate(v, head) \
for (v = (head)->n; v != head; v = v->n)
#define list_uniterate(v, head, start) \
for (v = (start)->p; v != head; v = v->p)
#define list_iterate_safe(v, t, head) \
for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
2003-09-15 22:22:50 +04:00
#define list_iterate_items(v, head) \
for (v = list_item((head)->n, typeof(*v)); &v->list != (head); \
v = list_item(v->list.n, typeof(*v)))
static inline unsigned int list_size(const struct list *head)
{
unsigned int s = 0;
const struct list *v;
list_iterate(v, head)
s++;
return s;
}
#endif