PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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. /* Size of stack-allocated buffer passed to buferror(). */
  4. #define BUFERROR_BUF 64
  5. /*
  6. * Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
  7. * large enough for all possible uses within jemalloc.
  8. */
  9. #define MALLOC_PRINTF_BUFSIZE 4096
  10. /*
  11. * Wrap a cpp argument that contains commas such that it isn't broken up into
  12. * multiple arguments.
  13. */
  14. #define JEMALLOC_CONCAT(...) __VA_ARGS__
  15. /*
  16. * Silence compiler warnings due to uninitialized values. This is used
  17. * wherever the compiler fails to recognize that the variable is never used
  18. * uninitialized.
  19. */
  20. #ifdef JEMALLOC_CC_SILENCE
  21. # define JEMALLOC_CC_SILENCE_INIT(v) = v
  22. #else
  23. # define JEMALLOC_CC_SILENCE_INIT(v)
  24. #endif
  25. /*
  26. * Define a custom assert() in order to reduce the chances of deadlock during
  27. * assertion failure.
  28. */
  29. #ifndef assert
  30. #define assert(e) do { \
  31. if (config_debug && !(e)) { \
  32. malloc_printf( \
  33. "<jemalloc>: %s:%d: Failed assertion: \"%s\"\n", \
  34. __FILE__, __LINE__, #e); \
  35. abort(); \
  36. } \
  37. } while (0)
  38. #endif
  39. /* Use to assert a particular configuration, e.g., cassert(config_debug). */
  40. #define cassert(c) do { \
  41. if ((c) == false) \
  42. assert(false); \
  43. } while (0)
  44. #ifndef not_reached
  45. #define not_reached() do { \
  46. if (config_debug) { \
  47. malloc_printf( \
  48. "<jemalloc>: %s:%d: Unreachable code reached\n", \
  49. __FILE__, __LINE__); \
  50. abort(); \
  51. } \
  52. } while (0)
  53. #endif
  54. #ifndef not_implemented
  55. #define not_implemented() do { \
  56. if (config_debug) { \
  57. malloc_printf("<jemalloc>: %s:%d: Not implemented\n", \
  58. __FILE__, __LINE__); \
  59. abort(); \
  60. } \
  61. } while (0)
  62. #endif
  63. #define assert_not_implemented(e) do { \
  64. if (config_debug && !(e)) \
  65. not_implemented(); \
  66. } while (0)
  67. #endif /* JEMALLOC_H_TYPES */
  68. /******************************************************************************/
  69. #ifdef JEMALLOC_H_STRUCTS
  70. #endif /* JEMALLOC_H_STRUCTS */
  71. /******************************************************************************/
  72. #ifdef JEMALLOC_H_EXTERNS
  73. int buferror(char *buf, size_t buflen);
  74. uintmax_t malloc_strtoumax(const char *nptr, char **endptr, int base);
  75. void malloc_write(const char *s);
  76. /*
  77. * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
  78. * point math.
  79. */
  80. int malloc_vsnprintf(char *str, size_t size, const char *format,
  81. va_list ap);
  82. int malloc_snprintf(char *str, size_t size, const char *format, ...)
  83. JEMALLOC_ATTR(format(printf, 3, 4));
  84. void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
  85. const char *format, va_list ap);
  86. void malloc_cprintf(void (*write)(void *, const char *), void *cbopaque,
  87. const char *format, ...) JEMALLOC_ATTR(format(printf, 3, 4));
  88. void malloc_printf(const char *format, ...)
  89. JEMALLOC_ATTR(format(printf, 1, 2));
  90. #endif /* JEMALLOC_H_EXTERNS */
  91. /******************************************************************************/
  92. #ifdef JEMALLOC_H_INLINES
  93. #ifndef JEMALLOC_ENABLE_INLINE
  94. size_t pow2_ceil(size_t x);
  95. void malloc_write(const char *s);
  96. void set_errno(int errnum);
  97. int get_errno(void);
  98. #endif
  99. #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
  100. /* Compute the smallest power of 2 that is >= x. */
  101. JEMALLOC_INLINE size_t
  102. pow2_ceil(size_t x)
  103. {
  104. x--;
  105. x |= x >> 1;
  106. x |= x >> 2;
  107. x |= x >> 4;
  108. x |= x >> 8;
  109. x |= x >> 16;
  110. #if (LG_SIZEOF_PTR == 3)
  111. x |= x >> 32;
  112. #endif
  113. x++;
  114. return (x);
  115. }
  116. /* Sets error code */
  117. JEMALLOC_INLINE void
  118. set_errno(int errnum)
  119. {
  120. #ifdef _WIN32
  121. SetLastError(errnum);
  122. #else
  123. errno = errnum;
  124. #endif
  125. }
  126. /* Get last error code */
  127. JEMALLOC_INLINE int
  128. get_errno(void)
  129. {
  130. #ifdef _WIN32
  131. return (GetLastError());
  132. #else
  133. return (errno);
  134. #endif
  135. }
  136. #endif
  137. #endif /* JEMALLOC_H_INLINES */
  138. /******************************************************************************/