From 1ec505b75159af4f206ed3864ccb0df6378d0835 Mon Sep 17 00:00:00 2001 From: guangli-dai Date: Tue, 28 Jul 2026 14:39:30 -0700 Subject: [PATCH] Move FMTd32-family printf macros to OS layer Move macros defining the FMT module to OS layer. --- include/jemalloc/internal/malloc_io.h | 39 +++++----------------- include/jemalloc/internal/os.h | 7 ++++ include/jemalloc/internal/os/fmt.h | 25 ++++++++++++++ include/jemalloc/internal/os/posix/fmt.h | 16 +++++++++ include/jemalloc/internal/os/windows/fmt.h | 21 ++++++++++++ 5 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 include/jemalloc/internal/os/fmt.h create mode 100644 include/jemalloc/internal/os/posix/fmt.h create mode 100644 include/jemalloc/internal/os/windows/fmt.h diff --git a/include/jemalloc/internal/malloc_io.h b/include/jemalloc/internal/malloc_io.h index 99b0fca3..54ee7332 100644 --- a/include/jemalloc/internal/malloc_io.h +++ b/include/jemalloc/internal/malloc_io.h @@ -3,36 +3,15 @@ #include "jemalloc/internal/jemalloc_preamble.h" #include "jemalloc/internal/jemalloc_internal_types.h" - -#ifdef _WIN32 -# ifdef _WIN64 -# define FMT64_PREFIX "ll" -# define FMTPTR_PREFIX "ll" -# else -# define FMT64_PREFIX "ll" -# define FMTPTR_PREFIX "" -# endif -# define FMTd32 "d" -# define FMTu32 "u" -# define FMTx32 "x" -# define FMTd64 FMT64_PREFIX "d" -# define FMTu64 FMT64_PREFIX "u" -# define FMTx64 FMT64_PREFIX "x" -# define FMTdPTR FMTPTR_PREFIX "d" -# define FMTuPTR FMTPTR_PREFIX "u" -# define FMTxPTR FMTPTR_PREFIX "x" -#else -# include -# define FMTd32 PRId32 -# define FMTu32 PRIu32 -# define FMTx32 PRIx32 -# define FMTd64 PRId64 -# define FMTu64 PRIu64 -# define FMTx64 PRIx64 -# define FMTdPTR PRIdPTR -# define FMTuPTR PRIuPTR -# define FMTxPTR PRIxPTR -#endif +/* + * os/fmt.h directly (not the os.h umbrella): this header is itself included + * from os/posix/error.h (for malloc_snprintf's declaration), so pulling in + * the full os.h here would risk the same circular-include hazard os/error.h + * avoids by not going through the umbrella either. os/fmt.h is a leaf module + * (macros only, no dependency back on malloc_io.h or anything else), so + * including it alone is safe. + */ +#include "jemalloc/internal/os/fmt.h" /* Size of stack-allocated buffer passed to buferror(). */ #define BUFERROR_BUF 64 diff --git a/include/jemalloc/internal/os.h b/include/jemalloc/internal/os.h index b66a1943..b3b01c64 100644 --- a/include/jemalloc/internal/os.h +++ b/include/jemalloc/internal/os.h @@ -56,4 +56,11 @@ */ #include "jemalloc/internal/os/error.h" +/* + * Printf format-specifier macros. malloc_io.h includes os/fmt.h directly + * (not via this umbrella) for the same early-reachability reason as + * os/error.h above. Included here too for discoverability. + */ +#include "jemalloc/internal/os/fmt.h" + #endif /* JEMALLOC_INTERNAL_OS_H */ diff --git a/include/jemalloc/internal/os/fmt.h b/include/jemalloc/internal/os/fmt.h new file mode 100644 index 00000000..520181cb --- /dev/null +++ b/include/jemalloc/internal/os/fmt.h @@ -0,0 +1,25 @@ +#ifndef JEMALLOC_INTERNAL_OS_FMT_H +#define JEMALLOC_INTERNAL_OS_FMT_H + +#include "jemalloc/internal/jemalloc_preamble.h" +#include "jemalloc/internal/os/detect.h" + +/* + * Printf format-specifier macros for fixed-width and pointer-sized integers + * (FMTd32/FMTu32/FMTx32/FMTd64/FMTu64/FMTx64/FMTdPTR/FMTuPTR/FMTxPTR). + * Default: posix/ ('s PRId32 etc). Override: Windows (MSVC's + * CRT historically didn't support the C99 macros the same way, + * so these are hand-rolled from an ll/I64-style prefix instead). + * + * Unlike other modules, this one defines only macros, no functions. + */ + +#if defined(_WIN32) +# include "jemalloc/internal/os/windows/fmt.h" +#elif defined(JEMALLOC_OS_POSIX) +# include "jemalloc/internal/os/posix/fmt.h" +#else +# error "OS layer: no fmt backend for this platform; add os//fmt.h" +#endif + +#endif /* JEMALLOC_INTERNAL_OS_FMT_H */ diff --git a/include/jemalloc/internal/os/posix/fmt.h b/include/jemalloc/internal/os/posix/fmt.h new file mode 100644 index 00000000..1a71dcdd --- /dev/null +++ b/include/jemalloc/internal/os/posix/fmt.h @@ -0,0 +1,16 @@ +#ifndef JEMALLOC_INTERNAL_OS_POSIX_FMT_H +#define JEMALLOC_INTERNAL_OS_POSIX_FMT_H + +#include + +#define FMTd32 PRId32 +#define FMTu32 PRIu32 +#define FMTx32 PRIx32 +#define FMTd64 PRId64 +#define FMTu64 PRIu64 +#define FMTx64 PRIx64 +#define FMTdPTR PRIdPTR +#define FMTuPTR PRIuPTR +#define FMTxPTR PRIxPTR + +#endif /* JEMALLOC_INTERNAL_OS_POSIX_FMT_H */ diff --git a/include/jemalloc/internal/os/windows/fmt.h b/include/jemalloc/internal/os/windows/fmt.h new file mode 100644 index 00000000..366c8988 --- /dev/null +++ b/include/jemalloc/internal/os/windows/fmt.h @@ -0,0 +1,21 @@ +#ifndef JEMALLOC_INTERNAL_OS_WINDOWS_FMT_H +#define JEMALLOC_INTERNAL_OS_WINDOWS_FMT_H + +#ifdef _WIN64 +# define FMT64_PREFIX "ll" +# define FMTPTR_PREFIX "ll" +#else +# define FMT64_PREFIX "ll" +# define FMTPTR_PREFIX "" +#endif +#define FMTd32 "d" +#define FMTu32 "u" +#define FMTx32 "x" +#define FMTd64 FMT64_PREFIX "d" +#define FMTu64 FMT64_PREFIX "u" +#define FMTx64 FMT64_PREFIX "x" +#define FMTdPTR FMTPTR_PREFIX "d" +#define FMTuPTR FMTPTR_PREFIX "u" +#define FMTxPTR FMTPTR_PREFIX "x" + +#endif /* JEMALLOC_INTERNAL_OS_WINDOWS_FMT_H */