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

Comparing libev/ev.c (file contents):
Revision 1.389 by root, Wed Aug 3 15:31:23 2011 UTC vs.
Revision 1.401 by root, Tue Dec 20 04:08:35 2011 UTC

183# include EV_H 183# include EV_H
184#else 184#else
185# include "ev.h" 185# include "ev.h"
186#endif 186#endif
187 187
188EV_CPP(extern "C" {)
189
190#ifndef _WIN32 188#ifndef _WIN32
191# include <sys/time.h> 189# include <sys/time.h>
192# include <sys/wait.h> 190# include <sys/wait.h>
193# include <unistd.h> 191# include <unistd.h>
194#else 192#else
464#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 462#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
465 463
466#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) 464#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
467#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) 465#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
468 466
469/* the following are taken from libecb */ 467/* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
470/* ecb.h start */ 468/* ECB.H BEGIN */
469/*
470 * libecb - http://software.schmorp.de/pkg/libecb
471 *
472 * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de>
473 * Copyright (©) 2011 Emanuele Giaquinta
474 * All rights reserved.
475 *
476 * Redistribution and use in source and binary forms, with or without modifica-
477 * tion, are permitted provided that the following conditions are met:
478 *
479 * 1. Redistributions of source code must retain the above copyright notice,
480 * this list of conditions and the following disclaimer.
481 *
482 * 2. Redistributions in binary form must reproduce the above copyright
483 * notice, this list of conditions and the following disclaimer in the
484 * documentation and/or other materials provided with the distribution.
485 *
486 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
487 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
488 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
489 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
490 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
491 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
492 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
493 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
494 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
495 * OF THE POSSIBILITY OF SUCH DAMAGE.
496 */
497
498#ifndef ECB_H
499#define ECB_H
500
501#ifdef _WIN32
502 typedef signed char int8_t;
503 typedef unsigned char uint8_t;
504 typedef signed short int16_t;
505 typedef unsigned short uint16_t;
506 typedef signed int int32_t;
507 typedef unsigned int uint32_t;
508 #if __GNUC__
509 typedef signed long long int64_t;
510 typedef unsigned long long uint64_t;
511 #else /* _MSC_VER || __BORLANDC__ */
512 typedef signed __int64 int64_t;
513 typedef unsigned __int64 uint64_t;
514 #endif
515#else
516 #include <inttypes.h>
517#endif
471 518
472/* many compilers define _GNUC_ to some versions but then only implement 519/* many compilers define _GNUC_ to some versions but then only implement
473 * what their idiot authors think are the "more important" extensions, 520 * what their idiot authors think are the "more important" extensions,
474 * causing enourmous grief in return for some better fake benchmark numbers. 521 * causing enormous grief in return for some better fake benchmark numbers.
475 * or so. 522 * or so.
476 * we try to detect these and simply assume they are not gcc - if they have 523 * we try to detect these and simply assume they are not gcc - if they have
477 * an issue with that they should have done it right in the first place. 524 * an issue with that they should have done it right in the first place.
478 */ 525 */
479#ifndef ECB_GCC_VERSION 526#ifndef ECB_GCC_VERSION
482 #else 529 #else
483 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 530 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
484 #endif 531 #endif
485#endif 532#endif
486 533
534/*****************************************************************************/
535
536/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
537/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
538
539#if ECB_NO_THREADS || ECB_NO_SMP
540 #define ECB_MEMORY_FENCE do { } while (0)
541#endif
542
543#ifndef ECB_MEMORY_FENCE
544 #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__)
545 #if __i386__
546 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
547 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */
548 #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */
549 #elif __amd64
550 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
551 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
552 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */
553 #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
554 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
555 #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \
556 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__)
557 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
558 #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \
559 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ )
560 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
561 #endif
562 #endif
563#endif
564
565#ifndef ECB_MEMORY_FENCE
566 #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__)
567 #define ECB_MEMORY_FENCE __sync_synchronize ()
568 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
569 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
570 #elif _MSC_VER >= 1400 /* VC++ 2005 */
571 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
572 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
573 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
574 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
575 #elif defined(_WIN32)
576 #include <WinNT.h>
577 #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
578 #endif
579#endif
580
581#ifndef ECB_MEMORY_FENCE
582 #if !ECB_AVOID_PTHREADS
583 /*
584 * if you get undefined symbol references to pthread_mutex_lock,
585 * or failure to find pthread.h, then you should implement
586 * the ECB_MEMORY_FENCE operations for your cpu/compiler
587 * OR provide pthread.h and link against the posix thread library
588 * of your system.
589 */
590 #include <pthread.h>
591 #define ECB_NEEDS_PTHREADS 1
592 #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
593
594 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
595 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
596 #endif
597#endif
598
599#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE)
600 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
601#endif
602
603#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE)
604 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
605#endif
606
607/*****************************************************************************/
608
609#define ECB_C99 (__STDC_VERSION__ >= 199901L)
610
487#if __cplusplus 611#if __cplusplus
488 #define ecb_inline static inline 612 #define ecb_inline static inline
489#elif ECB_GCC_VERSION(2,5) 613#elif ECB_GCC_VERSION(2,5)
490 #define ecb_inline static __inline__ 614 #define ecb_inline static __inline__
491#elif ECB_C99 615#elif ECB_C99
492 #define ecb_inline static inline 616 #define ecb_inline static inline
493#else 617#else
494 #define ecb_inline static 618 #define ecb_inline static
495#endif 619#endif
496 620
497#ifndef ECB_MEMORY_FENCE
498 #if ECB_GCC_VERSION(2,5) 621#if ECB_GCC_VERSION(3,3)
499 #if __x86 622 #define ecb_restrict __restrict__
500 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") 623#elif ECB_C99
501 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE 624 #define ecb_restrict restrict
502 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ 625#else
503 #elif __amd64 626 #define ecb_restrict
504 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
505 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
506 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence")
507 #endif
508 #endif 627#endif
509#endif
510 628
511#ifndef ECB_MEMORY_FENCE 629typedef int ecb_bool;
512 #if ECB_GCC_VERSION(4,4)
513 #define ECB_MEMORY_FENCE __sync_synchronize ()
514 #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); })
515 #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); })
516 #elif _MSC_VER >= 1400 /* VC++ 2005 */
517 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
518 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
519 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
520 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
521 #elif defined(_WIN32)
522 #include <WinNT.h>
523 #define ECB_MEMORY_FENCE MemoryBarrier ()
524 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
525 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
526 #endif
527#endif
528 630
529#ifndef ECB_MEMORY_FENCE 631#define ECB_CONCAT_(a, b) a ## b
530 #include <pthread.h> 632#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
633#define ECB_STRINGIFY_(a) # a
634#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
531 635
532 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; 636#define ecb_function_ ecb_inline
533 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
534 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
535 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
536#endif
537 637
538#if ECB_GCC_VERSION(3,1) 638#if ECB_GCC_VERSION(3,1)
539 #define ecb_attribute(attrlist) __attribute__(attrlist) 639 #define ecb_attribute(attrlist) __attribute__(attrlist)
540 #define ecb_is_constant(expr) __builtin_constant_p (expr) 640 #define ecb_is_constant(expr) __builtin_constant_p (expr)
541 #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 641 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
545 #define ecb_is_constant(expr) 0 645 #define ecb_is_constant(expr) 0
546 #define ecb_expect(expr,value) (expr) 646 #define ecb_expect(expr,value) (expr)
547 #define ecb_prefetch(addr,rw,locality) 647 #define ecb_prefetch(addr,rw,locality)
548#endif 648#endif
549 649
650/* no emulation for ecb_decltype */
651#if ECB_GCC_VERSION(4,5)
652 #define ecb_decltype(x) __decltype(x)
653#elif ECB_GCC_VERSION(3,0)
654 #define ecb_decltype(x) __typeof(x)
655#endif
656
550#define ecb_noinline ecb_attribute ((__noinline__)) 657#define ecb_noinline ecb_attribute ((__noinline__))
551#define ecb_noreturn ecb_attribute ((__noreturn__)) 658#define ecb_noreturn ecb_attribute ((__noreturn__))
552#define ecb_unused ecb_attribute ((__unused__)) 659#define ecb_unused ecb_attribute ((__unused__))
553#define ecb_const ecb_attribute ((__const__)) 660#define ecb_const ecb_attribute ((__const__))
554#define ecb_pure ecb_attribute ((__pure__)) 661#define ecb_pure ecb_attribute ((__pure__))
566/* put around conditional expressions if you are very sure that the */ 673/* put around conditional expressions if you are very sure that the */
567/* expression is mostly true or mostly false. note that these return */ 674/* expression is mostly true or mostly false. note that these return */
568/* booleans, not the expression. */ 675/* booleans, not the expression. */
569#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) 676#define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
570#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) 677#define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
571/* ecb.h end */ 678/* for compatibility to the rest of the world */
679#define ecb_likely(expr) ecb_expect_true (expr)
680#define ecb_unlikely(expr) ecb_expect_false (expr)
681
682/* count trailing zero bits and count # of one bits */
683#if ECB_GCC_VERSION(3,4)
684 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
685 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
686 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
687 #define ecb_ctz32(x) __builtin_ctz (x)
688 #define ecb_ctz64(x) __builtin_ctzll (x)
689 #define ecb_popcount32(x) __builtin_popcount (x)
690 /* no popcountll */
691#else
692 ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
693 ecb_function_ int
694 ecb_ctz32 (uint32_t x)
695 {
696 int r = 0;
697
698 x &= ~x + 1; /* this isolates the lowest bit */
699
700#if ECB_branchless_on_i386
701 r += !!(x & 0xaaaaaaaa) << 0;
702 r += !!(x & 0xcccccccc) << 1;
703 r += !!(x & 0xf0f0f0f0) << 2;
704 r += !!(x & 0xff00ff00) << 3;
705 r += !!(x & 0xffff0000) << 4;
706#else
707 if (x & 0xaaaaaaaa) r += 1;
708 if (x & 0xcccccccc) r += 2;
709 if (x & 0xf0f0f0f0) r += 4;
710 if (x & 0xff00ff00) r += 8;
711 if (x & 0xffff0000) r += 16;
712#endif
713
714 return r;
715 }
716
717 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
718 ecb_function_ int
719 ecb_ctz64 (uint64_t x)
720 {
721 int shift = x & 0xffffffffU ? 0 : 32;
722 return ecb_ctz32 (x >> shift) + shift;
723 }
724
725 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
726 ecb_function_ int
727 ecb_popcount32 (uint32_t x)
728 {
729 x -= (x >> 1) & 0x55555555;
730 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
731 x = ((x >> 4) + x) & 0x0f0f0f0f;
732 x *= 0x01010101;
733
734 return x >> 24;
735 }
736
737 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
738 ecb_function_ int ecb_ld32 (uint32_t x)
739 {
740 int r = 0;
741
742 if (x >> 16) { x >>= 16; r += 16; }
743 if (x >> 8) { x >>= 8; r += 8; }
744 if (x >> 4) { x >>= 4; r += 4; }
745 if (x >> 2) { x >>= 2; r += 2; }
746 if (x >> 1) { r += 1; }
747
748 return r;
749 }
750
751 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
752 ecb_function_ int ecb_ld64 (uint64_t x)
753 {
754 int r = 0;
755
756 if (x >> 32) { x >>= 32; r += 32; }
757
758 return r + ecb_ld32 (x);
759 }
760#endif
761
762/* popcount64 is only available on 64 bit cpus as gcc builtin */
763/* so for this version we are lazy */
764ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
765ecb_function_ int
766ecb_popcount64 (uint64_t x)
767{
768 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
769}
770
771ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
772ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
773ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
774ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
775ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
776ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
777ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
778ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
779
780ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
781ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
782ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
783ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
784ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
785ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
786ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
787ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
788
789#if ECB_GCC_VERSION(4,3)
790 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
791 #define ecb_bswap32(x) __builtin_bswap32 (x)
792 #define ecb_bswap64(x) __builtin_bswap64 (x)
793#else
794 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
795 ecb_function_ uint16_t
796 ecb_bswap16 (uint16_t x)
797 {
798 return ecb_rotl16 (x, 8);
799 }
800
801 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
802 ecb_function_ uint32_t
803 ecb_bswap32 (uint32_t x)
804 {
805 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
806 }
807
808 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
809 ecb_function_ uint64_t
810 ecb_bswap64 (uint64_t x)
811 {
812 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
813 }
814#endif
815
816#if ECB_GCC_VERSION(4,5)
817 #define ecb_unreachable() __builtin_unreachable ()
818#else
819 /* this seems to work fine, but gcc always emits a warning for it :/ */
820 ecb_function_ void ecb_unreachable (void) ecb_noreturn;
821 ecb_function_ void ecb_unreachable (void) { }
822#endif
823
824/* try to tell the compiler that some condition is definitely true */
825#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
826
827ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const;
828ecb_function_ unsigned char
829ecb_byteorder_helper (void)
830{
831 const uint32_t u = 0x11223344;
832 return *(unsigned char *)&u;
833}
834
835ecb_function_ ecb_bool ecb_big_endian (void) ecb_const;
836ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
837ecb_function_ ecb_bool ecb_little_endian (void) ecb_const;
838ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
839
840#if ECB_GCC_VERSION(3,0) || ECB_C99
841 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
842#else
843 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
844#endif
845
846#if __cplusplus
847 template<typename T>
848 static inline T ecb_div_rd (T val, T div)
849 {
850 return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
851 }
852 template<typename T>
853 static inline T ecb_div_ru (T val, T div)
854 {
855 return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
856 }
857#else
858 #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
859 #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
860#endif
861
862#if ecb_cplusplus_does_not_suck
863 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
864 template<typename T, int N>
865 static inline int ecb_array_length (const T (&arr)[N])
866 {
867 return N;
868 }
869#else
870 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
871#endif
872
873#endif
874
875/* ECB.H END */
876
877#if ECB_MEMORY_FENCE_NEEDS_PTHREADS
878/* if your architecture doesn't need memory fences, e.g. because it is
879 * single-cpu/core, or if you use libev in a project that doesn't use libev
880 * from multiple threads, then you can define ECB_AVOID_PTHREADS when compiling
881 * libev, in which casess the memory fences become nops.
882 * alternatively, you can remove this #error and link against libpthread,
883 * which will then provide the memory fences.
884 */
885# error "memory fences not defined for your architecture, please report"
886#endif
887
888#ifndef ECB_MEMORY_FENCE
889# define ECB_MEMORY_FENCE do { } while (0)
890# define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
891# define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
892#endif
572 893
573#define expect_false(cond) ecb_expect_false (cond) 894#define expect_false(cond) ecb_expect_false (cond)
574#define expect_true(cond) ecb_expect_true (cond) 895#define expect_true(cond) ecb_expect_true (cond)
575#define noinline ecb_noinline 896#define noinline ecb_noinline
576 897
872 #undef VAR 1193 #undef VAR
873 }; 1194 };
874 #include "ev_wrap.h" 1195 #include "ev_wrap.h"
875 1196
876 static struct ev_loop default_loop_struct; 1197 static struct ev_loop default_loop_struct;
877 struct ev_loop *ev_default_loop_ptr; 1198 EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a defintiino despite extern */
878 1199
879#else 1200#else
880 1201
881 ev_tstamp ev_rt_now; 1202 EV_API_DECL ev_tstamp ev_rt_now = 0; /* needs to be initialised to make it a defintiino despite extern */
882 #define VAR(name,decl) static decl; 1203 #define VAR(name,decl) static decl;
883 #include "ev_vars.h" 1204 #include "ev_vars.h"
884 #undef VAR 1205 #undef VAR
885 1206
886 static int ev_default_loop_ptr; 1207 static int ev_default_loop_ptr;
980 1301
981 do 1302 do
982 ncur <<= 1; 1303 ncur <<= 1;
983 while (cnt > ncur); 1304 while (cnt > ncur);
984 1305
985 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ 1306 /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
986 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) 1307 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
987 { 1308 {
988 ncur *= elem; 1309 ncur *= elem;
989 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); 1310 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
990 ncur = ncur - sizeof (void *) * 4; 1311 ncur = ncur - sizeof (void *) * 4;
1476 1797
1477 if (pipe_write_wanted) 1798 if (pipe_write_wanted)
1478 { 1799 {
1479 int old_errno; 1800 int old_errno;
1480 1801
1481 pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ 1802 pipe_write_skipped = 0; /* just an optimisation, no fence needed */
1482 1803
1483 old_errno = errno; /* save errno because write will clobber it */ 1804 old_errno = errno; /* save errno because write will clobber it */
1484 1805
1485#if EV_USE_EVENTFD 1806#if EV_USE_EVENTFD
1486 if (evfd >= 0) 1807 if (evfd >= 0)
4030 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); 4351 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
4031#endif 4352#endif
4032 4353
4033#if EV_IDLE_ENABLE 4354#if EV_IDLE_ENABLE
4034 if (types & EV_IDLE) 4355 if (types & EV_IDLE)
4035 for (j = NUMPRI; i--; ) 4356 for (j = NUMPRI; j--; )
4036 for (i = idlecnt [j]; i--; ) 4357 for (i = idlecnt [j]; i--; )
4037 cb (EV_A_ EV_IDLE, idles [j][i]); 4358 cb (EV_A_ EV_IDLE, idles [j][i]);
4038#endif 4359#endif
4039 4360
4040#if EV_FORK_ENABLE 4361#if EV_FORK_ENABLE
4093 4414
4094#if EV_MULTIPLICITY 4415#if EV_MULTIPLICITY
4095 #include "ev_wrap.h" 4416 #include "ev_wrap.h"
4096#endif 4417#endif
4097 4418
4098EV_CPP(})
4099

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines