129 |
return 0; |
return 0; |
130 |
} |
} |
131 |
|
|
132 |
|
list_comparator_t |
133 |
|
list_set_comparator (list_t list, list_comparator_t comp) |
134 |
|
{ |
135 |
|
list_comparator_t old_comp; |
136 |
|
if (list == NULL) |
137 |
|
return EINVAL; |
138 |
|
old_comp = list->comp; |
139 |
|
list->comp = comp; |
140 |
|
return old_comp; |
141 |
|
} |
142 |
|
|
143 |
|
static int |
144 |
|
def_comp (const void *item, const void *value) |
145 |
|
{ |
146 |
|
return item != value; |
147 |
|
} |
148 |
|
|
149 |
int |
int |
150 |
list_remove (list_t list, void *item) |
list_remove (list_t list, void *item) |
151 |
{ |
{ |
152 |
struct list_data *current, *previous; |
struct list_data *current, *previous; |
153 |
|
list_comparator_t comp; |
154 |
int status = ENOENT; |
int status = ENOENT; |
155 |
if (list == NULL) |
if (list == NULL) |
156 |
return EINVAL; |
return EINVAL; |
157 |
|
comp = list->comp ? list->comp : def_comp; |
158 |
monitor_wrlock (list->monitor); |
monitor_wrlock (list->monitor); |
159 |
for (previous = &(list->head), current = list->head.next; |
for (previous = &(list->head), current = list->head.next; |
160 |
current != &(list->head); previous = current, current = current->next) |
current != &(list->head); previous = current, current = current->next) |
161 |
{ |
{ |
162 |
if (current->item == item) |
if (comp (current->item, item) == 0) |
163 |
{ |
{ |
164 |
previous->next = current->next; |
previous->next = current->next; |
165 |
current->next->prev = previous; |
current->next->prev = previous; |
177 |
list_replace (list_t list, void *old_item, void *new_item) |
list_replace (list_t list, void *old_item, void *new_item) |
178 |
{ |
{ |
179 |
struct list_data *current, *previous; |
struct list_data *current, *previous; |
180 |
|
list_comparator_t comp; |
181 |
int status = ENOENT; |
int status = ENOENT; |
182 |
|
|
183 |
if (list == NULL) |
if (list == NULL) |
184 |
return EINVAL; |
return EINVAL; |
185 |
|
comp = list->comp ? list->comp : def_comp; |
186 |
monitor_wrlock (list->monitor); |
monitor_wrlock (list->monitor); |
187 |
for (previous = &(list->head), current = list->head.next; |
for (previous = &(list->head), current = list->head.next; |
188 |
current != &(list->head); previous = current, current = current->next) |
current != &(list->head); previous = current, current = current->next) |
189 |
{ |
{ |
190 |
if (current->item == old_item) |
if (comp (current->item, old_item) == 0) |
191 |
{ |
{ |
192 |
current->item = new_item; |
current->item = new_item; |
193 |
status = 0; |
status = 0; |