Project

General

Profile

Bug #17023 » string-relocate.rb

larskanis (Lars Kanis), 07/10/2020 05:27 PM

 
File.write "string-relocate.c", <<-EOC
static char *g_str;

void set(char* str) {
g_str = str;
}

char* get() {
return g_str;
}
EOC
system "gcc -shared -fPIC string-relocate.c -o string-relocate.so"

require 'ffi'

class Foo
extend FFI::Library
ffi_lib File.expand_path('string-relocate.so')

attach_function :set, [:string], :void
attach_function :get, [], :string

def initialize(count)
proc {} # necessary to trigger relocation
a = "a" * count
set(a)

GC.verify_compaction_references(toward: :empty, double_heap: true)

puts "get(#{count}): #{get} (should be: #{a})"
end
end

Foo.new(23)
Foo.new(24)
(1-1/2)