libev 宏展开

想看源码,宏太多,看着累,宏展开,再看,功力时间不够,先放下

放上宏展开后的代码。

libev4.20

展开方示为

./configure

修改makefile文件,字符串 替换CC为 CPP

注意要把基础的CC定义保留

make

mv ev.o ev.c

mv event.o event.c

make clean

修改makefile文件,字符串替换CPP-》CC

注意要把基础的CPP定义保留

宏展开后#号注释的代码,可以修改CPP的定义 加个参数

ev.c,event.c

及为展开后的代码

测试可编译通过

附ev.c部分代码

struct ev_loop;
enum {
    EV_UNDEF = (int)0xFFFFFFFF,
    EV_NONE = 0x00,
    EV_READ = 0x01,
    EV_WRITE = 0x02,
    EV__IOFDSET = 0x80,
    EV_IO = EV_READ,
    EV_TIMER = 0x00000100,

    EV_TIMEOUT = EV_TIMER,

    EV_PERIODIC = 0x00000200,
    EV_SIGNAL = 0x00000400,
    EV_CHILD = 0x00000800,
    EV_STAT = 0x00001000,
    EV_IDLE = 0x00002000,
    EV_PREPARE = 0x00004000,
    EV_CHECK = 0x00008000,
    EV_EMBED = 0x00010000,
    EV_FORK = 0x00020000,
    EV_CLEANUP = 0x00040000,
    EV_ASYNC = 0x00080000,
    EV_CUSTOM = 0x01000000,
    EV_ERROR = (int)0x80000000
};
typedef struct ev_watcher
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_watcher *w, int revents);
} ev_watcher;

typedef struct ev_watcher_list
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_watcher_list *w, int revents);

    struct ev_watcher_list *next;
} ev_watcher_list;

typedef struct ev_watcher_time
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_watcher_time *w, int revents);

    ev_tstamp at;
} ev_watcher_time;

typedef struct ev_io
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_io *w, int revents);

    struct ev_watcher_list *next;

    int fd;
    int events;
} ev_io;

typedef struct ev_timer
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_timer *w, int revents);
    ev_tstamp at;

    ev_tstamp repeat;
} ev_timer;

typedef struct ev_periodic
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_periodic *w, int revents); ev_tstamp at;

    ev_tstamp offset;
    ev_tstamp interval;
    ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) ;
} ev_periodic;

typedef struct ev_signal
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_signal *w, int revents);

    struct ev_watcher_list *next;

    int signum;
} ev_signal;

typedef struct ev_child
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_child *w, int revents);

    struct ev_watcher_list *next;

    int flags;
    int pid;
    int rpid;
    int rstatus;
} ev_child;

typedef struct stat ev_statdata;

typedef struct ev_stat
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_stat *w, int revents);

    struct ev_watcher_list *next;

    ev_timer timer;
    ev_tstamp interval;
    const char *path;
    ev_statdata prev;
    ev_statdata attr;

    int wd;
} ev_stat;

typedef struct ev_idle
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_idle *w, int revents);
} ev_idle;

typedef struct ev_prepare
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_prepare *w, int revents);
} ev_prepare;

typedef struct ev_check
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_check *w, int revents);
} ev_check;

typedef struct ev_fork
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_fork *w, int revents);
} ev_fork;

typedef struct ev_cleanup
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_cleanup *w, int revents);
} ev_cleanup;

typedef struct ev_embed
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_embed *w, int revents);

    struct ev_loop *other;
    ev_io io;
    ev_prepare prepare;
    ev_check check;
    ev_timer timer;
    ev_periodic periodic;
    ev_idle idle;
    ev_fork fork;

    ev_cleanup cleanup;

} ev_embed;

typedef struct ev_async
{
    int active; int pending; int priority; void *data; void (*cb)(struct ev_loop *loop, struct ev_async *w, int revents);

    sig_atomic_t volatile sent;
} ev_async;

union ev_any_watcher
{
    struct ev_watcher w;
    struct ev_watcher_list wl;

    struct ev_io io;
    struct ev_timer timer;
    struct ev_periodic periodic;
    struct ev_signal signal;
    struct ev_child child;

