From 4bd764f568ad312a78181d7a3187f3715388e33e Mon Sep 17 00:00:00 2001 From: Ryan Boland Date: Jun 02 2020 12:51:53 +0000 Subject: Merge pull request #205 from larskanis/remove-libc Use FFI::MemoryPointer instead of libc's malloc() --- diff --git a/lib/sassc/native.rb b/lib/sassc/native.rb index 74e0099..3b9f515 100644 --- a/lib/sassc/native.rb +++ b/lib/sassc/native.rb @@ -34,7 +34,6 @@ module SassC require_relative "native/sass_input_style" require_relative "native/sass_output_style" require_relative "native/string_list" - require_relative "native/lib_c" # Remove the redundant "sass_" from the beginning of every method name def self.attach_function(*args) @@ -53,10 +52,9 @@ module SassC end def self.native_string(string) - string = "#{string}\0" - data = Native::LibC.malloc(string.bytesize) - data.write_string(string) - data + m = FFI::MemoryPointer.from_string(string) + m.autorelease = false + m end require_relative "native/native_context_api" diff --git a/lib/sassc/native/lib_c.rb b/lib/sassc/native/lib_c.rb deleted file mode 100644 index 8e9b08e..0000000 --- a/lib/sassc/native/lib_c.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module SassC - module Native - module LibC - extend FFI::Library - ffi_lib FFI::Library::LIBC - - # memory allocators - attach_function :malloc, [:size_t], :pointer - # attach_function :calloc, [:size_t], :pointer - # attach_function :valloc, [:size_t], :pointer - # attach_function :realloc, [:pointer, :size_t], :pointer - # attach_function :free, [:pointer], :void - - # memory movers - # attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer - # attach_function :bcopy, [:pointer, :pointer, :size_t], :void - end - end -end diff --git a/test/native_test.rb b/test/native_test.rb index 9a5be47..876f56c 100644 --- a/test/native_test.rb +++ b/test/native_test.rb @@ -185,9 +185,9 @@ module SassC funct = FFI::Function.new(:pointer, [:pointer, :pointer, :pointer]) do |url, prev, cookie| list = Native.make_import_list(2) - str = "$var: 5px;\0" - data = Native::LibC.malloc(str.size) - data.write_string(str) + str = "$var: 5px;" + data = FFI::MemoryPointer.from_string(str) + data.autorelease = false entry0 = Native.make_import_entry("fake_includ.scss", data, nil) entry1 = Native.make_import_entry("not_included.scss", nil, nil)