<span style="white-space:pre"> </span>RTOS里面最常见的以及最核心的数据结构,队列的实现。可在VS2008 下编译测试。
/** @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 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的札记,Freertos内核详解,第3篇
时间: 2024-12-06 08:59:02