aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-05-15 11:37:46 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-05-15 11:37:46 +0000
commit8d1b72405662ed43007fd99d97921a71ee91eb9e (patch)
treef9cbfc26dc397be8231f27a55df404d431839c1c
parentgrid: include <stdlib.h>, for qsort_r() (diff)
grid: reflow: qsort_r() is not portable
Replace with qsort() + global variable. Not thread safe!
-rw-r--r--grid.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/grid.c b/grid.c
index 3eabeeb3..eb923756 100644
--- a/grid.c
+++ b/grid.c
@@ -385,25 +385,27 @@ _line_wrap(struct grid *old_grid, struct row **new_grid, struct row *row,
return new_row;
}
-struct tp_cmp_ctx {
+static struct {
int scrollback_start;
int rows;
-};
+} tp_cmp_ctx;
static int
-tp_cmp(const void *_a, const void *_b, void *_arg)
+tp_cmp(const void *_a, const void *_b)
{
const struct coord *a = *(const struct coord **)_a;
const struct coord *b = *(const struct coord **)_b;
- const struct tp_cmp_ctx *ctx = _arg;
- int a_row = (a->row - ctx->scrollback_start + ctx->rows) & (ctx->rows - 1);
- int b_row = (b->row - ctx->scrollback_start + ctx->rows) & (ctx->rows - 1);
+ int scrollback_start = tp_cmp_ctx.scrollback_start;
+ int num_rows = tp_cmp_ctx.rows;
+
+ int a_row = (a->row - scrollback_start + num_rows) & (num_rows - 1);
+ int b_row = (b->row - scrollback_start + num_rows) & (num_rows - 1);
xassert(a_row >= 0);
- xassert(a_row < ctx->rows || ctx->rows == 0);
+ xassert(a_row < num_rows || num_rows == 0);
xassert(b_row >= 0);
- xassert(b_row < ctx->rows || ctx->rows == 0);
+ xassert(b_row < num_rows || num_rows == 0);
if (a_row < b_row)
return -1;
@@ -486,9 +488,11 @@ grid_resize_and_reflow(
if (!view_follows)
tracking_points[tracking_points_count + 2] = &viewport;
- qsort_r(
- tracking_points, tp_count - 1, sizeof(tracking_points[0]), &tp_cmp,
- &(struct tp_cmp_ctx){.scrollback_start = offset, .rows = old_rows});
+ /* Not thread safe! */
+ tp_cmp_ctx.scrollback_start = offset;
+ tp_cmp_ctx.rows = old_rows;
+ qsort(
+ tracking_points, tp_count - 1, sizeof(tracking_points[0]), &tp_cmp);
/* NULL terminate */
struct coord terminator = {-1, -1};