xref: /aosp_15_r20/external/jemalloc_new/include/jemalloc/internal/util.h (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
1 #ifndef JEMALLOC_INTERNAL_UTIL_H
2 #define JEMALLOC_INTERNAL_UTIL_H
3 
4 #define UTIL_INLINE static inline
5 
6 /* Junk fill patterns. */
7 #ifndef JEMALLOC_ALLOC_JUNK
8 #  define JEMALLOC_ALLOC_JUNK	((uint8_t)0xa5)
9 #endif
10 #ifndef JEMALLOC_FREE_JUNK
11 #  define JEMALLOC_FREE_JUNK	((uint8_t)0x5a)
12 #endif
13 
14 /*
15  * Wrap a cpp argument that contains commas such that it isn't broken up into
16  * multiple arguments.
17  */
18 #define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__
19 
20 /* cpp macro definition stringification. */
21 #define STRINGIFY_HELPER(x) #x
22 #define STRINGIFY(x) STRINGIFY_HELPER(x)
23 
24 /*
25  * Silence compiler warnings due to uninitialized values.  This is used
26  * wherever the compiler fails to recognize that the variable is never used
27  * uninitialized.
28  */
29 #define JEMALLOC_CC_SILENCE_INIT(v) = v
30 
31 #ifdef __GNUC__
32 #  define likely(x)   __builtin_expect(!!(x), 1)
33 #  define unlikely(x) __builtin_expect(!!(x), 0)
34 #else
35 #  define likely(x)   !!(x)
36 #  define unlikely(x) !!(x)
37 #endif
38 
39 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
40 #  include <stddef.h> /* Use the C23 unreachable() macro. */
41 #else
42 #  if !defined(JEMALLOC_INTERNAL_UNREACHABLE)
43 #    error JEMALLOC_INTERNAL_UNREACHABLE should have been defined by configure
44 #  endif
45 #  define unreachable() JEMALLOC_INTERNAL_UNREACHABLE()
46 #endif
47 
48 /* Set error code. */
49 UTIL_INLINE void
set_errno(int errnum)50 set_errno(int errnum) {
51 #ifdef _WIN32
52 	SetLastError(errnum);
53 #else
54 	errno = errnum;
55 #endif
56 }
57 
58 /* Get last error code. */
59 UTIL_INLINE int
get_errno(void)60 get_errno(void) {
61 #ifdef _WIN32
62 	return GetLastError();
63 #else
64 	return errno;
65 #endif
66 }
67 
68 #undef UTIL_INLINE
69 
70 #endif /* JEMALLOC_INTERNAL_UTIL_H */
71