Skip to content

Commit c49cc79

Browse files
voxikkou
authored andcommitted
Use ffi_closure_free by default. (#20)
* Use ffi_closure_free unconditionally. The current conditionals reflect historic heritage of FFI. Usage of ffi_closure_free should be better default nowadays, because libffi 3.0.5 fixing issues of ffi_closure_free should be widely available. * RUBY_LIBFFI_MODVERSION is not used anymore. Because `ffi_closure_free()` is not used unconditionally, there is no other use for RUBY_LIBFFI_MODVERSION define, so drop its usage. * Use more meaningful variable name. `ver` variable used to be used to pupulate RUBY_LIBFFI_MODVERSION define. Since the define was removed, the `libffi_dir` variable name should better describe the remaining usage of the variable.
1 parent ea06b28 commit c49cc79

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

ext/fiddle/closure.c

-28
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,11 @@ typedef struct {
1313
ffi_type **argv;
1414
} fiddle_closure;
1515

16-
#if defined(USE_FFI_CLOSURE_ALLOC)
17-
#elif defined(__OpenBSD__) || defined(__APPLE__) || defined(__linux__)
18-
# define USE_FFI_CLOSURE_ALLOC 0
19-
#elif defined(RUBY_LIBFFI_MODVERSION) && RUBY_LIBFFI_MODVERSION < 3000005 && \
20-
(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_AMD64))
21-
# define USE_FFI_CLOSURE_ALLOC 0
22-
#else
23-
# define USE_FFI_CLOSURE_ALLOC 1
24-
#endif
25-
2616
static void
2717
dealloc(void * ptr)
2818
{
2919
fiddle_closure * cls = (fiddle_closure *)ptr;
30-
#if USE_FFI_CLOSURE_ALLOC
3120
ffi_closure_free(cls->pcl);
32-
#else
33-
munmap(cls->pcl, sizeof(*cls->pcl));
34-
#endif
3521
if (cls->argv) xfree(cls->argv);
3622
xfree(cls);
3723
}
@@ -205,12 +191,7 @@ allocate(VALUE klass)
205191
VALUE i = TypedData_Make_Struct(klass, fiddle_closure,
206192
&closure_data_type, closure);
207193

208-
#if USE_FFI_CLOSURE_ALLOC
209194
closure->pcl = ffi_closure_alloc(sizeof(ffi_closure), &closure->code);
210-
#else
211-
closure->pcl = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE,
212-
MAP_ANON | MAP_PRIVATE, -1, 0);
213-
#endif
214195

215196
return i;
216197
}
@@ -257,17 +238,8 @@ initialize(int rbargc, VALUE argv[], VALUE self)
257238
if (FFI_OK != result)
258239
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);
259240

260-
#if USE_FFI_CLOSURE_ALLOC
261241
result = ffi_prep_closure_loc(pcl, cif, callback,
262242
(void *)self, cl->code);
263-
#else
264-
result = ffi_prep_closure(pcl, cif, callback, (void *)self);
265-
cl->code = (void *)pcl;
266-
i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC);
267-
if (i) {
268-
rb_sys_fail("mprotect");
269-
}
270-
#endif
271243

272244
if (FFI_OK != result)
273245
rb_raise(rb_eRuntimeError, "error prepping closure %d", result);

ext/fiddle/extconf.rb

+6-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
if ! bundle
88
dir_config 'libffi'
99

10-
pkg_config("libffi") and
11-
ver = pkg_config("libffi", "modversion")
10+
pkg_config("libffi")
1211

1312
if have_header(ffi_header = 'ffi.h')
1413
true
@@ -28,20 +27,20 @@
2827
Dir.glob("#{$srcdir}/libffi-*/").each{|dir| FileUtils.rm_rf(dir)}
2928
extlibs.run(["--cache=#{cache_dir}", ext_dir])
3029
end
31-
ver = bundle != false &&
30+
libffi_dir = bundle != false &&
3231
Dir.glob("#{$srcdir}/libffi-*/")
3332
.map {|n| File.basename(n)}
3433
.max_by {|n| n.scan(/\d+/).map(&:to_i)}
35-
unless ver
34+
unless libffi_dir
3635
raise "missing libffi. Please install libffi."
3736
end
3837

39-
srcdir = "#{$srcdir}/#{ver}"
38+
srcdir = "#{$srcdir}/#{libffi_dir}"
4039
ffi_header = 'ffi.h'
4140
libffi = Struct.new(*%I[dir srcdir builddir include lib a cflags ldflags opt arch]).new
42-
libffi.dir = ver
41+
libffi.dir = libffi_dir
4342
if $srcdir == "."
44-
libffi.builddir = "#{ver}/#{RUBY_PLATFORM}"
43+
libffi.builddir = "#{libffi_dir}/#{RUBY_PLATFORM}"
4544
libffi.srcdir = "."
4645
else
4746
libffi.builddir = libffi.dir
@@ -52,7 +51,6 @@
5251
libffi.a = "#{libffi.lib}/libffi_convenience.#{$LIBEXT}"
5352
nowarn = CONFIG.merge("warnflags"=>"")
5453
libffi.cflags = RbConfig.expand("$(CFLAGS)".dup, nowarn)
55-
ver = ver[/libffi-(.*)/, 1]
5654

5755
FileUtils.mkdir_p(libffi.dir)
5856
libffi.opt = CONFIG['configure_args'][/'(-C)'/, 1]
@@ -112,12 +110,6 @@
112110
$INCFLAGS << " -I" << libffi.include
113111
end
114112

115-
if ver
116-
ver = ver.gsub(/-rc\d+/, '') # If ver contains rc version, just ignored.
117-
ver = (ver.split('.') + [0,0])[0,3]
118-
$defs.push(%{-DRUBY_LIBFFI_MODVERSION=#{ '%d%03d%03d' % ver }})
119-
end
120-
121113
have_header 'sys/mman.h'
122114

123115
if have_header "dlfcn.h"

0 commit comments

Comments
 (0)