2006-08-30 15:29:34 +04:00
/*
Unix SMB / CIFS implementation .
some simple double linked list macros
2010-02-11 02:53:58 +03:00
Copyright ( C ) Andrew Tridgell 1998 - 2010
2006-08-30 15:29:34 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-08-30 15:29:34 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-08-30 15:29:34 +04:00
*/
/* To use these macros you must have a structure containing a next and
prev pointer */
2006-09-17 09:11:57 +04:00
# ifndef _DLINKLIST_H
# define _DLINKLIST_H
2010-02-11 02:53:58 +03:00
/*
February 2010 - changed list format to have a prev pointer from the
list head . This makes DLIST_ADD_END ( ) O ( 1 ) even though we only have
one list pointer .
The scheme is as follows :
1 ) with no entries in the list :
list_head = = NULL
2 ) with 1 entry in the list :
list_head - > next = = NULL
list_head - > prev = = list_head
3 ) with 2 entries in the list :
list_head - > next = = element2
list_head - > prev = = element2
element2 - > prev = = list_head
element2 - > next = = NULL
4 ) with N entries in the list :
list_head - > next = = element2
list_head - > prev = = elementN
elementN - > prev = = element { N - 1 }
elementN - > next = = NULL
2006-08-30 15:29:34 +04:00
2010-02-11 02:53:58 +03:00
This allows us to find the tail of the list by using
list_head - > prev , which means we can add to the end of the list in
O ( 1 ) time
Note that the ' type ' arguments below are no longer needed , but
are kept for now to prevent an incompatible argument change
*/
/*
add an element at the front of a list
*/
2006-08-30 15:29:34 +04:00
# define DLIST_ADD(list, p) \
do { \
if ( ! ( list ) ) { \
2010-02-11 02:53:58 +03:00
( p ) - > prev = ( list ) = ( p ) ; \
( p ) - > next = NULL ; \
2006-08-30 15:29:34 +04:00
} else { \
2010-02-11 02:53:58 +03:00
( p ) - > prev = ( list ) - > prev ; \
2006-08-30 15:29:34 +04:00
( list ) - > prev = ( p ) ; \
( p ) - > next = ( list ) ; \
( list ) = ( p ) ; \
2010-02-11 02:53:58 +03:00
} \
2006-08-30 15:29:34 +04:00
} while ( 0 )
2010-02-11 02:53:58 +03:00
/*
remove an element from a list
Note that the element doesn ' t have to be in the list . If it
isn ' t then this is a no - op
*/
2006-08-30 15:29:34 +04:00
# define DLIST_REMOVE(list, p) \
do { \
if ( ( p ) = = ( list ) ) { \
2010-02-11 02:53:58 +03:00
if ( ( p ) - > next ) ( p ) - > next - > prev = ( p ) - > prev ; \
2006-08-30 15:29:34 +04:00
( list ) = ( p ) - > next ; \
2010-02-11 02:53:58 +03:00
} else if ( ( list ) & & ( p ) = = ( list ) - > prev ) { \
( p ) - > prev - > next = NULL ; \
( list ) - > prev = ( p ) - > prev ; \
2006-08-30 15:29:34 +04:00
} else { \
if ( ( p ) - > prev ) ( p ) - > prev - > next = ( p ) - > next ; \
if ( ( p ) - > next ) ( p ) - > next - > prev = ( p ) - > prev ; \
} \
2010-02-11 02:53:58 +03:00
if ( ( p ) ! = ( list ) ) ( p ) - > next = ( p ) - > prev = NULL ; \
2006-08-30 15:29:34 +04:00
} while ( 0 )
2010-02-11 02:53:58 +03:00
/*
find the head of the list given any element in it .
Note that this costs O ( N ) , so you should avoid this macro
if at all possible !
*/
2010-02-11 02:30:59 +03:00
# define DLIST_HEAD(p, result_head) \
do { \
2010-02-11 02:53:58 +03:00
( result_head ) = ( p ) ; \
while ( DLIST_PREV ( result_head ) ) ( result_head ) = ( result_head ) - > prev ; \
2010-02-11 02:30:59 +03:00
} while ( 0 )
/* return the last element in the list */
2010-02-11 02:53:58 +03:00
# define DLIST_TAIL(list) ((list)?(list)->prev:NULL)
2010-02-11 02:30:59 +03:00
/* return the previous element in the list. */
2010-02-11 02:53:58 +03:00
# define DLIST_PREV(p) (((p)->prev && (p)->prev->next != NULL)?(p)->prev:NULL)
2006-08-30 15:29:34 +04:00
/* insert 'p' after the given element 'el' in a list. If el is NULL then
this is the same as a DLIST_ADD ( ) */
# define DLIST_ADD_AFTER(list, p, el) \
do { \
if ( ! ( list ) | | ! ( el ) ) { \
DLIST_ADD ( list , p ) ; \
} else { \
2010-02-11 02:53:58 +03:00
( p ) - > prev = ( el ) ; \
( p ) - > next = ( el ) - > next ; \
( el ) - > next = ( p ) ; \
if ( ( p ) - > next ) ( p ) - > next - > prev = ( p ) ; \
if ( ( list ) - > prev = = ( el ) ) ( list ) - > prev = ( p ) ; \
2006-08-30 15:29:34 +04:00
} \
} while ( 0 )
2010-02-11 02:53:58 +03:00
/*
add to the end of a list .
Note that ' type ' is ignored
*/
# define DLIST_ADD_END(list, p, type) \
2006-08-30 15:29:34 +04:00
do { \
2010-02-11 02:53:58 +03:00
if ( ! ( list ) ) { \
DLIST_ADD ( list , p ) ; \
} else { \
DLIST_ADD_AFTER ( list , p , ( list ) - > prev ) ; \
} \
2006-08-30 15:29:34 +04:00
} while ( 0 )
2014-04-02 16:52:01 +04:00
/* promote an element to the front of a list */
2010-02-11 02:53:58 +03:00
# define DLIST_PROMOTE(list, p) \
2006-08-30 15:29:34 +04:00
do { \
2010-02-11 02:53:58 +03:00
DLIST_REMOVE ( list , p ) ; \
DLIST_ADD ( list , p ) ; \
} while ( 0 )
/*
demote an element to the end of a list .
Note that ' type ' is ignored
*/
# define DLIST_DEMOTE(list, p, type) \
do { \
DLIST_REMOVE ( list , p ) ; \
DLIST_ADD_END ( list , p , NULL ) ; \
} while ( 0 )
/*
concatenate two lists - putting all elements of the 2 nd list at the
end of the first list .
Note that ' type ' is ignored
*/
# define DLIST_CONCATENATE(list1, list2, type) \
do { \
if ( ! ( list1 ) ) { \
( list1 ) = ( list2 ) ; \
} else { \
( list1 ) - > prev - > next = ( list2 ) ; \
if ( list2 ) { \
void * _tmplist = ( void * ) ( list1 ) - > prev ; \
( list1 ) - > prev = ( list2 ) - > prev ; \
( list2 ) - > prev = _tmplist ; \
2006-08-30 15:29:34 +04:00
} \
2010-02-11 02:53:58 +03:00
} \
2006-08-30 15:29:34 +04:00
} while ( 0 )
2006-09-17 09:11:57 +04:00
# endif /* _DLINKLIST_H */