libansilove

Library for converting ANSI, ASCII, and other formats to PNG
Log | Files | Refs | README | LICENSE

commit be177d7a5fe3ed06f7cdebeb5133905b8f0ddadf
parent 04fab9ff04d5150b963144bfff5ce608dafe1617
Author: Frederic Cambus <fred@statdns.com>
Date:   Sun, 24 Apr 2022 22:55:01 +0200

Increment pcboard_buffer by more than one element at a time.

We now increment by PCB_BUFFER_SIZE (currently defined to 4096) when
calling reallocarray().

Diffstat:
Msrc/loaders/pcboard.c | 22+++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -26,6 +26,8 @@ #include "reallocarray.h" #endif +#define PCB_BUFFER_SIZE 4096 + #define STATE_TEXT 0 #define STATE_SEQUENCE 1 #define STATE_END 2 @@ -53,6 +55,8 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) /* PCB buffer structure array definition */ struct pcbChar *ptr, *pcboard_buffer = NULL; + size_t pcb_buffer_size = PCB_BUFFER_SIZE; + /* libgd image pointers */ gdImagePtr canvas; @@ -86,7 +90,7 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) select_font(&fontData, options->font); /* PCB buffer dynamic memory allocation */ - pcboard_buffer = malloc(sizeof (struct pcbChar)); + pcboard_buffer = malloc(pcb_buffer_size * sizeof(struct pcbChar)); if (pcboard_buffer == NULL) { ctx->error = ANSILOVE_MEMORY_ERROR; @@ -126,13 +130,17 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) rowMax = row; /* reallocate structure array memory */ - ptr = reallocarray(pcboard_buffer, structIndex + 1, sizeof(struct pcbChar)); - if (ptr == NULL) { - ctx->error = ANSILOVE_MEMORY_ERROR; - goto error; - } + if (structIndex == pcb_buffer_size) { + pcb_buffer_size += PCB_BUFFER_SIZE; + + ptr = reallocarray(pcboard_buffer, pcb_buffer_size, sizeof(struct pcbChar)); + if (ptr == NULL) { + ctx->error = ANSILOVE_MEMORY_ERROR; + goto error; + } - pcboard_buffer = ptr; + pcboard_buffer = ptr; + } /* write current character in pcbChar struct */ pcboard_buffer[structIndex] = (struct pcbChar) {