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