    struct ev_stat stat;

    struct ev_idle idle;

    struct ev_prepare prepare;
    struct ev_check check;

    struct ev_fork fork;

    struct ev_cleanup cleanup;

    struct ev_embed embed;

    struct ev_async async;

};

enum {

    EVFLAG_AUTO = 0x00000000U,

    EVFLAG_NOENV = 0x01000000U,
    EVFLAG_FORKCHECK = 0x02000000U,

    EVFLAG_NOINOTIFY = 0x00100000U,

    EVFLAG_NOSIGFD = 0,

    EVFLAG_SIGNALFD = 0x00200000U,
    EVFLAG_NOSIGMASK = 0x00400000U
};

enum {
    EVBACKEND_SELECT = 0x00000001U,
    EVBACKEND_POLL = 0x00000002U,
    EVBACKEND_EPOLL = 0x00000004U,
    EVBACKEND_KQUEUE = 0x00000008U,
    EVBACKEND_DEVPOLL = 0x00000010U,
    EVBACKEND_PORT = 0x00000020U,
    EVBACKEND_ALL = 0x0000003FU,
    EVBACKEND_MASK = 0x0000FFFFU
};

extern int ev_version_major (void) ;
extern int ev_version_minor (void) ;

extern unsigned int ev_supported_backends (void) ;
extern unsigned int ev_recommended_backends (void) ;
extern unsigned int ev_embeddable_backends (void) ;

extern ev_tstamp ev_time (void) ;
extern void ev_sleep (ev_tstamp delay) ;

extern void ev_set_allocator (void *(*cb)(void *ptr, long size) ) ;

extern void ev_set_syserr_cb (void (*cb)(const char *msg) ) ;

extern struct ev_loop *ev_default_loop (unsigned int flags ) ;

static inline struct ev_loop *
ev_default_loop_uc_ (void)
{
    extern struct ev_loop *ev_default_loop_ptr;

    return ev_default_loop_ptr;
}

static inline int
ev_is_default_loop (struct ev_loop *loop)
{
    return loop == ev_default_loop_uc_ ();
}

extern struct ev_loop *ev_loop_new (unsigned int flags ) ;

static inline void
idle_reify (struct ev_loop *loop)
{
    if (__builtin_expect ((!!(((loop)->idleall))),(0)))
    {
        int pri;

        for (pri = ((((0x7f) & 4) ? +2 : 0) - (((0x7f) & 4) ? -2 : 0) + 1); pri--; )
        {
            if (((loop)->pendingcnt) [pri])
                break;

            if (((loop)->idlecnt) [pri])
            {
                queue_events (loop, (W *)((loop)->idles) [pri], ((loop)->idlecnt) [pri], EV_IDLE);
                break;
            }
        }
    }
}

