Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3519863
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类:

2011-12-23 17:07:25

一些宏的测试代码,从kernel里面扒出来的, 弄到app里面,其实都是gcc一样的。
 
//check.c
 

#include <stdio.h>
#include <stdlib.h>


#define time_after(a,b) \
        (typecheck(unsigned long, a) && \
         typecheck(unsigned long, b) && \
         ((long)(b) - (long)(a) < 0))
#define time_before(a,b) time_after(b,a)

#define time_after_eq(a,b) \
        (typecheck(unsigned long, a) && \
         typecheck(unsigned long, b) && \
         ((long)(a) - (long)(b) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)

/*
* Have the 32 bit jiffies value wrap 5 minutes after boot
* so jiffies wrap bugs show up earlier.
*/



/*
* Check at compile time that something is of a particular type.
* Always evaluates to 1 so you may use it easily in comparisons.
*/

#define typecheck(type,x) \
({ type __dummy; \
        typeof(x) __dummy2; \
        (void)(&__dummy == &__dummy2); \
        1; \
})
/*
* Check at compile time that 'function' is a certain type, or is a pointer
* to that type (needs to use typedef for the function type.)
*/

#define typecheck_fn(type,function) \
({ typeof(type) __tmp = function; \
        (void)__tmp; \
})


typedef int (*fun)(int *,unsigned long *);
int main(void)
{
    
    int i=10;
    int val =0;
    
    val = typecheck(unsigned long,i);
    printf("val=%d\n",val);
    
    return 0;
}
    

 

//warn.c

 

#include <stdio.h>
#include <stdlib.h>
//功能有点类似于 assert()

#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;
}

 

//spinlock.c  比较复杂的宏, 值得借鉴:

 

#include <stdio.h>
#include <stdlib.h>

#define TYPE_EQUAL(n, type) \
        __builtin_types_compatible_p(typeof(n), type *)


//#define PICK_OP_PRINT(op, n)                        \

//do {                                    \

//    if (TYPE_EQUAL((n), int))                    \

//        printf("int number=%d\n",*n);                \

//    else if (TYPE_EQUAL(n, long))                    \

//        printf("long number=%ld\n",*n);                \

//    else                                 \

//        printf("error\n");                    \

//} while (0)


#define PICK_OP_PRINT(op, n)                        \
({                                    \
    if (TYPE_EQUAL((n), int))                    \
        printf("int number=%d\n",*n);                \
    else if (TYPE_EQUAL(n, long))                    \
        printf("long number=%ld\n",*n);                \
    else                                 \
        printf("error\n");                    \
})

#define PICK_OP_PRINT_RET(op, n)                        \
({                                    \
    unsigned int __ret = 1;                        \
    if (TYPE_EQUAL((n), int))                    \
        printf("int number=%d\n",*n);                \
    else if (TYPE_EQUAL(n, long))                    \
        printf("long number=%ld\n",*n);                \
    else                                 \
        printf("error\n");                    \
    __ret;                                \
})


#define printtype(n)    PICK_OP_PRINT(op, n)
#define printtypet(n)    PICK_OP_PRINT_RET(op, n)

int main(void)
{
    int a = 20;
    int ret = 0;
    printtype(&a);
    ret = printtypet(&a);
    printf("ret=%i\n",ret);
    
    
    long b = 100000000;
    printtype(&b);
    ret = printtypet(&b);
    printf("ret=%i\n",ret);
    
    float c = 3.6L;
    printf("float c=%f\n",c);
    printtype(&c);
    ret = printtypet(&c);
    printf("ret=%i\n",ret);
    
    return 0;
}


#define PICK_OP(op, lock)                        \
do {                                    \
    if (TYPE_EQUAL((lock), raw_spinlock_t))                \
        __spin##op((raw_spinlock_t *)(lock));            \
    else if (TYPE_EQUAL(lock, spinlock_t))                \
        _spin##op((spinlock_t *)(lock));            \
    else __bad_spinlock_type();                    \
} while (0)







//#define PICK_OP_RET(op, lock...)                    \

//({                                    \

//    unsigned long __ret;                        \

//                                    \

//    if (TYPE_EQUAL((lock), raw_spinlock_t))                 \

//        __ret = __spin##op((raw_spinlock_t *)(lock));        \

//    else if (TYPE_EQUAL(lock, spinlock_t))                \

//        __ret = _spin##op((spinlock_t *)(lock));        \

//    else __ret = __bad_spinlock_type();                \

//                                    \

//    __ret;                                \

//})

//

//#define PICK_OP2(op, lock, flags)                    \

//do {                                    \

//    if (TYPE_EQUAL((lock), raw_spinlock_t))                \

//        __spin##op((raw_spinlock_t *)(lock), flags);        \

//    else if (TYPE_EQUAL(lock, spinlock_t))                \

//        _spin##op((spinlock_t *)(lock), flags);            \

//    else __bad_spinlock_type();                    \

//} while (0)

//

//#define PICK_OP2_RET(op, lock, flags)                    \

//({                                    \

//    unsigned long __ret;                        \

//                                    \

//    if (TYPE_EQUAL((lock), raw_spinlock_t))                \

//        __ret = __spin##op((raw_spinlock_t *)(lock), flags);    \

//    else if (TYPE_EQUAL(lock, spinlock_t))                \

//        __ret = _spin##op((spinlock_t *)(lock), flags);        \

//    else __bad_spinlock_type();                    \

//                                    \

//    __ret;                                \

//})

 

阅读(364) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~