Commit 948ec300 authored by Guillaume Charifi's avatar Guillaume Charifi

compiler/utils: Introduce buffer_init_hint().

parent 8edb3378
......@@ -188,6 +188,21 @@ int buffer_init(struct buffer *buf)
{
buf->nb_lines = 0;
buf->nb_lines_allocated = 0;
buf->line_chunk_size = LINE_CHUNK_SIZE;
buf->lines = NULL;
buf->total_size = 0;
return 0;
}
int buffer_init_hint(struct buffer *buf, unsigned long line_chunk_size)
{
if (!line_chunk_size)
return -1;
buf->nb_lines = 0;
buf->nb_lines_allocated = 0;
buf->line_chunk_size = line_chunk_size;
buf->lines = NULL;
buf->total_size = 0;
......@@ -204,6 +219,7 @@ int buffer_init_from_split_str(struct buffer *buf, const wchar_t *str, wchar_t s
buf->nb_lines = 0;
buf->nb_lines_allocated = 0;
buf->line_chunk_size = LINE_CHUNK_SIZE;
buf->lines = NULL;
buf->total_size = str_len + 1;
......@@ -217,11 +233,11 @@ int buffer_init_from_split_str(struct buffer *buf, const wchar_t *str, wchar_t s
{
if (buf->nb_lines + 1 > buf->nb_lines_allocated)
{
lines = realloc(buf->lines, (buf->nb_lines_allocated + LINE_CHUNK_SIZE) * sizeof(buf->lines[0]));
lines = realloc(buf->lines, (buf->nb_lines_allocated + buf->line_chunk_size) * sizeof(buf->lines[0]));
if (!lines)
goto failed_realloc_lines;
buf->nb_lines_allocated += LINE_CHUNK_SIZE;
buf->nb_lines_allocated += buf->line_chunk_size;
buf->lines = lines;
}
......@@ -253,6 +269,7 @@ int buffer_init_from_line(struct buffer *buf, const wchar_t *line)
buf->nb_lines = 1;
buf->nb_lines_allocated = 1;
buf->line_chunk_size = LINE_CHUNK_SIZE;
buf->lines = NULL;
buf->total_size = str_len + 1;
......@@ -279,6 +296,7 @@ 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->line_chunk_size = buf->line_chunk_size;
out->total_size = buf->total_size;
out->lines = NULL;
......@@ -370,10 +388,16 @@ int buffer_append_line(struct buffer *buf, const wchar_t *line)
len = wcslen(line);
lines = realloc(buf->lines, (buf->nb_lines + 1) * sizeof(buf->lines[0]));
if (!lines)
return -1;
buf->lines = lines;
if (buf->nb_lines + 1 > buf->nb_lines_allocated)
{
lines = realloc(buf->lines, (buf->nb_lines_allocated + buf->line_chunk_size) * sizeof(buf->lines[0]));
if (!lines)
return -1;
buf->nb_lines_allocated += buf->line_chunk_size;
buf->lines = lines;
}
buf->lines[buf->nb_lines] = NULL;
scratch = wrealloc(buf->lines[0], buf->total_size + len + 1);
......
......@@ -25,6 +25,7 @@ struct buffer
{
unsigned long nb_lines;
unsigned long nb_lines_allocated;
unsigned long line_chunk_size;
wchar_t **lines;
size_t total_size;
};
......@@ -42,6 +43,7 @@ extern wchar_t *str_trim_in_place(wchar_t *str);
extern wchar_t *str_trim(wchar_t *str);
extern wchar_t *str_remove_comment(wchar_t *str);
extern int buffer_init(struct buffer *buf);
extern int buffer_init_hint(struct buffer *buf, unsigned long line_chunk_size);
extern int buffer_init_from_split_str(struct buffer *buf, const wchar_t *str, wchar_t separator);
extern int buffer_init_from_line(struct buffer *buf, const wchar_t *line);
extern int buffer_clone(const struct buffer *buf, struct buffer *out);
......
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