static inline void
timers_reify (struct ev_loop *loop)
{
    do { } while (0);

    if (((loop)->timercnt) && (((loop)->timers) [(4 - 1)]).at < ((loop)->mn_now))
    {
        do
        {
            ev_timer *w = (ev_timer *)(((loop)->timers) [(4 - 1)]).w;

            if (w->repeat)
            {
                ((WT)(w))->at += w->repeat;
                if (((WT)(w))->at < ((loop)->mn_now))
                    ((WT)(w))->at = ((loop)->mn_now);

                (__builtin_expect(!(("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)), 0) ? __assert_rtn(__func__, "ev.c", 3207, "(\"libev: negative ev_timer repeat value found while processing timers\", w->repeat > 0.)") : (void)0);

                (((loop)->timers) [(4 - 1)]).at = (((loop)->timers) [(4 - 1)]).w->at;
                downheap (((loop)->timers), ((loop)->timercnt), (4 - 1));
            }
            else
                ev_timer_stop (loop, w);

            do { } while (0);
            feed_reverse (loop, (W)w);
        }
        while (((loop)->timercnt) && (((loop)->timers) [(4 - 1)]).at < ((loop)->mn_now));

        feed_reverse_done (loop, EV_TIMER);
    }
}

static void __attribute__ ((__noinline__))
periodic_recalc (struct ev_loop *loop, ev_periodic *w)
{
    ev_tstamp interval = w->interval > 0.0001220703125 ? w->interval : 0.0001220703125;
    ev_tstamp at = w->offset + interval * floor ((((loop)->ev_rt_now) - w->offset) / interval);

    while (at <= ((loop)->ev_rt_now))
    {
        ev_tstamp nat = at + w->interval;

        if (__builtin_expect ((!!(nat == at)),(0)))
        {
            at = ((loop)->ev_rt_now);
            break;
        }

        at = nat;
    }

    ((WT)(w))->at = at;
}

static inline void
periodics_reify (struct ev_loop *loop)
{
    do { } while (0);

    while (((loop)->periodiccnt) && (((loop)->periodics) [(4 - 1)]).at < ((loop)->ev_rt_now))
    {
        do
        {
            ev_periodic *w = (ev_periodic *)(((loop)->periodics) [(4 - 1)]).w;

            if (w->reschedule_cb)
            {
                ((WT)(w))->at = w->reschedule_cb (w, ((loop)->ev_rt_now));

                (__builtin_expect(!(("libev: ev_periodic reschedule callback returned time in the past", ((WT)(w))->at >= ((loop)->ev_rt_now))), 0) ? __assert_rtn(__func__, "ev.c", 3269, "(\"libev: ev_periodic reschedule callback returned time in the past\", ev_at (w) >= ev_rt_now)") : (void)0);

                (((loop)->periodics) [(4 - 1)]).at = (((loop)->periodics) [(4 - 1)]).w->at;
                downheap (((loop)->periodics), ((loop)->periodiccnt), (4 - 1));
            }
            else if (w->interval)
            {
                periodic_recalc (loop, w);
                (((loop)->periodics) [(4 - 1)]).at = (((loop)->periodics) [(4 - 1)]).w->at;
                downheap (((loop)->periodics), ((loop)->periodiccnt), (4 - 1));
            }
            else
                ev_periodic_stop (loop, w);

            do { } while (0);
            feed_reverse (loop, (W)w);
        }
        while (((loop)->periodiccnt) && (((loop)->periodics) [(4 - 1)]).at < ((loop)->ev_rt_now));

        feed_reverse_done (loop, EV_PERIODIC);
    }
}

static void __attribute__ ((__noinline__))
periodics_reschedule (struct ev_loop *loop)
{
    int i;

    for (i = (4 - 1); i < ((loop)->periodiccnt) + (4 - 1); ++i)
    {
        ev_periodic *w = (ev_periodic *)(((loop)->periodics) [i]).w;

        if (w->reschedule_cb)
            ((WT)(w))->at = w->reschedule_cb (w, ((loop)->ev_rt_now));
        else if (w->interval)
            periodic_recalc (loop, w);

        (((loop)->periodics) [i]).at = (((loop)->periodics) [i]).w->at;
    }

    reheap (((loop)->periodics), ((loop)->periodiccnt));
}

static void __attribute__ ((__noinline__))
timers_reschedule (struct ev_loop *loop, ev_tstamp adjust)
{
    int i;

    for (i = 0; i < ((loop)->timercnt); ++i)
    {
        ANHE *he = ((loop)->timers) + i + (4 - 1);
        (*he).w->at += adjust;
        (*he).at = (*he).w->at;
    }
}

static inline void
time_update (struct ev_loop *loop, ev_tstamp max_block)
{
    {
        ((loop)->ev_rt_now) = ev_time ();

        if (__builtin_expect ((!!(((loop)->mn_now) > ((loop)->ev_rt_now) || ((loop)->ev_rt_now) > ((loop)->mn_now) + max_block + 1.)),(0)))
        {

            timers_reschedule (loop, ((loop)->ev_rt_now) - ((loop)->mn_now));

            periodics_reschedule (loop);

        }

        ((loop)->mn_now) = ((loop)->ev_rt_now);
    }
}

还没看,以后可能也不会看了

时间: 2024-10-12 16:35:47

libev 宏展开的相关文章

C语言中的宏展开

#include<stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf("%s\n",h(f(1,2))); 宏h(a) 是g(a),没有#,所以需要进行宏展开 printf("%s\n",g(f(1,2))); //宏g(a) 有#,所以不展开 return 0; } 如何展开宏函数的规则:在展开当前宏函数时,如果形参有#或##则不进行宏参数

宏------进阶

宏定义的黑魔法 - 宏菜鸟起飞手册 宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开发时,我们会将更多的重心放在业务逻辑上,似乎对宏的使用和依赖并不多.但是使用宏定义的好处是不言自明的,在节省工作量的同时,代码可读性大大增加.如果想成为一个能写出漂亮优雅代码的开发者,宏定义绝对是必不可少的技能(虽然宏本身可能并不漂亮优雅XD).但是因为宏定义对于很多人来说,并不像业务逻辑那

process调用protothread机制的相关宏定义——用HelloWorld进程诠释

HelloWorld例子 #include "contiki.h" #include <stdio.h> /* For printf() */ /*---------------------------------------------------------------------------*/ PROCESS(hello_world_process, "Hello world process"); AUTOSTART_PROCESSES(&

不带参数的宏定义与带参数的宏定义

宏定义是C提供的三种预处理功能的其中一种,这三种预处理包括:宏定义.文件包含.条件编译 1.不带参数的宏定义: 宏定义又称为宏代换.宏替换,简称“宏”. 格式: #define 标识符 字符串 其中的标识符就是所谓的符号常量,也称为“宏名”. 预处理(预编译)工作也叫做宏展开:将宏名替换为字符串. 掌握"宏"概念的关键是“换”.一切以换为前提.做任何事情之前先要换,准确理解之前就要“换”. 即在对相关命令或语句的含义和功能作具体分析之前就要换: 例: #define PI 3.1415

宏!!!!(拷贝别人的博客的只为了收藏)

入门 如果您完全不知道宏是什么的话,可以先来热个身.很多人在介绍宏的时候会说,宏嘛很简单,就是简单的查找替换嘛.嗯,只说对了的一半.C中的宏分为两类,对象宏(object-like macro)和函数宏(function-like macro).对于对象宏来说确实相对简单,但却也不是那么简单的查找替换.对象宏一般用来定义一些常数,举个例子: //This defines PI #define M_PI 3.14159265358979323846264338327950288 #define关键

linux内核宏container_of

首先来个简单版本 1 /* given a pointer @ptr to the field @member embedded into type (usually 2 * struct) @type, return pointer to the embedding instance of @type. */ 3 #define container_of(ptr, type, member) 4 ((type *)((char *)(ptr)-(char *)(&((type *)0)->

C语言中的宏

1.不带参数的宏定义:宏定义又称为宏代换.宏替换,简称“宏”.格式:#define 标识符 字符串其中的标识符就是所谓的符号常量,也称为“宏名”.预处理(预编译)工作也叫做宏展开:将宏名替换为字符串.掌握"宏"概念的关键是“换”.一切以换为前提.做任何事情之前先要换,准确理解之前就要“换”.即在对相关命令或语句的含义和功能作具体分析之前就要换:例:#define PI 3.1415926把程序中出现的PI全部换成3.1415926说明:(1)宏名一般用大写(2)使用宏可提高程序的通用性

&lt;转&gt;Verilog HDL宏定义define

宏定义 `define 用一个指定的标识符(即名字)来代表一个字符串,它的一般形式为: `define 标识符(宏名) 字符串(宏内容) 如:`define signal string 它的作用是指定用标识符signal来代替string这个字符串,在编译预处理时,把程序中在该命令以后所有的signal都替换成string.这种方法使用户能以一个简单的名字代替一个长的字符串,也可以用一个有含义的名字来代替没有含义的数字和符号,因此把这个标识符(名字)称为“宏名”,在编译预处理时将宏名替换成字符串

宏定义的黑魔法 - 宏菜鸟起飞手册

转载:https://onevcat.com/2014/01/black-magic-in-macro/ 宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开发时,我们会将更多的重心放在业务逻辑上,似乎对宏的使用和依赖并不多.但是使用宏定义的好处是不言自明的,在节省工作量的同时,代码可读性大大增加.如果想成为一个能写出漂亮优雅代码的开发者,宏定义绝对是必不可少的技能(虽然宏本身