00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "src/common/shared.hpp"
00018 #include "src/memory/align.hpp"
00019 #include "src/memory/memset.hpp"
00020 #include "src/memory/heap.hpp"
00021 #include "src/common/blist.hpp"
00022 #include "src/common/blistsort.hpp"
00023
00024 struct blist_s *blistsort_add(blist_t *list, void *data, size_t size, class blist_t *tree,
00025 int(*compar)(const void*,const void*)) {
00026 return blistsort_add(list,data,size,tree,compar,list->head);
00027 }
00028
00029 struct blist_s *blistsort_add(blist_t *list, void *data, size_t size, class blist_t *tree,
00030 int(*compar)(const void*,const void*), blist_s *head)
00031 {
00032 blist_s *new_obj;
00033 void *copied_data;
00034
00035 copied_data=list->heap->malloc(size,NO_ALIGN,FAC_BLIST|list->facility,NULL);
00036 if (copied_data==NULL)
00037 { complain("out of memory"); return NULL; }
00038 memmove(copied_data,data,size);
00039
00040 new_obj=(blist_s*) list->heap->malloc(sizeof(blist_s),NO_ALIGN,FAC_BLIST_BK|list->facility,NULL);
00041 if (new_obj == NULL)
00042 { complain("out of memory"); list->heap->free(copied_data); return NULL; }
00043
00044 new_obj->tree=tree;
00045 new_obj->size=size;
00046 new_obj->data=copied_data;
00047
00048 return blistsort_link(list,new_obj,compar,head);
00049
00050 }
00051
00052 struct blist_s *blistsort_ptr(blist_t *list, void *data, class blist_t *tree, int(*compar)(const void*,const void*))
00053 {
00054 blist_s *new_obj;
00055
00056 new_obj=(blist_s*) list->heap->malloc(sizeof(blist_s),NO_ALIGN,FAC_BLIST_PTR|list->facility,NULL);
00057 if (new_obj == NULL)
00058 { complain("out of memory"); return NULL; }
00059
00060 new_obj->tree=tree;
00061 new_obj->size=0;
00062 new_obj->data=data;
00063
00064 return blistsort_link(list, new_obj, compar);
00065 }
00066
00067 struct blist_s *blistsort_link(blist_t *list, blist_s *item,
00068 int(*compar)(const void*,const void*)) {
00069 return blistsort_link(list,item,compar,list->head);
00070 }
00071
00072 struct blist_s *blistsort_link(blist_t *list, blist_s *item,
00073 int(*compar)(const void*,const void*), blist_s *head)
00074 {
00075 list->lock_blist.lock();
00076
00077 if ((head == NULL) ||
00078 (compar(head,item) >= 0))
00079 {
00080 item->next=head;
00081 if (head)
00082 item->prev=head->prev;
00083 else
00084 item->prev=NULL;
00085 if (item->next)
00086 item->next->prev=item;
00087 else
00088 list->tail=item;
00089 if ((head == NULL) || (list->head == head))
00090 list->head=item;
00091 list->lock_blist.ulock();
00092 return item;
00093 }
00094
00095
00096 if (compar(list->tail, item) <= 0)
00097 {
00098 item->next=NULL;
00099 item->prev=list->tail;
00100 if (item->prev)
00101 item->prev->next=item;
00102 else
00103 list->head=item;
00104 list->tail=item;
00105 list->lock_blist.ulock();
00106 return item;
00107 }
00108
00109 blist_s *obj;
00110 for (obj=head;obj;obj=obj->next)
00111 {
00112 if (compar(obj, item) < 0)
00113 continue;
00114 item->next=obj;
00115 item->prev=obj->prev;
00116 if (item->prev)
00117 item->prev->next=item;
00118 else
00119 list->head=item;
00120 obj->prev=item;
00121 list->lock_blist.ulock();
00122 return item;
00123 }
00124
00125 list->lock_blist.ulock();
00126 return NULL;
00127 }
00128
00129
00130
00131
00132
00133
00134