diff src/window.c @ 617:af57a77cbc92

[xemacs-hg @ 2001-06-18 07:09:50 by ben] --------------------------------------------------------------- DOCUMENTATION FIXES: --------------------------------------------------------------- eval.c: Correct documentation. elhash.c: Doc correction. --------------------------------------------------------------- LISP OBJECT CLEANUP: --------------------------------------------------------------- bytecode.h, buffer.h, casetab.h, chartab.h, console-msw.h, console.h, database.c, device.h, eldap.h, elhash.h, events.h, extents.h, faces.h, file-coding.h, frame.h, glyphs.h, gui-x.h, gui.h, keymap.h, lisp-disunion.h, lisp-union.h, lisp.h, lrecord.h, lstream.h, mule-charset.h, objects.h, opaque.h, postgresql.h, process.h, rangetab.h, specifier.h, toolbar.h, tooltalk.h, ui-gtk.h: Add wrap_* to all objects (it was already there for a few of them) -- an expression to encapsulate a pointer into a Lisp object, rather than the inconvenient XSET*. "wrap" was chosen because "make" as in make_int(), make_char() is not appropriate. (It implies allocation. The issue does not exist for ints and chars because they are not allocated.) Full error checking has been added to these expressions. When used without error checking, non-union build, use of these expressions will incur no loss of efficiency. (In fact, XSET* is now defined in terms of wrap_* in a non-union build.) In a union build, you will also get no loss of efficiency provided that you have a decent optimizing compiler, and a compiler that either understands inlines or automatically inlines those particular functions. (And since people don't normally do their production builds on union, it doesn't matter.) Update the sample Lisp object definition in lrecord.h accordingly. dumper.c: Fix places in dumper that referenced wrap_object to reference its new name, wrap_pointer_1. buffer.c, bufslots.h, conslots.h, console.c, console.h, devslots.h, device.c, device.h, frame.c, frame.h, frameslots.h, window.c, window.h, winslots.h: -- Extract out the Lisp objects of `struct device' into devslots.h, just like for the other structures. -- Extract out the remaining (not copied into the window config) Lisp objects in `struct window' into winslots.h; use different macros (WINDOW_SLOT vs. WINDOW_SAVED_SLOT) to differentiate them. -- Eliminate the `dead' flag of `struct frame', since it duplicates information already available in `framemeths', and fix FRAME_LIVE_P accordingly. (Devices and consoles already work this way.) -- In *slots.h, switch to system where MARKED_SLOT is automatically undef'd at the end of the file. (Follows what winslots.h already does.) -- Update the comments at the beginning of *slots.h to be accurate. -- When making any of the above objects dead, zero it out entirely and reset all Lisp object slots to Qnil. (We were already doing this somewhat, but not consistently.) This (1) Eliminates the possibility of extra objects hanging around that ought to be GC'd, (2) Causes an immediate crash if anyone tries to access a structure in one of these objects, (3) Ensures consistent behavior wrt dead objects. dialog-msw.c: Use internal_object_printer, since this object should not escape. --------------------------------------------------------------- FIXING A CRASH THAT I HIT ONCE (AND A RELATED BAD BEHAVIOR): --------------------------------------------------------------- eval.c: Fix up some comments about the FSF implementation. Fix two nasty bugs: (1) condition_case_unwind frees the conses sitting in the catch->tag slot too quickly, resulting in a crash that I hit. (2) catches need to be unwound one at a time when calling unwind-protect code, rather than all at once at the end; otherwise, incorrect behavior can result. (A comment shows exactly how.) backtrace.h: Improve comment about FSF differences in the handler stack. --------------------------------------------------------------- FIXING A CRASH THAT I REPEATEDLY HIT WHEN USING THE MOUSE WHEEL UNDER MSWINDOWS: --------------------------------------------------------------- Basic idea: My crash is due either to a dead, non-marked, GC-collected frame inside of a window mirror, or a prematurely freed window mirror. We need to mark the Lisp objects inside of window mirrors. Tracking the lifespan of window mirrors and scrollbar instances is extremely hard, and there may well be lurking bugs where such objects are freed too soon. The only safe way to fix these problems (and it fixes both problems at once) is to make both of these structures Lisp objects. lrecord.h, emacs.c, inline.c, scrollbar-gtk.c, scrollbar-msw.c, scrollbar-x.c, scrollbar.c, scrollbar.h, symsinit.h: Make scrollbar instances actual Lisp objects. Mark the window mirrors in them. inline.c needs to know about scrollbar.h now. Record the new type in lrecord.h. Fix up scrollbar-*.c appropriately. Create a hash table in scrollbar-msw.c so that the scrollbar instances stored in scrollbar HWND's are properly GC-protected. Create complex_vars_of_scrollbar_mswindows() to create the hash table at startup, and call it from emacs.c. Don't store the scrollbar instance as a property of the GTK scrollbar, as it's not used and if we did this, we'd have to separately GC-protect it in a hash table, like in MS Windows. lrecord.h, frame.h, frame.c, frameslots.h, redisplay.c, window.c, window.h: Move mark_window_mirror from redisplay.c to window.c. Make window mirrors actual Lisp objects. Tell lrecord.h about them. Change the window mirror member of struct frame from a pointer to a Lisp object, and add XWINDOW_MIRROR in appropriate places. Mark the scrollbar instances in the window mirror. redisplay.c, redisplay.h, alloc.c: Delete mark_redisplay. Don't call mark_redisplay. We now mark frame-specific structures in mark_frame. NOTE: I also deleted an extremely questionable call to update_frame_window_mirrors(). It was extremely questionable before, and now totally impossible, since it will create Lisp objects during redisplay. frame.c: Mark the scrollbar instances, which are now Lisp objects. Call mark_gutter() here, not in mark_redisplay(). gutter.c: Update comments about correct marking. --------------------------------------------------------------- ISSUES BROUGHT UP BY MARTIN: --------------------------------------------------------------- buffer.h: Put back these macros the way Steve T and I think they ought to be. I already explained in a previous changelog entry why I think these macros should be the way I'd defined them. Once again: We fix these macros so they don't care about the type of their lvalues. The non-C-string equivalents of these already function in the same way, and it's correct because it should be OK to pass in a CBufbyte *, a BufByte *, a Char_Binary *, an UChar_Binary *, etc. The whole reason for these different types is to work around errors caused by signed-vs-unsigned non-matching types. Any possible error that might be caught in a DFC macro would also be caught wherever the argument is used elsewhere. So creating multiple macro versions would add no useful error-checking and just further complicate an already complicated area. As for Martin's "ANSI aliasing" bug, XEmacs is not ANSI-aliasing clean and probably never will be. Unless the board agrees to change XEmacs in this way (and we really don't want to go down that road), this is not a bug. sound.h: Undo Martin's type change. signal.c: Fix problem identified by Martin with Linux and g++ due to non-standard declaration of setitimer(). systime.h: Update the docs for "qxe_" to point out why making the encapsulation explicit is always the right way to go. (setitimer() itself serves as an example.) For 21.4: update-elc-2.el: Correct misplaced parentheses, making lisp/mule not get recompiled.
author ben
date Mon, 18 Jun 2001 07:10:32 +0000
parents 190b164ddcac
children 16ff693bd768
line wrap: on
line diff
--- a/src/window.c	Mon Jun 11 07:47:20 2001 +0000
+++ b/src/window.c	Mon Jun 18 07:10:32 2001 +0000
@@ -149,30 +149,11 @@
 mark_window (Lisp_Object obj)
 {
   struct window *window = XWINDOW (obj);
-  mark_object (window->frame);
-  mark_object (window->mini_p);
-  mark_object (window->next);
-  mark_object (window->prev);
-  mark_object (window->hchild);
-  mark_object (window->vchild);
-  mark_object (window->parent);
-  mark_object (window->buffer);
-  MARK_DISP_VARIABLE (start);
-  MARK_DISP_VARIABLE (pointm);
-  mark_object (window->sb_point);	/* #### move to scrollbar.c? */
-  mark_object (window->use_time);
-  MARK_DISP_VARIABLE (last_modified);
-  MARK_DISP_VARIABLE (last_point);
-  MARK_DISP_VARIABLE (last_start);
-  MARK_DISP_VARIABLE (last_facechange);
-  mark_object (window->line_cache_last_updated);
-  mark_object (window->redisplay_end_trigger);
-  mark_object (window->subwindow_instance_cache);
 
   mark_face_cachels (window->face_cachels);
   mark_glyph_cachels (window->glyph_cachels);
 
-#define WINDOW_SLOT(slot, compare) mark_object (window->slot)
+#define WINDOW_SLOT(slot) mark_object (window->slot);
 #include "winslots.h"
 
   return Qnil;
@@ -261,14 +242,10 @@
   XSETWINDOW (val, p);
 
   p->dead = 0;
-  p->frame = Qnil;
-  p->mini_p = Qnil;
-  p->next = Qnil;
-  p->prev = Qnil;
-  p->hchild = Qnil;
-  p->vchild = Qnil;
-  p->parent = Qnil;
-  p->buffer = Qnil;
+
+#define WINDOW_SLOT(slot) p->slot = Qnil;
+#include "winslots.h"
+
   INIT_DISP_VARIABLE (start, Fmake_marker ());
   INIT_DISP_VARIABLE (pointm, Fmake_marker ());
   p->sb_point = Fmake_marker ();
@@ -286,16 +263,12 @@
   INIT_DISP_VARIABLE (last_point_x, 0);
   INIT_DISP_VARIABLE (last_point_y, 0);
   INIT_DISP_VARIABLE (window_end_pos, 0);
-  p->redisplay_end_trigger = Qnil;
 
   p->gutter_extent_modiff[0] = 0;
   p->gutter_extent_modiff[1] = 0;
   p->gutter_extent_modiff[2] = 0;
   p->gutter_extent_modiff[3] = 0;
 
-#define WINDOW_SLOT(slot, compare) p->slot = Qnil
-#include "winslots.h"
-
   p->windows_changed = 1;
   p->shadow_thickness_changed = 1;
 
@@ -303,6 +276,10 @@
 }
 #undef INIT_DISP_VARIABLE
 
+/************************************************************************/
+/*                        Window mirror structure                       */
+/************************************************************************/
+
 /*
  * The redisplay structures used to be stored with each window.  While
  * they are logically something associated with frames they can't be
@@ -322,23 +299,71 @@
  * the display structures.
  */
 
+/* NOTE: The window-mirror structure formerly was not a Lisp object, and
+   marking was handled specially.  I've gotten recurring crashes, however,
+   using the mouse wheel under Windows, where either the window mirror
+   accessed through a scrollbar instance, or the frame pointed to by that
+   window mirror, gets garbaged.  Things are tricky under windows because
+   the scrollbar instances are stored in HWND-specific data.  Furthermore,
+   we have scrollbar-instance caches to complicate things.  Both of these
+   make it very difficult (for me at least, not being intimately familiar
+   with the redisplay code) to track exactly when and where a particular
+   window mirror or scrollbar instance has pointers to it, or whether a
+   window mirror might have a dead frame or buffer in it (i.e. not
+   necessarily gc-protected by being on a global list).  By far the safest
+   thing, then, is to make both structures Lisp objects and not explicitly
+   xfree() them.  This should make no practical difference in memory usage
+   because neither structure is created very often (only when windows are
+   created or deleted). --ben */
+
+static Lisp_Object
+mark_window_mirror (Lisp_Object obj)
+{
+  struct window_mirror *mir = XWINDOW_MIRROR (obj);
+
+  if (mir->current_display_lines)
+    mark_redisplay_structs (mir->current_display_lines);
+  if (mir->desired_display_lines)
+    mark_redisplay_structs (mir->desired_display_lines);
+
+  if (mir->hchild)
+    mark_object (wrap_window_mirror (mir->hchild));
+  if (mir->vchild)
+    mark_object (wrap_window_mirror (mir->vchild));
+
+  if (mir->frame)
+    mark_object (wrap_frame (mir->frame));
+  if (mir->buffer)
+    mark_object (wrap_buffer (mir->buffer));
+
+#ifdef HAVE_SCROLLBARS
+  if (mir->scrollbar_vertical_instance)
+    mark_object (wrap_scrollbar_instance (mir->scrollbar_vertical_instance));
+  if (mir->scrollbar_horizontal_instance)
+    mark_object (wrap_scrollbar_instance (mir->scrollbar_horizontal_instance));
+#endif /* HAVE_SCROLLBARS */
+  if (mir->next)
+    return wrap_window_mirror (mir->next);
+  else
+    return Qnil;
+}
+
+DEFINE_LRECORD_IMPLEMENTATION ("window-mirror", window_mirror,
+                               mark_window_mirror, internal_object_printer,
+			       0, 0, 0, 0, struct window_mirror);
+
 /* Create a new window mirror structure and associated redisplay
    structs. */
 static struct window_mirror *
 new_window_mirror (struct frame *f)
 {
-  struct window_mirror *t = xnew_and_zero (struct window_mirror);
+  struct window_mirror *t =
+    alloc_lcrecord_type (struct window_mirror, &lrecord_window_mirror);
+  zero_lcrecord (t);
 
   t->frame = f;
-
   t->current_display_lines = Dynarr_new (display_line);
   t->desired_display_lines = Dynarr_new (display_line);
-  t->buffer = NULL;
-
-#ifdef HAVE_SCROLLBARS
-  t->scrollbar_vertical_instance = NULL;
-  t->scrollbar_horizontal_instance = NULL;
-#endif
 
   return t;
 }
@@ -456,7 +481,11 @@
 void
 update_frame_window_mirror (struct frame *f)
 {
-  f->root_mirror = update_mirror_internal (f->root_window, f->root_mirror);
+  f->root_mirror =
+    wrap_window_mirror (update_mirror_internal
+			(f->root_window,
+			 NILP (f->root_mirror) ? 0 :
+			 XWINDOW_MIRROR (f->root_mirror)));
   f->mirror_dirty = 0;
 }
 
@@ -467,7 +496,6 @@
 {
   while (mir)
     {
-      struct window_mirror *prev = mir;
       if (mir->hchild) free_window_mirror (mir->hchild);
       if (mir->vchild) free_window_mirror (mir->vchild);
 #ifdef HAVE_SCROLLBARS
@@ -475,7 +503,10 @@
 #endif
       free_display_structs (mir);
       mir = mir->next;
-      xfree (prev);
+      /* not worth calling free_managed_lcrecord() -- window mirrors
+	 are not created that frequently and it's dangerous.  we don't
+	 know for sure that there aren't other pointers around -- e.g.
+	 in a scrollbar instance. */
     }
 }
 
@@ -484,8 +515,9 @@
 Lisp_Object
 real_window (struct window_mirror *mir, int no_abort)
 {
-  Lisp_Object retval = real_window_internal (mir->frame->root_window,
-					     mir->frame->root_mirror, mir);
+  Lisp_Object retval =
+    real_window_internal (mir->frame->root_window,
+			  XWINDOW_MIRROR (mir->frame->root_mirror), mir);
   if (NILP (retval) && !no_abort)
     abort ();
 
@@ -500,7 +532,8 @@
   struct frame *f = XFRAME (w->frame);
   if (f->mirror_dirty)
     update_frame_window_mirror (f);
-  return find_window_mirror_internal (f->root_window, f->root_mirror, w);
+  return find_window_mirror_internal (f->root_window,
+				      XWINDOW_MIRROR (f->root_mirror), w);
 }
 
 /*****************************************************************************
@@ -1904,7 +1937,20 @@
   ERROR_CHECK_SUBWINDOW_CACHE (w);
   window_unmap_subwindows (w);
 
-  /* In the loop
+  /* Free the extra data structures attached to windows immediately so
+     they don't sit around consuming excess space.  They will be
+     reinitialized by the window-configuration code as necessary. */
+  finalize_window ((void *) w, 0);
+
+  /* Nobody should be accessing anything in this object any more, and
+     making them Qnil allows for better GC'ing in case a pointer to
+     the dead window continues to hang around.  Zero all other structs
+     in case someone tries to access something through them.
+
+     As an example of why setting the values to Qnil is good, here
+     is an old comment:
+
+     In the loop
      (while t (split-window) (delete-window))
      we end up with a tree of deleted windows which are all connected
      through the `next' slot.  This might not seem so bad, as they're
@@ -1916,19 +1962,13 @@
      pointers to other windows (they are all recreated from the
      window-config data), we set them all to nil so that we
      are able to collect more actual garbage. */
-  w->next = Qnil;
-  w->prev = Qnil;
-  w->hchild = Qnil;
-  w->vchild = Qnil;
-  w->parent = Qnil;
-  w->subwindow_instance_cache = Qnil;
+
+  zero_lcrecord (w);
+
+#define WINDOW_SLOT(slot) w->slot = Qnil;
+#include "winslots.h"
 
   w->dead = 1;
-
-  /* Free the extra data structures attached to windows immediately so
-     they don't sit around consuming excess space.  They will be
-     reinitialized by the window-configuration code as necessary. */
-  finalize_window ((void *) w, 0);
 }
 
 DEFUN ("delete-window", Fdelete_window, 0, 2, "", /*
@@ -5002,7 +5042,8 @@
   char start_at_line_beg; /* boolean */
 
 #define WINDOW_SLOT_DECLARATION
-#define WINDOW_SLOT(slot, compare) Lisp_Object slot
+#define WINDOW_SLOT(slot)
+#define WINDOW_SAVED_SLOT(slot, compare) Lisp_Object slot;
 #include "winslots.h"
 };
 
