1234370Sjasone/******************************************************************************/
2234370Sjasone#ifdef JEMALLOC_H_TYPES
3234370Sjasone
4234370Sjasone/* Size of stack-allocated buffer passed to buferror(). */
5234370Sjasone#define	BUFERROR_BUF		64
6234370Sjasone
7234370Sjasone/*
8234370Sjasone * Size of stack-allocated buffer used by malloc_{,v,vc}printf().  This must be
9234370Sjasone * large enough for all possible uses within jemalloc.
10234370Sjasone */
11234370Sjasone#define	MALLOC_PRINTF_BUFSIZE	4096
12234370Sjasone
13234370Sjasone/*
14234370Sjasone * Wrap a cpp argument that contains commas such that it isn't broken up into
15234370Sjasone * multiple arguments.
16234370Sjasone */
17234370Sjasone#define JEMALLOC_CONCAT(...) __VA_ARGS__
18234370Sjasone
19234370Sjasone/*
20234370Sjasone * Silence compiler warnings due to uninitialized values.  This is used
21234370Sjasone * wherever the compiler fails to recognize that the variable is never used
22234370Sjasone * uninitialized.
23234370Sjasone */
24234370Sjasone#ifdef JEMALLOC_CC_SILENCE
25234370Sjasone#  define JEMALLOC_CC_SILENCE_INIT(v) = v
26234370Sjasone#else
27234370Sjasone#  define JEMALLOC_CC_SILENCE_INIT(v)
28234370Sjasone#endif
29234370Sjasone
30234370Sjasone/*
31234370Sjasone * Define a custom assert() in order to reduce the chances of deadlock during
32234370Sjasone * assertion failure.
33234370Sjasone */
34234370Sjasone#ifndef assert
35234370Sjasone#define	assert(e) do {							\
36234370Sjasone	if (config_debug && !(e)) {					\
37234370Sjasone		malloc_printf(						\
38234370Sjasone		    "<jemalloc>: %s:%d: Failed assertion: \"%s\"\n",	\
39234370Sjasone		    __FILE__, __LINE__, #e);				\
40234370Sjasone		abort();						\
41234370Sjasone	}								\
42234370Sjasone} while (0)
43234370Sjasone#endif
44234370Sjasone
45234370Sjasone/* Use to assert a particular configuration, e.g., cassert(config_debug). */
46234370Sjasone#define	cassert(c) do {							\
47234370Sjasone	if ((c) == false)						\
48234370Sjasone		assert(false);						\
49234370Sjasone} while (0)
50234370Sjasone
51234370Sjasone#ifndef not_reached
52234370Sjasone#define	not_reached() do {						\
53234370Sjasone	if (config_debug) {						\
54234370Sjasone		malloc_printf(						\
55234370Sjasone		    "<jemalloc>: %s:%d: Unreachable code reached\n",	\
56234370Sjasone		    __FILE__, __LINE__);				\
57234370Sjasone		abort();						\
58234370Sjasone	}								\
59234370Sjasone} while (0)
60234370Sjasone#endif
61234370Sjasone
62234370Sjasone#ifndef not_implemented
63234370Sjasone#define	not_implemented() do {						\
64234370Sjasone	if (config_debug) {						\
65234370Sjasone		malloc_printf("<jemalloc>: %s:%d: Not implemented\n",	\
66234370Sjasone		    __FILE__, __LINE__);				\
67234370Sjasone		abort();						\
68234370Sjasone	}								\
69234370Sjasone} while (0)
70234370Sjasone#endif
71234370Sjasone
72234370Sjasone#define	assert_not_implemented(e) do {					\
73234370Sjasone	if (config_debug && !(e))					\
74234370Sjasone		not_implemented();					\
75234370Sjasone} while (0)
76234370Sjasone
77234370Sjasone#endif /* JEMALLOC_H_TYPES */
78234370Sjasone/******************************************************************************/
79234370Sjasone#ifdef JEMALLOC_H_STRUCTS
80234370Sjasone
81234370Sjasone#endif /* JEMALLOC_H_STRUCTS */
82234370Sjasone/******************************************************************************/
83234370Sjasone#ifdef JEMALLOC_H_EXTERNS
84234370Sjasone
85235238Sjasoneint	buferror(char *buf, size_t buflen);
86234370Sjasoneuintmax_t	malloc_strtoumax(const char *nptr, char **endptr, int base);
87235238Sjasonevoid	malloc_write(const char *s);
88234370Sjasone
89234370Sjasone/*
90234370Sjasone * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
91234370Sjasone * point math.
92234370Sjasone */
93234370Sjasoneint	malloc_vsnprintf(char *str, size_t size, const char *format,
94234370Sjasone    va_list ap);
95234370Sjasoneint	malloc_snprintf(char *str, size_t size, const char *format, ...)
96234370Sjasone    JEMALLOC_ATTR(format(printf, 3, 4));
97234370Sjasonevoid	malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
98234370Sjasone    const char *format, va_list ap);
99234370Sjasonevoid malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
100234370Sjasone    const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
101234370Sjasonevoid	malloc_printf(const char *format, ...)
102234370Sjasone    JEMALLOC_ATTR(format(printf, 1, 2));
103234370Sjasone
104234370Sjasone#endif /* JEMALLOC_H_EXTERNS */
105234370Sjasone/******************************************************************************/
106234370Sjasone#ifdef JEMALLOC_H_INLINES
107234370Sjasone
108234370Sjasone#ifndef JEMALLOC_ENABLE_INLINE
109234370Sjasonesize_t	pow2_ceil(size_t x);
110234370Sjasonevoid	malloc_write(const char *s);
111235238Sjasonevoid	set_errno(int errnum);
112235238Sjasoneint	get_errno(void);
113234370Sjasone#endif
114234370Sjasone
115234370Sjasone#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
116234370Sjasone/* Compute the smallest power of 2 that is >= x. */
117234370SjasoneJEMALLOC_INLINE size_t
118234370Sjasonepow2_ceil(size_t x)
119234370Sjasone{
120234370Sjasone
121234370Sjasone	x--;
122234370Sjasone	x |= x >> 1;
123234370Sjasone	x |= x >> 2;
124234370Sjasone	x |= x >> 4;
125234370Sjasone	x |= x >> 8;
126234370Sjasone	x |= x >> 16;
127234370Sjasone#if (LG_SIZEOF_PTR == 3)
128234370Sjasone	x |= x >> 32;
129234370Sjasone#endif
130234370Sjasone	x++;
131234370Sjasone	return (x);
132234370Sjasone}
133234370Sjasone
134235238Sjasone/* Sets error code */
135234370SjasoneJEMALLOC_INLINE void
136235238Sjasoneset_errno(int errnum)
137234370Sjasone{
138234370Sjasone
139235238Sjasone#ifdef _WIN32
140235238Sjasone	SetLastError(errnum);
141235238Sjasone#else
142235238Sjasone	errno = errnum;
143235238Sjasone#endif
144234370Sjasone}
145235238Sjasone
146235238Sjasone/* Get last error code */
147235238SjasoneJEMALLOC_INLINE int
148235238Sjasoneget_errno(void)
149235238Sjasone{
150235238Sjasone
151235238Sjasone#ifdef _WIN32
152235238Sjasone	return (GetLastError());
153235238Sjasone#else
154235238Sjasone	return (errno);
155234370Sjasone#endif
156235238Sjasone}
157235238Sjasone#endif
158234370Sjasone
159234370Sjasone#endif /* JEMALLOC_H_INLINES */
160234370Sjasone/******************************************************************************/
161