]>
git.proxmox.com Git - rustc.git/blob - src/jemalloc/include/jemalloc/internal/util.h
1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
6 # define FMT64_PREFIX "ll"
7 # define FMTPTR_PREFIX "ll"
9 # define FMT64_PREFIX "ll"
10 # define FMTPTR_PREFIX ""
15 # define FMTd64 FMT64_PREFIX "d"
16 # define FMTu64 FMT64_PREFIX "u"
17 # define FMTx64 FMT64_PREFIX "x"
18 # define FMTdPTR FMTPTR_PREFIX "d"
19 # define FMTuPTR FMTPTR_PREFIX "u"
20 # define FMTxPTR FMTPTR_PREFIX "x"
22 # include <inttypes.h>
23 # define FMTd32 PRId32
24 # define FMTu32 PRIu32
25 # define FMTx32 PRIx32
26 # define FMTd64 PRId64
27 # define FMTu64 PRIu64
28 # define FMTx64 PRIx64
29 # define FMTdPTR PRIdPTR
30 # define FMTuPTR PRIuPTR
31 # define FMTxPTR PRIxPTR
34 /* Size of stack-allocated buffer passed to buferror(). */
35 #define BUFERROR_BUF 64
38 * Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
39 * large enough for all possible uses within jemalloc.
41 #define MALLOC_PRINTF_BUFSIZE 4096
44 * Wrap a cpp argument that contains commas such that it isn't broken up into
47 #define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__
50 * Silence compiler warnings due to uninitialized values. This is used
51 * wherever the compiler fails to recognize that the variable is never used
54 #ifdef JEMALLOC_CC_SILENCE
55 # define JEMALLOC_CC_SILENCE_INIT(v) = v
57 # define JEMALLOC_CC_SILENCE_INIT(v)
60 #define JEMALLOC_GNUC_PREREQ(major, minor) \
61 (!defined(__clang__) && \
62 (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))))
64 # define __has_builtin(builtin) (0)
66 #define JEMALLOC_CLANG_HAS_BUILTIN(builtin) \
67 (defined(__clang__) && __has_builtin(builtin))
70 # define likely(x) __builtin_expect(!!(x), 1)
71 # define unlikely(x) __builtin_expect(!!(x), 0)
72 # if JEMALLOC_GNUC_PREREQ(4, 6) || \
73 JEMALLOC_CLANG_HAS_BUILTIN(__builtin_unreachable)
74 # define unreachable() __builtin_unreachable()
76 # define unreachable()
79 # define likely(x) !!(x)
80 # define unlikely(x) !!(x)
81 # define unreachable()
84 #include "jemalloc/internal/assert.h"
86 /* Use to assert a particular configuration, e.g., cassert(config_debug). */
87 #define cassert(c) do { \
92 #endif /* JEMALLOC_H_TYPES */
93 /******************************************************************************/
94 #ifdef JEMALLOC_H_STRUCTS
96 #endif /* JEMALLOC_H_STRUCTS */
97 /******************************************************************************/
98 #ifdef JEMALLOC_H_EXTERNS
100 int buferror(int err
, char *buf
, size_t buflen
);
101 uintmax_t malloc_strtoumax(const char *restrict nptr
,
102 char **restrict endptr
, int base
);
103 void malloc_write(const char *s
);
106 * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
109 int malloc_vsnprintf(char *str
, size_t size
, const char *format
,
111 int malloc_snprintf(char *str
, size_t size
, const char *format
, ...)
112 JEMALLOC_FORMAT_PRINTF(3, 4);
113 void malloc_vcprintf(void (*write_cb
)(void *, const char *), void *cbopaque
,
114 const char *format
, va_list ap
);
115 void malloc_cprintf(void (*write
)(void *, const char *), void *cbopaque
,
116 const char *format
, ...) JEMALLOC_FORMAT_PRINTF(3, 4);
117 void malloc_printf(const char *format
, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
119 #endif /* JEMALLOC_H_EXTERNS */
120 /******************************************************************************/
121 #ifdef JEMALLOC_H_INLINES
123 #ifndef JEMALLOC_ENABLE_INLINE
124 unsigned ffs_llu(unsigned long long bitmap
);
125 unsigned ffs_lu(unsigned long bitmap
);
126 unsigned ffs_u(unsigned bitmap
);
127 unsigned ffs_zu(size_t bitmap
);
128 unsigned ffs_u64(uint64_t bitmap
);
129 unsigned ffs_u32(uint32_t bitmap
);
130 uint64_t pow2_ceil_u64(uint64_t x
);
131 uint32_t pow2_ceil_u32(uint32_t x
);
132 size_t pow2_ceil_zu(size_t x
);
133 unsigned lg_floor(size_t x
);
134 void set_errno(int errnum
);
138 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
141 #if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \
142 || !defined(JEMALLOC_INTERNAL_FFS)
143 # error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure
146 JEMALLOC_ALWAYS_INLINE
unsigned
147 ffs_llu(unsigned long long bitmap
)
150 return (JEMALLOC_INTERNAL_FFSLL(bitmap
));
153 JEMALLOC_ALWAYS_INLINE
unsigned
154 ffs_lu(unsigned long bitmap
)
157 return (JEMALLOC_INTERNAL_FFSL(bitmap
));
160 JEMALLOC_ALWAYS_INLINE
unsigned
161 ffs_u(unsigned bitmap
)
164 return (JEMALLOC_INTERNAL_FFS(bitmap
));
167 JEMALLOC_ALWAYS_INLINE
unsigned
168 ffs_zu(size_t bitmap
)
171 #if LG_SIZEOF_PTR == LG_SIZEOF_INT
172 return (ffs_u(bitmap
));
173 #elif LG_SIZEOF_PTR == LG_SIZEOF_LONG
174 return (ffs_lu(bitmap
));
175 #elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG
176 return (ffs_llu(bitmap
));
178 #error No implementation for size_t ffs()
182 JEMALLOC_ALWAYS_INLINE
unsigned
183 ffs_u64(uint64_t bitmap
)
186 #if LG_SIZEOF_LONG == 3
187 return (ffs_lu(bitmap
));
188 #elif LG_SIZEOF_LONG_LONG == 3
189 return (ffs_llu(bitmap
));
191 #error No implementation for 64-bit ffs()
195 JEMALLOC_ALWAYS_INLINE
unsigned
196 ffs_u32(uint32_t bitmap
)
199 #if LG_SIZEOF_INT == 2
200 return (ffs_u(bitmap
));
202 #error No implementation for 32-bit ffs()
204 return (ffs_u(bitmap
));
207 JEMALLOC_INLINE
uint64_t
208 pow2_ceil_u64(uint64_t x
)
222 JEMALLOC_INLINE
uint32_t
223 pow2_ceil_u32(uint32_t x
)
236 /* Compute the smallest power of 2 that is >= x. */
237 JEMALLOC_INLINE
size_t
238 pow2_ceil_zu(size_t x
)
241 #if (LG_SIZEOF_PTR == 3)
242 return (pow2_ceil_u64(x
));
244 return (pow2_ceil_u32(x
));
248 #if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
249 JEMALLOC_INLINE
unsigned
257 : "=r"(ret
) // Outputs.
260 assert(ret
< UINT_MAX
);
261 return ((unsigned)ret
);
263 #elif (defined(_MSC_VER))
264 JEMALLOC_INLINE
unsigned
271 #if (LG_SIZEOF_PTR == 3)
272 _BitScanReverse64(&ret
, x
);
273 #elif (LG_SIZEOF_PTR == 2)
274 _BitScanReverse(&ret
, x
);
276 # error "Unsupported type size for lg_floor()"
278 assert(ret
< UINT_MAX
);
279 return ((unsigned)ret
);
281 #elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
282 JEMALLOC_INLINE
unsigned
288 #if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
289 return (((8 << LG_SIZEOF_PTR
) - 1) - __builtin_clz(x
));
290 #elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
291 return (((8 << LG_SIZEOF_PTR
) - 1) - __builtin_clzl(x
));
293 # error "Unsupported type size for lg_floor()"
297 JEMALLOC_INLINE
unsigned
308 #if (LG_SIZEOF_PTR == 3)
312 return ((8 << LG_SIZEOF_PTR
) - 1);
314 return (ffs_zu(x
) - 2);
318 /* Set error code. */
320 set_errno(int errnum
)
324 SetLastError(errnum
);
330 /* Get last error code. */
336 return (GetLastError());
343 #endif /* JEMALLOC_H_INLINES */
344 /******************************************************************************/