… | |
… | |
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 | |
188 | EV_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 |
… | |
… | |
541 | #if ECB_NO_THREADS || ECB_NO_SMP |
539 | #if ECB_NO_THREADS || ECB_NO_SMP |
542 | #define ECB_MEMORY_FENCE do { } while (0) |
540 | #define ECB_MEMORY_FENCE do { } while (0) |
543 | #endif |
541 | #endif |
544 | |
542 | |
545 | #ifndef ECB_MEMORY_FENCE |
543 | #ifndef ECB_MEMORY_FENCE |
546 | #if ECB_GCC_VERSION(2,5) |
544 | #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) |
547 | #if __x86 |
545 | #if __i386__ |
548 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
546 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
549 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
547 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ |
550 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
548 | #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ |
551 | #elif __amd64 |
549 | #elif __amd64 |
552 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
550 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
… | |
… | |
563 | #endif |
561 | #endif |
564 | #endif |
562 | #endif |
565 | #endif |
563 | #endif |
566 | |
564 | |
567 | #ifndef ECB_MEMORY_FENCE |
565 | #ifndef ECB_MEMORY_FENCE |
568 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) |
566 | #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) |
569 | #define ECB_MEMORY_FENCE __sync_synchronize () |
567 | #define ECB_MEMORY_FENCE __sync_synchronize () |
570 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
568 | /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ |
571 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
569 | /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ |
572 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
570 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
573 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
571 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
… | |
… | |
843 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
841 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
844 | #else |
842 | #else |
845 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
843 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
846 | #endif |
844 | #endif |
847 | |
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 | |
848 | #if ecb_cplusplus_does_not_suck |
862 | #if ecb_cplusplus_does_not_suck |
849 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
863 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
850 | template<typename T, int N> |
864 | template<typename T, int N> |
851 | static inline int ecb_array_length (const T (&arr)[N]) |
865 | static inline int ecb_array_length (const T (&arr)[N]) |
852 | { |
866 | { |
… | |
… | |
859 | #endif |
873 | #endif |
860 | |
874 | |
861 | /* ECB.H END */ |
875 | /* ECB.H END */ |
862 | |
876 | |
863 | #if ECB_MEMORY_FENCE_NEEDS_PTHREADS |
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 | |
864 | # undef ECB_MEMORY_FENCE |
888 | #ifndef ECB_MEMORY_FENCE |
865 | # undef ECB_MEMORY_FENCE_ACQUIRE |
889 | # define ECB_MEMORY_FENCE do { } while (0) |
866 | # undef ECB_MEMORY_FENCE_RELEASE |
890 | # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
891 | # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
867 | #endif |
892 | #endif |
868 | |
893 | |
869 | #define expect_false(cond) ecb_expect_false (cond) |
894 | #define expect_false(cond) ecb_expect_false (cond) |
870 | #define expect_true(cond) ecb_expect_true (cond) |
895 | #define expect_true(cond) ecb_expect_true (cond) |
871 | #define noinline ecb_noinline |
896 | #define noinline ecb_noinline |
… | |
… | |
1276 | |
1301 | |
1277 | do |
1302 | do |
1278 | ncur <<= 1; |
1303 | ncur <<= 1; |
1279 | while (cnt > ncur); |
1304 | while (cnt > ncur); |
1280 | |
1305 | |
1281 | /* 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 */ |
1282 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1307 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
1283 | { |
1308 | { |
1284 | ncur *= elem; |
1309 | ncur *= elem; |
1285 | 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); |
1286 | ncur = ncur - sizeof (void *) * 4; |
1311 | ncur = ncur - sizeof (void *) * 4; |
… | |
… | |
4389 | |
4414 | |
4390 | #if EV_MULTIPLICITY |
4415 | #if EV_MULTIPLICITY |
4391 | #include "ev_wrap.h" |
4416 | #include "ev_wrap.h" |
4392 | #endif |
4417 | #endif |
4393 | |
4418 | |
4394 | EV_CPP(}) |
|
|
4395 | |
|
|