Skip to content

Commit 913150f

Browse files
committed
Add documentation for new C-API low-level memory functions
1 parent 2e77da8 commit 913150f

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

docs/api-doc.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,14 @@ This will automatically load all other headers too!
1616
#include "sass/context.h"
1717
```
1818

19-
### Deprecated usage
20-
21-
The old API is kept in the source for backward compatibility.
22-
It's deprecated and incompatible with this documentation, use `sass/context.h`!
23-
24-
```C
25-
// deprecated interface
26-
#include "sass/interface.h"
27-
```
28-
2919
## Basic C Example
3020

3121
```C
3222
#include <stdio.h>
3323
#include "sass/context.h"
3424

3525
int main() {
36-
puts(libsass_VERSION());
26+
puts(libsass_version());
3727
return 0;
3828
}
3929
```
@@ -114,6 +104,34 @@ This mirrors very well how `libsass` uses these structures.
114104

115105
Structs can be down-casted to access `context` or `options`!
116106

107+
## Memory handling and life-cycles
108+
109+
We keep memory around for as long as the main [context](api-context.md) object is not destroyed (`sass_delete_context`). LibSass will create copies of most inputs/options beside the main sass code.
110+
You need to allocate and fill that buffer before passing it to LibSass. You may also overtake memory management from libsass for certain return values (i.e. `sass_context_take_output_string`).
111+
112+
```C
113+
// to allocate buffer to be filled
114+
void* sass_alloc_memory(size_t size);
115+
// to allocate a buffer from existing string
116+
char* sass_copy_c_string(const char* str);
117+
// to free overtaken memory when done
118+
void sass_free_memory(void* ptr);
119+
```
120+
121+
## Miscellaneous API functions
122+
123+
```C
124+
// Some convenient string helper function
125+
char* sass_string_unquote (const char* str);
126+
char* sass_string_quote (const char* str, const char quote_mark);
127+
128+
// Resolve a file via the given include paths in the include char* array
129+
char* sass_resolve_file (const char* path, const char* incs[]);
130+
131+
// Get compiled libsass version
132+
const char* libsass_version(void);
133+
```
134+
117135
## Common Pitfalls
118136

119137
**input_path**

docs/api-importer-example.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct
1010
// get the cookie from importer descriptor
1111
void* cookie = sass_importer_get_cookie(cb);
1212
Sass_Import_List list = sass_make_import_list(2);
13-
const char* local = "local { color: green; }";
14-
const char* remote = "remote { color: red; }";
15-
list[0] = sass_make_import_entry("/tmp/styles.scss", strdup(local), 0);
16-
list[1] = sass_make_import_entry("http://www.example.com", strdup(remote), 0);
13+
char* local = sass_copy_c_string("local { color: green; }");
14+
char* remote = sass_copy_c_string("remote { color: red; }");
15+
list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
16+
list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
1717
return list;
1818
}
1919

@@ -84,7 +84,7 @@ Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_
8484
Sass_Import_List list = sass_make_import_list(1);
8585
const char* message = "some error message";
8686
list[0] = sass_make_import_entry(path, 0, 0);
87-
sass_import_set_error(list[0], strdup(message), 0, 0);
87+
sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
8888
return list;
8989
}
9090

0 commit comments

Comments
 (0)