]> git.eshelyaron.com Git - emacs.git/commitdiff
Don’t include stdlib.h from conf_post.h
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 26 Dec 2024 23:44:34 +0000 (15:44 -0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 27 Dec 2024 15:45:19 +0000 (16:45 +0100)
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.

(cherry picked from commit 577714e3fe722625236ce060f53b5d76f7933454)

src/conf_post.h
src/gmalloc.c
src/lisp.h
src/unexmacosx.c

index 6aa2c901cc7078b5ba82bb7248130f363df49096..e204a85b9730e021c7e5ee62f1f2febf14c3d1da 100644 (file)
@@ -93,55 +93,6 @@ typedef bool bool_bf;
 # 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 <stdlib.h>
-# 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
index 1faf6506167870dcc4e5915b8810068001e8bfc4..d6c5d35192546f2c3b31bf2136118cb9b757eef4 100644 (file)
@@ -62,7 +62,7 @@ extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void);
    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
index bf6b023fc2a27be34d7c8107cd8a9a156ec2c38a..f3566ad29738b91e73a2c2f37050ff4c386eff18 100644 (file)
@@ -4930,10 +4930,45 @@ Lisp_Object funcall_general (Lisp_Object fun,
 
 /* 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 <stdlib.h>
+#  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 <stdlib.h>
+# 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,
index 7b2326441b486acc928367535e9ed7a94782a5d5..d6c7686607f501352f623cc73ef367c59905b4f8 100644 (file)
@@ -87,15 +87,10 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 
-/* Although <config.h> redefines malloc to unexec_malloc, etc., this
-   file wants stdlib.h to declare the originals.  */
-#undef malloc
-#undef realloc
-#undef free
-
 #include <stdlib.h>
 
 #include "unexec.h"
+#define UNEXMACOSX_C /* Tell lisp.h we want the system malloc, etc.  */
 #include "lisp.h"
 #include "sysstdio.h"