多層位圖查表法
具體的實現(xiàn)如下:
本文引用地址:http://www.ex-cimer.com/article/201612/324515.htmz的每一個bit對應著1個y。也就是一共對應8個y。
y的每一個bit對應著一個x,也就是一共對應著8*8個x。
每一個x剛好也就對應著8個任務優(yōu)先級號。這樣就能夠通過x,y,z設置優(yōu)先級。
因此可以采用下面的形式定義一個結(jié)構(gòu)體:
#ifndef __HIGH_BITMAP_H_H__
#define __HIGH_BITMAP_H_H__
#define LENGTH_HIGHLAYER 8
#define LENGTH_BYTE 8
typedef unsigned char Byte;
typedef struct
{
Byte high_Layer;
Byte mid_Layer[LENGTH_HIGHLAYER];
Byte low_Layer[LENGTH_HIGHLAYER*LENGTH_BYTE];
}BitMaps;
#ifdef __cplusplus
extern "C"
{
#endif
void inital_bitmap(BitMaps *bitmap);
void set_bitmap(BitMaps *bitmap,int prio);
int calculate_high_prio(BitMaps *bitmap);
#ifdef __cplusplus
}
#endif
#endif
基本的操作函數(shù)如下:
#include"high_bitmap.h"
#include
int const OSUnMapTbl[256] = {
0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */
};
void inital_bitmap(BitMaps *bitmap)
{
int i = 0;
if(NULL == bitmap)
{
return ;
}
bitmap->high_Layer = 0x00;
for(; i < sizeof(bitmap->mid_Layer); ++ i)
{
bitmap->mid_Layer[i] = 0x00;
}
for (i = 0; i < sizeof(bitmap->low_Layer); ++ i)
{
bitmap->low_Layer[i] = 0x00;
}
}
void set_bitmap(BitMaps *bitmap,int prio)
{
int x,y,z;
if(NULL == bitmap || prio >= 512)
{
return ;
}
z = (prio >> 6)& 0x7; x = prio & 0x7; int calculate_high_prio(BitMaps *bitmap) if(NULL == bitmap) z = OSUnMapTbl[bitmap->high_Layer]; z = (z << 6) + (y << 3) + x; return z; 這種分層的實現(xiàn)方式能夠方便的解決位圖中多種可能性問題,通過分層可以使得各個變量x,y,z都能過使用查詢表(256種可能),解決了超大可能性的問題。當然這種方式也不是唯一的,但是確實是一種可行的方案,共享查詢表的。 文章查詢的思路uC/OS-II中查詢的形式相同,也就是當對應的任務需要就緒時,可以通過設置對應的x,y,z對應的bit為1即可。
bitmap->high_Layer |= 1<
y = (prio >> 3) & 0x7;
bitmap->mid_Layer[z] |= 1<
bitmap->low_Layer[z*8+y] |= 1<
{
int x,y,z;
{
return -1;
}
y = OSUnMapTbl[bitmap->mid_Layer[z]];
x = OSUnMapTbl[bitmap->low_Layer[(z << 3)+y]];
}
評論