__builtin_expect myassert()
来源:互联网 发布:c socket编程 编辑:程序博客网 时间:2024/06/06 04:14
这个assert功能比较齐全,原帖地址是
http://blog.csdn.net/sealyao/archive/2011/01/30/6169568.aspx
- //-------file main.c----------
- #include <stdio.h>
- #include "myassert.h"
- int func(const char *filename);
- int main(int argc,char **argv)
- {
- MyAssert("two args are needed",argc==2);
- func(argv[1]);
- return 0;
- }
- //-------file func.c----------
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "myassert.h"
- int func(const char *filename)
- {
- int fd;
- MyAssert("filename can not be null",filename);
- MyAssert("file not exist",0==access(filename,F_OK));
- fd = open(filename,O_RDONLY);
- close(fd);
- return 0;
- }
- //-------file myassert.h----------
- #ifndef __MY_ASSERT_H__
- #define __MY_ASSERT_H__
- #include <stdio.h>
- #include <stdlib.h>
- #define MyAssert(message,assertion) do{/
- if(!(assertion)){/
- printf("line %d in %s(%s)", __LINE__, __FILE__,__FUNCTION__);/
- if(message){/
- printf(" : %s",message);/
- }/
- printf("/n");/
- abort();/
- }/
- }while(0);
- #endif
- #Makefile
- TARGET = test
- CC = gcc
- CCFLAGS = -Wall
- OBJS = main.o func.o
- $(TARGET) : $(OBJS)
- $(CC) -o $@ $(OBJS) $(CCFLAGS)
- %.o : %.c
- $(CC) -o $@ -c $< $(CCFLAGS)
- clean:
- rm -rf *.o
- rm -rf $(TARGET)
#include <stdio.h>
#include <stdlib.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define BUG_ON(condition) do /
{ if (unlikely((condition)!=0)) /
printf("BUG: failure at %s:%d/%s()!/n", __FILE__, __LINE__, __FUNCTION__); /
} while(0)
int main(void)
{
int i = 7;
BUG_ON(i!=10);
return 0;
}
- __builtin_expect myassert()
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect详解
- 关于__builtin_expect
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect 解惑
- __builtin_expect 解惑
- __builtin_expect详解
- __builtin_expect详解
- __builtin_expect 解惑
- __builtin_expect(!!(x), 1) 之__builtin_expect
- GCC __builtin_expect的作用
- (转)UNICODE与ANSI的区别
- 【补足基础】结构体作为类中成员变量(一)
- UML类图关系模式及C++代码说明
- 初来咋到,要想在此安家,先要共享3500份课后答案
- 去掉word 2007中参考文献下自动生成的横线
- __builtin_expect myassert()
- 好书推荐——嵌入式系统的实时概念 (作者 Qing Li 译者 王安生)
- 感言
- Maven学习笔记
- 近期问题小结
- Google Closure Compiler --js压缩优化
- C++ 模板
- C++面试汇总
- 我是新手!