Chinaunix首页 | 论坛 | 博客
  • 博客访问: 162179
  • 博文数量: 33
  • 博客积分: 2530
  • 博客等级: 少校
  • 技术积分: 580
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-25 16:03
文章分类
文章存档

2011年(1)

2010年(2)

2009年(17)

2008年(13)

我的朋友

分类: LINUX

2008-09-28 17:19:05

 
//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;                                \

//})

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