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