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
87int	buferror(int err, char *buf, size_t buflen);
88uintmax_t	malloc_strtoumax(const char *restrict nptr,
89    char **restrict endptr, int base);
90void	malloc_write(const char *s);
91
92/*
93 * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
94 * point math.
95 */
96int	malloc_vsnprintf(char *str, size_t size, const char *format,
97    va_list ap);
98int	malloc_snprintf(char *str, size_t size, const char *format, ...)
99    JEMALLOC_ATTR(format(printf, 3, 4));
100void	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
101    const char *format, va_list ap);
102void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
103    const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
104void	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
112int	jemalloc_ffsl(long bitmap);
113int	jemalloc_ffs(int bitmap);
114size_t	pow2_ceil(size_t x);
115size_t	lg_floor(size_t x);
116void	set_errno(int errnum);
117int	get_errno(void);
118#endif
119
120#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
121
122/* Sanity check: */
123#if !defined(JEMALLOC_INTERNAL_FFSL) || !defined(JEMALLOC_INTERNAL_FFS)
124#  error Both JEMALLOC_INTERNAL_FFSL && JEMALLOC_INTERNAL_FFS should have been defined by configure
125#endif
126
127JEMALLOC_ALWAYS_INLINE int
128jemalloc_ffsl(long bitmap)
129{
130
131        return (JEMALLOC_INTERNAL_FFSL(bitmap));
132}
133
134JEMALLOC_ALWAYS_INLINE int
135jemalloc_ffs(int bitmap)
136{
137
138        return (JEMALLOC_INTERNAL_FFS(bitmap));
139}
140
141/* Compute the smallest power of 2 that is >= x. */
142JEMALLOC_INLINE size_t
143pow2_ceil(size_t x)
144{
145
146	x--;
147	x |= x >> 1;
148	x |= x >> 2;
149	x |= x >> 4;
150	x |= x >> 8;
151	x |= x >> 16;
152#if (LG_SIZEOF_PTR == 3)
153	x |= x >> 32;
154#endif
155	x++;
156	return (x);
157}
158
159#if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
160JEMALLOC_INLINE size_t
161lg_floor(size_t x)
162{
163	size_t ret;
164
165	asm ("bsr %1, %0"
166	    : "=r"(ret) // Outputs.
167	    : "r"(x)    // Inputs.
168	    );
169	return (ret);
170}
171#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))
172JEMALLOC_INLINE size_t
173lg_floor(size_t x)
174{
175
176#if (LG_SIZEOF_PTR == LG_SIZEOF_INT)
177	return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x));
178#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)
179	return (((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x));
180#else
181#  error "Unsupported type sizes for lg_floor()"
182#endif
183}
184#else
185JEMALLOC_INLINE size_t
186lg_floor(size_t x)
187{
188
189	x |= (x >> 1);
190	x |= (x >> 2);
191	x |= (x >> 4);
192	x |= (x >> 8);
193	x |= (x >> 16);
194#if (LG_SIZEOF_PTR == 3 && LG_SIZEOF_PTR == LG_SIZEOF_LONG)
195	x |= (x >> 32);
196	if (x == KZU(0xffffffffffffffff))
197		return (63);
198	x++;
199	return (jemalloc_ffsl(x) - 2);
200#elif (LG_SIZEOF_PTR == 2)
201	if (x == KZU(0xffffffff))
202		return (31);
203	x++;
204	return (jemalloc_ffs(x) - 2);
205#else
206#  error "Unsupported type sizes for lg_floor()"
207#endif
208}
209#endif
210
211/* Sets error code */
212JEMALLOC_INLINE void
213set_errno(int errnum)
214{
215
216#ifdef _WIN32
217	SetLastError(errnum);
218#else
219	errno = errnum;
220#endif
221}
222
223/* Get last error code */
224JEMALLOC_INLINE int
225get_errno(void)
226{
227
228#ifdef _WIN32
229	return (GetLastError());
230#else
231	return (errno);
232#endif
233}
234#endif
235
236#endif /* JEMALLOC_H_INLINES */
237/******************************************************************************/
238