diff options
| author | Mattias Andrée <maandree@kth.se> | 2016-03-13 23:54:56 +0100 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2016-03-13 23:54:56 +0100 |
| commit | 5b10b24044b3350a19ab3d3c0b37b5e9c12365b1 (patch) | |
| tree | 3f6aa8de29becc5d7511fcca50e6d31982c1b64b /src/allocator.c | |
| parent | Optimisations (diff) | |
| download | libzahl-5b10b24044b3350a19ab3d3c0b37b5e9c12365b1.tar.gz libzahl-5b10b24044b3350a19ab3d3c0b37b5e9c12365b1.tar.bz2 libzahl-5b10b24044b3350a19ab3d3c0b37b5e9c12365b1.tar.xz | |
Multiple changes:
1) Compile test with -O0, it takes too long otherwise.
2) Add error codes: ZERROR_0_POW_0, ZERROR_0_DIV_0, ZERROR_DIV_0, ZERROR_NEGATIVE.
3) Add workaround for a bug in clang (src/allocator.c).
4) Cleanups.
5) Minor optimisations.
6) Add inclusion guard for zahl.h.
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/allocator.c')
| -rw-r--r-- | src/allocator.c | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/src/allocator.c b/src/allocator.c index 33c8677..41ce9ed 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -1,27 +1,47 @@ /* See LICENSE file for copyright and license details. */ #include "internals.h" +#include <stdio.h> + + +/* Find r such that r is a minimal power of 2 ≥ n. */ +#define NEXT_2POW(n)\ + do if (likely((n & (~n + 1)) != n)) {\ + size_t x;\ + n |= n >> 1;\ + n |= n >> 2;\ + n |= n >> 4;\ + for (i = sizeof(n), x = 8; i; i >>= 1, x <<= 1)\ + n |= n >> x;\ + n += 1;\ + } while (0) + + +#if defined(__clang__) +static inline __attribute__((optnone)) size_t +clang_warkaround_next_2pow(size_t need) +{ + size_t i; + NEXT_2POW(need); + return need; +} +#endif void libzahl_realloc(z_t a, size_t need) { - size_t i, x; + size_t i; zahl_char_t *new; - /* Find n such that n is a minimal power of 2 ≥ need. */ - if ((need & (~need + 1)) != need) { - need |= need >> 1; - need |= need >> 2; - need |= need >> 4; - for (i = sizeof(need), x = 8; i; i >>= 1, x <<= 1) - need |= need >> x; - need += 1; - } +#if defined(__clang__) + need = clang_warkaround_next_2pow(need); +#else + NEXT_2POW(need); +#endif - for (i = 0, x = need; x; x >>= 1) - i += 1; + i = libzahl_msb_nz_zu(need); - if (libzahl_pool_n[i]) { + if (likely(libzahl_pool_n[i])) { libzahl_pool_n[i]--; new = libzahl_pool[i][libzahl_pool_n[i]]; zmemcpy(new, a->chars, a->alloced); @@ -32,7 +52,7 @@ libzahl_realloc(z_t a, size_t need) if (!a->chars) { if (!errno) /* sigh... */ errno = ENOMEM; - FAILURE(errno); + libzahl_failure(errno); } } a->alloced = need; |
