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

Comparing libev/ev.c (file contents):
Revision 1.68 by root, Mon Nov 5 20:19:00 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 WL head; 205 WL head;
161 unsigned char events; 206 unsigned char events;
162 unsigned char reify; 207 unsigned char reify;
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)
702 747
703#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
704struct ev_loop * 749struct ev_loop *
705ev_loop_new (int methods) 750ev_loop_new (int methods)
706{ 751{
707 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));
708 755
709 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
710 757
711 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
712 return loop; 759 return loop;
716 763
717void 764void
718ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
719{ 766{
720 loop_destroy (EV_A); 767 loop_destroy (EV_A);
721 free (loop); 768 ev_free (loop);
722} 769}
723 770
724void 771void
725ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
726{ 773{
1395 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1396 void *arg = once->arg; 1443 void *arg = once->arg;
1397 1444
1398 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1399 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1400 free (once); 1447 ev_free (once);
1401 1448
1402 cb (revents, arg); 1449 cb (revents, arg);
1403} 1450}
1404 1451
1405static void 1452static void
1415} 1462}
1416 1463
1417void 1464void
1418ev_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)
1419{ 1466{
1420 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1421 1468
1422 if (!once) 1469 if (!once)
1423 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1424 else 1471 else
1425 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines