2017-01-11 10:06:31 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_UTIL_INLINES_H
|
|
|
|
#define JEMALLOC_INTERNAL_UTIL_INLINES_H
|
2012-03-07 06:57:45 +08:00
|
|
|
|
|
|
|
#ifndef JEMALLOC_ENABLE_INLINE
|
2016-02-25 02:32:45 +08:00
|
|
|
unsigned ffs_llu(unsigned long long bitmap);
|
|
|
|
unsigned ffs_lu(unsigned long bitmap);
|
|
|
|
unsigned ffs_u(unsigned bitmap);
|
|
|
|
unsigned ffs_zu(size_t bitmap);
|
|
|
|
unsigned ffs_u64(uint64_t bitmap);
|
|
|
|
unsigned ffs_u32(uint32_t bitmap);
|
2016-02-10 08:28:40 +08:00
|
|
|
uint64_t pow2_ceil_u64(uint64_t x);
|
|
|
|
uint32_t pow2_ceil_u32(uint32_t x);
|
|
|
|
size_t pow2_ceil_zu(size_t x);
|
2016-02-25 03:04:51 +08:00
|
|
|
unsigned lg_floor(size_t x);
|
2012-04-30 18:38:26 +08:00
|
|
|
void set_errno(int errnum);
|
|
|
|
int get_errno(void);
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
|
2014-05-29 10:37:02 +08:00
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Sanity check. */
|
2016-02-21 15:41:33 +08:00
|
|
|
#if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \
|
|
|
|
|| !defined(JEMALLOC_INTERNAL_FFS)
|
|
|
|
# error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure
|
2014-05-29 10:37:02 +08:00
|
|
|
#endif
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_llu(unsigned long long bitmap) {
|
2016-02-21 15:41:33 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFSLL(bitmap));
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_lu(unsigned long bitmap) {
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFSL(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_u(unsigned bitmap) {
|
2015-02-05 08:41:55 +08:00
|
|
|
return (JEMALLOC_INTERNAL_FFS(bitmap));
|
2014-05-29 10:37:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:32:45 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_zu(size_t bitmap) {
|
2016-02-25 06:01:47 +08:00
|
|
|
#if LG_SIZEOF_PTR == LG_SIZEOF_INT
|
2016-02-25 02:32:45 +08:00
|
|
|
return (ffs_u(bitmap));
|
2016-02-25 06:01:47 +08:00
|
|
|
#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG
|
|
|
|
return (ffs_lu(bitmap));
|
|
|
|
#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG
|
|
|
|
return (ffs_llu(bitmap));
|
2016-02-25 02:32:45 +08:00
|
|
|
#else
|
|
|
|
#error No implementation for size_t ffs()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_u64(uint64_t bitmap) {
|
2016-02-25 02:32:45 +08:00
|
|
|
#if LG_SIZEOF_LONG == 3
|
|
|
|
return (ffs_lu(bitmap));
|
|
|
|
#elif LG_SIZEOF_LONG_LONG == 3
|
|
|
|
return (ffs_llu(bitmap));
|
|
|
|
#else
|
|
|
|
#error No implementation for 64-bit ffs()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
ffs_u32(uint32_t bitmap) {
|
2016-02-25 02:32:45 +08:00
|
|
|
#if LG_SIZEOF_INT == 2
|
|
|
|
return (ffs_u(bitmap));
|
|
|
|
#else
|
|
|
|
#error No implementation for 32-bit ffs()
|
|
|
|
#endif
|
|
|
|
return (ffs_u(bitmap));
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
JEMALLOC_INLINE uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
pow2_ceil_u64(uint64_t x) {
|
2012-03-07 06:57:45 +08:00
|
|
|
x--;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
x |= x >> 32;
|
|
|
|
x++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
2016-02-10 08:28:40 +08:00
|
|
|
JEMALLOC_INLINE uint32_t
|
2017-01-16 08:56:30 +08:00
|
|
|
pow2_ceil_u32(uint32_t x) {
|
2016-02-10 08:28:40 +08:00
|
|
|
x--;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
x++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the smallest power of 2 that is >= x. */
|
|
|
|
JEMALLOC_INLINE size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
pow2_ceil_zu(size_t x) {
|
2016-02-10 08:28:40 +08:00
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
|
|
|
return (pow2_ceil_u64(x));
|
|
|
|
#else
|
|
|
|
return (pow2_ceil_u32(x));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
#if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
lg_floor(size_t x) {
|
2014-05-29 07:11:55 +08:00
|
|
|
size_t ret;
|
|
|
|
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
asm ("bsr %1, %0"
|
|
|
|
: "=r"(ret) // Outputs.
|
|
|
|
: "r"(x) // Inputs.
|
|
|
|
);
|
2016-02-25 03:04:51 +08:00
|
|
|
assert(ret < UINT_MAX);
|
|
|
|
return ((unsigned)ret);
|
2014-05-29 07:11:55 +08:00
|
|
|
}
|
2014-09-22 22:54:33 +08:00
|
|
|
#elif (defined(_MSC_VER))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
lg_floor(size_t x) {
|
2015-02-05 08:41:55 +08:00
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
assert(x != 0);
|
2014-09-22 22:54:33 +08:00
|
|
|
|
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
2015-02-05 08:41:55 +08:00
|
|
|
_BitScanReverse64(&ret, x);
|
2014-09-22 22:54:33 +08:00
|
|
|
#elif (LG_SIZEOF_PTR == 2)
|
2015-02-05 08:41:55 +08:00
|
|
|
_BitScanReverse(&ret, x);
|
2014-09-22 22:54:33 +08:00
|
|
|
#else
|
2016-02-25 02:32:45 +08:00
|
|
|
# error "Unsupported type size for lg_floor()"
|
2014-09-22 22:54:33 +08:00
|
|
|
#endif
|
2016-02-25 03:04:51 +08:00
|
|
|
assert(ret < UINT_MAX);
|
|
|
|
return ((unsigned)ret);
|
2014-09-22 22:54:33 +08:00
|
|
|
}
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
lg_floor(size_t x) {
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-05-29 07:11:55 +08:00
|
|
|
#if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
|
2014-06-02 13:05:08 +08:00
|
|
|
return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x));
|
2014-05-29 07:11:55 +08:00
|
|
|
#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
|
2014-06-02 13:05:08 +08:00
|
|
|
return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x));
|
2014-05-29 07:11:55 +08:00
|
|
|
#else
|
2016-02-25 02:32:45 +08:00
|
|
|
# error "Unsupported type size for lg_floor()"
|
2014-05-29 07:11:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2016-02-25 03:04:51 +08:00
|
|
|
JEMALLOC_INLINE unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
lg_floor(size_t x) {
|
2015-02-05 08:41:55 +08:00
|
|
|
assert(x != 0);
|
|
|
|
|
2014-06-02 13:05:08 +08:00
|
|
|
x |= (x >> 1);
|
|
|
|
x |= (x >> 2);
|
|
|
|
x |= (x >> 4);
|
|
|
|
x |= (x >> 8);
|
|
|
|
x |= (x >> 16);
|
2016-02-25 02:32:45 +08:00
|
|
|
#if (LG_SIZEOF_PTR == 3)
|
2014-06-02 13:05:08 +08:00
|
|
|
x |= (x >> 32);
|
2014-05-29 07:11:55 +08:00
|
|
|
#endif
|
2017-01-16 08:56:30 +08:00
|
|
|
if (x == SIZE_T_MAX) {
|
2016-02-25 02:32:45 +08:00
|
|
|
return ((8 << LG_SIZEOF_PTR) - 1);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-02-25 02:32:45 +08:00
|
|
|
x++;
|
|
|
|
return (ffs_zu(x) - 2);
|
2014-05-29 07:11:55 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Set error code. */
|
2012-04-30 18:38:26 +08:00
|
|
|
JEMALLOC_INLINE void
|
2017-01-16 08:56:30 +08:00
|
|
|
set_errno(int errnum) {
|
2012-04-30 18:38:26 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
SetLastError(errnum);
|
|
|
|
#else
|
|
|
|
errno = errnum;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-09 06:40:14 +08:00
|
|
|
/* Get last error code. */
|
2012-04-30 18:38:26 +08:00
|
|
|
JEMALLOC_INLINE int
|
2017-01-16 08:56:30 +08:00
|
|
|
get_errno(void) {
|
2012-04-30 18:38:26 +08:00
|
|
|
#ifdef _WIN32
|
2012-04-30 18:38:27 +08:00
|
|
|
return (GetLastError());
|
2012-04-30 18:38:26 +08:00
|
|
|
#else
|
2012-04-30 18:38:27 +08:00
|
|
|
return (errno);
|
2012-04-30 18:38:26 +08:00
|
|
|
#endif
|
|
|
|
}
|
2012-03-07 06:57:45 +08:00
|
|
|
#endif
|
|
|
|
|
2017-01-11 10:06:31 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_UTIL_INLINES_H */
|