diff options
| author | Jason Evans <jasone@canonware.com> | 2015-02-05 00:41:55 (GMT) |
|---|---|---|
| committer | Jason Evans <jasone@canonware.com> | 2015-02-05 00:51:53 (GMT) |
| commit | c810fcea1fa7983ef5bcabe6556cdc19dde6dd8d (patch) | |
| tree | 2c81f384fe80658347a053d8496bcfd021e24165 /include | |
| parent | f500a10b2e94852b867334703ad77467dcfd2ddd (diff) | |
| download | jemalloc-c810fcea1fa7983ef5bcabe6556cdc19dde6dd8d.zip jemalloc-c810fcea1fa7983ef5bcabe6556cdc19dde6dd8d.tar.gz jemalloc-c810fcea1fa7983ef5bcabe6556cdc19dde6dd8d.tar.bz2 | |
Add (x != 0) assertion to lg_floor(x).
lg_floor(0) is undefined, but depending on compiler options may not
cause a crash. This assertion makes it harder to accidentally abuse
lg_floor().
Diffstat (limited to 'include')
| -rw-r--r-- | include/jemalloc/internal/util.h | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/include/jemalloc/internal/util.h b/include/jemalloc/internal/util.h index b2b4ab7..5ad4933 100644 --- a/include/jemalloc/internal/util.h +++ b/include/jemalloc/internal/util.h @@ -136,14 +136,14 @@ JEMALLOC_ALWAYS_INLINE int jemalloc_ffsl(long bitmap) { - return (JEMALLOC_INTERNAL_FFSL(bitmap)); + return (JEMALLOC_INTERNAL_FFSL(bitmap)); } JEMALLOC_ALWAYS_INLINE int jemalloc_ffs(int bitmap) { - return (JEMALLOC_INTERNAL_FFS(bitmap)); + return (JEMALLOC_INTERNAL_FFS(bitmap)); } /* Compute the smallest power of 2 that is >= x. */ @@ -170,6 +170,8 @@ lg_floor(size_t x) { size_t ret; + assert(x != 0); + asm ("bsr %1, %0" : "=r"(ret) // Outputs. : "r"(x) // Inputs. @@ -180,22 +182,26 @@ lg_floor(size_t x) JEMALLOC_INLINE size_t lg_floor(size_t x) { - unsigned long ret; + unsigned long ret; + + assert(x != 0); #if (LG_SIZEOF_PTR == 3) - _BitScanReverse64(&ret, x); + _BitScanReverse64(&ret, x); #elif (LG_SIZEOF_PTR == 2) - _BitScanReverse(&ret, x); + _BitScanReverse(&ret, x); #else # error "Unsupported type sizes for lg_floor()" #endif - return (ret); + return (ret); } #elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ)) JEMALLOC_INLINE size_t lg_floor(size_t x) { + assert(x != 0); + #if (LG_SIZEOF_PTR == LG_SIZEOF_INT) return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x)); #elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG) @@ -209,6 +215,8 @@ JEMALLOC_INLINE size_t lg_floor(size_t x) { + assert(x != 0); + x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); |
