📄 zbcachep.nc
字号:
/*
* zigbee packet fifo cache
*
*
* <free queue>
* pFree--->[pNext|element_t]
* |
* --->[pNext|element_t]
* |
* --->[pNext|element_t]
* |
* --->...
* |
* --->[pNext|element_t]
* |
* --->NULL
*
*
* <full queue>
* pFull--->[pNext|element_t]
* |
* --->[pNext|element_t]
* |
* --->[pNext|element_t]
* |
* --->...
* |
* --->[pNext|element_t]
* |
* --->NULL
*
*
*
*
*
*
*/
generic module ZbCacheP(typedef element_t, uint8_t cacheSize)
{
provides interface Init;
provides interface ZbCache<element_t>;
}
implementation
{
typedef struct cache_t{
struct cache_t* pNext;
element_t element;
}cache_t;
cache_t cache[cacheSize];
cache_t* pFree;
cache_t* pFull;
uint8_t getNum(cache_t* p);
command error_t Init.init()
{
uint8_t i;
pFull = NULL;
for(i=0; i<(cacheSize-1); i++){
cache[i].pNext = &cache[i+1];
}
pFree = &cache[0];
cache[cacheSize-1].pNext = NULL;
return SUCCESS;
}
async command element_t* ZbCache.fetchFree()
{
cache_t* pc;
element_t* pe;
atomic
{
if(pFree == NULL){
return NULL;
}
pc = pFree;
pFree = pFree->pNext;
pc->pNext = NULL;
pe = (element_t*)( (uint8_t*)pc + sizeof(cache_t*) );
return pe;
}
}
async command error_t ZbCache.putFree(element_t* pElement)
{
cache_t* pc;
pc = (cache_t*)( (uint8_t*)pElement - sizeof(cache_t*) );
atomic
{
pc->pNext = pFree;
pFree = pc;
}
return SUCCESS;
}
async command element_t* ZbCache.getFull()
{
element_t* pe;
atomic
{
pe = (element_t*)( (uint8_t*)pFull + sizeof(cache_t*) );
return pe;
}
}
async command element_t* ZbCache.fetchFull()
{
cache_t* pc;
element_t* pe;
atomic
{
if(pFull == NULL){
return NULL;
}
pc = pFull;
pFull = pFull->pNext;
pc->pNext = NULL;
pe = (element_t*)( (uint8_t*)pc + sizeof(cache_t*) );
return pe;
}
}
async command error_t ZbCache.putFull(element_t* pElement)
{
cache_t* pc;
cache_t* pc1 = NULL;
cache_t* pc2 = NULL;
if(pElement == NULL)
return FAIL;
pc = (cache_t*)( (uint8_t*)pElement - sizeof(cache_t*) );
atomic
{
if(pFull == NULL){
pFull = pc;
pc->pNext = NULL;
}
else{
pc1 = pFull;
while(pc1 != NULL){
pc2 = pc1;
pc1 = pc1->pNext;
}
pc2->pNext = pc;
pc->pNext = NULL;
}
}
return SUCCESS;
}
async command uint8_t ZbCache.getFreeNum()
{
return getNum(pFree);
}
async command uint8_t ZbCache.getFullNum()
{
return getNum(pFull);
}
uint8_t getNum(cache_t* p)
{
uint8_t num = 0;
cache_t* pc;
pc = p;
atomic
{
while(pc){
num++;
pc = pc->pNext;
}
}
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -