ZealOS/src/Kernel/KDataTypes.CC

324 lines
5.7 KiB
Text
Raw Normal View History

2020-02-16 00:26:51 +00:00
U0 LinkedListDel(U8 **_list)
2020-02-15 20:01:48 +00:00
{//Free entire linked-list.
U8 **tmpl;
2020-02-16 00:26:51 +00:00
while (_list) {
tmpl=*_list;
Free(_list);
_list=tmpl;
2020-02-15 20:01:48 +00:00
}
}
2020-02-16 00:26:51 +00:00
U8 *LinkedListCopy(U8 **_list,CTask *mem_task=NULL)
2020-02-15 20:01:48 +00:00
{//MAlloc ident copy of entire linked-list.
U8 *res=NULL,**tmpl=&res;
2020-02-16 00:26:51 +00:00
while (_list) {
tmpl=*tmpl=MAllocIdent(_list,mem_task);
_list=*_list;
2020-02-15 20:01:48 +00:00
}
return res;
}
2020-02-16 00:26:51 +00:00
I64 LinkedListCount(U8 **_list)
2020-02-15 20:01:48 +00:00
{//Count of nodes in linked-list.
I64 res=0;
2020-02-16 00:26:51 +00:00
while (_list) {
2020-02-15 20:01:48 +00:00
res++;
2020-02-16 00:26:51 +00:00
_list=*_list;
2020-02-15 20:01:48 +00:00
}
return res;
}
2020-02-16 00:26:51 +00:00
I64 LinkedListSize(U8 **_list)
2020-02-15 20:01:48 +00:00
{//Mem size of all nodes in linked-list.
I64 res=0;
2020-02-16 00:26:51 +00:00
while (_list) {
res+=MSize2(_list);
_list=*_list;
2020-02-15 20:01:48 +00:00
}
return res;
}
2020-02-15 22:53:02 +00:00
U0 QueueDel(CQueue *head,Bool querem=FALSE)
2020-02-15 20:01:48 +00:00
{//Free entries in queue, not head.
2020-02-15 22:53:02 +00:00
CQueue *tmpq=head->next,*tmpq1;
2020-02-15 20:01:48 +00:00
while (tmpq!=head) {
tmpq1=tmpq->next;
if (querem)
2020-02-15 23:13:27 +00:00
QueueRemove(tmpq);
2020-02-15 20:01:48 +00:00
Free(tmpq);
tmpq=tmpq1;
}
}
2020-02-15 22:53:02 +00:00
CQueue *QueueCopy(CQueue *head,CTask *mem_task=NULL)
2020-02-15 20:01:48 +00:00
{//MAlloc ident copy of entire queue and head.
2020-02-15 22:53:02 +00:00
CQueue *res=MAllocIdent(head,mem_task),*tmpq=head->next,*tmpq1;
QueueInit(res);
2020-02-15 20:01:48 +00:00
while (tmpq!=head) {
tmpq1=MAllocIdent(tmpq,mem_task);
2020-02-15 22:53:02 +00:00
QueueInsert(tmpq1,res->last);
2020-02-15 20:01:48 +00:00
tmpq=tmpq->next;
}
return res;
}
2020-02-16 00:20:04 +00:00
I64 QueueCount(CQueue *head)
2020-02-15 20:01:48 +00:00
{//Count of nodes in queue, not head.
2020-02-15 22:53:02 +00:00
CQueue *tmpq=head->next;
2020-02-15 20:01:48 +00:00
I64 res=0;
while (tmpq!=head) {
res++;
tmpq=tmpq->next;
}
return res;
}
2020-02-15 22:53:02 +00:00
I64 QueueSize(CQueue *head)
2020-02-15 20:01:48 +00:00
{//Mem size of all nodes in queue, not head.
2020-02-15 22:53:02 +00:00
CQueue *tmpq=head->next;
2020-02-15 20:01:48 +00:00
I64 res=0;
while (tmpq!=head) {
res+=MSize2(tmpq);
tmpq=tmpq->next;
}
return res;
}
2020-02-15 22:53:02 +00:00
CQueueVectU8 *QueueVectU8New(I64 min_idx=0)
2020-02-15 20:01:48 +00:00
{//Create new queue vecter.
2020-02-15 22:53:02 +00:00
CQueueVectU8 *res=MAlloc(sizeof(CQueueVectU8));
QueueInit(res);
2020-02-16 00:20:04 +00:00
res->total_count=res->node_count=0;
2020-02-15 20:01:48 +00:00
res->min_idx=min_idx;
return res;
}
2020-02-15 22:53:02 +00:00
U0 QueueVectU8Put(CQueueVectU8 *v,I64 idx,U8 ch)
2020-02-15 20:01:48 +00:00
{//Put U8 at idx i.
2020-02-15 22:53:02 +00:00
CQueueVectU8 *tmpv;
2020-02-15 20:01:48 +00:00
idx-=v->min_idx;
if (idx<0) return;
2020-02-16 00:20:04 +00:00
if (idx<v->total_count) {
2020-02-15 20:01:48 +00:00
tmpv=v;
do {
2020-02-16 00:20:04 +00:00
idx-=tmpv->node_count;
2020-02-15 20:01:48 +00:00
if (idx<0) {
2020-02-16 00:20:04 +00:00
tmpv->body[idx+tmpv->node_count]=ch;
2020-02-15 20:01:48 +00:00
return;
}
tmpv=tmpv->next;
} while (tmpv!=v);
} else
2020-02-16 00:20:04 +00:00
idx-=v->total_count;
2020-02-15 20:01:48 +00:00
while (TRUE) {
tmpv=v->last;
2020-02-16 01:19:05 +00:00
if (tmpv->node_count>=QUE_VECT_U8_COUNT) {
2020-02-15 22:53:02 +00:00
tmpv=MAlloc(sizeof(CQueueVectU8));
2020-02-16 00:20:04 +00:00
tmpv->node_count=0;
2020-02-15 22:53:02 +00:00
QueueInsert(tmpv,v->last);
2020-02-15 20:01:48 +00:00
}
if (idx--) {
2020-02-16 00:20:04 +00:00
tmpv->body[tmpv->node_count++]=0;
v->total_count++;
2020-02-15 20:01:48 +00:00
} else {
2020-02-16 00:20:04 +00:00
tmpv->body[tmpv->node_count++]=ch;
v->total_count++;
2020-02-15 20:01:48 +00:00
break;
}
}
}
2020-02-15 22:53:02 +00:00
U0 QueueVectU8Del(CQueueVectU8 *v)
2020-02-15 20:01:48 +00:00
{//Free entire queue vector.
if (v) {
2020-02-15 22:53:02 +00:00
QueueDel(v);
2020-02-15 20:01:48 +00:00
Free(v);
}
}
2020-02-15 22:53:02 +00:00
I64 QueueVectU8Get(CQueueVectU8 *v,I64 idx)
2020-02-15 20:01:48 +00:00
{//Get U8 at idx i.
2020-02-15 22:53:02 +00:00
CQueueVectU8 *tmpv;
2020-02-15 20:01:48 +00:00
idx-=v->min_idx;
2020-02-16 00:20:04 +00:00
if (!(0<=idx<v->total_count)) return 0;
2020-02-15 20:01:48 +00:00
tmpv=v;
do {
2020-02-16 00:20:04 +00:00
idx-=tmpv->node_count;
2020-02-15 20:01:48 +00:00
if (idx<0)
2020-02-16 00:20:04 +00:00
return tmpv->body[idx+tmpv->node_count];
2020-02-15 20:01:48 +00:00
tmpv=tmpv->next;
} while (tmpv!=v);
return 0;
}
CFifoU8 *FifoU8New(I64 size,CTask *mem_task=NULL)
{//Create new fifo.
CFifoU8 *f;
if (!mem_task) mem_task=Fs;
f=MAlloc(sizeof(CFifoU8),mem_task);
f->buf=MAlloc(size,mem_task);
f->mask=size-1;
f->in_ptr=0;
f->out_ptr=0;
return f;
}
U0 FifoU8Del(CFifoU8 *f)
{//Free fifo.
Free(f->buf);
Free(f);
}
Bool FifoU8Ins(CFifoU8 *f,U8 b)
{//Insert U8 into fifo.
I64 new_in_ptr;
PUSHFD
CLI
new_in_ptr=(f->in_ptr+1)&f->mask;
if (new_in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
f->buf[f->in_ptr]=b;
f->in_ptr=new_in_ptr;
POPFD
return TRUE;
}
}
2020-02-15 23:13:27 +00:00
Bool FifoU8Remove(CFifoU8 *f,U8 *_b)
2020-02-15 20:01:48 +00:00
{//Remove U8 from fifo.
PUSHFD
CLI
if (f->in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
*_b=f->buf[f->out_ptr];
f->out_ptr=(f->out_ptr+1)&f->mask;
POPFD
return TRUE;
}
}
Bool FifoU8Peek(CFifoU8 *f,U8 *_b)
{//Peek at front of fifo and don't remove.
PUSHFD
CLI
if (f->in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
*_b=f->buf[f->out_ptr];
POPFD
return TRUE;
}
}
U0 FifoU8Flush(CFifoU8 *f)
{//Flush fifo getting rid of all U8's.
PUSHFD
CLI
f->out_ptr=f->in_ptr;
POPFD
}
2020-02-16 00:20:04 +00:00
I64 FifoU8Count(CFifoU8 *f)
2020-02-15 20:01:48 +00:00
{//Count of U8's in fifo.
I64 res;
PUSHFD
CLI
if (f->out_ptr>f->in_ptr)
res=f->mask+1-(f->out_ptr-f->in_ptr);
else
res=f->in_ptr-f->out_ptr;
POPFD
return res;
}
CFifoI64 *FifoI64New(I64 size,CTask *mem_task=NULL)
{//Create new fifo.
CFifoI64 *f;
if (!mem_task) mem_task=Fs;
f=MAlloc(sizeof(CFifoI64),mem_task);
f->buf=MAlloc(size*sizeof(I64),mem_task);
f->mask=size-1;
f->in_ptr=0;
f->out_ptr=0;
return f;
}
U0 FifoI64Del(CFifoI64 *f)
{//Free fifo.
Free(f->buf);
Free(f);
}
Bool FifoI64Ins(CFifoI64 *f,I64 q)
{//Insert I64 into fifo.
I64 new_in_ptr;
PUSHFD
CLI
new_in_ptr=(f->in_ptr+1)&f->mask;
if (new_in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
f->buf[f->in_ptr]=q;
f->in_ptr=new_in_ptr;
POPFD
return TRUE;
}
}
2020-02-15 23:13:27 +00:00
Bool FifoI64Remove(CFifoI64 *f,I64 *_q)
2020-02-15 20:01:48 +00:00
{//Remove I64 from fifo.
PUSHFD
CLI
if (f->in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
*_q=f->buf[f->out_ptr];
f->out_ptr=(f->out_ptr+1)&f->mask;
POPFD
return TRUE;
}
}
Bool FifoI64Peek(CFifoI64 *f,I64 *_q)
{//Peek at front of fifo and don't remove.
PUSHFD
CLI
if (f->in_ptr==f->out_ptr) {
POPFD
return FALSE;
} else {
*_q=f->buf[f->out_ptr];
POPFD
return TRUE;
}
}
U0 FifoI64Flush(CFifoI64 *f)
{//Flush fifo getting rid of all I64's.
PUSHFD
CLI
f->out_ptr=f->in_ptr;
POPFD
}
2020-02-16 00:20:04 +00:00
I64 FifoI64Count(CFifoI64 *f)
2020-02-15 20:01:48 +00:00
{//Count of I64's in fifo.
I64 res;
PUSHFD
CLI
if (f->out_ptr>f->in_ptr)
res=f->mask+1-(f->out_ptr-f->in_ptr);
else
res=f->in_ptr-f->out_ptr;
POPFD
return res;
}