ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.67 by root, Mon Nov 5 16:42:15 2007 UTC vs.
Revision 1.69 by root, Tue Nov 6 00:10:04 2007 UTC

153volatile double SIGFPE_REQ = 0.0f; 153volatile double SIGFPE_REQ = 0.0f;
154#endif 154#endif
155 155
156/*****************************************************************************/ 156/*****************************************************************************/
157 157
158static void (*syserr_cb)(void);
159
160void ev_set_syserr_cb (void (*cb)(void))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (void)
167{
168 if (syserr_cb)
169 syserr_cb ();
170 else
171 {
172 perror ("libev");
173 abort ();
174 }
175}
176
177static void *(*alloc)(void *ptr, long size);
178
179void ev_set_allocator (void *(*cb)(void *ptr, long size))
180{
181 alloc = cb;
182}
183
184static void *
185ev_realloc (void *ptr, long size)
186{
187 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
188
189 if (!ptr && size)
190 {
191 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
192 abort ();
193 }
194
195 return ptr;
196}
197
198#define ev_malloc(size) ev_realloc (0, (size))
199#define ev_free(ptr) ev_realloc ((ptr), 0)
200
201/*****************************************************************************/
202
158typedef struct 203typedef struct
159{ 204{
160 struct ev_watcher_list *head; 205 WL head;
161 unsigned char events; 206 unsigned char events;
162 unsigned char reify; 207 unsigned char reify;
163} ANFD; 208} ANFD;
164 209
165typedef struct 210typedef struct
223 return rt_now; 268 return rt_now;
224} 269}
225 270
226#define array_roundsize(base,n) ((n) | 4 & ~3) 271#define array_roundsize(base,n) ((n) | 4 & ~3)
227 272
228#define array_needsize(base,cur,cnt,init) \ 273#define array_needsize(base,cur,cnt,init) \
229 if (expect_false ((cnt) > cur)) \ 274 if (expect_false ((cnt) > cur)) \
230 { \ 275 { \
231 int newcnt = cur; \ 276 int newcnt = cur; \
232 do \ 277 do \
233 { \ 278 { \
234 newcnt = array_roundsize (base, newcnt << 1); \ 279 newcnt = array_roundsize (base, newcnt << 1); \
235 } \ 280 } \
236 while ((cnt) > newcnt); \ 281 while ((cnt) > newcnt); \
237 \ 282 \
238 base = realloc (base, sizeof (*base) * (newcnt)); \ 283 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
239 init (base + cur, newcnt - cur); \ 284 init (base + cur, newcnt - cur); \
240 cur = newcnt; \ 285 cur = newcnt; \
241 } 286 }
242 287
243#define array_slim(stem) \ 288#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 289 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \ 290 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \ 291 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \ 292 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 293 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 } 294 }
250 295
251#define array_free(stem, idx) \ 296#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253 298
254/*****************************************************************************/ 299/*****************************************************************************/
255 300
256static void 301static void
257anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
376 int fd; 421 int fd;
377 422
378 for (fd = anfdmax; fd--; ) 423 for (fd = anfdmax; fd--; )
379 if (anfds [fd].events) 424 if (anfds [fd].events)
380 { 425 {
381 close (fd);
382 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
383 return; 427 return;
384 } 428 }
385} 429}
386 430
444 488
445/*****************************************************************************/ 489/*****************************************************************************/
446 490
447typedef struct 491typedef struct
448{ 492{
449 struct ev_watcher_list *head; 493 WL head;
450 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
451} ANSIG; 495} ANSIG;
452 496
453static ANSIG *signals; 497static ANSIG *signals;
454static int signalmax; 498static int signalmax;
488} 532}
489 533
490static void 534static void
491sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
492{ 536{
493 struct ev_watcher_list *w; 537 WL w;
494 int signum; 538 int signum;
495 539
496 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
497 gotsig = 0; 541 gotsig = 0;
498 542
703 747
704#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
705struct ev_loop * 749struct ev_loop *
706ev_loop_new (int methods) 750ev_loop_new (int methods)
707{ 751{
708 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 752 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
753
754 memset (loop, 0, sizeof (struct ev_loop));
709 755
710 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
711 757
712 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
713 return loop; 759 return loop;
717 763
718void 764void
719ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
720{ 766{
721 loop_destroy (EV_A); 767 loop_destroy (EV_A);
722 free (loop); 768 ev_free (loop);
723} 769}
724 770
725void 771void
726ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
727{ 773{
1396 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1397 void *arg = once->arg; 1443 void *arg = once->arg;
1398 1444
1399 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1400 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1401 free (once); 1447 ev_free (once);
1402 1448
1403 cb (revents, arg); 1449 cb (revents, arg);
1404} 1450}
1405 1451
1406static void 1452static void
1416} 1462}
1417 1463
1418void 1464void
1419ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1465ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1420{ 1466{
1421 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1422 1468
1423 if (!once) 1469 if (!once)
1424 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1425 else 1471 else
1426 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines