ev_realloc()
函数原型:
inline_speed void *
ev_realloc (void *ptr, long size)
{
ptr = alloc (ptr, size);
if (!ptr && size)
{
#if EV_AVOID_STDIO
ev_printerr ("(libev) memory allocation failed, aborting.\n");
#else
fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
#endif
abort ();
}
return ptr;
}
该函数的底层使用realloc() 函数,通过传递的指针和size的参数来使用。如果想要扩充内存和缩小内存,必须要绕过 ev_malloc() 和 ev_free() 因为它们只支持一个参数。
示例:
struct ev_loop* p;
printf("p:%d\n",p);
p = ev_loop_new(sizeof (ev_loop));
printf("p:%d\n",p);
ev_now()
函数声明:
EV_API_DECL ev_tstamp ev_now (EV_P) EV_THROW;
函数定义:
#if EV_MULTIPLICITY
ev_tstamp
ev_now (EV_P) EV_THROW
{
return ev_rt_now;
}
#endif
这个函数返回是POSIX时期距离现在的一个秒数
示例:
ev_tstamp time = ev_now(p);
printf("time:%lf\n",time);
ev_loop_destory()
函数声明:
EV_API_DECL void ev_loop_destroy (EV_P);
函数原型:
ecb_cold
void
ev_loop_destroy (EV_P)
{
int i;
#if EV_MULTIPLICITY
if (!EV_A)
return;
#endif
#if EV_CLEANUP_ENABLE
if (expect_false (cleanupcnt))
{
queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
EV_INVOKE_PENDING;
}
#endif
#if EV_CHILD_ENABLE
if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
{
ev_ref (EV_A);
ev_signal_stop (EV_A_ &childev);
}
#endif
if (ev_is_active (&pipe_w))
{
if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
}
#if EV_USE_SIGNALFD
if (ev_is_active (&sigfd_w))
close (sigfd);
#endif
#if EV_USE_INOTIFY
if (fs_fd >= 0)
close (fs_fd);
#endif
if (backend_fd >= 0)
close (backend_fd);
#if EV_USE_IOCP
if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
#endif
#if EV_USE_PORT
if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
#endif
#if EV_USE_KQUEUE
if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
#endif
#if EV_USE_EPOLL
if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
#endif
#if EV_USE_POLL
if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
#endif
#if EV_USE_SELECT
if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
#endif
for (i = NUMPRI; i--; )
{
array_free (pending, [i]);
#if EV_IDLE_ENABLE
array_free (idle, [i]);
#endif
}
ev_free (anfds); anfds = 0; anfdmax = 0;
array_free (rfeed, EMPTY);
array_free (fdchange, EMPTY);
array_free (timer, EMPTY);
#if EV_PERIODIC_ENABLE
array_free (periodic, EMPTY);
#endif
#if EV_FORK_ENABLE
array_free (fork, EMPTY);
#endif
#if EV_CLEANUP_ENABLE
array_free (cleanup, EMPTY);
#endif
array_free (prepare, EMPTY);
array_free (check, EMPTY);
#if EV_ASYNC_ENABLE
array_free (async, EMPTY);
#endif
backend = 0;
#if EV_MULTIPLICITY
if (ev_is_default_loop (EV_A))
#endif
ev_default_loop_ptr = 0;
#if EV_MULTIPLICITY
else
ev_free (EV_A);
#endif
}
涉及到的一些函数 和 结构体:
W,WL,WT
typedef ev_watcher *W;
typedef ev_watcher_list *WL;
typedef ev_watcher_time *WT;
queue_events()
inline_speed void
queue_events (EV_P_ W *events, int eventcnt, int type)
{
int i;
for (i = 0; i < eventcnt; ++i)
ev_feed_event (EV_A_ events [i], type);
}
ev_feed_event()
noinline
void
ev_feed_event (EV_P_ void *w, int revents) EV_THROW
{
W w_ = (W)w;
int pri = ABSPRI (w_);
if (expect_false (w_->pending))
pendings [pri][w_->pending - 1].events |= revents;
else
{
w_->pending = ++pendingcnt [pri];
array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
pendings [pri][w_->pending - 1].w = w_;
pendings [pri][w_->pending - 1].events = revents;
}
pendingpri = NUMPRI - 1;
}
array_needsize()
#define array_needsize(type,base,cur,cnt,init) \
if (expect_false ((cnt) > (cur))) \
{ \
ecb_unused int ocur_ = (cur); \
(base) = (type *)array_realloc \
(sizeof (type), (base), &(cur), (cnt)); \
init ((base) + (ocur_), (cur) - ocur_); \
}
array_realloc()
noinline ecb_cold
static void *
array_realloc (int elem, void *base, int *cur, int cnt)
{
*cur = array_nextsize (elem, *cur, cnt);
return ev_realloc (base, elem * *cur);
}
array_nextsize()
inline_size int
array_nextsize (int elem, int cur, int cnt)
{
int ncur = cur + 1;
do
ncur <<= 1;
while (cnt > ncur);
if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
{
ncur *= elem;
ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
ncur = ncur - sizeof (void *) * 4;
ncur /= elem;
}
return ncur;
}
ev_ref()
void
ev_ref (EV_P) EV_THROW
{
++activecnt;
}
array_free()
#define array_free(stem, idx) \
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
|