Commit 7d7603e0 authored by Guillaume Charifi's avatar Guillaume Charifi

compiler/utils: Allocate line array by chunks.

parent 49c5c117
Pipeline #427 passed with stages
in 1 minute and 42 seconds
......@@ -25,6 +25,8 @@
#include "utils.h"
#define LINE_CHUNK_SIZE 128
wchar_t *wmalloc(size_t size)
{
return calloc(size, sizeof(wchar_t));
......@@ -185,6 +187,7 @@ wchar_t *str_remove_comment(wchar_t *str)
int buffer_init(struct buffer *buf)
{
buf->nb_lines = 0;
buf->nb_lines_allocated = 0;
buf->lines = NULL;
buf->total_size = 0;
......@@ -200,6 +203,7 @@ int buffer_init_from_split_str(struct buffer *buf, const wchar_t *str, wchar_t s
str_len = wcslen(str);
buf->nb_lines = 0;
buf->nb_lines_allocated = 0;
buf->lines = NULL;
buf->total_size = str_len + 1;
......@@ -211,10 +215,15 @@ int buffer_init_from_split_str(struct buffer *buf, const wchar_t *str, wchar_t s
while (s)
{
lines = realloc(buf->lines, (buf->nb_lines + 1) * sizeof(buf->lines[0]));
if (!lines)
goto failed_realloc_lines;
buf->lines = lines;
if (buf->nb_lines + 1 > buf->nb_lines_allocated)
{
lines = realloc(buf->lines, (buf->nb_lines_allocated + LINE_CHUNK_SIZE) * sizeof(buf->lines[0]));
if (!lines)
goto failed_realloc_lines;
buf->nb_lines_allocated += LINE_CHUNK_SIZE;
buf->lines = lines;
}
buf->lines[buf->nb_lines++] = s;
......@@ -243,10 +252,11 @@ int buffer_init_from_line(struct buffer *buf, const wchar_t *line)
str_len = wcslen(line);
buf->nb_lines = 1;
buf->nb_lines_allocated = 1;
buf->lines = NULL;
buf->total_size = str_len + 1;
buf->lines = malloc(1 * sizeof(buf->lines[0]));
buf->lines = malloc(buf->nb_lines_allocated * sizeof(buf->lines[0]));
if (!buf->lines)
goto failed_alloc_lines;
......@@ -268,13 +278,14 @@ failed_alloc_lines:
int buffer_clone(const struct buffer *buf, struct buffer *out)
{
out->nb_lines = buf->nb_lines;
out->nb_lines_allocated = buf->nb_lines_allocated;
out->total_size = buf->total_size;
out->lines = NULL;
if (!out->nb_lines || !out->total_size)
return 0;
out->lines = malloc(out->nb_lines * sizeof(out->lines[0]));
out->lines = malloc(out->nb_lines_allocated * sizeof(out->lines[0]));
if (!out->lines)
goto failed_alloc_lines;
......
......@@ -24,6 +24,7 @@
struct buffer
{
unsigned long nb_lines;
unsigned long nb_lines_allocated;
wchar_t **lines;
size_t total_size;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment