unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Basil L. Contovounesios" via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Matt Armstrong <matt@rfc20.org>, 58975@debbugs.gnu.org
Subject: bug#58975: 29.0.50; noverlay fails to build with --enable-checking=structs
Date: Thu, 03 Nov 2022 14:26:50 +0200	[thread overview]
Message-ID: <87zgd8dy0l.fsf@tcd.ie> (raw)
In-Reply-To: <jwva658x0s6.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Wed, 02 Nov 2022 21:55:07 -0400")

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

Stefan Monnier [2022-11-02 21:55 -0400] wrote:

>> Is this the right fix?
>
> The patch looks good, thanks.
>
>> BTW, every time itree_create is called via add_buffer_overlay, it in
>> turn calls itree_init,
>
> Oh, that's not right.  `itree_init` should be called once only, from
> `emacs.c`, like things like `init_lread`.

Like this?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Port-interval-trees-to-enable-checking-structs.patch --]
[-- Type: text/x-diff, Size: 10852 bytes --]

From 321484838251ec8cea4cf296e3b8b0134ba892bc Mon Sep 17 00:00:00 2001
From: "Basil L. Contovounesios" <contovob@tcd.ie>
Date: Wed, 2 Nov 2022 03:50:38 +0200
Subject: [PATCH] Port interval trees to --enable-checking=structs

Some names under the interval_* namespace were renamed under the
itree_* namespace in commits:

  0. f421b58db5 of 2022-10-19
  "Prefix all itree.h type names with itree_".
  1. 37a1145410 of 2022-10-19
  "Rename all exported itree.h functions with the itree_ prefix"

Further, some values still referenced in commentary were removed in
commits:

  2. 258e618364 of 2022-10-17
  "Delete the itree_null sentinel node, use NULL everywhere."
  3. 2c4a3910b3 of 2022-10-02
  "itree: Use a single iterator object"

* src/emacs.c (main): Allocate global itree iterator once and for
all.
* src/alloc.c (mark_overlay):
* src/buffer.c (set_overlays_multibyte):
* src/itree.c (itree_destroy): Update commentary.
(interval_stack_ensure_space, itree_insert_gap): Prefer
unsigned-to-unsigned comparisons over signed-to-unsigned.
(interval_stack_push_flagged, interval_tree_insert)
(interval_tree_contains, itree_iterator_start)
(itree_iterator_finish, itree_iterator_next, itree_iterator_narrow):
Improve assertions.
(itree_init): Rename...
(init_itree): ...to this, for consistency with other global init
functions.
(itree_create): Stop leaking a global iterator allocation on each
call.
(interval_tree_init): Complete renames of
interval_tree -> itree_tree and interval_tree_clear -> itree_clear.
(interval_tree_remove_fix): Fix indentation.
* src/itree.h: Declare init_itree.
(ITREE_FOREACH): Fix typo in commentary.

