all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Keith David Bershatsky <esq@lawlist.com>
To: emacs-devel@gnu.org
Subject: Re: Universal functions to manage multiple window caches.
Date: Wed, 17 Apr 2019 20:17:39 -0700	[thread overview]
Message-ID: <m2ftqg0zos.wl%esq@lawlist.com> (raw)

The following two functions for reset / populate are working; however, several sections of code are essentially repeated (for lack of figuring out a more concise approach).  Although not critical to the overall design, a more efficient way to write these up would be greatly appreciated.

void
mc_reset_cache (struct window *w, enum type_of_cache cache_type)
{
  switch (cache_type)
    {
      case NO_CACHE:
        {
          return;
        }
      case MC_TEMP_CACHE:
        {
          if (w->temp_nelts > 1)
            {
              /* Decrease the size of the array to a bare minimum. */
              xnrealloc (w->temp_elts, 1, sizeof *w->temp_elts);
              w->temp_nelts = 0;
              w->temp_elts_allocated = 1;
            }
            else
              {
                /* Set all _used_ elements of the array to zero.  elts_allocated remain
                the same. */
                memset (w->temp_elts, 0, w->temp_nelts * (sizeof *w->temp_elts));
                w->temp_nelts = 0;
              }
          break;
        }
      case MC_CACHE:
        {
          if (BUFFERP (w->contents) && NILP (BVAR (XBUFFER (w->contents), mc_conf))
              && w->mc_nelts > 1)
            {
              /* Decrease the size of the array to a bare minimum. */
              xnrealloc (w->mc_elts, 1, sizeof *w->mc_elts);
              w->mc_nelts = 0;
              w->mc_elts_allocated = 1;
            }
            else if (BUFFERP (w->contents) && !NILP (BVAR (XBUFFER (w->contents), mc_conf)))
              {
                /* Set all _used_ elements of the array to zero.  elts_allocated remain
                the same. */
                memset (w->mc_elts, 0, w->mc_nelts * (sizeof *w->mc_elts));
                w->mc_nelts = 0;
              }
          break;
        }
      case CH_CACHE:
        {
          if (BUFFERP (w->contents) && NILP (BVAR (XBUFFER (w->contents), crosshairs))
              && w->ch_nelts > 1)
            {
              /* Decrease the size of the array to a bare minimum. */
              xnrealloc (w->ch_elts, 1, sizeof *w->ch_elts);
              w->ch_nelts = 0;
              w->ch_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 (w->ch_elts, 0, w->ch_nelts * (sizeof *w->ch_elts));
                w->ch_nelts = 0;
              }
          break;
        }
      case FC_CACHE:
        {
          if (BUFFERP (w->contents) && NILP (BVAR (XBUFFER (w->contents), fc_visible))
              && w->fc_nelts > 1)
            {
              /* Decrease the size of the array to a bare minimum. */
              xnrealloc (w->fc_elts, 1, sizeof *w->fc_elts);
              w->fc_nelts = 0;
              w->fc_elts_allocated = 1;
            }
            else if (BUFFERP (w->contents) && !NILP (BVAR (XBUFFER (w->contents), fc_visible)))
              {
                /* Set all _used_ elements of the array to zero.  elts_allocated remain
                the same. */
                memset (w->fc_elts, 0, w->fc_nelts * (sizeof *w->fc_elts));
                w->fc_nelts = 0;
              }
          break;
        }
    }
}

