2001-10-31 15:47:01 +03:00
/*
2004-03-30 23:35:44 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
* Copyright ( C ) 2004 Red Hat , Inc . All rights reserved .
2001-10-31 15:47:01 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2001-10-31 15:47:01 +03:00
*/
2001-09-28 17:19:17 +04:00
2001-10-01 19:14:39 +04:00
# ifndef _LVM_LIST_H
# define _LVM_LIST_H
2001-09-28 17:19:17 +04:00
2003-03-24 21:08:53 +03:00
# include <assert.h>
2001-10-31 15:47:01 +03:00
struct list {
struct list * n , * p ;
2001-09-28 17:19:17 +04:00
} ;
2003-07-05 02:34:56 +04:00
# define LIST_INIT(name) struct list name = { &(name), &(name) }
2002-12-12 23:55:49 +03:00
static inline void list_init ( struct list * head )
{
2001-10-31 15:47:01 +03:00
head - > n = head - > p = head ;
}
2001-09-28 17:19:17 +04:00
2002-12-12 23:55:49 +03:00
static inline void list_add ( struct list * head , struct list * elem )
{
2001-10-31 15:47:01 +03:00
assert ( head - > n ) ;
2001-09-28 17:19:17 +04:00
2001-10-31 15:47:01 +03:00
elem - > n = head ;
elem - > p = head - > p ;
2001-09-28 17:19:17 +04:00
2001-10-31 15:47:01 +03:00
head - > p - > n = elem ;
head - > p = elem ;
2001-09-28 17:19:17 +04:00
}
2002-12-12 23:55:49 +03:00
static inline void list_add_h ( struct list * head , struct list * elem )
{
2001-10-31 15:47:01 +03:00
assert ( head - > n ) ;
2001-09-28 17:19:17 +04:00
2001-10-31 15:47:01 +03:00
elem - > n = head - > n ;
elem - > p = head ;
2001-09-28 17:19:17 +04:00
2001-10-31 15:47:01 +03:00
head - > n - > p = elem ;
head - > n = elem ;
2001-09-28 17:19:17 +04:00
}
2002-12-12 23:55:49 +03:00
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 ;
2001-09-28 17:19:17 +04:00
}
2002-12-12 23:55:49 +03:00
static inline int list_empty ( struct list * head )
{
2001-10-31 15:47:01 +03:00
return head - > n = = head ;
2001-09-28 17:19:17 +04:00
}
2002-12-12 23:55:49 +03:00
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) \
2002-01-29 19:28:52 +03:00
for ( v = ( head ) - > n ; v ! = head ; v = v - > n )
2003-07-05 02:34:56 +04:00
# define list_uniterate(v, head, start) \
for ( v = ( start ) - > p ; v ! = head ; v = v - > p )
2002-01-29 19:28:52 +03:00
# define list_iterate_safe(v, t, head) \
for ( v = ( head ) - > n , t = v - > n ; v ! = head ; v = t , t = v - > n )
2001-09-28 17:19:17 +04:00
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 ) ) )
2002-12-20 02:25:55 +03:00
static inline unsigned int list_size ( const struct list * head )
2002-12-12 23:55:49 +03:00
{
2002-12-20 02:25:55 +03:00
unsigned int s = 0 ;
const struct list * v ;
2001-11-28 16:45:50 +03:00
list_iterate ( v , head )
2002-12-12 23:55:49 +03:00
s + + ;
2001-11-28 16:45:50 +03:00
return s ;
}
2001-09-28 17:19:17 +04:00
# endif