/jemalloc-3.0.0/include/jemalloc/internal/util.h
C Header | 160 lines | 107 code | 22 blank | 31 comment | 15 complexity | c0b201cc5032c39a44720c159cbb8da9 MD5 | raw file
Possible License(s): BSD-2-Clause
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);
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/******************************************************************************/