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