hschumann2/TempleOS-Source-Code
0847
1 2U0 LinkedLstDel(U8 **_lst)3{//Free entire linked-list.4 U8 **tmpl;5 while (_lst) {6 tmpl=*_lst;7 Free(_lst);8 _lst=tmpl;9 }10}11 12U8 *LinkedLstCopy(U8 **_lst,CTask *mem_task=NULL)13{//MAlloc ident copy of entire linked-list.14 U8 *res=NULL,**tmpl=&res;15 while (_lst) {16 tmpl=*tmpl=MAllocIdent(_lst,mem_task);17 _lst=*_lst;18 }19 return res;20}21 22I64 LinkedLstCnt(U8 **_lst)23{//Count of nodes in linked-list.24 I64 res=0;25 while (_lst) {26 res++;27 _lst=*_lst;28 }29 return res;30}31 32I64 LinkedLstSize(U8 **_lst)33{//Mem size of all nodes in linked-list.34 I64 res=0;35 while (_lst) {36 res+=MSize2(_lst);37 _lst=*_lst;38 }39 return res;40}41 42U0 QueDel(CQue *head,Bool querem=FALSE)43{//Free entries in queue, not head.44 CQue *tmpq=head->next,*tmpq1;45 while (tmpq!=head) {46 tmpq1=tmpq->next;47 if (querem)48 QueRem(tmpq);49 Free(tmpq);50 tmpq=tmpq1;51 }52}53 54CQue *QueCopy(CQue *head,CTask *mem_task=NULL)55{//MAlloc ident copy of entire queue and head.56 CQue *res=MAllocIdent(head,mem_task),*tmpq=head->next,*tmpq1;57 QueInit(res);58 while (tmpq!=head) {59 tmpq1=MAllocIdent(tmpq,mem_task);60 QueIns(tmpq1,res->last);61 tmpq=tmpq->next;62 }63 return res;64}65 66I64 QueCnt(CQue *head)67{//Count of nodes in queue, not head.68 CQue *tmpq=head->next;69 I64 res=0;70 while (tmpq!=head) {71 res++;72 tmpq=tmpq->next;73 }74 return res;75}76 77I64 QueSize(CQue *head)78{//Mem size of all nodes in queue, not head.79 CQue *tmpq=head->next;80 I64 res=0;81 while (tmpq!=head) {82 res+=MSize2(tmpq);83 tmpq=tmpq->next;84 }85 return res;86}87 88CQueVectU8 *QueVectU8New(I64 min_idx=0)89{//Create new queue vecter.90 CQueVectU8 *res=MAlloc(sizeof(CQueVectU8));91 QueInit(res);92 res->total_cnt=res->node_cnt=0;93 res->min_idx=min_idx;94 return res;95}96 97U0 QueVectU8Put(CQueVectU8 *v,I64 idx,U8 ch)98{//Put U8 at idx i.99 CQueVectU8 *tmpv;100 idx-=v->min_idx;101 if (idx<0) return;102 if (idx<v->total_cnt) {103 tmpv=v;104 do {105 idx-=tmpv->node_cnt;106 if (idx<0) {107 tmpv->body[idx+tmpv->node_cnt]=ch;108 return;109 }110 tmpv=tmpv->next;111 } while (tmpv!=v);112 } else113 idx-=v->total_cnt;114 115 while (TRUE) {116 tmpv=v->last;117 if (tmpv->node_cnt>=QUE_VECT_U8_CNT) {118 tmpv=MAlloc(sizeof(CQueVectU8));119 tmpv->node_cnt=0;120 QueIns(tmpv,v->last);121 }122 if (idx--) {123 tmpv->body[tmpv->node_cnt++]=0;124 v->total_cnt++;125 } else {126 tmpv->body[tmpv->node_cnt++]=ch;127 v->total_cnt++;128 break;129 }130 }131}132 133U0 QueVectU8Del(CQueVectU8 *v)134{//Free entire queue vector.135 if (v) {136 QueDel(v);137 Free(v);138 }139}140 141I64 QueVectU8Get(CQueVectU8 *v,I64 idx)142{//Get U8 at idx i.143 CQueVectU8 *tmpv;144 idx-=v->min_idx;145 if (!(0<=idx<v->total_cnt)) return 0;146 tmpv=v;147 do {148 idx-=tmpv->node_cnt;149 if (idx<0)150 return tmpv->body[idx+tmpv->node_cnt];151 tmpv=tmpv->next;152 } while (tmpv!=v);153 return 0;154}155 156CFifoU8 *FifoU8New(I64 size,CTask *mem_task=NULL)157{//Create new fifo.158 CFifoU8 *f;159 if (!mem_task) mem_task=Fs;160 f=MAlloc(sizeof(CFifoU8),mem_task);161 f->buf=MAlloc(size,mem_task);162 f->mask=size-1;163 f->in_ptr=0;164 f->out_ptr=0;165 return f;166}167 168U0 FifoU8Del(CFifoU8 *f)169{//Free fifo.170 Free(f->buf);171 Free(f);172}173 174Bool FifoU8Ins(CFifoU8 *f,U8 b)175{//Insert U8 into fifo.176 I64 new_in_ptr;177 PUSHFD178 CLI179 new_in_ptr=(f->in_ptr+1)&f->mask;180 if (new_in_ptr==f->out_ptr) {181 POPFD182 return FALSE;183 } else {184 f->buf[f->in_ptr]=b;185 f->in_ptr=new_in_ptr;186 POPFD187 return TRUE;188 }189}190 191Bool FifoU8Rem(CFifoU8 *f,U8 *_b)192{//Remove U8 from fifo.193 PUSHFD194 CLI195 if (f->in_ptr==f->out_ptr) {196 POPFD197 return FALSE;198 } else {199 *_b=f->buf[f->out_ptr];200 f->out_ptr=(f->out_ptr+1)&f->mask;201 POPFD202 return TRUE;203 }204}205 206Bool FifoU8Peek(CFifoU8 *f,U8 *_b)207{//Peek at front of fifo and don't remove.208 PUSHFD209 CLI210 if (f->in_ptr==f->out_ptr) {211 POPFD212 return FALSE;213 } else {214 *_b=f->buf[f->out_ptr];215 POPFD216 return TRUE;217 }218}219 220U0 FifoU8Flush(CFifoU8 *f)221{//Flush fifo getting rid of all U8's.222 PUSHFD223 CLI224 f->out_ptr=f->in_ptr;225 POPFD226}227 228I64 FifoU8Cnt(CFifoU8 *f)229{//Count of U8's in fifo.230 I64 res;231 PUSHFD232 CLI233 if (f->out_ptr>f->in_ptr)234 res=f->mask+1-(f->out_ptr-f->in_ptr);235 else236 res=f->in_ptr-f->out_ptr;237 POPFD238 return res;239}240 241CFifoI64 *FifoI64New(I64 size,CTask *mem_task=NULL)242{//Create new fifo.243 CFifoI64 *f;244 if (!mem_task) mem_task=Fs;245 f=MAlloc(sizeof(CFifoI64),mem_task);246 f->buf=MAlloc(size*sizeof(I64),mem_task);247 f->mask=size-1;248 f->in_ptr=0;249 f->out_ptr=0;250 return f;251}252 253U0 FifoI64Del(CFifoI64 *f)254{//Free fifo.255 Free(f->buf);256 Free(f);257}258 259Bool FifoI64Ins(CFifoI64 *f,I64 q)260{//Insert I64 into fifo.261 I64 new_in_ptr;262 PUSHFD263 CLI264 new_in_ptr=(f->in_ptr+1)&f->mask;265 if (new_in_ptr==f->out_ptr) {266 POPFD267 return FALSE;268 } else {269 f->buf[f->in_ptr]=q;270 f->in_ptr=new_in_ptr;271 POPFD272 return TRUE;273 }274}275 276Bool FifoI64Rem(CFifoI64 *f,I64 *_q)277{//Remove I64 from fifo.278 PUSHFD279 CLI280 if (f->in_ptr==f->out_ptr) {281 POPFD282 return FALSE;283 } else {284 *_q=f->buf[f->out_ptr];285 f->out_ptr=(f->out_ptr+1)&f->mask;286 POPFD287 return TRUE;288 }289}290 291Bool FifoI64Peek(CFifoI64 *f,I64 *_q)292{//Peek at front of fifo and don't remove.293 PUSHFD294 CLI295 if (f->in_ptr==f->out_ptr) {296 POPFD297 return FALSE;298 } else {299 *_q=f->buf[f->out_ptr];300 POPFD301 return TRUE;302 }303}304 305U0 FifoI64Flush(CFifoI64 *f)306{//Flush fifo getting rid of all I64's.307 PUSHFD308 CLI309 f->out_ptr=f->in_ptr;310 POPFD311}312 313I64 FifoI64Cnt(CFifoI64 *f)314{//Count of I64's in fifo.315 I64 res;316 PUSHFD317 CLI318 if (f->out_ptr>f->in_ptr)319 res=f->mask+1-(f->out_ptr-f->in_ptr);320 else321 res=f->in_ptr-f->out_ptr;322 POPFD323 return res;324}325 