Commit 9b7dc272 9b7dc2723bd164421d1dc25818a5e3e037eb43d0 by Sergey Poznyakoff

(list_locate): New function

1 parent 74f72efa
...@@ -35,6 +35,7 @@ extern int list_remove __P ((list_t, void *item)); ...@@ -35,6 +35,7 @@ extern int list_remove __P ((list_t, void *item));
35 extern int list_replace __P ((list_t list, void *old_item, void *new_item)); 35 extern int list_replace __P ((list_t list, void *old_item, void *new_item));
36 extern int list_get __P ((list_t, size_t _index, void **pitem)); 36 extern int list_get __P ((list_t, size_t _index, void **pitem));
37 extern int list_to_array __P ((list_t list, void **array, size_t count, size_t *pcount)); 37 extern int list_to_array __P ((list_t list, void **array, size_t count, size_t *pcount));
38 extern int list_locate __P ((list_t list, void *item, void **ret_item));
38 39
39 typedef int list_action_t __PMT ((void* item, void* cbdata)); 40 typedef int list_action_t __PMT ((void* item, void* cbdata));
40 41
......
...@@ -156,6 +156,32 @@ def_comp (const void *item, const void *value) ...@@ -156,6 +156,32 @@ def_comp (const void *item, const void *value)
156 } 156 }
157 157
158 int 158 int
159 list_locate (list_t list, void *item, void **ret_item)
160 {
161 struct list_data *current, *previous;
162 list_comparator_t comp;
163 int status = ENOENT;
164
165 if (list == NULL)
166 return EINVAL;
167 comp = list->comp ? list->comp : def_comp;
168 monitor_wrlock (list->monitor);
169 for (previous = &(list->head), current = list->head.next;
170 current != &(list->head); previous = current, current = current->next)
171 {
172 if (comp (current->item, item) == 0)
173 {
174 if (ret_item)
175 *ret_item = current->item;
176 status = 0;
177 break;
178 }
179 }
180 monitor_unlock (list->monitor);
181 return status;
182 }
183
184 int
159 list_insert (list_t list, void *item, void *new_item) 185 list_insert (list_t list, void *item, void *new_item)
160 { 186 {
161 struct list_data *current; 187 struct list_data *current;
......