@@ -5032,6 +5073,7 @@
 #define SAVED_WINDOW_N(conf, n) (&((conf)->saved_windows[(n)]))
 #define XWINDOW_CONFIGURATION(x) XRECORD (x, window_configuration, struct window_config)
 #define XSETWINDOW_CONFIGURATION(x, p) XSETRECORD (x, p, window_configuration)
+#define wrap_window_configuration(p) wrap_record (p, window_configuration)
 #define WINDOW_CONFIGURATIONP(x) RECORDP (x, window_configuration)
 #define CHECK_WINDOW_CONFIGURATION(x) CHECK_RECORD (x, window_configuration)
 
@@ -5061,7 +5103,8 @@
 	 aren't they?  -- kkm */
       mark_object (s->dedicated);
 #else
-#define WINDOW_SLOT(slot, compare) mark_object (s->slot)
+#define WINDOW_SLOT(slot)
+#define WINDOW_SAVED_SLOT(slot, compare) mark_object (s->slot);
 #include "winslots.h"
 #endif
     }
@@ -5108,7 +5151,8 @@
 static int
 saved_window_equal (struct saved_window *win1, struct saved_window *win2)
 {
-#define WINDOW_SLOT(slot, compare)		\
+#define WINDOW_SLOT(slot)
+#define WINDOW_SAVED_SLOT(slot, compare)	\
   if (!compare (win1->slot, win2->slot))	\
     return 0;
 #include "winslots.h"
@@ -5498,8 +5542,9 @@
 	  SET_LAST_FACECHANGE (w);
 	  w->config_mark = 0;
 
-	  /* #### Consider making the instance cache a winslot. */
-#define WINDOW_SLOT(slot, compare) w->slot = p->slot
+	  /* #### Consider making the instance cache a WINDOW_SAVED_SLOT. */
+#define WINDOW_SLOT(slot)
+#define WINDOW_SAVED_SLOT(slot, compare) w->slot = p->slot;
 #include "winslots.h"
 
 	  /* Reinstall the saved buffer and pointers into it.  */
@@ -5784,7 +5829,8 @@
       p->hscroll = w->hscroll;
       p->modeline_hscroll = w->modeline_hscroll;
 
-#define WINDOW_SLOT(slot, compare) p->slot = w->slot
+#define WINDOW_SLOT(slot)
+#define WINDOW_SAVED_SLOT(slot, compare) p->slot = w->slot;
 #include "winslots.h"
 
       if (!NILP (w->buffer))
@@ -6063,6 +6109,7 @@
 {
   INIT_LRECORD_IMPLEMENTATION (window);
   INIT_LRECORD_IMPLEMENTATION (window_configuration);
+  INIT_LRECORD_IMPLEMENTATION (window_mirror);
 
   DEFSYMBOL (Qwindowp);
   DEFSYMBOL (Qwindow_live_p);