From 577714e3fe722625236ce060f53b5d76f7933454 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 26 Dec 2024 15:44:34 -0800 Subject: [PATCH 2/2] =?UTF-8?q?Don=E2=80=99t=20include=20stdlib.h=20from?= =?UTF-8?q?=20conf=5Fpost.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is brittle, as evinced by the recent problem with lib/stdlib.c. * src/conf_post.h: Move potential inclusion of stdlib.h and redefinitions of malloc, free, realloc, aligned_alloc, and calloc from here ... * src/lisp.h: ... to here. Do not redefine the symbols if UNEXMACOS_C is defined. * src/unexmacosx.c: Do not undef malloc, realloc, free. (UNEXMACOS_C): New symbol, to prevent lisp.h from defining them. --- src/conf_post.h | 49 ------------------------------------------------ src/gmalloc.c | 2 +- src/lisp.h | 35 ++++++++++++++++++++++++++++++++++ src/unexmacosx.c | 7 +------ 4 files changed, 37 insertions(+), 56 deletions(-) diff --git a/src/conf_post.h b/src/conf_post.h index 6aa2c901cc7..e204a85b973 100644 --- a/src/conf_post.h +++ b/src/conf_post.h @@ -93,55 +93,6 @@ Copyright (C) 1988, 1993-1994, 1999-2002, 2004-2024 Free Software # define ADDRESS_SANITIZER false #endif -#ifdef emacs -/* We include stdlib.h here, because Gnulib's stdlib.h might redirect - 'free' to its replacement, and we want to avoid that in unexec - builds. Including it here will render its inclusion after config.h - a no-op. */ -# if (defined DARWIN_OS && defined HAVE_UNEXEC) || defined HYBRID_MALLOC -# include -# endif -#endif - -#if defined DARWIN_OS && defined emacs && defined HAVE_UNEXEC -# undef malloc -# define malloc unexec_malloc -# undef realloc -# define realloc unexec_realloc -# undef free -# define free unexec_free - -extern void *unexec_malloc (size_t); -extern void *unexec_realloc (void *, size_t); -extern void unexec_free (void *); - -#endif - -/* If HYBRID_MALLOC is defined (e.g., on Cygwin), emacs will use - gmalloc before dumping and the system malloc after dumping. - hybrid_malloc and friends, defined in gmalloc.c, are wrappers that - accomplish this. */ -#ifdef HYBRID_MALLOC -#ifdef emacs -#undef malloc -#define malloc hybrid_malloc -#undef realloc -#define realloc hybrid_realloc -#undef aligned_alloc -#define aligned_alloc hybrid_aligned_alloc -#undef calloc -#define calloc hybrid_calloc -#undef free -#define free hybrid_free - -extern void *hybrid_malloc (size_t); -extern void *hybrid_calloc (size_t, size_t); -extern void hybrid_free (void *); -extern void *hybrid_aligned_alloc (size_t, size_t); -extern void *hybrid_realloc (void *, size_t); -#endif /* emacs */ -#endif /* HYBRID_MALLOC */ - /* We have to go this route, rather than the old hpux9 approach of renaming the functions via macros. The system's stdlib.h has fully prototyped declarations, which yields a conflicting definition of diff --git a/src/gmalloc.c b/src/gmalloc.c index 1faf6506167..d6c5d351925 100644 --- a/src/gmalloc.c +++ b/src/gmalloc.c @@ -62,7 +62,7 @@ #define USE_PTHREAD grealloc... via the macros that follow). The dumped emacs, however, will use the system malloc, realloc.... In other source files, malloc, realloc... are renamed hybrid_malloc, - hybrid_realloc... via macros in conf_post.h. hybrid_malloc and + hybrid_realloc... via macros in lisp.h. hybrid_malloc and friends are wrapper functions defined later in this file. */ #undef malloc #undef realloc diff --git a/src/lisp.h b/src/lisp.h index bf6b023fc2a..f3566ad2973 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4930,10 +4930,45 @@ #define safe_calln(...) \ /* Defined in unexmacosx.c. */ #if defined DARWIN_OS && defined HAVE_UNEXEC +/* Redirect calls to malloc, realloc and free to a macOS zone memory allocator. + FIXME: Either also redirect unexec_aligned_alloc and unexec_calloc, + or fix this comment to explain why those two redirections are not needed. */ extern void unexec_init_emacs_zone (void); extern void *unexec_malloc (size_t); extern void *unexec_realloc (void *, size_t); extern void unexec_free (void *); +# ifndef UNEXMACOSX_C +# include +# undef malloc +# undef realloc +# undef free +# define malloc unexec_malloc +# define realloc unexec_realloc +# define free unexec_free +# endif +#endif + +/* Defined in gmalloc.c. */ +#ifdef HYBRID_MALLOC +/* Redirect calls to malloc and friends to a hybrid allocator that + uses gmalloc before dumping and the system malloc after dumping. + This can be useful on Cygwin, for example. */ +extern void *hybrid_aligned_alloc (size_t, size_t); +extern void *hybrid_calloc (size_t, size_t); +extern void *hybrid_malloc (size_t); +extern void *hybrid_realloc (void *, size_t); +extern void hybrid_free (void *); +# include +# undef aligned_alloc +# undef calloc +# undef malloc +# undef realloc +# undef free +# define aligned_alloc hybrid_aligned_alloc +# define calloc hybrid_calloc +# define malloc hybrid_malloc +# define realloc hybrid_realloc +# define free hybrid_free #endif /* The definition of Lisp_Module_Function depends on emacs-module.h, diff --git a/src/unexmacosx.c b/src/unexmacosx.c index 7b2326441b4..d6c7686607f 100644 --- a/src/unexmacosx.c +++ b/src/unexmacosx.c @@ -87,15 +87,10 @@ #include -/* Although redefines malloc to unexec_malloc, etc., this - file wants stdlib.h to declare the originals. */ -#undef malloc -#undef realloc -#undef free - #include #include "unexec.h" +#define UNEXMACOSX_C /* Tell lisp.h we want the system malloc, etc. */ #include "lisp.h" #include "sysstdio.h" -- 2.45.2