static void
mc_helper (struct window *w, struct glyph_matrix *matrix, struct glyph_row *row,
           struct glyph *glyph, int x, int fx, int y, int fy, int hpos, int vpos,
           int wd, int h, enum text_cursor_kinds cursor_type, int cursor_width,
           struct RGB foreground, struct RGB background, bool active_p,
           enum mc_flavor glyph_flavor, bool draw_p, enum type_of_cache cache_type)
{
  . . .
  struct multiple_cursors_cache *foo_elts;
  ptrdiff_t *foo_elts_allocated;
  int *foo_nelts;
  switch (cache_type)
    {
      case NO_CACHE:
        {
          return;
        }
      case MC_TEMP_CACHE:
        {
          ++w->temp_nelts;
          if (w->temp_elts_allocated < w->temp_nelts)
            {
              int old_alloc = w->temp_elts_allocated;
              int new_elts = w->temp_nelts - w->temp_elts_allocated;
              w->temp_elts = xpalloc (w->temp_elts, &w->temp_elts_allocated,
                                      new_elts, INT_MAX, sizeof *w->temp_elts);
              memset (w->temp_elts + old_alloc, 0,
                       (w->temp_elts_allocated - old_alloc) * sizeof *w->temp_elts);
            }
          foo_elts = w->temp_elts;
          foo_elts_allocated = &w->temp_elts_allocated;
          foo_nelts = &w->temp_nelts;
          break;
        }
      case MC_CACHE:
        {
          ++w->mc_nelts;
          if (w->mc_elts_allocated < w->mc_nelts)
            {
              int old_alloc = w->mc_elts_allocated;
              int new_elts = w->mc_nelts - w->mc_elts_allocated;
              w->mc_elts = xpalloc (w->mc_elts, &w->mc_elts_allocated,
                                      new_elts, INT_MAX, sizeof *w->mc_elts);
              memset (w->mc_elts + old_alloc, 0,
                       (w->mc_elts_allocated - old_alloc) * sizeof *w->mc_elts);
            }
          foo_elts = w->mc_elts;
          foo_elts_allocated = &w->mc_elts_allocated;
          foo_nelts = &w->mc_nelts;
          break;
        }
      case CH_CACHE:
        {
          ++w->ch_nelts;
          if (w->ch_elts_allocated < w->ch_nelts)
            {
              int old_alloc = w->ch_elts_allocated;
              int new_elts = w->ch_nelts - w->ch_elts_allocated;
              w->ch_elts = xpalloc (w->ch_elts, &w->ch_elts_allocated,
                                      new_elts, INT_MAX, sizeof *w->ch_elts);
              memset (w->ch_elts + old_alloc, 0,
                       (w->ch_elts_allocated - old_alloc) * sizeof *w->ch_elts);
            }
          foo_elts = w->ch_elts;
          foo_elts_allocated = &w->ch_elts_allocated;
          foo_nelts = &w->ch_nelts;
          break;
        }
      case FC_CACHE:
        {
          ++w->fc_nelts;
          if (w->fc_elts_allocated < w->fc_nelts)
            {
              int old_alloc = w->fc_elts_allocated;
              int new_elts = w->fc_nelts - w->fc_elts_allocated;
              w->fc_elts = xpalloc (w->fc_elts, &w->fc_elts_allocated,
                                      new_elts, INT_MAX, sizeof *w->fc_elts);
              memset (w->fc_elts + old_alloc, 0,
                       (w->fc_elts_allocated - old_alloc) * sizeof *w->fc_elts);
            }
          foo_elts = w->fc_elts;
          foo_elts_allocated = &w->fc_elts_allocated;
          foo_nelts = &w->fc_nelts;
          break;
        }
    }
  int nth = *foo_nelts - 1;
  foo_elts[nth].x = x;
  foo_elts[nth].fx = fx;
  foo_elts[nth].y = y;
  foo_elts[nth].fy = fy;
  foo_elts[nth].hpos = hpos;
  foo_elts[nth].vpos = vpos;
  foo_elts[nth].wd = wd;
  foo_elts[nth].h = h;
  foo_elts[nth].cursor_type = cursor_type;
  foo_elts[nth].cursor_width = cursor_width;
  foo_elts[nth].foreground.red = foreground.red;
  foo_elts[nth].foreground.green = foreground.green;
  foo_elts[nth].foreground.blue = foreground.blue;
  foo_elts[nth].background.red = background.red;
  foo_elts[nth].background.green = background.green;
  foo_elts[nth].background.blue = background.blue;
  foo_elts[nth].active_p = active_p;
  foo_elts[nth].glyph_flavor = glyph_flavor;
  foo_elts[nth].enabled_p = true;
}



             reply	other threads:[~2019-04-18  3:17 UTC|newest]

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

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=m2ftqg0zos.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 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.