[DOUG_LEA_MALLOC] (malloc_initialize_hook):
[bpt/emacs.git] / src / emacs.c
index 859e1dd..5721c2c 100644 (file)
@@ -545,27 +545,17 @@ argmatch (argv, argc, sstr, lstr, minlen, valptr, skipptr)
     }
 }
 
-/* ARGSUSED */
-int
-main (argc, argv, envp)
-     int argc;
-     char **argv;
-     char **envp;
-{
-  char stack_bottom_variable;
-  int skip_args = 0;
-  extern int errno;
-  extern int sys_nerr;
-#ifdef HAVE_SETRLIMIT
-  struct rlimit rlim;
-#endif
-  int no_loadup = 0;
+#ifdef DOUG_LEA_MALLOC
 
-#ifdef LINUX_SBRK_BUG
-  __sbrk (1);
-#endif
+/* malloc can be invoked even before main (e.g. by the dynamic
+   linker), so the dumped malloc state must be restored as early as
+   possible using this special hook.  */
+
+static void
+malloc_initialize_hook ()
+{
+  extern char **environ;
 
-#ifdef DOUG_LEA_MALLOC
   if (initialized)
     {
       if (!malloc_using_checking)
@@ -575,7 +565,7 @@ main (argc, argv, envp)
        {
          char **p;
 
-         for (p = envp; *p; p++)
+         for (p = environ; *p; p++)
            if (strncmp (*p, "MALLOC_CHECK_=", 14) == 0)
              {
                do
@@ -584,11 +574,36 @@ main (argc, argv, envp)
                break;
              }
        }
+
       malloc_set_state (malloc_state_ptr);
       free (malloc_state_ptr);
     }
   else
     malloc_using_checking = getenv ("MALLOC_CHECK_") != NULL;
+}
+
+void (*__malloc_initialize_hook) () = malloc_initialize_hook;
+
+#endif /* DOUG_LEA_MALLOC */
+
+/* ARGSUSED */
+int
+main (argc, argv, envp)
+     int argc;
+     char **argv;
+     char **envp;
+{
+  char stack_bottom_variable;
+  int skip_args = 0;
+  extern int errno;
+  extern int sys_nerr;
+#ifdef HAVE_SETRLIMIT
+  struct rlimit rlim;
+#endif
+  int no_loadup = 0;
+
+#ifdef LINUX_SBRK_BUG
+  __sbrk (1);
 #endif
 
 #ifdef RUN_TIME_REMAP