通用代码结构记录
声明类型定义(core_types.h)
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef char int8;
typedef volatile char vint8;
typedef unsigned char uint8;
typedef volatile unsigned char vuint8;
typedef int int16;
typedef unsigned short uint16;
typedef long int32;
typedef unsigned long uint32;
typedef uint8 u_char;
typedef uint8 SOCKET;
typedef uint16 u_short;
typedef uint16 u_int;
typedef uint32 u_long;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef volatile uint32_t vu32;
typedef volatile uint16_t vu16;
typedef volatile uint8_t vu8;
typedef u8 (*CoreHandleFunc)(void *pArg);
声明核心结构体(core_struct.h)
typedef enum {
Result_Ok,
Result_Error
} Core_Result_Enum;
typedef struct {
u16 handle_code;
CoreHandleFunc handle_func;
} Core_Handle_t;
typedef struct {
u16 cur_size;
u16 cap;
Core_Handle_t *core_handle;
} Core_Handle_TB_t;
声明核心结构体的容器
#define HANDLE_TABLE_SIZE 20
Core_Handle_TB_t handleTb;
定义操作结构体容器的函数
Core_Result_Enum Handle_TB_Init(uint16_t cap) {
if (cap == 0) {
handleTb.cap = HANDLE_TABLE_SIZE;
} else {
handleTb.cap = cap;
}
handleTb.cur_size = 0;
handleTb.core_handle = (Core_Handle_t *) malloc(sizeof(Core_Handle_t) * handleTb.cap);
if (handleTb.core_handle == NULL) {
return Result_Error;
}
return Result_Ok;
}
Core_Result_Enum Handle_TB_Add(Core_Handle_t *item) {
if (handleTb.cap <= handleTb.cur_size || item == NULL || item->handle_func == NULL) {
return Result_Error;
}
int findIdx = Handle_TB_SearchByCode(item->handle_code);
if (findIdx != -1) {
printf("func exist\n");
return Result_Error;
}
handleTb.core_handle[handleTb.cur_size].handle_func = item->handle_func;
handleTb.core_handle[handleTb.cur_size].handle_code = item->handle_code;
handleTb.cur_size++;
return Result_Ok;
}
Core_Result_Enum Handle_TB_Add2(u16 itemCode, CoreHandleFunc func) {
if (handleTb.cap <= handleTb.cur_size || Handle_TB_SearchByCode(itemCode) != -1) {
return Result_Error;
}
handleTb.core_handle[handleTb.cur_size].handle_func = func;
handleTb.core_handle[handleTb.cur_size].handle_code = itemCode;
handleTb.cur_size++;
return Result_Ok;
}
int Handle_TB_SearchByCode(u16 itemCode) {
if (handleTb.cur_size == 0 || handleTb.core_handle == NULL) {
return -1;
}
for (int i = 0; i < handleTb.cur_size; ++i) {
if (handleTb.core_handle[i].handle_code == itemCode) {
return i;
}
}
return -1;
}
Core_Handle_t *Handle_TB_GetHandle(u16 itemCode) {
int idx = Handle_TB_SearchByCode(itemCode);
if (idx == -1) {
return NULL;
}
return &handleTb.core_handle[idx];
}
Core_Result_Enum Handle_TB_Remove(u16 itemCode) {
if (handleTb.core_handle == NULL) {
return Result_Ok;
}
int idx = Handle_TB_SearchByCode(itemCode);
if (idx == -1) {
return Result_Error;
}
memcpy(handleTb.core_handle + idx, handleTb.core_handle + idx + 1,
(handleTb.cur_size - idx) * sizeof(Core_Handle_t));
handleTb.cur_size--;
return Result_Ok;
}
void Handle_Free(void) {
free(handleTb.core_handle);
handleTb.core_handle = NULL;
}
void Handle_TB_PrintAll(void) {
for (int i = 0; i < handleTb.cur_size; ++i) {
printf("cur code: %#x\n", handleTb.core_handle[i].handle_code);
}
}
测试用例
u8 FirstHandle(void *pArg) {
printf("hello first\n");
return 1;
}
int main() {
Handle_TB_Init(20);
Core_Handle_t nw;
nw.handle_code = 0x0001;
nw.handle_func = FirstHandle;
Handle_TB_Add(&nw);
nw.handle_code = 0x0002;
nw.handle_func = FirstHandle;
Handle_TB_Add(&nw);
nw.handle_code = 0x0003;
nw.handle_func = FirstHandle;
Handle_TB_Add(&nw);
nw.handle_code = 0x0004;
nw.handle_func = FirstHandle;
Handle_TB_Add(&nw);
printf("%d;%d\n", handleTb.cap, handleTb.cur_size);
Core_Handle_t *handle = Handle_TB_GetHandle(nw.handle_code);
handle->handle_func(NULL);
Handle_TB_PrintAll();
Handle_TB_Remove(0x1);
printf("------------------------\n");
Handle_TB_PrintAll();
return 0;
}