all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jussi Lahdenniemi <jussi@aprikoodi.fi>
To: Eli Zaretskii <eliz@gnu.org>,
	Fabrice Popineau <fabrice.popineau@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Windows 9X crash
Date: Fri, 15 Jan 2016 11:35:20 +0200	[thread overview]
Message-ID: <5698BD58.2090708@aprikoodi.fi> (raw)
In-Reply-To: <831t9jgt8y.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 866 bytes --]

On 15.1.2016 10.08, Eli Zaretskii wrote:
> Right, this can explain everything.  It probably means that a build
> configured --with-wide-int should also be tested there, as it has
> different alignment needs.

I'll test that as well, then.

>> How should we fix this?  I can write and test the fix, but I'd like to
>> hear your opinion on the preferred mechanism.
>
> What fix did you have in mind?  Over-allocating and recording the
> offset in the initial part of the block that we don't pass to the
> application?

That was my first idea, yes.  Of course, this introduces some memory 
overhead, especially if there are many small allocations.

A proposal patch is attached.  Reallocs needed some additional care, as 
there are two special cases to handle (source being in dumped memory, 
and source and destination alignments mismatching).

-- 
Jussi Lahdenniemi


[-- Attachment #2: win98-heap-fix.diff --]
[-- Type: text/plain, Size: 3478 bytes --]

diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h
index cdbfac0..a37510e 100644
--- a/nt/inc/ms-w32.h
+++ b/nt/inc/ms-w32.h
@@ -457,6 +457,10 @@ extern void *malloc_after_dump(size_t);
 extern void *realloc_after_dump(void *, size_t);
 extern void free_after_dump(void *);
 
+extern void *malloc_after_dump_9x(size_t);
+extern void *realloc_after_dump_9x(void *, size_t);
+extern void free_after_dump_9x(void *);
+
 extern malloc_fn the_malloc_fn;
 extern realloc_fn the_realloc_fn;
 extern free_fn the_free_fn;
diff --git a/src/w32heap.c b/src/w32heap.c
index 54646bf..3f6ed67 100644
--- a/src/w32heap.c
+++ b/src/w32heap.c
@@ -258,9 +258,18 @@ init_heap (void)
 	}
 #endif
 
-      the_malloc_fn = malloc_after_dump;
-      the_realloc_fn = realloc_after_dump;
-      the_free_fn = free_after_dump;
+      if (os_subtype == OS_9X)
+        {
+          the_malloc_fn = malloc_after_dump_9x;
+          the_realloc_fn = realloc_after_dump_9x;
+          the_free_fn = free_after_dump_9x;
+        }
+      else
+        {
+          the_malloc_fn = malloc_after_dump;
+          the_realloc_fn = realloc_after_dump;
+          the_free_fn = free_after_dump;
+        }
     }
   else
     {
@@ -291,9 +300,18 @@ init_heap (void)
 	  exit (-1);
 	}
       heap = s_pfn_Rtl_Create_Heap (0, data_region_base, 0, 0, NULL, &params);
-      the_malloc_fn = malloc_before_dump;
-      the_realloc_fn = realloc_before_dump;
-      the_free_fn = free_before_dump;
+
+      if (os_subtype == OS_9X)
+        {
+          fprintf (stderr, "Cannot run pre-dump Emacs on Windows 9X.\n" );
+          exit (-1);
+        }
+      else
+        {
+          the_malloc_fn = malloc_before_dump;
+          the_realloc_fn = realloc_before_dump;
+          the_free_fn = free_before_dump;
+        }
     }
 
   /* Update system version information to match current system.  */
@@ -504,6 +522,65 @@ free_before_dump (void *ptr)
     }
 }
 
