X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libopkg%2Factive_list.c;h=ffc6db06c83f5568aaeac1eb853b9cdb244e895e;hb=29208c4e40c475b544011681c3d735e4d46e88b2;hp=e297cfca4d7a63b6bcc5eecbef29a9605248f6c2;hpb=014e4c306c4765c65abf1f2ce879bebcdd8c7261;p=oweals%2Fopkg-lede.git diff --git a/libopkg/active_list.c b/libopkg/active_list.c index e297cfc..ffc6db0 100644 --- a/libopkg/active_list.c +++ b/libopkg/active_list.c @@ -18,7 +18,10 @@ #include "active_list.h" #include +#include +#include +#include "libbb/libbb.h" void active_list_init(struct active_list *ptr) { INIT_LIST_HEAD(&ptr->node); @@ -31,7 +34,7 @@ void active_list_init(struct active_list *ptr) { struct active_list * active_list_next(struct active_list *head, struct active_list *ptr) { struct active_list *next=NULL; if ( !head ) { - fprintf(stderr, "active_list_next head = %p, ptr = %p invalid value!!\n", head, ptr); + opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); return NULL; } if ( !ptr ) @@ -53,7 +56,7 @@ struct active_list * active_list_next(struct active_list *head, struct active_li struct active_list * active_list_prev(struct active_list *head, struct active_list *ptr) { struct active_list *prev=NULL; if ( !head ) { - fprintf(stderr, "active_list_prev head = %p, ptr = %p invalid value!!\n", head, ptr); + opkg_msg(ERROR, "Internal error: head=%p, ptr=%p\n", head, ptr); return NULL; } if ( !ptr ) @@ -71,7 +74,37 @@ struct active_list * active_list_prev(struct active_list *head, struct active_li return prev; } + +struct active_list *active_list_move_node(struct active_list *old_head, struct active_list *new_head, struct active_list *node) { + struct active_list *prev; + if (!old_head || !new_head || !node) + return NULL; + if (old_head == new_head) + return node; + prev = active_list_prev(old_head, node); + active_list_add(new_head, node); + return prev; +} + +static void list_head_clear (struct list_head *head) { + struct active_list *next; + struct list_head *n, *ptr; + if (!head) + return; + list_for_each_safe(ptr, n , head) { + next = list_entry(ptr, struct active_list, node); + if (next->depend.next != &next->depend) { + list_head_clear(&next->depend); + } + active_list_init(next); + } +} void active_list_clear(struct active_list *head) { + list_head_clear(&head->node); + if (head->depend.next != &head->depend) { + list_head_clear(&head->depend); + } + active_list_init(head); } void active_list_add_depend(struct active_list *node, struct active_list *depend) { @@ -85,3 +118,48 @@ void active_list_add(struct active_list *head, struct active_list *node) { list_add_tail(&node->node, &head->node); node->depended = head; } + +struct active_list * active_list_head_new(void) { + struct active_list * head = xcalloc(1, sizeof(struct active_list)); + active_list_init(head); + return head; +} + +void active_list_head_delete(struct active_list *head) { + active_list_clear(head); + free(head); +} + +/* + * Using insert sort. + * Note. the list should not be large, or it will be very inefficient. + * + */ +struct active_list * active_list_sort(struct active_list *head, int (*compare)(const void *, const void *)) { + struct active_list tmphead; + struct active_list *node, *ptr; + if ( !head ) + return NULL; + active_list_init(&tmphead); + for (node = active_list_next(head, NULL); node; node = active_list_next(head, NULL)) { + if (tmphead.node.next == &tmphead.node) { + active_list_move_node(head, &tmphead, node); + } else { + for (ptr = active_list_next(&tmphead, NULL); ptr; ptr=active_list_next(&tmphead, ptr)) { + if (compare(ptr, node) <= 0) { + break; + } + } + if (!ptr) { + active_list_move_node(head, &tmphead, node); + } else { + active_list_move_node(head, ptr, node); + } + } + node->depended = &tmphead; + } + for (ptr = active_list_prev(&tmphead, NULL); ptr; ptr=active_list_prev(&tmphead, NULL)) { + active_list_move_node(&tmphead, head, ptr); + } + return head; +}