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)
# 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
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
/* 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,
#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"