commit e746f06265981fd17bc656ca59e0ed82f6cea84d
Author:     Mattias Andrée <[email protected]>
AuthorDate: Wed Apr 27 17:19:53 2016 +0200
Commit:     Mattias Andrée <[email protected]>
CommitDate: Wed Apr 27 17:19:53 2016 +0200

    Add option UNSAFE which disables all internal error checks
    
    Signed-off-by: Mattias Andrée <[email protected]>

diff --git a/src/allocator.c b/src/allocator.c
index 10170ae..1a2d505 100644
--- a/src/allocator.c
+++ b/src/allocator.c
@@ -22,7 +22,7 @@ libzahl_realloc(z_t a, size_t need)
                a->chars = new;
        } else {
                a->chars = realloc(a->chars, new_size * sizeof(zahl_char_t));
-               if (unlikely(!a->chars))
+               if (check(unlikely(!a->chars)))
                        libzahl_memfailure();
        }
        a->alloced = new_size;
diff --git a/src/internals.h b/src/internals.h
index 2459f04..c59b792 100644
--- a/src/internals.h
+++ b/src/internals.h
@@ -97,6 +97,12 @@ extern void *libzahl_temp_allocation;
 #define likely(expr)                 ZAHL_LIKELY(expr)
 #define unlikely(expr)               ZAHL_UNLIKELY(expr)
 
+#if defined(UNSAFE)
+# define check(expr)                 0
+#else
+# define check(expr)                 (expr)
+#endif
+
 #define SET_SIGNUM(a, signum)        ZAHL_SET_SIGNUM(a, signum)
 #define SET(a, b)                    ZAHL_SET(a, b)
 #define ENSURE_SIZE(a, n)            do { if ((a)->alloced < (n)) 
