Cstyle的札记,Freertos内核详解,第3篇

<span style="white-space:pre">	</span>RTOS里面最常见的以及最核心的数据结构,队列的实现。可在VS2008 下编译测试。
</pre><pre name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#ifndef _QUEUE_H_
#define _QUEUE_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "Syslib.h"

#define Cfg_QUE_LEN 20

typedef struct
{
    UINT8 Data[Cfg_QUE_LEN];
    UINT8 pHead;
    UINT8 pTail;
    UINT16 size;
}Queue_t;

Queue_t * Queue_Creat();
UINT8 Queue_IsEmpty(Queue_t * const q);
UINT8 Queue_IsFull(Queue_t * const q);
UINT8 Queue_Push(Queue_t * const q,UINT8 const * const Dat);
UINT8 Queue_Pop(Queue_t * const q,UINT8 * const Dat);
UINT16 Queue_GetSize(Queue_t *const q);
void Queue_Test();

#ifdef __cplusplus
}
#endif
#endif
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#include "Queue.h"

Queue_t * Queue_Creat()
{
    Queue_t *p;
    p=malloc(sizeof(Queue_t));
    if(0!=p)
    {
        p->pHead=0;
        p->pTail=0;
        p->size=0;
        
        return p;
    }
    else return 0;
}


UINT8 Queue_IsEmpty(Queue_t * const q)
{
    if(!q->size) return 1;
    else return 0;
}

UINT8 Queue_IsFull(Queue_t * const q)
{
    if((q->size!=0)&&(q->pHead ==q->pTail)) return 1;//full
    else return 0;
}


UINT8 Queue_Push(Queue_t * const q,UINT8 const * const Dat)
{
        Assert(!Queue_IsFull(q));
        q->Data[q->pHead] = *Dat;
        q->pHead++;
        q->pHead = q->pHead % Cfg_QUE_LEN;
        q->size++;
        return 0;
}

UINT8 Queue_Pop(Queue_t * const q,UINT8 * const Dat)
{
    Assert(!Queue_IsEmpty(q));
    *Dat = q->Data[q->pTail];
    q->pTail++;
    q->pTail = q->pTail % Cfg_QUE_LEN;
    q->size--;
    return 0;
}

UINT16 Queue_GetSize(Queue_t *const q)
{
	return q->size;
}

void Queue_Test()
{
	
	
	 Queue_t *test;
	 UINT8 a=1,b,i;

	 printf("-----------------------------------------------\n");
	 printf("------------Start Queue Test!------------------\n");
	 printf("-----------------------------------------------\n");

	 //creat queue
	 printf("creat queue test:\n\n");
     test=Queue_Creat();
     printf("p=%x\np->pHead =%x\np->pTail =%x\np->size =%x\n",test,test->pHead,test->pTail,test->size);

	 //push queue
	 printf("push queue test1:\n\n");
     Queue_Push(test,&a);
     printf("p=%x\np->pHead =%x\np->pTail =%x\np->size =%x\n",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data[%d]=%x",i,test->Data[i]);
     printf("\n \n");
	
	 //push queue
	 printf("push queue test2:\n\n");
	 a++;
     Queue_Push(test,&a);
     printf("p=%x\np->pHead =%x\np->pTail =%x\np->size =%x\n",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data=%x",test->Data[i]);
     printf("\n \n");

	 //pop queue
	 printf("push queue test:\n\n");
     Queue_Pop(test,&b);
     printf("b=%x \n",b);

     printf("p=%x\np->pHead =%x\np->pTail =%x\np->size =%x\n",test,test->pHead,test->pTail,test->size);
     for(i=0;i<Queue_GetSize(test);i++)
     printf("Data=%x",test->Data[i]);
	 printf("\n\n");
	 
	
}

 

转载请注明出处
Cstyle.z.zhou@gmail.com  //  http://blog.csdn.net/CStyle_0x007
图片

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值