util.h revision 6556e28be15d9acd8f3835fb9fad90145e1edbff
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#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#define	assert_not_implemented(e) do {					\
67	if (config_debug && !(e))					\
68		not_implemented();					\
69} while (0)
70
71/* Use to assert a particular configuration, e.g., cassert(config_debug). */
72#define	cassert(c) do {							\
73	if ((c) == false)						\
74		not_reached();						\
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
85int	buferror(char *buf, size_t buflen);
86uintmax_t	malloc_strtoumax(const char *nptr, char **endptr, int base);
87void	malloc_write(const char *s);
88
89/*
90 * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
91 * point math.
92 */
93int	malloc_vsnprintf(char *str, size_t size, const char *format,
94    va_list ap);
95int	malloc_snprintf(char *str, size_t size, const char *format, ...)
96    JEMALLOC_ATTR(format(printf, 3, 4));
97void	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
98    const char *format, va_list ap);
99void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
100    const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
101void	malloc_printf(const char *format, ...)
102    JEMALLOC_ATTR(format(printf, 1, 2));
103
104#endif /* JEMALLOC_H_EXTERNS */
105/******************************************************************************/
106#ifdef JEMALLOC_H_INLINES
107
108#ifndef JEMALLOC_ENABLE_INLINE
109size_t	pow2_ceil(size_t x);
110void	malloc_write(const char *s);
111void	set_errno(int errnum);
112int	get_errno(void);
113#endif
114
115#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
116/* Compute the smallest power of 2 that is >= x. */
117JEMALLOC_INLINE size_t
118pow2_ceil(size_t x)
119{
120
121	x--;
122	x |= x >> 1;
123	x |= x >> 2;
124	x |= x >> 4;
125	x |= x >> 8;
126	x |= x >> 16;
127#if (LG_SIZEOF_PTR == 3)
128	x |= x >> 32;
129#endif
130	x++;
131	return (x);
132}
133
134/* Sets error code */
135JEMALLOC_INLINE void
136set_errno(int errnum)
137{
138
139#ifdef _WIN32
140	SetLastError(errnum);
141#else
142	errno = errnum;
143#endif
144}
145
146/* Get last error code */
147JEMALLOC_INLINE int
148get_errno(void)
149{
150
151#ifdef _WIN32
152	return (GetLastError());
153#else
154	return (errno);
155#endif
156}
157#endif
158
159#endif /* JEMALLOC_H_INLINES */
160/******************************************************************************/
161