xref: /freebsd-src/contrib/jemalloc/include/jemalloc/internal/util.h (revision 276da39af92f48350aa01091a2b8b3e735217eea)
1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
3 
4 /* Size of stack-allocated buffer passed to buferror(). */
5 #define	BUFERROR_BUF		64
6 
7 /*
8  * Size of stack-allocated buffer used by malloc_{,v,vc}printf().  This must be
9  * large enough for all possible uses within jemalloc.
10  */
11 #define	MALLOC_PRINTF_BUFSIZE	4096
12 
13 /*
14  * Wrap a cpp argument that contains commas such that it isn't broken up into
15  * multiple arguments.
16  */
17 #define	JEMALLOC_ARG_CONCAT(...) __VA_ARGS__
18 
19 /*
20  * Silence compiler warnings due to uninitialized values.  This is used
21  * wherever the compiler fails to recognize that the variable is never used
22  * uninitialized.
23  */
24 #ifdef JEMALLOC_CC_SILENCE
25 #  define JEMALLOC_CC_SILENCE_INIT(v) = v
26 #else
27 #  define JEMALLOC_CC_SILENCE_INIT(v)
28 #endif
29 
30 /*
31  * Define a custom assert() in order to reduce the chances of deadlock during
32  * assertion failure.
33  */
34 #ifndef assert
35 #define	assert(e) do {							\
36 	if (config_debug && !(e)) {					\
37 		malloc_printf(						\
38 		    "<jemalloc>: %s:%d: Failed assertion: \"%s\"\n",	\
39 		    __FILE__, __LINE__, #e);				\
40 		abort();						\
41 	}								\
42 } while (0)
43 #endif
44 
45 #ifndef not_reached
46 #define	not_reached() do {						\
47 	if (config_debug) {						\
48 		malloc_printf(						\
49 		    "<jemalloc>: %s:%d: Unreachable code reached\n",	\
50 		    __FILE__, __LINE__);				\
51 		abort();						\
52 	}								\
53 } while (0)
54 #endif
55 
56 #ifndef not_implemented
57 #define	not_implemented() do {						\
58 	if (config_debug) {						\
59 		malloc_printf("<jemalloc>: %s:%d: Not implemented\n",	\
60 		    __FILE__, __LINE__);				\
61 		abort();						\
62 	}								\
63 } while (0)
64 #endif
65 
66 #ifndef assert_not_implemented
67 #define	assert_not_implemented(e) do {					\
68 	if (config_debug && !(e))					\
69 		not_implemented();					\
70 } while (0)
71 #endif
72 
73 /* Use to assert a particular configuration, e.g., cassert(config_debug). */
74 #define	cassert(c) do {							\
75 	if ((c) == false)						\
76 		not_reached();						\
77 } while (0)
78 
79 #endif /* JEMALLOC_H_TYPES */
80 /******************************************************************************/
81 #ifdef JEMALLOC_H_STRUCTS
82 
83 #endif /* JEMALLOC_H_STRUCTS */
84 /******************************************************************************/
85 #ifdef JEMALLOC_H_EXTERNS
86 
87 int	buferror(int err, char *buf, size_t buflen);
88 uintmax_t	malloc_strtoumax(const char *restrict nptr,
89     char **restrict endptr, int base);
90 void	malloc_write(const char *s);
91 
92 /*
93  * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
94  * point math.
95  */
96 int	malloc_vsnprintf(char *str, size_t size, const char *format,
97     va_list ap);
98 int	malloc_snprintf(char *str, size_t size, const char *format, ...)
99     JEMALLOC_ATTR(format(printf, 3, 4));
100 void	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
101     const char *format, va_list ap);
102 void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
103     const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
104 void	malloc_printf(const char *format, ...)
105     JEMALLOC_ATTR(format(printf, 1, 2));
106 
107 #endif /* JEMALLOC_H_EXTERNS */
108 /******************************************************************************/
109 #ifdef JEMALLOC_H_INLINES
110 
111 #ifndef JEMALLOC_ENABLE_INLINE
112 size_t	pow2_ceil(size_t x);
113 void	set_errno(int errnum);
114 int	get_errno(void);
115 #endif
116 
117 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
118 /* Compute the smallest power of 2 that is >= x. */
119 JEMALLOC_INLINE size_t
120 pow2_ceil(size_t x)
121 {
122 
123 	x--;
124 	x |= x >> 1;
125 	x |= x >> 2;
126 	x |= x >> 4;
127 	x |= x >> 8;
128 	x |= x >> 16;
129 #if (LG_SIZEOF_PTR == 3)
130 	x |= x >> 32;
131 #endif
132 	x++;
133 	return (x);
134 }
135 
136 /* Sets error code */
137 JEMALLOC_INLINE void
138 set_errno(int errnum)
139 {
140 
141 #ifdef _WIN32
142 	SetLastError(errnum);
143 #else
144 	errno = errnum;
145 #endif
146 }
147 
148 /* Get last error code */
149 JEMALLOC_INLINE int
150 get_errno(void)
151 {
152 
153 #ifdef _WIN32
154 	return (GetLastError());
155 #else
156 	return (errno);
157 #endif
158 }
159 #endif
160 
161 #endif /* JEMALLOC_H_INLINES */
162 /******************************************************************************/
163