Commit a3823623 a382362346aa2ec5700d53411081f0d8161b53af by Sergey Poznyakoff

(list_insert): New function.

1 parent 25d8229f
...@@ -28,6 +28,7 @@ extern int list_create __P ((list_t *)); ...@@ -28,6 +28,7 @@ extern int list_create __P ((list_t *));
28 extern void list_destroy __P ((list_t *)); 28 extern void list_destroy __P ((list_t *));
29 extern int list_append __P ((list_t, void *item)); 29 extern int list_append __P ((list_t, void *item));
30 extern int list_prepend __P ((list_t, void *item)); 30 extern int list_prepend __P ((list_t, void *item));
31 extern int list_insert __P ((list_t list, void *item, void *new_item));
31 extern int list_is_empty __P ((list_t)); 32 extern int list_is_empty __P ((list_t));
32 extern int list_count __P ((list_t, size_t *pcount)); 33 extern int list_count __P ((list_t, size_t *pcount));
33 extern int list_remove __P ((list_t, void *item)); 34 extern int list_remove __P ((list_t, void *item));
......
...@@ -154,6 +154,50 @@ def_comp (const void *item, const void *value) ...@@ -154,6 +154,50 @@ def_comp (const void *item, const void *value)
154 } 154 }
155 155
156 int 156 int
157 list_insert (list_t list, void *item, void *new_item)
158 {
159 struct list_data *current;
160 list_comparator_t comp;
161 int status = ENOENT;
162
163 if (list == NULL)
164 return EINVAL;
165 comp = list->comp ? list->comp : def_comp;
166
167 monitor_wrlock (list->monitor);
168 for (current = list->head.next;
169 current != &(list->head);
170 current = current->next)
171 {
172 if (comp (current->item, item) == 0)
173 {
174 struct list_data *ldata = calloc (sizeof (*ldata), 1);
175 if (ldata == NULL)
176 return ENOMEM;
177
178 ldata->item = new_item;
179 ldata->next = current->next;
180 ldata->prev = current;
181 if (current->next)
182 current->next->prev = ldata;
183 else
184 list->head.prev = ldata;
185
186 if (current == list->head.next)
187 current = list->head.next = ldata;
188 else
189 current->next = ldata;
190
191 list->count++;
192 status = 0;
193 break;
194 }
195 }
196 monitor_unlock (list->monitor);
197 return status;
198 }
199
200 int
157 list_remove (list_t list, void *item) 201 list_remove (list_t list, void *item)
158 { 202 {
159 struct list_data *current, *previous; 203 struct list_data *current, *previous;
......