+/* On Windows 98, HeapAlloc may return pointers that are not aligned
+   to 8-byte boundary, which is expected by the Lisp memory
+   management.  To circumvent this problem, manually enforce alignment
+   on Windows 9X.  */
+
+void *
+malloc_after_dump_9x (size_t size)
+{
+  void *p = malloc_after_dump (size + 8);
+  void *pa;
+  if (p == NULL)
+    return p;
+  pa = (void*)(((intptr_t)p + 8) & ~7);
+  *((void**)pa-1) = p;
+  return pa;
+}
+
+void *
+realloc_after_dump_9x (void *ptr, size_t size)
+{
+  if (FREEABLE_P (ptr))
+    {
+      void *po = *((void**)ptr-1);
+      void *p;
+      void *pa;
+      p = realloc_after_dump (po, size + 8);
+      if (p == NULL)
+        return p;
+      pa = (void*)(((intptr_t)p + 8) & ~7);
+      if (ptr != NULL &&
+          (char*)pa - (char*)p != (char*)ptr - (char*)po)
+        {
+          /* Handle the case where alignment in pre-realloc and
+             post-realloc blocks do not match.  */
+          MoveMemory (pa, (void*)((char*)p + ((char*)ptr - (char*)po)), size);
+        }
+      *((void**)pa-1) = p;
+      return pa;
+    }
+  else
+    {
+      /* Non-freeable pointers have no alignment-enforcing header
+         (since dumping is not allowed on Windows 9X).  */
+      void* p = malloc_after_dump_9x (size);
+      if (p != NULL)
+	CopyMemory (p, ptr, size);
+      return p;
+    }
+}
+
+void
+free_after_dump_9x (void *ptr)
+{
+  if (FREEABLE_P (ptr))
+    {
+      free_after_dump (*((void**)ptr-1));
+    }
+}
+
 #ifdef ENABLE_CHECKING
 void
 report_temacs_memory_usage (void)

  reply	other threads:[~2016-01-15  9:35 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-05 12:51 [PATCH] Override Windows default Win-* key combinations when using Emacs Jussi Lahdenniemi
2016-01-05 13:54 ` Herring, Davis
2016-01-05 17:05   ` Eli Zaretskii
2016-01-05 19:32     ` Jussi Lahdenniemi
2016-01-05 17:05 ` Eli Zaretskii
2016-01-05 19:03   ` Rasmus
2016-01-05 19:17     ` Eli Zaretskii
2016-01-05 19:31       ` Jussi Lahdenniemi
     [not found]     ` <<83d1tf4z0h.fsf@gnu.org>
2016-01-05 19:45       ` Drew Adams
2016-01-05 19:41   ` Jussi Lahdenniemi
2016-01-05 20:01     ` Eli Zaretskii
2016-01-09 19:58   ` Jussi Lahdenniemi
2016-01-11 18:54     ` Eli Zaretskii
2016-01-12 11:16       ` Jussi Lahdenniemi
2016-01-12 15:08         ` covici
2016-01-13  5:07           ` Jussi Lahdenniemi
2016-01-12 15:54         ` Eli Zaretskii
2016-01-13  5:31           ` Jussi Lahdenniemi
2016-01-13 15:49             ` Eli Zaretskii
2016-01-13  9:16           ` Jussi Lahdenniemi
2016-01-13 11:28             ` Jussi Lahdenniemi
2016-01-13 15:57               ` Eli Zaretskii
2016-01-14 12:49                 ` Jussi Lahdenniemi
2016-01-14 17:10                   ` Jussi Lahdenniemi
2016-01-14 18:07                     ` Eli Zaretskii
2016-01-14 18:15                   ` Eli Zaretskii
2016-01-15  6:56                     ` Jussi Lahdenniemi
2016-01-15  7:52                       ` Jussi Lahdenniemi
2016-01-15  8:08                         ` Windows 9X crash (was: [PATCH] Override Windows default Win-* key combinations when using Emacs) Eli Zaretskii
2016-01-15  9:35                           ` Jussi Lahdenniemi [this message]
2016-01-15 10:18                             ` Windows 9X crash Eli Zaretskii
2016-01-15 10:31                               ` Fabrice Popineau
2016-01-15 10:33                               ` Jussi Lahdenniemi
2016-01-16  9:15                                 ` Eli Zaretskii
2016-01-15  9:47                           ` Windows 9X crash (was: [PATCH] Override Windows default Win-* key combinations when using Emacs) Fabrice Popineau
2016-01-15  9:56                             ` Windows 9X crash Jussi Lahdenniemi
2016-01-15 10:19                               ` Eli Zaretskii
2016-01-15  9:23                         ` [PATCH] Override Windows default Win-* key combinations when using Emacs Yuri Khan
2016-01-16  9:58                     ` Eli Zaretskii
2016-01-16 11:00                       ` Jussi Lahdenniemi
2016-01-16 11:41                         ` Eli Zaretskii
2016-02-03 13:59                         ` Jussi Lahdenniemi
2016-02-03 15:42                           ` Eli Zaretskii
2016-02-26 10:55                             ` Eli Zaretskii
2016-02-26 10:59                               ` Jussi Lahdenniemi
2016-02-27 11:34                                 ` Eli Zaretskii
2016-02-28  8:42                                   ` Paul Eggert
2016-02-28 15:50                                     ` Eli Zaretskii
2016-02-28 20:03                                       ` Paul Eggert
2016-02-28 20:10                                         ` Eli Zaretskii
2016-02-28 23:27                                           ` Paul Eggert
2016-02-29  3:31                                             ` Eli Zaretskii
2016-02-29  7:07                                               ` Paul Eggert
2016-02-29 15:52                                                 ` Eli Zaretskii
2016-02-29 19:01                                                   ` Paul Eggert
2016-03-05 10:16                                                     ` Eli Zaretskii
2016-03-08  2:57                                                       ` Paul Eggert
2016-03-08  4:46                                                         ` Clément Pit--Claudel
2016-02-28 23:33                                         ` John Wiegley
2016-02-27  2:11                               ` Lars Ingebrigtsen
2016-02-27  7:47                                 ` Eli Zaretskii
2016-02-28  4:40                                   ` Lars Ingebrigtsen
2016-01-13 15:53             ` Eli Zaretskii
     [not found] <<568BBC58.50702@aprikoodi.fi>

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5698BD58.2090708@aprikoodi.fi \
    --to=jussi@aprikoodi.fi \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=fabrice.popineau@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.