=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/grid.c,v retrieving revision 1.74 retrieving revision 1.75 diff -u -r1.74 -r1.75 --- src/usr.bin/tmux/grid.c 2017/05/16 12:57:26 1.74 +++ src/usr.bin/tmux/grid.c 2017/08/30 18:13:47 1.75 @@ -1,4 +1,4 @@ -/* $OpenBSD: grid.c,v 1.74 2017/05/16 12:57:26 nicm Exp $ */ +/* $OpenBSD: grid.c,v 1.75 2017/08/30 18:13:47 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -162,6 +162,26 @@ return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0); } +/* Free one line. */ +static void +grid_free_line(struct grid *gd, u_int py) +{ + free(gd->linedata[py].celldata); + gd->linedata[py].celldata = NULL; + free(gd->linedata[py].extddata); + gd->linedata[py].extddata = NULL; +} + +/* Free several lines. */ +static void +grid_free_lines(struct grid *gd, u_int py, u_int ny) +{ + u_int yy; + + for (yy = py; yy < py + ny; yy++) + grid_free_line(gd, yy); +} + /* Create a new grid. */ struct grid * grid_create(u_int sx, u_int sy, u_int hlimit) @@ -187,15 +207,8 @@ void grid_destroy(struct grid *gd) { - struct grid_line *gl; - u_int yy; + grid_free_lines(gd, 0, gd->hsize + gd->sy); - for (yy = 0; yy < gd->hsize + gd->sy; yy++) { - gl = &gd->linedata[yy]; - free(gl->celldata); - free(gl->extddata); - } - free(gd->linedata); free(gd); @@ -233,19 +246,26 @@ * and shift up. */ void -grid_collect_history(struct grid *gd, u_int bg) +grid_collect_history(struct grid *gd) { - u_int yy; + u_int ny; if (gd->hsize < gd->hlimit) return; - yy = gd->hlimit / 10; - if (yy < 1) - yy = 1; + ny = gd->hlimit / 10; + if (ny < 1) + ny = 1; - grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy, bg); - gd->hsize -= yy; + /* + * Free the lines from 0 to ny then move the remaining lines over + * them. + */ + grid_free_lines(gd, 0, ny); + memmove(&gd->linedata[0], &gd->linedata[ny], + (gd->hsize + gd->sy - ny) * (sizeof *gd->linedata)); + + gd->hsize -= ny; if (gd->hscrolled > gd->hsize) gd->hscrolled = gd->hsize; } @@ -272,8 +292,9 @@ void grid_clear_history(struct grid *gd) { - grid_clear_lines(gd, 0, gd->hsize, 8); - grid_move_lines(gd, 0, gd->hsize, gd->sy, 8); + grid_free_lines(gd, 0, gd->hsize); + memmove(&gd->linedata[0], &gd->linedata[gd->hsize], + gd->sy * (sizeof *gd->linedata)); gd->hscrolled = 0; gd->hsize = 0; @@ -481,8 +502,7 @@ void grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg) { - struct grid_line *gl; - u_int yy; + u_int yy; if (ny == 0) return; @@ -493,9 +513,7 @@ return; for (yy = py; yy < py + ny; yy++) { - gl = &gd->linedata[yy]; - free(gl->celldata); - free(gl->extddata); + grid_free_line(gd, yy); grid_empty_line(gd, yy, bg); } } @@ -522,13 +540,16 @@ for (yy = dy; yy < dy + ny; yy++) { if (yy >= py && yy < py + ny) continue; - grid_clear_lines(gd, yy, 1, bg); + grid_free_line(gd, yy); } memmove(&gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata)); - /* Wipe any lines that have been moved (without freeing them). */ + /* + * Wipe any lines that have been moved (without freeing them - they are + * still present). + */ for (yy = py; yy < py + ny; yy++) { if (yy < dy || yy >= dy + ny) grid_empty_line(gd, yy, bg); @@ -845,9 +866,8 @@ } /* - * Duplicate a set of lines between two grids. If there aren't enough lines in - * either source or destination, the number of lines is limited to the number - * available. + * Duplicate a set of lines between two grids. Both source and destination + * should be big enough. */ void grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy, @@ -860,7 +880,7 @@ ny = dst->hsize + dst->sy - dy; if (sy + ny > src->hsize + src->sy) ny = src->hsize + src->sy - sy; - grid_clear_lines(dst, dy, ny, 8); + grid_free_lines(dst, dy, ny); for (yy = 0; yy < ny; yy++) { srcl = &src->linedata[sy];