1998-08-17 10:47:53 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-08-17 10:47:53 +04:00
some simple double linked list macros
Copyright ( C ) Andrew Tridgell 1998
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-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1998-08-17 10:47:53 +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 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1998-08-17 10:47:53 +04:00
*/
/* To use these macros you must have a structure containing a next and
prev pointer */
2006-09-18 11:52:16 +04:00
# ifndef _DLINKLIST_H
# define _DLINKLIST_H
1998-08-17 10:47:53 +04:00
/* hook into the front of the list */
# define DLIST_ADD(list, p) \
2006-09-18 11:52:16 +04:00
do { \
1998-08-17 10:47:53 +04:00
if ( ! ( list ) ) { \
( list ) = ( p ) ; \
( p ) - > next = ( p ) - > prev = NULL ; \
} else { \
( list ) - > prev = ( p ) ; \
( p ) - > next = ( list ) ; \
( p ) - > prev = NULL ; \
( list ) = ( p ) ; \
1998-09-24 01:49:09 +04:00
} \
2006-09-18 11:52:16 +04:00
} while ( 0 )
1998-08-17 10:47:53 +04:00
2001-03-21 02:07:36 +03:00
/* remove an element from a list - element doesn't have to be in list. */
1998-08-17 10:47:53 +04:00
# define DLIST_REMOVE(list, p) \
2006-09-18 11:52:16 +04:00
do { \
1998-08-17 10:47:53 +04:00
if ( ( p ) = = ( list ) ) { \
( list ) = ( p ) - > next ; \
if ( list ) ( list ) - > prev = NULL ; \
} else { \
2001-03-21 02:07:36 +03:00
if ( ( p ) - > prev ) ( p ) - > prev - > next = ( p ) - > next ; \
1998-08-17 10:47:53 +04:00
if ( ( p ) - > next ) ( p ) - > next - > prev = ( p ) - > prev ; \
1998-09-24 01:49:09 +04:00
} \
2006-08-28 05:44:40 +04:00
if ( ( p ) ! = ( list ) ) ( p ) - > next = ( p ) - > prev = NULL ; \
2006-09-18 11:52:16 +04:00
} while ( 0 )
1998-08-17 10:47:53 +04:00
/* promote an element to the top of the list */
# define DLIST_PROMOTE(list, p) \
2006-09-18 11:52:16 +04:00
do { \
DLIST_REMOVE ( list , p ) ; \
DLIST_ADD ( list , p ) ; \
} while ( 0 )
2001-03-17 05:06:16 +03:00
2006-12-12 17:52:13 +03:00
/* hook into the end of the list - needs the entry type */
2006-09-18 11:52:16 +04:00
# define DLIST_ADD_END(list, p, type) \
do { \
2001-03-17 05:06:16 +03:00
if ( ! ( list ) ) { \
( list ) = ( p ) ; \
( p ) - > next = ( p ) - > prev = NULL ; \
} else { \
2006-09-18 11:52:16 +04:00
type tmp ; \
for ( tmp = ( list ) ; tmp - > next ; tmp = tmp - > next ) ; \
tmp - > next = ( p ) ; \
2001-03-17 05:06:16 +03:00
( p ) - > next = NULL ; \
2006-09-18 11:52:16 +04:00
( p ) - > prev = tmp ; \
2001-03-17 05:06:16 +03:00
} \
2006-09-18 11:52:16 +04:00
} while ( 0 )
2001-03-21 02:07:36 +03:00
2005-09-30 21:13:37 +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 { \
p - > prev = el ; \
p - > next = el - > next ; \
el - > next = p ; \
if ( p - > next ) p - > next - > prev = p ; \
} \
} while ( 0 )
2006-09-18 11:52:16 +04:00
/* demote an element to the end of the list, needs a tmp pointer */
2001-03-21 02:07:36 +03:00
# define DLIST_DEMOTE(list, p, tmp) \
2006-09-18 11:52:16 +04:00
do { \
DLIST_REMOVE ( list , p ) ; \
DLIST_ADD_END ( list , p , tmp ) ; \
} while ( 0 )
/* concatenate two lists - putting all elements of the 2nd list at the
end of the first list */
# define DLIST_CONCATENATE(list1, list2, type) \
do { \
if ( ! ( list1 ) ) { \
( list1 ) = ( list2 ) ; \
} else { \
type tmp ; \
for ( tmp = ( list1 ) ; tmp - > next ; tmp = tmp - > next ) ; \
tmp - > next = ( list2 ) ; \
if ( list2 ) { \
( list2 ) - > prev = tmp ; \
} \
} \
} while ( 0 )
# endif /* _DLINKLIST_H */