* src/pdumper.c [CHECK_STRUCTS]
(dump_interval_node): Use the correct name in the HASH condition
and #error message.
(dump_overlay, dump_buffer): Update HASH (bug#58975).
---
 src/alloc.c   |  2 +-
 src/buffer.c  |  2 +-
 src/emacs.c   |  2 ++
 src/itree.c   | 42 +++++++++++++++++-------------------------
 src/itree.h   |  3 ++-
 src/pdumper.c |  8 ++++----
 6 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index f69c65dedc..6862cf916f 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -6508,7 +6508,7 @@ mark_char_table (struct Lisp_Vector *ptr, enum pvec_type pvectype)
 static void
 mark_overlay (struct Lisp_Overlay *ov)
 {
-  /* We don't mark the `interval_node` object, because it is managed manually
+  /* We don't mark the `itree_node` object, because it is managed manually
      rather than by the GC.  */
   eassert (BASE_EQ (ov->interval->data, make_lisp_ptr (ov, Lisp_Vectorlike)));
   set_vectorlike_marked (&ov->header);
diff --git a/src/buffer.c b/src/buffer.c
index 3129aa2890..3b0e6f1f9a 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -982,7 +982,7 @@ set_overlays_multibyte (bool multibyte)
   struct itree_tree *tree = current_buffer->overlays;
   const intmax_t size = itree_size (tree);
 
-  /* We can't use `interval_node_set_region` at the same time
+  /* We can't use `itree_node_set_region` at the same time
      as we iterate over the itree, so we need an auxiliary storage
      to keep the list of nodes.  */
   USE_SAFE_ALLOCA;
diff --git a/src/emacs.c b/src/emacs.c
index 8ad70fecd4..40ba0db340 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -82,6 +82,7 @@ #define MAIN_PROGRAM
 #endif /* HAVE_WINDOW_SYSTEM */
 
 #include "bignum.h"
+#include "itree.h"
 #include "intervals.h"
 #include "character.h"
 #include "buffer.h"
@@ -1931,6 +1932,7 @@ main (int argc, char **argv)
   running_asynch_code = 0;
   init_random ();
   init_xfaces ();
+  init_itree ();
 
 #if defined HAVE_JSON && !defined WINDOWSNT
   init_json ();
diff --git a/src/itree.c b/src/itree.c
index bd4e8cc574..3137cb6358 100644
--- a/src/itree.c
+++ b/src/itree.c
@@ -191,7 +191,7 @@ interval_stack_clear (struct interval_stack *stack)
 }
 
 static inline void
-interval_stack_ensure_space (struct interval_stack *stack, intmax_t nelements)
+interval_stack_ensure_space (struct interval_stack *stack, uintmax_t nelements)
 {
   if (nelements > stack->size)
     {
@@ -207,7 +207,7 @@ interval_stack_ensure_space (struct interval_stack *stack, intmax_t nelements)
 interval_stack_push_flagged (struct interval_stack *stack,
 			     struct itree_node *node, bool flag)
 {
-  eassert (node && node != NULL);
+  eassert (node);
 
   /* FIXME: While the stack used in the iterator is bounded by the tree
      depth and could be easily pre-allocated to a large enough size to avoid
@@ -287,8 +287,8 @@ itree_iterator_create (struct itree_tree *tree)
   return g;
 }
 
-static void
-itree_init (void)
+void
+init_itree (void)
 {
   iter = itree_iterator_create (NULL);
 }
@@ -555,16 +555,11 @@ itree_node_end (struct itree_tree *tree,
   return node->end;
 }
 
-/* Allocate an interval_tree. Free with interval_tree_destroy. */
+/* Allocate an itree_tree.  Free with itree_destroy.  */
 
 struct itree_tree *
 itree_create (void)
 {
-  /* FIXME?  Maybe avoid the initialization of itree_null in the same
-     way that is used to call mem_init in alloc.c?  It's not really
-     important though.  */
-  itree_init ();
-
   struct itree_tree *tree = xmalloc (sizeof (*tree));
   itree_clear (tree);
   return tree;
@@ -584,10 +579,9 @@ itree_clear (struct itree_tree *tree)
 /* Initialize a pre-allocated tree (presumably on the stack).  */
 
 static void
-interval_tree_init (struct interval_tree *tree)
+interval_tree_init (struct itree_tree *tree)
 {
-  interval_tree_clear (tree);
-  /* tree->iter = itree_iterator_create (tree); */
+  itree_clear (tree);
 }
 #endif
 
@@ -596,8 +590,6 @@ interval_tree_init (struct interval_tree *tree)
 itree_destroy (struct itree_tree *tree)
 {
   eassert (tree->root == NULL);
-  /* if (tree->iter)
-   *   itree_iterator_destroy (tree->iter); */
   xfree (tree);
 }
 
@@ -775,7 +767,7 @@ interval_tree_insert_fix (struct itree_tree *tree,
 static void
 interval_tree_insert (struct itree_tree *tree, struct itree_node *node)
 {
-  eassert (node->begin <= node->end && node != NULL);
+  eassert (node && node->begin <= node->end);
   /* FIXME: The assertion below fails because `delete_all_overlays`
      doesn't set left/right/parent to NULL.  */
   /* eassert (node->left == NULL && node->right == NULL
@@ -868,7 +860,7 @@ itree_node_set_region (struct itree_tree *tree,
 static bool
 interval_tree_contains (struct itree_tree *tree, struct itree_node *node)
 {
-  eassert (node);
+  eassert (iter && node);
   struct itree_node *other;
   ITREE_FOREACH (other, tree, node->begin, PTRDIFF_MAX, ASCENDING)
     if (other == node)
@@ -912,7 +904,7 @@ interval_tree_remove_fix (struct itree_tree *tree,
   if (parent == NULL)
     eassert (node == tree->root);
   else
-  eassert (node == NULL || node->parent == parent);
+    eassert (node == NULL || node->parent == parent);
 
   while (parent != NULL && null_safe_is_black (node))
     {
@@ -1151,7 +1143,7 @@ itree_iterator_start (struct itree_tree *tree, ptrdiff_t begin,
 		      ptrdiff_t end, enum itree_order order,
 		      const char *file, int line)
 {
-  /* struct itree_iterator *iter = tree->iter; */
+  eassert (iter);
   if (iter->running)
     {
       fprintf (stderr,
@@ -1179,7 +1171,7 @@ itree_iterator_start (struct itree_tree *tree, ptrdiff_t begin,
 void
 itree_iterator_finish (struct itree_iterator *iter)
 {
-  eassert (iter->running);
+  eassert (iter && iter->running);
   iter->running = false;
 }
 
@@ -1212,7 +1204,7 @@ itree_insert_gap (struct itree_tree *tree,
 	  && (node->begin != node->end || node->rear_advance))
 	interval_stack_push (saved, node);
     }
-  for (int i = 0; i < saved->length; ++i)
+  for (size_t i = 0; i < saved->length; ++i)
     itree_remove (tree, nav_nodeptr (saved->nodes[i]));
 
   /* We can't use an iterator here, because we can't effectively
@@ -1352,7 +1344,7 @@ interval_node_intersects (const struct itree_node *node,
 struct itree_node *
 itree_iterator_next (struct itree_iterator *g)
 {
-  eassert (g->running);
+  eassert (g && g->running);
 
   struct itree_node *const null = NULL;
   struct itree_node *node;
@@ -1424,9 +1416,9 @@ itree_iterator_next (struct itree_iterator *g)
 itree_iterator_narrow (struct itree_iterator *g,
 		       ptrdiff_t begin, ptrdiff_t end)
 {
-  eassert (g->running);
+  eassert (g && g->running);
   eassert (begin >= g->begin);
   eassert (end <= g->end);
-  g->begin =  max (begin, g->begin);
-  g->end =  min (end, g->end);
+  g->begin = max (begin, g->begin);
+  g->end = min (end, g->end);
 }
diff --git a/src/itree.h b/src/itree.h
index c6b68d3667..49a0333f34 100644
--- a/src/itree.h
+++ b/src/itree.h
@@ -106,6 +106,7 @@ #define ITREE_H
     ITREE_PRE_ORDER,
   };
 
+extern void init_itree (void);
 extern void itree_node_init (struct itree_node *, bool, bool, Lisp_Object);
 extern ptrdiff_t itree_node_begin (struct itree_tree *, struct itree_node *);
 extern ptrdiff_t itree_node_end (struct itree_tree *, struct itree_node *);
@@ -147,7 +148,7 @@ #define ITREE_H
 
    BEWARE:
    - The expression T may be evaluated more than once, so make sure
-     it is cheap a pure.
+     it is cheap and pure.
    - Only a single iteration can happen at a time, so make sure none of the
      code within the loop can start another tree iteration, i.e. it shouldn't
      be able to run ELisp code, nor GC since GC can run ELisp by way
diff --git a/src/pdumper.c b/src/pdumper.c
index d6ae57afb2..0a5d96dbb7 100644
--- a/src/pdumper.c
+++ b/src/pdumper.c
@@ -2137,8 +2137,8 @@ dump_marker (struct dump_context *ctx, const struct Lisp_Marker *marker)
 dump_interval_node (struct dump_context *ctx, struct itree_node *node,
                     dump_off parent_offset)
 {
-#if CHECK_STRUCTS && !defined (HASH_interval_node_5765524F7E)
-# error "interval_node changed. See CHECK_STRUCTS comment in config.h."
+#if CHECK_STRUCTS && !defined (HASH_itree_node_50DE304F13)
+# error "itree_node changed. See CHECK_STRUCTS comment in config.h."
 #endif
   struct itree_node out;
   dump_object_start (ctx, &out, sizeof (out));
@@ -2179,7 +2179,7 @@ dump_interval_node (struct dump_context *ctx, struct itree_node *node,
 static dump_off
 dump_overlay (struct dump_context *ctx, const struct Lisp_Overlay *overlay)
 {
-#if CHECK_STRUCTS && !defined (HASH_Lisp_Overlay_1CD4249AEC)
+#if CHECK_STRUCTS && !defined (HASH_Lisp_Overlay_EB4C05D8D2)
 # error "Lisp_Overlay changed. See CHECK_STRUCTS comment in config.h."
 #endif
   START_DUMP_PVEC (ctx, &overlay->header, struct Lisp_Overlay, out);
@@ -2748,7 +2748,7 @@ dump_hash_table (struct dump_context *ctx,
 static dump_off
 dump_buffer (struct dump_context *ctx, const struct buffer *in_buffer)
 {
-#if CHECK_STRUCTS && !defined HASH_buffer_F0F08347A5
+#if CHECK_STRUCTS && !defined HASH_buffer_193CAA5E45
 # error "buffer changed. See CHECK_STRUCTS comment in config.h."
 #endif
   struct buffer munged_buffer = *in_buffer;
-- 
2.35.1


[-- Attachment #3: Type: text/plain, Size: 348 bytes --]


>> which calls itree_iterator_create, which xmallocs a new
>> itree_iterator, interval_stack, and itree_nodes.  Is this memory leak
>> going to rust my computer?
>
> No, Emacs is pure C and ELisp,

But not pure enough to avoid side-effects such as headaches.

> so it doesn't suffer from Rust.

Not in this lifetime anyway ;).

Thanks,

-- 
Basil

  reply	other threads:[~2022-11-03 12:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-03  0:17 bug#58975: 29.0.50; noverlay fails to build with --enable-checking=structs Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-03  1:55 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-03 12:26   ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-11-03 14:24     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-03 14:57       ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87zgd8dy0l.fsf@tcd.ie \
    --to=bug-gnu-emacs@gnu.org \
    --cc=58975@debbugs.gnu.org \
    --cc=contovob@tcd.ie \
    --cc=matt@rfc20.org \
    --cc=monnier@iro.umontreal.ca \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).