]> git.eshelyaron.com Git - emacs.git/commitdiff
(unexec): Use mmap/munmap to allocate buffers
authorGerd Moellmann <gerd@gnu.org>
Sat, 10 Nov 2001 11:40:04 +0000 (11:40 +0000)
committerGerd Moellmann <gerd@gnu.org>
Sat, 10 Nov 2001 11:40:04 +0000 (11:40 +0000)
instead of malloc/free.

src/ChangeLog
src/unexelf.c

index 4ac5ee529670b94eb0087882fda633e0a5b4f47b..0a19005c33ff21fcc46402daa524a66fbd8d6567 100644 (file)
@@ -1,3 +1,8 @@
+2001-11-10  Gerd Moellmann  <gerd@gnu.org>
+
+       * unexelf.c (unexec): Use mmap/munmap to allocate buffers 
+       instead of malloc/free.
+
 2001-11-09  Pavel Jan\e,Bm\e(Bk  <Pavel@Janik.cz>
 
        * xfaces.c (merge_face_vectors): Use braces to follow GNU
index 222de55ed56fcff698f7d0eb661f8cc1e9f464c3..8ea893e532f717e0def38545b83d3c9a4a1ba629 100644 (file)
@@ -668,6 +668,7 @@ unexec (new_name, old_name, data_start, bss_start, entry_address)
   int old_data_index, new_data2_index;
   int old_mdebug_index;
   struct stat stat_buf;
+  int old_file_size;
 
   /* Open the old file, allocate a buffer of the right size, and read
      in the file contents.  */
@@ -680,16 +681,16 @@ unexec (new_name, old_name, data_start, bss_start, entry_address)
   if (fstat (old_file, &stat_buf) == -1)
     fatal ("Can't fstat (%s): errno %d\n", old_name, errno);
 
-  old_base = malloc (stat_buf.st_size);
-
-  if (old_base == 0)
+  /* We cannot use malloc here because that may use sbrk.  If it does,
+     we'd dump our temporary buffers with Emacs, and we'd have to be
+     extra careful to use the correct value of sbrk(0) after
+     allocating all buffers in the code below, which we aren't.  */
+  old_file_size = stat_buf.st_size;
+  old_base = mmap (NULL, old_file_size, PROT_READ | PROT_WRITE,
+                  MAP_ANON | MAP_PRIVATE, -1, 0);
+  if (old_base == (caddr_t) -1)
     fatal ("Can't allocate buffer for %s\n", old_name);
 
-#ifdef DEBUG
-  fprintf (stderr, "%s: malloc(%d) -> %x\n", old_name, stat_buf.st_size,
-          old_base);
-#endif
-
   if (read (old_file, old_base, stat_buf.st_size) != stat_buf.st_size)
     fatal ("Didn't read all of %s: errno %d\n", old_name, errno);
 
@@ -774,16 +775,11 @@ unexec (new_name, old_name, data_start, bss_start, entry_address)
   if (ftruncate (new_file, new_file_size))
     fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
 
-  new_base = malloc (new_file_size);
-
-  if (new_base == 0)
+  new_base = mmap (NULL, new_file_size, PROT_READ | PROT_WRITE,
+                  MAP_ANON | MAP_PRIVATE, -1, 0);
+  if (new_base == (caddr_t) -1)
     fatal ("Can't allocate buffer for %s\n", old_name);
 
-#ifdef DEBUG
-  fprintf (stderr, "%s: malloc(%d) -> %x\n", new_name, new_file_size
-          new_base);
-#endif
-
   new_file_h = (ElfW(Ehdr) *) new_base;
   new_program_h = (ElfW(Phdr) *) ((byte *) new_base + old_file_h->e_phoff);
   new_section_h = (ElfW(Shdr) *)
@@ -1202,8 +1198,8 @@ unexec (new_name, old_name, data_start, bss_start, entry_address)
     fatal ("Didn't write %d bytes to %s: errno %d\n", 
           new_file_size, new_base, errno);
 
-  free (old_base);
-  free (new_base);
+  munmap (old_base, old_file_size);
+  munmap (new_base, new_file_size);
 
   /* Close the files and make the new file executable.  */