libzahl_realloc(a, (n)); } while (0)
@@ -331,7 +337,7 @@ zinit_temp(z_t a)
                size_t n = (size_t)(libzahl_temp_stack_end - 
libzahl_temp_stack);
                void* old = libzahl_temp_stack;
                libzahl_temp_stack = realloc(old, 2 * n * 
sizeof(*libzahl_temp_stack));
-               if (unlikely(!libzahl_temp_stack)) {
+               if (check(unlikely(!libzahl_temp_stack))) {
                        libzahl_temp_stack = old;
                        libzahl_memfailure();
                }
diff --git a/src/zdivmod.c b/src/zdivmod.c
index d907450..9cf3611 100644
--- a/src/zdivmod.c
+++ b/src/zdivmod.c
@@ -72,9 +72,9 @@ zdivmod(z_t a, z_t b, z_t c, z_t d)
        sign = zsignum(c) * zsignum(d);
 
        if (unlikely(!sign)) {
-               if (unlikely(!zzero(c))) {
+               if (check(unlikely(!zzero(c)))) {
                        libzahl_failure(-ZERROR_DIV_0);
-               } else if (unlikely(zzero(d))) {
+               } else if (check(unlikely(zzero(d)))) {
                        libzahl_failure(-ZERROR_0_DIV_0);
                } else {
                        SET_SIGNUM(a, 0);
diff --git a/src/zfree.c b/src/zfree.c
index 5048048..cfd7667 100644
--- a/src/zfree.c
+++ b/src/zfree.c
@@ -17,7 +17,7 @@ zfree(z_t a)
        if (j == libzahl_pool_alloc[i]) {
                x = j ? ((j * 3) >> 1) : 128;
                new = realloc(libzahl_pool[i], x * sizeof(zahl_char_t *));
-               if (!new) {
+               if (check(!new)) {
                        free(a->chars);
                        free(libzahl_pool[i]);
                        libzahl_pool_n[i] = 0;
diff --git a/src/zmodpow.c b/src/zmodpow.c
index 9e002ff..34e687f 100644
--- a/src/zmodpow.c
+++ b/src/zmodpow.c
@@ -14,18 +14,18 @@ zmodpow(z_t a, z_t b, z_t c, z_t d)
 
        if (unlikely(zsignum(c) <= 0)) {
                if (zzero(c)) {
-                       if (zzero(b))
+                       if (check(zzero(b)))
                                libzahl_failure(-ZERROR_0_POW_0);
-                       else if (zzero(d))
+                       else if (check(zzero(d)))
                                libzahl_failure(-ZERROR_DIV_0);
                        zsetu(a, 1);
-               } else if (zzero1(b, d)) {
+               } else if (check(zzero1(b, d))) {
                        libzahl_failure(-ZERROR_DIV_0);
                } else {
                        SET_SIGNUM(a, 0);
                }
                return;
-       } else if (unlikely(zzero(d))) {
+       } else if (check(unlikely(zzero(d)))) {
                libzahl_failure(-ZERROR_DIV_0);
        } else if (unlikely(zzero(b))) {
                SET_SIGNUM(a, 0);
diff --git a/src/zmodpowu.c b/src/zmodpowu.c
index fd5e925..7bb4f2b 100644
--- a/src/zmodpowu.c
+++ b/src/zmodpowu.c
@@ -9,14 +9,14 @@ void
 zmodpowu(z_t a, z_t b, unsigned long long int c, z_t d)
 {
        if (unlikely(!c)) {
-               if (zzero(b))
+               if (check(zzero(b)))
                        libzahl_failure(-ZERROR_0_POW_0);
-               else if (zzero(d))
+               else if (check(zzero(d)))
                        libzahl_failure(-ZERROR_DIV_0);
                else
                        zsetu(a, 1);
                return;
-       } else if (unlikely(zzero(d))) {
+       } else if (check(unlikely(zzero(d)))) {
                libzahl_failure(-ZERROR_DIV_0);
        } else if (unlikely(zzero(b))) {
                SET_SIGNUM(a, 0);
diff --git a/src/zpow.c b/src/zpow.c
index f0cbcd9..af45eb9 100644
--- a/src/zpow.c
+++ b/src/zpow.c
@@ -20,10 +20,10 @@ zpow(z_t a, z_t b, z_t c)
 
        if (unlikely(zsignum(c) <= 0)) {
                if (zzero(c)) {
-                       if (zzero(b))
+                       if (check(zzero(b)))
                                libzahl_failure(-ZERROR_0_POW_0);
                        zsetu(a, 1);
-               } else if (zzero(b)) {
+               } else if (check(zzero(b))) {
                        libzahl_failure(-ZERROR_DIV_0);
                } else {
                        SET_SIGNUM(a, 0);
diff --git a/src/zpowu.c b/src/zpowu.c
index 3d58aa4..447147d 100644
--- a/src/zpowu.c
+++ b/src/zpowu.c
@@ -10,7 +10,7 @@ zpowu(z_t a, z_t b, unsigned long long int c)
        int neg;
 
        if (unlikely(!c)) {
-               if (zzero(b))
+               if (check(zzero(b)))
                        libzahl_failure(-ZERROR_0_POW_0);
                zsetu(a, 1);
                return;
diff --git a/src/zrand.c b/src/zrand.c
index f1913e1..079520e 100644
--- a/src/zrand.c
+++ b/src/zrand.c
@@ -104,7 +104,7 @@ zrand_fd(void *out, size_t n, void *statep)
 
        while (n) {
                read_just = read(fd, buf + read_total, n);
-               if (unlikely(read_just < 0))
+               if (check(unlikely(read_just < 0)))
                        libzahl_failure(errno);
                read_total += (size_t)read_just;
                n -= (size_t)read_just;
@@ -141,7 +141,7 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n)
 {
 #define RANDOM_UNIFORM(RETRY)\
        do {\
-               if (unlikely(znegative(n)))\
+               if (check(unlikely(znegative(n))))\
                        libzahl_failure(-ZERROR_NEGATIVE);\
                bits = zbits(n);\
                do\
@@ -185,7 +185,7 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n)
 
        if (pathname) {
                fd = open(pathname, O_RDONLY);
-               if (unlikely(fd < 0))
+               if (check(unlikely(fd < 0)))
                        libzahl_failure(errno);
                statep = &fd;
        }
@@ -209,7 +209,10 @@ zrand(z_t r, enum zranddev dev, enum zranddist dist, z_t n)
                break;
 
        default:
+#if !defined(UNSAFE)
                libzahl_failure(EINVAL);
+#endif
+               break;
        }
 
        if (fd >= 0)
diff --git a/src/zsets.c b/src/zsets.c
index e1506f6..1701c6b 100644
--- a/src/zsets.c
+++ b/src/zsets.c
@@ -17,12 +17,12 @@ zsets(z_t a, const char *str)
 
        str += neg || (*str == '+');
 
-       if (unlikely(!*str)) {
+       if (check(unlikely(!*str))) {
                errno = EINVAL;
                return -1;
        }
        for (str_end = str; *str_end; str_end++) {
-               if (unlikely(!isdigit(*str_end))) {
+               if (check(unlikely(!isdigit(*str_end)))) {
                        errno = EINVAL;
                        return -1;
                }
diff --git a/src/zsetup.c b/src/zsetup.c
index aebef32..df8c0c2 100644
--- a/src/zsetup.c
+++ b/src/zsetup.c
@@ -46,7 +46,7 @@ zsetup(jmp_buf env)
                        zinit(libzahl_tmp_divmod_ds[i]);
 
                libzahl_temp_stack = malloc(256 * sizeof(*libzahl_temp_stack));
-               if (unlikely(!libzahl_temp_stack))
+               if (check(unlikely(!libzahl_temp_stack)))
                        libzahl_memfailure();
                libzahl_temp_stack_head = libzahl_temp_stack;
                libzahl_temp_stack_end = libzahl_temp_stack + 256;
diff --git a/test.c b/test.c
index 5b7642a..ffd9066 100644
--- a/test.c
+++ b/test.c
@@ -787,6 +787,9 @@ main(void)
        assert((zseti(a, 11), zptest(0, a, 100)), != NONPRIME);
        assert((zseti(a, 101), zptest(0, a, 100)), != NONPRIME);
 
+#if defined(UNSAFE)
+       (void) env2;
+#else
        assert_nr(zdivmod(a, b, _0, _0));
        assert_nr(zdivmod(a, b, _1, _0));
        zdivmod(a, b, _0, _1);
@@ -828,6 +831,7 @@ main(void)
        assert_nr(zmodpowu(a, _0, 1, _0));
        assert_nr(zmodpowu(a, _1, 0, _0));
        assert_nr((zneg(_1, _1), zmodpowu(a, _1, 0, _0))); zneg(_1, _1);
+#endif
 
        zsetu(a, 1LL);
        assert_s(zstr(a, buf, 1), "1");

Reply via email to