Skip to content

Commit a495d11

Browse files
committed
Expose low-level memory (libsass heap) allocator
1 parent 131540c commit a495d11

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

docs/api-context.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ char* source_map_file;
8888
char* source_map_root;
8989
```
9090
```C
91-
// Custom functions that can be called from sccs code
91+
// Custom functions that can be called from Sass code
9292
Sass_C_Function_List c_functions;
9393
```
9494
```C
@@ -168,12 +168,12 @@ struct Sass_Compiler* sass_make_data_compiler (struct Sass_Data_Context* data_ct
168168

169169
// Execute the different compilation steps individually
170170
// Usefull if you only want to query the included files
171-
int sass_compiler_parse(struct Sass_Compiler* compiler);
172-
int sass_compiler_execute(struct Sass_Compiler* compiler);
171+
int sass_compiler_parse (struct Sass_Compiler* compiler);
172+
int sass_compiler_execute (struct Sass_Compiler* compiler);
173173

174174
// Release all memory allocated with the compiler
175175
// This does _not_ include any contexts or options
176-
void sass_delete_compiler(struct Sass_Compiler* compiler);
176+
void sass_delete_compiler (struct Sass_Compiler* compiler);
177177

178178
// Release all memory allocated and also ourself
179179
void sass_delete_file_context (struct Sass_File_Context* ctx);

include/sass/base.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ enum Sass_Output_Style {
6262
SASS_STYLE_TO_SASS
6363
};
6464

65+
// to allocate buffer to be filled
66+
void* sass_alloc_memory(size_t size);
67+
// to allocate a buffer from existing string
68+
char* sass_copy_c_string(const char* str);
69+
// to free overtaken memory when done
70+
void sass_free_memory(void* ptr);
71+
6572
// Some convenient string helper function
6673
ADDAPI char* ADDCALL sass_string_quote (const char* str, const char quote_mark);
6774
ADDAPI char* ADDCALL sass_string_unquote (const char* str);

src/sass.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111
extern "C" {
1212
using namespace Sass;
1313

14+
// Allocate libsass heap memory
15+
// Don't forget string termination!
16+
void* ADDCALL sass_alloc_memory(size_t size)
17+
{
18+
void* ptr = malloc(size);
19+
if (ptr == NULL)
20+
out_of_memory();
21+
return ptr;
22+
}
23+
24+
char* ADDCALL sass_copy_c_string(const char* str)
25+
{
26+
size_t len = strlen(str) + 1;
27+
char* cpy = (char*) sass_alloc_memory(len);
28+
std::memcpy(cpy, str, len);
29+
return cpy;
30+
}
31+
32+
// Deallocate libsass heap memory
33+
void ADDCALL sass_free_memory(void* ptr)
34+
{
35+
if (ptr) free (ptr);
36+
}
37+
1438
// caller must free the returned memory
1539
char* ADDCALL sass_string_quote (const char *str, const char quote_mark)
1640
{

src/util.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212

1313
namespace Sass {
1414

15-
#define out_of_memory() do { \
16-
std::cerr << "Out of memory.\n"; \
17-
exit(EXIT_FAILURE); \
18-
} while (0)
19-
2015
double round(double val, size_t precision)
2116
{
2217
// https://github.com/sass/sass/commit/4e3e1d5684cc29073a507578fc977434ff488c93

src/util.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
namespace Sass {
1414

15+
#define out_of_memory() do { \
16+
std::cerr << "Out of memory.\n"; \
17+
exit(EXIT_FAILURE); \
18+
} while (0)
19+
1520
double round(double val, size_t precision = 0);
1621
char* sass_strdup(const char* str);
1722
double sass_atof(const char* str);

0 commit comments

Comments
 (0)