Rizin
unix-like reverse engineering framework and cli tools
util.h
Go to the documentation of this file.
1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
3 
4 // XXX: remove all asserts
5 
6 #ifdef _WIN32
7 # ifdef _WIN64
8 # define FMT64_PREFIX "ll"
9 # define FMTPTR_PREFIX "ll"
10 # else
11 # define FMT64_PREFIX "ll"
12 # define FMTPTR_PREFIX ""
13 # endif
14 # define FMTd32 "d"
15 # define FMTu32 "u"
16 # define FMTx32 "x"
17 # define FMTd64 FMT64_PREFIX "d"
18 # define FMTu64 FMT64_PREFIX "u"
19 # define FMTx64 FMT64_PREFIX "x"
20 # define FMTdPTR FMTPTR_PREFIX "d"
21 # define FMTuPTR FMTPTR_PREFIX "u"
22 # define FMTxPTR FMTPTR_PREFIX "x"
23 #else
24 # include <inttypes.h>
25 # define FMTd32 PRId32
26 # define FMTu32 PRIu32
27 # define FMTx32 PRIx32
28 # define FMTd64 PRId64
29 # define FMTu64 PRIu64
30 # define FMTx64 PRIx64
31 # define FMTdPTR PRIdPTR
32 # define FMTuPTR PRIuPTR
33 # define FMTxPTR PRIxPTR
34 #endif
35 
36 /* NOTE: This file was left out of correcting assert() with error handling */
37 /* since it provides rather complex math functions and returning an error might */
38 /* break compatability. */
39 /* ~Debily */
40 
41 /* Size of stack-allocated buffer passed to buferror(). */
42 #define BUFERROR_BUF 64
43 
44 /*
45  * Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
46  * large enough for all possible uses within jemalloc.
47  */
48 #define MALLOC_PRINTF_BUFSIZE 4096
49 
50 /* Junk fill patterns. */
51 #ifndef JEMALLOC_ALLOC_JUNK
52 # define JEMALLOC_ALLOC_JUNK ((uint8_t)0xa5)
53 #endif
54 #ifndef JEMALLOC_FREE_JUNK
55 # define JEMALLOC_FREE_JUNK ((uint8_t)0x5a)
56 #endif
57 
58 /*
59  * Wrap a cpp argument that contains commas such that it isn't broken up into
60  * multiple arguments.
61  */
62 #define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__
63 
64 /*
65  * Silence compiler warnings due to uninitialized values. This is used
66  * wherever the compiler fails to recognize that the variable is never used
67  * uninitialized.
68  */
69 #ifdef JEMALLOC_CC_SILENCE
70 # define JEMALLOC_CC_SILENCE_INIT(v) = v
71 #else
72 # define JEMALLOC_CC_SILENCE_INIT(v)
73 #endif
74 
75 #ifdef __GNUC__
76 # define likely(x) __builtin_expect(!!(x), 1)
77 # define unlikely(x) __builtin_expect(!!(x), 0)
78 #else
79 # define likely(x) !!(x)
80 # define unlikely(x) !!(x)
81 #endif
82 
83 #if !defined(JEMALLOC_INTERNAL_UNREACHABLE)
84 # error JEMALLOC_INTERNAL_UNREACHABLE should have been defined by configure
85 #endif
86 
87 #define unreachable() JEMALLOC_INTERNAL_UNREACHABLE()
88 
89 #include "assert.h"
90 
91 /* Use to assert a particular configuration, e.g., cassert(config_debug). */
92 #define cassert(c) do { \
93  if (unlikely(!(c))) \
94  not_reached(); \
95 } while (0)
96 
97 #endif /* JEMALLOC_H_TYPES */
98 /******************************************************************************/
99 #ifdef JEMALLOC_H_STRUCTS
100 
101 #endif /* JEMALLOC_H_STRUCTS */
102 /******************************************************************************/
103 #ifdef JEMALLOC_H_EXTERNS
104 
105 int buferror(int err, char *buf, size_t buflen);
106 uintmax_t malloc_strtoumax(const char *nptr,
107  char **endptr, int base);
108 void malloc_write(const char *s);
109 
110 /*
111  * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
112  * point math.
113  */
114 size_t malloc_vsnprintf(char *str, size_t size, const char *format,
115  va_list ap);
116 /*
117 size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
118  JEMALLOC_FORMAT_PRINTF(3, 4);
119 void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
120  const char *format, va_list ap);
121 void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
122  const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4);
123 void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
124 */
125 
126 #endif /* JEMALLOC_H_EXTERNS */
127 /******************************************************************************/
128 #ifdef JEMALLOC_H_INLINES
129 
130 #ifndef JEMALLOC_ENABLE_INLINE
131 unsigned ffs_llu(unsigned long long bitmap);
132 unsigned ffs_lu(unsigned long bitmap);
133 unsigned ffs_u(unsigned bitmap);
134 unsigned ffs_zu(size_t bitmap);
135 unsigned ffs_u64(uint64_t bitmap);
136 unsigned ffs_u32(uint32_t bitmap);
139 size_t pow2_ceil_zu(size_t x);
140 unsigned lg_floor(size_t x);
141 void set_errno(int errnum);
142 int get_errno(void);
143 #endif
144 
145 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
146 
147 /* Sanity check. */
148 #if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \
149  || !defined(JEMALLOC_INTERNAL_FFS)
150 # error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure
151 #endif
152 
153 JEMALLOC_ALWAYS_INLINE unsigned
154 ffs_llu(unsigned long long bitmap)
155 {
156 
157  return (JEMALLOC_INTERNAL_FFSLL(bitmap));
158 }
159 
160 JEMALLOC_ALWAYS_INLINE unsigned
161 ffs_lu(unsigned long bitmap)
162 {
163 
164  return (JEMALLOC_INTERNAL_FFSL(bitmap));
165 }
166 
167 JEMALLOC_ALWAYS_INLINE unsigned
168 ffs_u(unsigned bitmap)
169 {
170 
171  return (JEMALLOC_INTERNAL_FFS(bitmap));
172 }
173 
174 JEMALLOC_ALWAYS_INLINE unsigned
175 ffs_zu(size_t bitmap)
176 {
177 
178 #if LG_SIZEOF_PTR == LG_SIZEOF_INT
179  return (ffs_u(bitmap));
180 #elif LG_SIZEOF_PTR == LG_SIZEOF_LONG
181  return (ffs_lu(bitmap));
182 #elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG
183  return (ffs_llu(bitmap));
184 #else
185 #error No implementation for size_t ffs()
186 #endif
187 }
188 
189 JEMALLOC_ALWAYS_INLINE unsigned
190 ffs_u64(uint64_t bitmap)
191 {
192 
193 #if LG_SIZEOF_LONG == 3
194  return (ffs_lu(bitmap));
195 #elif LG_SIZEOF_LONG_LONG == 3
196  return (ffs_llu(bitmap));
197 #else
198 #error No implementation for 64-bit ffs()
199 #endif
200 }
201 
202 JEMALLOC_ALWAYS_INLINE unsigned
203 ffs_u32(uint32_t bitmap)
204 {
205 
206 #if LG_SIZEOF_INT == 2
207  return (ffs_u(bitmap));
208 #else
209 #error No implementation for 32-bit ffs()
210 #endif
211  return (ffs_u(bitmap));
212 }
213 
216 {
217 
218  x--;
219  x |= x >> 1;
220  x |= x >> 2;
221  x |= x >> 4;
222  x |= x >> 8;
223  x |= x >> 16;
224  x |= x >> 32;
225  x++;
226  return (x);
227 }
228 
231 {
232 
233  x--;
234  x |= x >> 1;
235  x |= x >> 2;
236  x |= x >> 4;
237  x |= x >> 8;
238  x |= x >> 16;
239  x++;
240  return (x);
241 }
242 
243 /* Compute the smallest power of 2 that is >= x. */
244 JEMALLOC_INLINE size_t
245 pow2_ceil_zu(size_t x)
246 {
247 
248 #if (LG_SIZEOF_PTR == 3)
249  return (pow2_ceil_u64(x));
250 #else
251  return (pow2_ceil_u32(x));
252 #endif
253 }
254 
255 #if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
256 JEMALLOC_INLINE unsigned
257 lg_floor(size_t x)
258 {
259  size_t ret;
260 
261  if (x == 0) {
262  return UINT_MAX;
263  }
264 
265  asm ("bsr %1, %0"
266  : "=r"(ret) // Outputs.
267  : "r"(x) // Inputs.
268  );
269  // assert(ret < UINT_MAX);
270  return ((unsigned)ret);
271 }
272 #elif (defined(_MSC_VER))
273 JEMALLOC_INLINE unsigned
274 lg_floor(size_t x)
275 {
276  unsigned long ret;
277 
278  assert(x != 0);
279 
280 #if (LG_SIZEOF_PTR == 3)
281  _BitScanReverse64(&ret, x);
282 #elif (LG_SIZEOF_PTR == 2)
283  _BitScanReverse(&ret, x);
284 #else
285 # error "Unsupported type size for lg_floor()"
286 #endif
287  assert(ret < UINT_MAX);
288  return ((unsigned)ret);
289 }
290 #elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
291 JEMALLOC_INLINE unsigned
292 lg_floor(size_t x)
293 {
294 
295  assert(x != 0);
296 
297 #if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
298  return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x));
299 #elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
300  return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x));
301 #else
302 # error "Unsupported type size for lg_floor()"
303 #endif
304 }
305 #else
306 JEMALLOC_INLINE unsigned
307 lg_floor(size_t x)
308 {
309 
310  assert(x != 0);
311 
312  x |= (x >> 1);
313  x |= (x >> 2);
314  x |= (x >> 4);
315  x |= (x >> 8);
316  x |= (x >> 16);
317 #if (LG_SIZEOF_PTR == 3)
318  x |= (x >> 32);
319 #endif
320  if (x == SIZE_T_MAX)
321  return ((8 << LG_SIZEOF_PTR) - 1);
322  x++;
323  return (ffs_zu(x) - 2);
324 }
325 #endif
326 
327 /* Set error code. */
328 JEMALLOC_INLINE void
329 set_errno(int errnum)
330 {
331 
332 #ifdef _WIN32
333  SetLastError(errnum);
334 #else
335  errno = errnum;
336 #endif
337 }
338 
339 /* Get last error code. */
340 JEMALLOC_INLINE int
341 get_errno(void)
342 {
343 
344 #ifdef _WIN32
345  return (GetLastError());
346 #else
347  return (errno);
348 #endif
349 }
350 #endif
351 
352 #endif /* JEMALLOC_H_INLINES */
353 /******************************************************************************/
static bool err
Definition: armass.c:435
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
#define LG_SIZEOF_PTR
Definition: jemalloc.h:51
#define SIZE_T_MAX
#define JEMALLOC_INTERNAL_FFSLL
#define JEMALLOC_INTERNAL_FFS
#define JEMALLOC_INTERNAL_FFSL
#define JEMALLOC_INLINE
#define JEMALLOC_ALWAYS_INLINE
assert(limit<=UINT32_MAX/2)
int x
Definition: mipsasm.c:20
#define pow2_ceil_u64
#define get_errno
#define ffs_u
#define buferror
#define pow2_ceil_zu
#define ffs_lu
#define ffs_zu
#define ffs_u64
#define malloc_write
#define lg_floor
#define malloc_vsnprintf
#define set_errno
#define malloc_strtoumax
#define ffs_llu
#define pow2_ceil_u32
#define ffs_u32
static RzSocket * s
Definition: rtr.c:28
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
uint64_t uintmax_t
#define UINT_MAX
Definition: md5.h:55
ut64 buflen
Definition: core.c:76