From e2c016172ea5201961edec7dca2821cbf71255d2 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Wed, 31 May 2006 22:14:34 +0200 Subject: [PATCH] Refactor view->line reallocation --- tig.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tig.c b/tig.c index decc434..22943f0 100644 --- a/tig.c +++ b/tig.c @@ -862,6 +862,7 @@ struct view { /* Buffering */ unsigned long lines; /* Total number of lines */ struct line *line; /* Line index */ + unsigned long line_size;/* Total number of allocated lines */ unsigned int digits; /* Number of digits in the lines member. */ /* Loading */ @@ -1309,12 +1310,24 @@ begin_update(struct view *view) return TRUE; } +static struct line * +realloc_lines(struct view *view, size_t line_size) +{ + struct line *tmp = realloc(view->line, sizeof(*view->line) * line_size); + + if (!tmp) + return NULL; + + view->line = tmp; + view->line_size = line_size; + return view->line; +} + static bool update_view(struct view *view) { char buffer[BUFSIZ]; char *line; - struct line *tmp; /* The number of lines to read. If too low it will cause too much * redrawing (and possible flickering), if too high responsiveness * will suffer. */ @@ -1328,12 +1341,9 @@ update_view(struct view *view) if (view->offset + view->height >= view->lines) redraw_from = view->lines - view->offset; - tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines)); - if (!tmp) + if (!realloc_lines(view, view->lines + lines)) goto alloc_error; - view->line = tmp; - while ((line = fgets(buffer, sizeof(buffer), view->pipe))) { int linelen = strlen(line); -- 2.30.2