Skip to content

Commit

Permalink
Let API user pass ownership of potentially big strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mgreter committed Nov 10, 2014
1 parent 6cb2122 commit 208d085
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
10 changes: 6 additions & 4 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 11 additions & 4 deletions sass_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern "C" {
struct Sass_Import {
const char* path;
char* source;
const char* srcmap;
char* srcmap;
};

struct Sass_C_Import_Descriptor {
Expand All @@ -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;
Expand All @@ -82,6 +83,8 @@ extern "C" {
{
struct Sass_Import** it = list;
while(*list) {
free((*list)->source);
free((*list)->srcmap);
free(*list);
++list;
}
Expand All @@ -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; }

}
9 changes: 6 additions & 3 deletions sass_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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**);
Expand Down

0 comments on commit 208d085

Please sign in to comment.