util.h revision da99e31105eb709ef4ec8a120b115c32a6b9723a
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
85int	buferror(char *buf, size_t buflen);
86uintmax_t	malloc_strtoumax(const char *nptr, char **endptr, int base);
87
88/*
89 * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
90 * point math.
91 */
92int	malloc_vsnprintf(char *str, size_t size, const char *format,
93    va_list ap);
94int	malloc_snprintf(char *str, size_t size, const char *format, ...)
95    JEMALLOC_ATTR(format(printf, 3, 4));
96void	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
97    const char *format, va_list ap);
98void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
99    const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
100void	malloc_printf(const char *format, ...)
101    JEMALLOC_ATTR(format(printf, 1, 2));
102
103#endif /* JEMALLOC_H_EXTERNS */
104/******************************************************************************/
105#ifdef JEMALLOC_H_INLINES
106
107#ifndef JEMALLOC_ENABLE_INLINE
108size_t	pow2_ceil(size_t x);
109void	malloc_write(const char *s);
110void	set_errno(int errnum);
111int	get_errno(void);
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
144/* Sets error code */
145JEMALLOC_INLINE void
146set_errno(int errnum)
147{
148
149#ifdef _WIN32
150	SetLastError(errnum);
151#else
152	errno = errnum;
153#endif
154}
155
156/* Get last error code */
157JEMALLOC_INLINE int
158get_errno(void)
159{
160
161#ifdef _WIN32
162	return (GetLastError());
163#else
164	return (errno);
165#endif
166}
167#endif
168
169#endif /* JEMALLOC_H_INLINES */
170/******************************************************************************/
171