]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-list.c
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation version 2.1.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libkmod-private.h"
25 static inline struct list_node
*list_node_init(struct list_node
*node
)
33 static inline struct list_node
*list_node_next(struct list_node
*node
)
41 static inline struct list_node
*list_node_prev(struct list_node
*node
)
49 static inline void list_node_append(struct list_node
*list
,
50 struct list_node
*node
)
57 node
->prev
= list
->prev
;
58 list
->prev
->next
= node
;
63 static inline struct list_node
*list_node_remove(struct list_node
*node
)
65 if (node
->prev
== node
|| node
->next
== node
)
68 node
->prev
->next
= node
->next
;
69 node
->next
->prev
= node
->prev
;
74 struct kmod_list
*kmod_list_append(struct kmod_list
*list
, void *data
)
76 struct kmod_list
*new;
78 new = malloc(sizeof(*new));
83 list_node_append(list
? &list
->node
: NULL
, &new->node
);
85 return list
? list
: new;
88 struct kmod_list
*kmod_list_prepend(struct kmod_list
*list
, void *data
)
90 struct kmod_list
*new;
92 new = malloc(sizeof(*new));
97 list_node_append(list
? &list
->node
: NULL
, &new->node
);
102 struct kmod_list
*kmod_list_remove(struct kmod_list
*list
)
104 struct list_node
*node
;
109 node
= list_node_remove(&list
->node
);
115 return container_of(node
, struct kmod_list
, node
);
118 struct kmod_list
*kmod_list_remove_data(struct kmod_list
*list
,
121 struct kmod_list
*itr
;
122 struct list_node
*node
;
124 for (itr
= list
; itr
!= NULL
; itr
= kmod_list_next(list
, itr
)) {
125 if (itr
->data
== data
)
132 node
= list_node_remove(&itr
->node
);
138 return container_of(node
, struct kmod_list
, node
);
142 * n must be greater to or equal the number of elements (we don't check the
145 struct kmod_list
*kmod_list_remove_n_latest(struct kmod_list
*list
,
152 * Get last element, remove all appended elments and if list became
153 * empty, set return pointer to NULL
155 l
= kmod_list_prev(list
, list
);
159 for (i
= 0; i
< n
; i
++)
160 l
= kmod_list_remove(l
);
162 /* If list became empty, save it*/
169 KMOD_EXPORT
struct kmod_list
*kmod_list_prev(struct kmod_list
*list
,
170 struct kmod_list
*curr
)
172 if (list
== NULL
|| curr
== NULL
)
175 if (curr
->node
.prev
== &list
->node
)
178 return container_of(curr
->node
.prev
, struct kmod_list
, node
);
181 KMOD_EXPORT
struct kmod_list
*kmod_list_next(struct kmod_list
*list
,
182 struct kmod_list
*curr
)
184 if (list
== NULL
|| curr
== NULL
)
187 if (curr
->node
.next
== &list
->node
)
190 return container_of(curr
->node
.next
, struct kmod_list
, node
);