diff --git a/gfx/drivers_shader/glslang_util.c b/gfx/drivers_shader/glslang_util.c index 4b05ec1388..51d4b03b56 100644 --- a/gfx/drivers_shader/glslang_util.c +++ b/gfx/drivers_shader/glslang_util.c @@ -139,13 +139,14 @@ enum slang_texture_semantic slang_name_to_texture_semantic_array( bool glslang_read_shader_file(const char *path, struct string_list *output, bool root_file) { + size_t i; char tmp[PATH_MAX_LENGTH]; union string_list_elem_attr attr; - size_t i; const char *basename = NULL; uint8_t *buf = NULL; int64_t buf_len = 0; - struct string_list *lines = NULL; + struct string_list lines = {0}; + bool ret = false; tmp[0] = '\0'; attr.i = 0; @@ -173,7 +174,8 @@ bool glslang_read_shader_file(const char *path, /* Split into lines * (Blank lines must be included) */ - lines = string_separate((char*)buf, "\n"); + string_list_initialize(&lines); + ret = string_separate_noalloc(&lines, (char*)buf, "\n"); } /* Buffer is no longer required - clean up */ @@ -181,17 +183,17 @@ bool glslang_read_shader_file(const char *path, free(buf); /* Sanity check */ - if (!lines) + if (!ret) return false; - if (lines->size < 1) + if (lines.size < 1) goto error; /* If this is the 'parent' shader file, ensure that first * line is a 'VERSION' string */ if (root_file) { - const char *line = lines->elems[0].data; + const char *line = lines.elems[0].data; if (strncmp("#version ", line, STRLEN_CONST("#version "))) { @@ -217,10 +219,10 @@ bool glslang_read_shader_file(const char *path, goto error; /* Loop through lines of file */ - for (i = root_file ? 1 : 0; i < lines->size; i++) + for (i = root_file ? 1 : 0; i < lines.size; i++) { unsigned push_line = 0; - const char *line = lines->elems[i].data; + const char *line = lines.elems[i].data; /* Check for 'include' statements */ if (!strncmp("#include ", line, STRLEN_CONST("#include "))) @@ -276,15 +278,12 @@ bool glslang_read_shader_file(const char *path, } } - string_list_free(lines); + string_list_deinitialize(&lines); return true; error: - - if (lines) - string_list_free(lines); - + string_list_deinitialize(&lines); return false; } diff --git a/libretro-common/include/lists/string_list.h b/libretro-common/include/lists/string_list.h index c5ed5108eb..82767713b8 100644 --- a/libretro-common/include/lists/string_list.h +++ b/libretro-common/include/lists/string_list.h @@ -104,6 +104,9 @@ bool string_split_noalloc(struct string_list *list, */ struct string_list *string_separate(char *str, const char *delim); +bool string_separate_noalloc(struct string_list *list, + char *str, const char *delim); + bool string_list_deinitialize(struct string_list *list); bool string_list_initialize(struct string_list *list); diff --git a/libretro-common/lists/string_list.c b/libretro-common/lists/string_list.c index 99ada45333..0df12d7a18 100644 --- a/libretro-common/lists/string_list.c +++ b/libretro-common/lists/string_list.c @@ -411,6 +411,39 @@ error: return NULL; } +bool string_separate_noalloc( + struct string_list *list, + char *str, const char *delim) +{ + char *token = NULL; + char **str_ptr = NULL; + + /* Sanity check */ + if (!str || string_is_empty(delim) || !list) + return false; + + str_ptr = &str; + token = string_tokenize(str_ptr, delim); + + while (token) + { + union string_list_elem_attr attr; + + attr.i = 0; + + if (!string_list_append(list, token, attr)) + { + free(token); + return false; + } + + free(token); + token = string_tokenize(str_ptr, delim); + } + + return true; +} + /** * string_list_find_elem: * @list : pointer to string list