X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libopkg%2Factive_list.c;h=69ac1d11c0df5fa9b5b819277d319cab92dc1961;hb=0d9f9342d4b6071c158351d4c30370ddf36dc7ec;hp=e100101d2f69a7642d3ff91db645892e6fc33160;hpb=4ec6ca94f200ed52f2cc00ff740bae13d7fef6be;p=oweals%2Fopkg-lede.git diff --git a/libopkg/active_list.c b/libopkg/active_list.c index e100101..69ac1d1 100644 --- a/libopkg/active_list.c +++ b/libopkg/active_list.c @@ -2,7 +2,7 @@ Tick Chen - Copyright (C) 2008 Openmoko Inc. + Copyright (C) 2008 Openmoko Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -21,6 +21,7 @@ #include #include +#include "libbb/libbb.h" void active_list_init(struct active_list *ptr) { INIT_LIST_HEAD(&ptr->node); @@ -29,11 +30,11 @@ 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 ) @@ -46,7 +47,7 @@ struct active_list * active_list_next(struct active_list *head, struct active_li return ptr->depended; } while ( next->depend.next != &next->depend ) { - next = list_entry(next->depend.next, struct active_list, node); + next = list_entry(next->depend.next, struct active_list, node); } return next; } @@ -55,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 ) @@ -63,16 +64,28 @@ struct active_list * active_list_prev(struct active_list *head, struct active_li if ( ptr->depend.prev != &ptr->depend ) { prev = list_entry(ptr->depend.prev, struct active_list, node); return prev; - } + } if ( ptr->depended && ptr->depended != head && &ptr->depended->depend == ptr->node.prev ) { prev = list_entry(ptr->depended->node.prev, struct active_list, node); - } else + } else prev = list_entry(ptr->node.prev, struct active_list, node); if ( prev == head ) return NULL; 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; @@ -106,8 +119,8 @@ void active_list_add(struct active_list *head, struct active_list *node) { node->depended = head; } -struct active_list * active_list_head_new() { - struct active_list * head = calloc(1, sizeof(struct active_list)); +struct active_list * active_list_head_new(void) { + struct active_list * head = xcalloc(1, sizeof(struct active_list)); active_list_init(head); return head; } @@ -117,3 +130,36 @@ void active_list_head_delete(struct active_list *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; +}