emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] /srv/bzr/emacs/trunk r105471: Fix memory allocation proble


From: Ken Brown
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r105471: Fix memory allocation problems in Cygwin build (Bug#9273).
Date: Tue, 16 Aug 2011 09:27:12 -0400
User-agent: Bazaar (2.3.1)

------------------------------------------------------------
revno: 105471
committer: Ken Brown <address@hidden>
branch nick: trunk
timestamp: Tue 2011-08-16 09:27:12 -0400
message:
  Fix memory allocation problems in Cygwin build (Bug#9273).
  
  * src/gmalloc.c [CYGWIN] (bss_sbrk_heapbase, bss_sbrk_heapinfo): New
  variables.
  (malloc_initialize_1) [CYGWIN]: Prepare for reinitializing the
  dumped emacs.
  (_free_internal_nolock) [CYGWIN]: Ignore requests to free storage
  in the static heap.
  [CYGWIN] (special_realloc): New function.
  (_realloc_internal_nolock) [CYGWIN]: Use the new function on
  requests to realloc storage in the static heap.
  
  * src/unexcw.c ( __malloc_initialized): Declare external variable.
  (fixup_executable): Force the dumped emacs to reinitialize malloc.
modified:
  src/ChangeLog
  src/gmalloc.c
  src/unexcw.c
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2011-08-15 18:54:18 +0000
+++ b/src/ChangeLog     2011-08-16 13:27:12 +0000
@@ -1,3 +1,20 @@
+2011-08-16  Ken Brown  <address@hidden>
+
+       Fix memory allocation problems in Cygwin build (Bug#9273).
+
+       * unexcw.c ( __malloc_initialized): Declare external variable.
+       (fixup_executable): Force the dumped emacs to reinitialize malloc.
+
+       * gmalloc.c [CYGWIN] (bss_sbrk_heapbase, bss_sbrk_heapinfo): New
+       variables.
+       (malloc_initialize_1) [CYGWIN]: Prepare for reinitializing the
+       dumped emacs.
+       (_free_internal_nolock) [CYGWIN]: Ignore requests to free storage
+       in the static heap.
+       [CYGWIN] (special_realloc): New function.
+       (_realloc_internal_nolock) [CYGWIN]: Use the new function on
+       requests to realloc storage in the static heap.
+
 2011-08-15  Paul Eggert  <address@hidden>
 
        * bidi.c (bidi_initialize): Remove unused local.

=== modified file 'src/gmalloc.c'
--- a/src/gmalloc.c     2011-08-04 17:04:39 +0000
+++ b/src/gmalloc.c     2011-08-16 13:27:12 +0000
@@ -351,10 +351,17 @@
 #endif
 #include <errno.h>
 
-/* How to really get more memory.  */
-#if defined(CYGWIN)
+/* On Cygwin there are two heaps.  temacs uses the static heap
+   (defined in sheap.c and managed with bss_sbrk), and the dumped
+   emacs uses the Cygwin heap (managed with sbrk).  When emacs starts
+   on Cygwin, it reinitializes malloc, and we save the old info for
+   use by free and realloc if they're called with a pointer into the
+   static heap. */
+#ifdef CYGWIN
 extern __ptr_t bss_sbrk PP ((ptrdiff_t __size));
 extern int bss_sbrk_did_unexec;
+char *bss_sbrk_heapbase;       /* _heapbase for static heap */
+malloc_info *bss_sbrk_heapinfo;        /* _heapinfo for static heap */
 #endif
 __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)) = __default_morecore;
 
@@ -584,6 +591,16 @@
   mcheck (NULL);
 #endif
 
+#ifdef CYGWIN
+  if (bss_sbrk_did_unexec)
+    /* we're reinitializing the dumped emacs */
+    {
+      bss_sbrk_heapbase = _heapbase;
+      bss_sbrk_heapinfo = _heapinfo;
+      memset (_fraghead, 0, BLOCKLOG * sizeof (struct list));
+    }
+#endif
+
   if (__malloc_initialize_hook)
     (*__malloc_initialize_hook) ();
 
@@ -1054,6 +1071,12 @@
   if (ptr == NULL)
     return;
 
+#ifdef CYGWIN
+  if (ptr < _heapbase)
+    /* We're being asked to free something in the static heap. */
+    return;
+#endif
+
   PROTECT_MALLOC_STATE (0);
 
   LOCK_ALIGNED_BLOCKS ();
@@ -1349,6 +1372,31 @@
 
 #define min(A, B) ((A) < (B) ? (A) : (B))
 
+/* On Cygwin the dumped emacs may try to realloc storage allocated in
+   the static heap.  We just malloc space in the new heap and copy the
+   data.  */
+#ifdef CYGWIN
+__ptr_t
+special_realloc (ptr, size)
+     __ptr_t ptr;
+     __malloc_size_t size;
+{
+  __ptr_t result;
+  int type;
+  __malloc_size_t block, oldsize;
+
+  block = ((char *) ptr - bss_sbrk_heapbase) / BLOCKSIZE + 1;
+  type = bss_sbrk_heapinfo[block].busy.type;
+  oldsize =
+    type == 0 ? bss_sbrk_heapinfo[block].busy.info.size * BLOCKSIZE
+    : (__malloc_size_t) 1 << type;
+  result = _malloc_internal_nolock (size);
+  if (result != NULL)
+    memcpy (result, ptr, min (oldsize, size));
+  return result;
+}
+#endif
+
 /* Debugging hook for realloc.  */
 __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
 
@@ -1375,6 +1423,12 @@
   else if (ptr == NULL)
     return _malloc_internal_nolock (size);
 
+#ifdef CYGWIN
+  if (ptr < _heapbase)
+    /* ptr points into the static heap */
+    return special_realloc (ptr, size);
+#endif
+
   block = BLOCK (ptr);
 
   PROTECT_MALLOC_STATE (0);

=== modified file 'src/unexcw.c'
--- a/src/unexcw.c      2011-03-17 20:18:59 +0000
+++ b/src/unexcw.c      2011-08-16 13:27:12 +0000
@@ -33,6 +33,8 @@
 
 extern int bss_sbrk_did_unexec;
 
+extern int __malloc_initialized;
+
 /* emacs symbols that indicate where bss and data end for emacs internals */
 extern char my_endbss[];
 extern char my_edata[];
@@ -210,9 +212,12 @@
            lseek (fd, (long) (exe_header->section_header[i].s_scnptr),
                   SEEK_SET);
          assert (ret != -1);
+         /* force the dumped emacs to reinitialize malloc */
+         __malloc_initialized = 0;
          ret =
            write (fd, (char *) start_address,
                   my_endbss - (char *) start_address);
+         __malloc_initialized = 1;
          assert (ret == (my_endbss - (char *) start_address));
          if (debug_unexcw)
            printf ("         .bss, mem start 0x%08x mem length %d\n",


reply via email to

[Prev in Thread] Current Thread [Next in Thread]