00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "src/common/shared.hpp"
00021 #include "src/memory/align.hpp"
00022 #include "src/memory/memset.hpp"
00023 #include "src/memory/heap.hpp"
00024 #include "src/common/blist.hpp"
00025 #include "src/common/io.hpp"
00026
00027
00028 void blist_t::init(uint32 fac) {
00029 head=NULL;tail=NULL;facility=fac;
00030 heap=&Memory::Heap::heap0;
00031 lock_blist.init(const_cast<char*>("blist"));
00032 }
00033
00034
00035 void blist_t::init (uint32 fac, Memory::Heap::heapbox *hp)
00036 {
00037 head=NULL;tail=NULL;facility=fac;
00038 heap=hp;
00039 lock_blist.init(const_cast<char*>("blist"));
00040 }
00041
00042
00043 void blist_t::init (const char *name, class Memory::Heap::heapbox *hp)
00044 {
00045 head=NULL;tail=NULL;facility=(uint32)name;
00046
00047 heap=hp;
00048 lock_blist.init((char*)name);
00049 }
00050
00051
00052 void blist_t::done()
00053 {
00054 while (head) del(head);
00055 memset(this,0,sizeof(blist_t));
00056 }
00057
00058
00059 blist_s *blist_t::add(void *data, size_t size, blist_t *tree)
00060 {
00061 blist_s *new_obj;
00062 void *copied_data;
00063
00064 copied_data=heap->malloc(size,NO_ALIGN,FAC_BLIST|facility,NULL);
00065 if (copied_data==NULL)
00066 { complain("out of memory"); return NULL; }
00067 memmove(copied_data,data,size);
00068
00069 new_obj=(blist_s*) heap->malloc(sizeof(blist_s),NO_ALIGN,FAC_BLIST_BK|facility,NULL);
00070 if (new_obj == NULL)
00071 { complain("out of memory"); heap->free(copied_data); return NULL; }
00072
00073 memset(new_obj,0,sizeof(blist_s));
00074 new_obj->tree=tree;
00075 new_obj->size=size;
00076 new_obj->data=copied_data;
00077
00078 return link(new_obj);
00079 }
00080
00081
00082 blist_s *blist_t::find(void *data)
00083 {
00084 blist_s *item;
00085 for (item=head;item;item=item->next)
00086 if (item->data == data)
00087 return item;
00088
00089 return NULL;
00090 }
00091
00092
00093 int blist_t::del(blist_s *obj)
00094 {
00095 lock_blist.lock();
00096
00097 if (obj->prev)
00098 obj->prev->next=obj->next;
00099 else
00100 head=obj->next;
00101
00102 if (obj->next)
00103 obj->next->prev=obj->prev;
00104 else
00105 tail=obj->prev;
00106
00107 if (obj->size != 0)
00108 heap->free(obj->data);
00109
00110 heap->free(obj);
00111 lock_blist.ulock();
00112
00113 return ESUCCESS;
00114 }
00115
00116
00117 blist_s *blist_t::move(blist_s *obj, blist_t *dest)
00118 {
00119
00120 if (obj -> prev)
00121 obj -> prev -> next = obj -> next;
00122 else
00123 head = obj->next;
00124
00125 if (obj->next)
00126 obj->next->prev = obj->prev;
00127 else
00128 tail=obj->prev;
00129
00130 return dest->link(obj);
00131 }
00132
00133
00134 blist_s *blist_t::link(blist_s *obj)
00135 {
00136 lock_blist.lock();
00137
00138 if (head == NULL)
00139 {
00140 obj->prev=NULL;
00141 head=obj;
00142 } else
00143 {
00144 tail->next=obj;
00145 obj->prev=tail;
00146 }
00147
00148 obj->next=NULL;
00149 tail=obj;
00150 lock_blist.ulock();
00151
00152 return obj;
00153 }
00154
00155
00156 blist_s *blist_t::unlink(blist_s *obj)
00157 {
00158 if (obj->prev)
00159 obj->prev->next=obj->next;
00160 else
00161 head=obj->next;
00162
00163 if (obj->next)
00164 obj->next->prev=obj->prev;
00165 else
00166 tail=obj->prev;
00167 obj->prev=NULL;obj->next=NULL;
00168
00169 return obj;
00170 }
00171
00172
00173 blist_s *blist_t::ptr(void *data, blist_t *tree)
00174 {
00175 blist_s *new_obj;
00176
00177 new_obj=(blist_s*) heap->malloc(sizeof(blist_s),NO_ALIGN,FAC_BLIST_PTR|facility,NULL);
00178 if (new_obj == NULL)
00179 { complain("out of memory"); return NULL; }
00180
00181 memset(new_obj,0,sizeof(blist_s));
00182 new_obj->tree=tree;
00183 new_obj->size=0;
00184 new_obj->data=data;
00185
00186 return link(new_obj);
00187 }
00188
00189
00190
00191
00192
00193
00194