emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Universal functions to manage multiple window caches.


From: Keith David Bershatsky
Subject: Re: Universal functions to manage multiple window caches.
Date: Wed, 17 Apr 2019 20:17:39 -0700

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 via email to

[Prev in Thread] Current Thread [Next in Thread]