unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Keith David Bershatsky <esq@lawlist.com>
To: Emacs Devel <emacs-devel@gnu.org>
Subject: Universal functions to manage multiple window caches.
Date: Wed, 17 Apr 2019 13:03:51 -0700	[thread overview]
Message-ID: <m21s20flg8.wl%esq@lawlist.com> (raw)

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

I am working on feature requests 22873 (multiple fake cursors) and 17684 (crosshairs that track the cursor position).

window.h defines four different caches of fake cursors, with the only difference between them being their names:

  struct multiple_cursors_cache *temp_elts;
  ptrdiff_t temp_elts_allocated;
  int temp_nelts;

  struct multiple_cursors_cache *mc_elts;
  ptrdiff_t mc_elts_allocated;
  int mc_nelts;

  struct multiple_cursors_cache *ch_elts;
  ptrdiff_t ch_elts_allocated;
  int ch_nelts;

  struct multiple_cursors_cache *fc_elts;
  ptrdiff_t fc_elts_allocated;
  int fc_nelts;

I am trying to create two functions (reset and populate) to manage all four caches, but am having troubling figuring out how to substitute the names of the applicable caches depending upon which cache is needed.  I tried several variations of the following approach, but ran into problems either compiling or Emacs crashed when the crosshairs feature was activated.

    struct multiple_cursors_cache **foo_elts = w->ch_elts;
    ptrdiff_t *foo_elts_allocated = &w->ch_elts_allocated;
    int *foo_nelts = &w->ch_nelts;

Q:  What is the best way to create (1) a reset function and (2) a populate function that can handle all four caches?

void
mc_reset_cache (struct window *w, ...)
{
  if (BUFFERP (w->contents) && NILP (BVAR (XBUFFER (w->contents), crosshairs))
      && foo_nelts > 1)
    {
      /* Decrease the size of the array to a bare minimum. */
      xnrealloc (foo_elts, 1, sizeof *foo_elts);
      foo_nelts = 0;
      foo_elts_allocated = 1;
    }
    else if (BUFFERP (w->contents) && !NILP (BVAR (XBUFFER (w->contents), crosshairs)))
      {
        /* Set all _used_ elements of the array to zero.  elts_allocated remain
        the same. */
        memset (foo_elts, 0, foo_nelts * (sizeof *foo_elts));
        foo_nelts = 0;
      }
}

void
mc_populate_cache (struct window *w, ...)
{
  if (BUFFERP (w->contents)
      && !NILP (BVAR (XBUFFER (w->contents), crosshairs)))
    {
      /* Increase the size of the array. */
      if (foo_elts_allocated < foo_nelts
          && foo_nelts < max_elts)
        {
          int old_alloc = foo_elts_allocated;
          int new_elts = foo_nelts - foo_elts_allocated;
          foo_elts = xpalloc (foo_elts, &foo_elts_allocated,
                                  new_elts, INT_MAX, sizeof *foo_elts);
          memset (foo_elts + old_alloc, 0,
                   (foo_elts_allocated - old_alloc) * sizeof *foo_elts);
        }
      /* Below this comment is where the applicable cache will be populated. */
    }
}

Attached is a draft patch that applies to the master branch as of 04/08/2019 (a038df77de7b1aa2d73a6478493b8838b59e4982).


[-- Attachment #2: 2019_04_17__12_46_02_063.diff --]
[-- Type: application/diff, Size: 12400 bytes --]

             reply	other threads:[~2019-04-17 20:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-17 20:03 Keith David Bershatsky [this message]
2019-04-18 21:02 ` Universal functions to manage multiple window caches Alex Gramiak
  -- strict thread matches above, loose matches on Subject: below --
2019-04-18  3:17 Keith David Bershatsky
2019-04-19  1:44 Keith David Bershatsky
2019-04-19 13:59 ` Alex Gramiak
2019-04-20  6:58 Keith David Bershatsky
2019-04-20 17:17 ` Alex Gramiak
2019-04-20 20:29 Keith David Bershatsky

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=m21s20flg8.wl%esq@lawlist.com \
    --to=esq@lawlist.com \
    --cc=emacs-devel@gnu.org \
    /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).