You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

60 lines
2.3 KiB

/*
Copyright (C) 2012, Aaron Lindsay <aaron@aclindsay.com>
This file is part of Aedrix.
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
the Free Software Foundation; either version 2 of the License, or
(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 along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef LIST_H
#define LIST_H
/*
* Assuming ptr is a pointer to a member variable of type parent_type, and ptr
* is named member in parent_type, find the address of the struct which owns
* ptr.
*/
#define container(ptr, parent_type, member) \
((parent_type *) ((char *)ptr - (char *)&((parent_type *)0)->member))
/*
* Given a pointer to a struct (head) of type dlist_node, which points to a
* list of type parent_type, and a pointer of that type (it), this macro will
* execute the contained instructions once for each list element, with 'it' set
* to each element in the list, in turn.
*/
#define for_each_list(it, head, parent_type, member) \
for (it = container((head)->next, parent_type, member); it->member.next != (head); it = container(it->member.next, parent_type, member))
#define for_each_list_noinit(it, head, parent_type, member) \
for (; it->member.next != (head); it = container(it->member.next, parent_type, member))
struct dlist_node {
struct dlist_node *next, *prev;
};
void init_list(struct dlist_node *n);
int list_empty(struct dlist_node *n);
//functions for dealing with single list elements
void insert_after(struct dlist_node *n, struct dlist_node *to_add);
void insert_before(struct dlist_node *n, struct dlist_node *to_add);
void remove(struct dlist_node *to_remove);
//functions for dealing with one or more elements
void remove_splice(struct dlist_node *from, struct dlist_node *to);
void insert_splice_before(struct dlist_node *n, struct dlist_node *first, struct dlist_node *last);
#endif /* LIST_H */