diff --git a/GNUmakefile b/GNUmakefile index 6225b6d..8ab8a06 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -60,7 +60,7 @@ $(DNS_OBJS): dns.h stralloc.h taia.h tai.h uint64.h iopause.h $(CASE_OBJS): case.h $(ARRAY_OBJS): uint64.h array.h $(MULT_OBJS): uint64.h uint32.h uint16.h safemult.h -$(IO_OBJS): uint64.h array.h io.h +$(IO_OBJS): uint64.h array.h io.h io_internal.h taia.h tai.h iopause.o: select.h openreadclose.o readclose.o: readclose.h diff --git a/array/array_allocate.c b/array/array_allocate.c index 21c4d88..0c25e67 100644 --- a/array/array_allocate.c +++ b/array/array_allocate.c @@ -54,7 +54,7 @@ void* array_allocate(array* x,uint64 membersize,int64 pos) { if (__unlikely(x->allocated<0)) return 0; /* array is failed */ if (__unlikely(pos+1<1)) return 0; /* second case of overflow: pos*membersize too large */ - if (__unlikely(umult64(membersize,pos+1,&wanted))) return 0; + if (__unlikely(!umult64(membersize,pos+1,&wanted))) return 0; if (__unlikely(wanted > x->allocated)) { /* round up a little */ diff --git a/array/array_get.c b/array/array_get.c index 2fe14e8..e10195b 100644 --- a/array/array_get.c +++ b/array/array_get.c @@ -23,7 +23,7 @@ void* array_get(array* x,uint64 membersize,int64 pos) { uint64 wanted; if (__unlikely(pos+1<1)) return 0; - if (__unlikely(umult64(membersize,pos,&wanted))) return 0; + if (__unlikely(!umult64(membersize,pos,&wanted))) return 0; if (__unlikely(wanted > x->allocated)) return 0; return x->p+pos*membersize; diff --git a/array/array_truncate.c b/array/array_truncate.c index 71624da..512a5cf 100644 --- a/array/array_truncate.c +++ b/array/array_truncate.c @@ -11,7 +11,7 @@ void array_truncate(array* x,uint64 membersize,int64 len) { uint64 wanted; if (__unlikely(len<0)) return; - if (__unlikely(umult64(membersize,len,&wanted))) return; + if (__unlikely(!umult64(membersize,len,&wanted))) return; if (__unlikely(wanted > x->initialized)) return; x->initialized=wanted; } diff --git a/io/io_wait.c b/io/io_wait.c index 7ea4d8c..9c0ff29 100644 --- a/io/io_wait.c +++ b/io/io_wait.c @@ -5,30 +5,5 @@ #include "io_internal.h" void io_wait() { - struct pollfd* p; - long i,r; - if (!io_wanted_fds) return; - for (i=r=0; icanread=e->canwrite=0; - if (e->wantread || e->wantwrite) { - struct pollfd* p; - if ((p=array_allocate(&io_pollfds,sizeof(struct pollfd),r))) { - p->fd=i; - p->events=(e->wantread?POLLIN:0) + (e->wantwrite?POLLOUT:0); - ++r; - } else - return; - } - } - p=array_start(&io_pollfds); - while ((i=poll(array_start(&io_pollfds),r,99999999))==0); - if (i==-1) return; - for (i=0; ifd); - if (p->revents&POLLIN) e->canread=1; - if (p->revents&POLLOUT) e->canwrite=1; - p++; - } + io_waituntil2(-1); } diff --git a/io_internal.h b/io_internal.h index 89babfc..eb206aa 100644 --- a/io_internal.h +++ b/io_internal.h @@ -14,3 +14,5 @@ typedef struct { array io_fds; uint64 io_wanted_fds; array io_pollfds; + +int64 io_waituntil2(int64 milliseconds); diff --git a/mult/imult16.c b/mult/imult16.c index 45bc8b9..3cff0c0 100644 --- a/mult/imult16.c +++ b/mult/imult16.c @@ -4,7 +4,7 @@ int imult16(int16 a,int16 b,int16* c) { int neg=(a<0); if (neg) a=-a; if (b<0) { neg^=1; b=-b; } - if (umult16(a,b,c)) return 1; + if (umult16(a,b,c)) return 0; if (neg) *c=-*c; - return 0; + return 1; } diff --git a/mult/imult32.c b/mult/imult32.c index 044fabb..27393dc 100644 --- a/mult/imult32.c +++ b/mult/imult32.c @@ -4,7 +4,7 @@ int imult32(int32 a,int32 b,int32* c) { int neg=(a<0); if (neg) a=-a; if (b<0) { neg^=1; b=-b; } - if (umult32(a,b,c)) return 1; + if (umult32(a,b,c)) return 0; if (neg) *c=-*c; - return 0; + return 1; } diff --git a/mult/imult64.c b/mult/imult64.c index c227aad..f51bbeb 100644 --- a/mult/imult64.c +++ b/mult/imult64.c @@ -4,8 +4,8 @@ int imult64(int64 a,int64 b,int64* c) { int neg=(a<0); if (neg) a=-a; if (b<0) { neg^=1; b=-b; } - if (umult64(a,b,c)) return 1; + if (umult64(a,b,c)) return 0; if (neg) *c=-*c; - return 0; + return 1; } diff --git a/mult/umult16.c b/mult/umult16.c index 879ffe5..8007f6e 100644 --- a/mult/umult16.c +++ b/mult/umult16.c @@ -2,7 +2,7 @@ int umult16(uint16 a,uint16 b,uint16* c) { unsigned long x=(unsigned long)a*b; - if (x>0xffff) return 1; + if (x>0xffff) return 0; *c=x&0xffff; - return 0; + return 1; } diff --git a/mult/umult32.c b/mult/umult32.c index ba04ed7..89ee9a6 100644 --- a/mult/umult32.c +++ b/mult/umult32.c @@ -2,7 +2,7 @@ int umult32(uint32 a,uint32 b,uint32* c) { unsigned long long x=(unsigned long long)a*b; - if (x>0xffffffff) return 1; + if (x>0xffffffff) return 0; *c=x&0xffffffff; - return 0; + return 1; } diff --git a/mult/umult64.c b/mult/umult64.c index a72d1ca..6d5e37a 100644 --- a/mult/umult64.c +++ b/mult/umult64.c @@ -12,11 +12,11 @@ int umult64(uint64 a,uint64 b,uint64* c) { // = ahi*x*bhi*x + ahi*x*blo + alo*bhi*x + alo*blo // -> overflow if ahi*bhi != zero */ - if (ahi && bhi) return 1; + if (ahi && bhi) return 0; a=(uint64)(ahi)*blo+(uint64)(alo)*bhi; - if (a>0xffffffff) return 1; + if (a>0xffffffff) return 0; *c=(a<<32)+(uint64)(alo)*blo; - return 0; + return 1; } diff --git a/safemult.h b/safemult.h index a1194a4..9ef14ec 100644 --- a/safemult.h +++ b/safemult.h @@ -5,7 +5,7 @@ #include "uint32.h" #include "uint64.h" -/* return 1 for overflow, 0 for ok */ +/* return 0 for overflow, 1 for ok */ int umult16(uint16 a,uint16 b,uint16* c); int imult16( int16 a, int16 b, int16* c); diff --git a/test/array.c b/test/array.c index c3217c3..1797f25 100644 --- a/test/array.c +++ b/test/array.c @@ -15,4 +15,5 @@ main() { assert(byte_equal(x.p,11,"fnordfoobar")); array_cate(&x,&y,1,4); assert(x.initialized=14 && byte_equal(x.p,14,"fnordfoobarnor")); + return 0; }