util.h revision 122449b073bcbaa504c4f592ea2d733503c272d2
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_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/* Use to assert a particular configuration, e.g., cassert(config_debug). */
46#define	cassert(c) do {							\
47	if ((c) == false)						\
48		assert(false);						\
49} while (0)
50
51#ifndef not_reached
52#define	not_reached() do {						\
53	if (config_debug) {						\
54		malloc_printf(						\
55		    "<jemalloc>: %s:%d: Unreachable code reached\n",	\
56		    __FILE__, __LINE__);				\
57		abort();						\
58	}								\
59} while (0)
60#endif
61
62#ifndef not_implemented
63#define	not_implemented() do {						\
64	if (config_debug) {						\
65		malloc_printf("<jemalloc>: %s:%d: Not implemented\n",	\
66		    __FILE__, __LINE__);				\
67		abort();						\
68	}								\
69} while (0)
70#endif
71
72#define	assert_not_implemented(e) do {					\
73	if (config_debug && !(e))					\
74		not_implemented();					\
75} while (0)
76
77#endif /* JEMALLOC_H_TYPES */
78/******************************************************************************/
79#ifdef JEMALLOC_H_STRUCTS
80
81#endif /* JEMALLOC_H_STRUCTS */
82/******************************************************************************/
83#ifdef JEMALLOC_H_EXTERNS
84
85extern void	(*je_malloc_message)(void *wcbopaque, const char *s);
86
87int	buferror(int errnum, char *buf, size_t buflen);
88uintmax_t	malloc_strtoumax(const char *nptr, char **endptr, int base);
89
90/*
91 * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
92 * point math.
93 */
94int	malloc_vsnprintf(char *str, size_t size, const char *format,
95    va_list ap);
96int	malloc_snprintf(char *str, size_t size, const char *format, ...)
97    JEMALLOC_ATTR(format(printf, 3, 4));
98void	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
99    const char *format, va_list ap);
100void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
101    const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
102void	malloc_printf(const char *format, ...)
103    JEMALLOC_ATTR(format(printf, 1, 2));
104
105#endif /* JEMALLOC_H_EXTERNS */
106/******************************************************************************/
107#ifdef JEMALLOC_H_INLINES
108
109#ifndef JEMALLOC_ENABLE_INLINE
110size_t	pow2_ceil(size_t x);
111void	malloc_write(const char *s);
112#endif
113
114#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
115/* Compute the smallest power of 2 that is >= x. */
116JEMALLOC_INLINE size_t
117pow2_ceil(size_t x)
118{
119
120	x--;
121	x |= x >> 1;
122	x |= x >> 2;
123	x |= x >> 4;
124	x |= x >> 8;
125	x |= x >> 16;
126#if (LG_SIZEOF_PTR == 3)
127	x |= x >> 32;
128#endif
129	x++;
130	return (x);
131}
132
133/*
134 * Wrapper around malloc_message() that avoids the need for
135 * je_malloc_message(...) throughout the code.
136 */
137JEMALLOC_INLINE void
138malloc_write(const char *s)
139{
140
141	je_malloc_message(NULL, s);
142}
143#endif
144
145#endif /* JEMALLOC_H_INLINES */
146/******************************************************************************/
147