File a495d111-Expose-low-level-memory-libsass-heap-allocator.patch of Package libsass.7438

From a495d1114859170882d9e9e7cd5c79ccf8f94cb9 Mon Sep 17 00:00:00 2001
From: Marcel Greter <marcel.greter@ocbnet.ch>
Date: Wed, 6 Apr 2016 03:45:14 +0200
Subject: [PATCH] Expose low-level memory (libsass heap) allocator

---
 docs/api-context.md |  8 ++++----
 include/sass/base.h |  7 +++++++
 src/sass.cpp        | 24 ++++++++++++++++++++++++
 src/util.cpp        |  5 -----
 src/util.hpp        |  5 +++++
 5 files changed, 40 insertions(+), 9 deletions(-)

Index: libsass-3.3.2/docs/api-context.md
===================================================================
--- libsass-3.3.2.orig/docs/api-context.md
+++ libsass-3.3.2/docs/api-context.md
@@ -88,7 +88,7 @@ char* source_map_file;
 char* source_map_root;
 ```
 ```C
-// Custom functions that can be called from sccs code
+// Custom functions that can be called from Sass code
 Sass_C_Function_List c_functions;
 ```
 ```C
@@ -168,12 +168,12 @@ struct Sass_Compiler* sass_make_data_com
 
 // Execute the different compilation steps individually
 // Usefull if you only want to query the included files
-int sass_compiler_parse(struct Sass_Compiler* compiler);
-int sass_compiler_execute(struct Sass_Compiler* compiler);
+int sass_compiler_parse (struct Sass_Compiler* compiler);
+int sass_compiler_execute (struct Sass_Compiler* compiler);
 
 // Release all memory allocated with the compiler
 // This does _not_ include any contexts or options
-void sass_delete_compiler(struct Sass_Compiler* compiler);
+void sass_delete_compiler (struct Sass_Compiler* compiler);
 
 // Release all memory allocated and also ourself
 void sass_delete_file_context (struct Sass_File_Context* ctx);
Index: libsass-3.3.2/include/sass/base.h
===================================================================
--- libsass-3.3.2.orig/include/sass/base.h
+++ libsass-3.3.2/include/sass/base.h
@@ -59,6 +59,13 @@ enum Sass_Output_Style {
   SASS_STYLE_COMPRESSED
 };
 
+// to allocate buffer to be filled
+void* sass_alloc_memory(size_t size);
+// to allocate a buffer from existing string
+char* sass_copy_c_string(const char* str);
+// to free overtaken memory when done
+void sass_free_memory(void* ptr);
+
 // Some convenient string helper function
 ADDAPI char* ADDCALL sass_string_quote (const char* str, const char quote_mark);
 ADDAPI char* ADDCALL sass_string_unquote (const char* str);
Index: libsass-3.3.2/src/sass.cpp
===================================================================
--- libsass-3.3.2.orig/src/sass.cpp
+++ libsass-3.3.2/src/sass.cpp
@@ -10,6 +10,30 @@
 extern "C" {
   using namespace Sass;
 
+  // Allocate libsass heap memory
+  // Don't forget string termination!
+  void* ADDCALL sass_alloc_memory(size_t size)
+  {
+    void* ptr = malloc(size);
+    if (ptr == NULL)
+      out_of_memory();
+    return ptr;
+  }
+
+  char* ADDCALL sass_copy_c_string(const char* str)
+  {
+    size_t len = strlen(str) + 1;
+    char* cpy = (char*) sass_alloc_memory(len);
+    std::memcpy(cpy, str, len);
+    return cpy;
+  }
+
+  // Deallocate libsass heap memory
+  void ADDCALL sass_free_memory(void* ptr)
+  {
+    if (ptr) free (ptr);
+  }
+
   // caller must free the returned memory
   char* ADDCALL sass_string_quote (const char *str, const char quote_mark)
   {
Index: libsass-3.3.2/src/util.cpp
===================================================================
--- libsass-3.3.2.orig/src/util.cpp
+++ libsass-3.3.2/src/util.cpp
@@ -11,11 +11,6 @@
 
 namespace Sass {
 
-  #define out_of_memory() do {            \
-      std::cerr << "Out of memory.\n";    \
-      exit(EXIT_FAILURE);                 \
-    } while (0)
-
   double round(double val)
   {
     // work around some compiler issue
Index: libsass-3.3.2/src/util.hpp
===================================================================
--- libsass-3.3.2.orig/src/util.hpp
+++ libsass-3.3.2/src/util.hpp
@@ -11,6 +11,11 @@
 
 namespace Sass {
 
+  #define out_of_memory() do {            \
+      std::cerr << "Out of memory.\n";    \
+      exit(EXIT_FAILURE);                 \
+    } while (0)
+
   double round(double val);
   char* sass_strdup(const char* str);
   double sass_atof(const char* str);
openSUSE Build Service is sponsored by