From 208d08578b6fe5a6e9bb8ec95a9d16caca40b542 Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Mon, 10 Nov 2014 22:07:14 +0100 Subject: [PATCH] Let API user pass ownership of potentially big strings User will need to create a copy if the memory is static! Otherwise he can pass the string to libsass and we will take care to free the memory when the context is destroyed. --- parser.cpp | 10 ++++++---- sass_functions.cpp | 15 +++++++++++---- sass_functions.h | 9 ++++++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/parser.cpp b/parser.cpp index 3ee5d2a39c..3bdb8fd2a0 100644 --- a/parser.cpp +++ b/parser.cpp @@ -167,17 +167,19 @@ namespace Sass { while (*includes) { struct Sass_Import* include = *includes; const char *file = sass_import_get_path(include); - const char *source = sass_import_get_source(include); - // const char *srcmap = sass_import_get_srcmap[include]; + char *source = sass_import_get_source(include); + // char *srcmap = sass_import_get_srcmap(include); if (source) { string inc_path = unquote(import_path); if (file) { - ctx.add_source(file, import_path, strdup(source)); + ctx.add_source(file, import_path, source); imp->files().push_back(file); } else { - ctx.add_source(import_path, import_path, strdup(source)); + ctx.add_source(import_path, import_path, source); imp->files().push_back(import_path); } + // we passed ownership to context + sass_import_forget_source(include); } else if(file) { add_single_file(imp, file); } diff --git a/sass_functions.cpp b/sass_functions.cpp index b698cde8de..a1b3363110 100644 --- a/sass_functions.cpp +++ b/sass_functions.cpp @@ -38,7 +38,7 @@ extern "C" { struct Sass_Import { const char* path; char* source; - const char* srcmap; + char* srcmap; }; struct Sass_C_Import_Descriptor { @@ -64,7 +64,8 @@ extern "C" { } // Creator for a single import entry returned by the custom importer inside the list - struct Sass_Import* sass_make_import_entry(const char* path, char* source, const char* srcmap) + // We take ownership of the memory for source and srcmap (freed when context is destroyd) + struct Sass_Import* sass_make_import_entry(const char* path, char* source, char* srcmap) { Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import)); v->path = path; @@ -82,6 +83,8 @@ extern "C" { { struct Sass_Import** it = list; while(*list) { + free((*list)->source); + free((*list)->srcmap); free(*list); ++list; } @@ -90,7 +93,11 @@ extern "C" { // Getter for import entry const char*sass_import_get_path(struct Sass_Import* entry) { return entry->path; } - const char*sass_import_get_source(struct Sass_Import* entry) { return entry->source; } - const char*sass_import_get_srcmap(struct Sass_Import* entry) { return entry->srcmap; } + char* sass_import_get_source(struct Sass_Import* entry) { return entry->source; } + char* sass_import_get_srcmap(struct Sass_Import* entry) { return entry->srcmap; } + + // Explicit functions once the ownership is passed on + void sass_import_forget_source (struct Sass_Import* entry) { entry->source = 0; } + void sass_import_forget_srcmap (struct Sass_Import* entry) { entry->srcmap = 0; } } diff --git a/sass_functions.h b/sass_functions.h index 49f4278325..e84a6b6fd8 100644 --- a/sass_functions.h +++ b/sass_functions.h @@ -29,7 +29,7 @@ void* sass_import_get_cookie (Sass_C_Import_Callback fn); // Creator for sass custom importer return argument list struct Sass_Import** sass_make_import_list (size_t length); // Creator for a single import entry returned by the custom importer inside the list -struct Sass_Import* sass_make_import_entry (const char* path, char* source, const char* srcmap); +struct Sass_Import* sass_make_import_entry (const char* path, char* source, char* srcmap); // Setters to insert an entry into the import list (you may also use [] access directly) // Since we are dealing with pointers they should have a guaranteed and fixed size @@ -38,8 +38,11 @@ struct Sass_Import* sass_import_get_list_entry (struct Sass_Import** list, size_ // Getters for import entry const char*sass_import_get_path (struct Sass_Import*); -const char*sass_import_get_source (struct Sass_Import*); -const char*sass_import_get_srcmap (struct Sass_Import*); +char* sass_import_get_source (struct Sass_Import*); +char* sass_import_get_srcmap (struct Sass_Import*); +// Explicit functions once the ownership is passed on +void sass_import_forget_source (struct Sass_Import*); +void sass_import_forget_srcmap (struct Sass_Import*); // Deallocator for associated memory (incl. entries) void sass_delete_import_list (struct Sass_Import**);