编译时条件不成立,则发生Assert
#define COMPILE_TIME_ASSERT(x) \
do { \
switch (0) { case 0: case ((x) ? 1: 0): default : break; } \
} while (0)
#endif
剖析: 实现原理,其实就是当x为false是,switch case中写了两遍case 0
,此时编译会报错.
使用示例:
COMPILE_TIME_ASSERT(XLAT_TABLES_SIZE == sizeof(xlat_tables));
C语言中 LOG函数重定义–加TAG
#define EMSG(fmt, args...) TEE_LogPrintf("aes_demo "fmt"\n", ##args)
C语言中字符串的形式打印16进制数据
#define PRINT_BUF_MAX (32)
#define TAG_STRING_MAX (32)
static void print_step_hex_data(const char *tag, void *pbuf, size_t len)
{
int i;
char buf[PRINT_BUF_MAX * 4] = {0};
char temp_buf[4] = {0};
sprintf(buf,"%s",tag);
for (i=0;i<len;i++) {
if(i % 16 ==0)
strcat(buf,"\n");
sprintf(temp_buf," %02x",*((char *)pbuf + i)&0xff);
strcat(buf, temp_buf);
}
strcat(buf,"\n");
LOGI("%s", buf);
}
void print_hex_data(const char *tag, void *pbuf, size_t len)
{
int i;
uint32_t block;
uint32_t remainder;
void *p;
char tag_buf[TAG_STRING_MAX + 6] = {0};
char tail_buf[6+1] = {0};
p = pbuf;
if (len<=PRINT_BUF_MAX)
return print_step_hex_data(tag, p, len);
else {
block = len / PRINT_BUF_MAX;
remainder = len % PRINT_BUF_MAX;
for (i=0;i<block;i++) {
memcpy(tag_buf,tag,TAG_STRING_MAX);
sprintf(tail_buf,"[%i]",i);
strcat(tag_buf, tail_buf);
print_step_hex_data(tag_buf, p, PRINT_BUF_MAX);
p = (char *)p + PRINT_BUF_MAX;
}
if (remainder) {
memcpy(tag_buf,tag,TAG_STRING_MAX);
sprintf(tail_buf,"[%i]",i);
strcat(tag_buf, tail_buf);
print_step_hex_data(tag_buf, p, remainder);
}
}
}