malloc_io.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef JEMALLOC_INTERNAL_MALLOC_IO_H
  2. #define JEMALLOC_INTERNAL_MALLOC_IO_H
  3. #include "jemalloc/internal/jemalloc_internal_types.h"
  4. #ifdef _WIN32
  5. # ifdef _WIN64
  6. # define FMT64_PREFIX "ll"
  7. # define FMTPTR_PREFIX "ll"
  8. # else
  9. # define FMT64_PREFIX "ll"
  10. # define FMTPTR_PREFIX ""
  11. # endif
  12. # define FMTd32 "d"
  13. # define FMTu32 "u"
  14. # define FMTx32 "x"
  15. # define FMTd64 FMT64_PREFIX "d"
  16. # define FMTu64 FMT64_PREFIX "u"
  17. # define FMTx64 FMT64_PREFIX "x"
  18. # define FMTdPTR FMTPTR_PREFIX "d"
  19. # define FMTuPTR FMTPTR_PREFIX "u"
  20. # define FMTxPTR FMTPTR_PREFIX "x"
  21. #else
  22. # include <inttypes.h>
  23. # define FMTd32 PRId32
  24. # define FMTu32 PRIu32
  25. # define FMTx32 PRIx32
  26. # define FMTd64 PRId64
  27. # define FMTu64 PRIu64
  28. # define FMTx64 PRIx64
  29. # define FMTdPTR PRIdPTR
  30. # define FMTuPTR PRIuPTR
  31. # define FMTxPTR PRIxPTR
  32. #endif
  33. /* Size of stack-allocated buffer passed to buferror(). */
  34. #define BUFERROR_BUF 64
  35. /*
  36. * Size of stack-allocated buffer used by malloc_{,v,vc}printf(). This must be
  37. * large enough for all possible uses within jemalloc.
  38. */
  39. #define MALLOC_PRINTF_BUFSIZE 4096
  40. write_cb_t wrtmessage;
  41. int buferror(int err, char *buf, size_t buflen);
  42. uintmax_t malloc_strtoumax(const char *restrict nptr, char **restrict endptr,
  43. int base);
  44. void malloc_write(const char *s);
  45. /*
  46. * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
  47. * point math.
  48. */
  49. size_t malloc_vsnprintf(char *str, size_t size, const char *format,
  50. va_list ap);
  51. size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
  52. JEMALLOC_FORMAT_PRINTF(3, 4);
  53. /*
  54. * The caller can set write_cb to null to choose to print with the
  55. * je_malloc_message hook.
  56. */
  57. void malloc_vcprintf(write_cb_t *write_cb, void *cbopaque, const char *format,
  58. va_list ap);
  59. void malloc_cprintf(write_cb_t *write_cb, void *cbopaque, const char *format,
  60. ...) JEMALLOC_FORMAT_PRINTF(3, 4);
  61. void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
  62. static inline ssize_t
  63. malloc_write_fd(int fd, const void *buf, size_t count) {
  64. #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)
  65. /*
  66. * Use syscall(2) rather than write(2) when possible in order to avoid
  67. * the possibility of memory allocation within libc. This is necessary
  68. * on FreeBSD; most operating systems do not have this problem though.
  69. *
  70. * syscall() returns long or int, depending on platform, so capture the
  71. * result in the widest plausible type to avoid compiler warnings.
  72. */
  73. long result = syscall(SYS_write, fd, buf, count);
  74. #else
  75. ssize_t result = (ssize_t)write(fd, buf,
  76. #ifdef _WIN32
  77. (unsigned int)
  78. #endif
  79. count);
  80. #endif
  81. return (ssize_t)result;
  82. }
  83. static inline ssize_t
  84. malloc_read_fd(int fd, void *buf, size_t count) {
  85. #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)
  86. long result = syscall(SYS_read, fd, buf, count);
  87. #else
  88. ssize_t result = read(fd, buf,
  89. #ifdef _WIN32
  90. (unsigned int)
  91. #endif
  92. count);
  93. #endif
  94. return (ssize_t)result;
  95. }
  96. #endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */