Skip to content
Browse files

Replaced Zlib FFI with C-ext. Closes #1727.

While it saddens me greatly to replace Ruby code with very problematic
MRI C code, the reality is that time is better spent eventually replacing
MRI C code with quality Ruby code rather than wasting time debugging
unmaintained Ruby-ified MRI C code.
  • Loading branch information...
1 parent 252ba59 commit 9aa6a37f3cdb598d82c88db9cbf8f1695d02d2cb @brixen brixen committed Jul 2, 2012
View
2 .gitignore
@@ -79,8 +79,6 @@ tmtags
/lib/etc.rb
/lib/kernel.rb
-/lib/18/zlib.rb
-/lib/19/zlib.rb
/lib/fcntl.rb
/lib/syslog.rb
/lib/readline.rb
View
1 lib/18/zlib.rb
@@ -0,0 +1 @@
+require "zlib/ext/zlib"
View
1,633 lib/18/zlib.rb.ffi
0 additions, 1,633 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 lib/18/zlib/ext/.gitignore
@@ -0,0 +1,2 @@
+mkmf.log
+Makefile
View
61 lib/18/zlib/ext/extconf.rb
@@ -0,0 +1,61 @@
+#
+# extconf.rb
+#
+# $Id: extconf.rb 16679 2008-05-29 11:23:36Z knu $
+#
+
+require 'mkmf'
+require 'rbconfig'
+
+dir_config 'zlib'
+
+
+if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and
+ have_header('zlib.h') then
+
+ defines = []
+
+ message 'checking for kind of operating system... '
+ os_code = with_config('os-code') ||
+ case RUBY_PLATFORM.split('-',2)[1]
+ when 'amigaos' then
+ os_code = 'AMIGA'
+ when /\Aos2[\-_]emx\z/ then
+ os_code = 'OS2'
+ when /mswin|mingw|bccwin/ then
+ # NOTE: cygwin should be regarded as Unix.
+ os_code = 'WIN32'
+ else
+ os_code = 'UNIX'
+ end
+ os_code = 'OS_' + os_code.upcase
+
+ OS_NAMES = {
+ 'OS_MSDOS' => 'MS-DOS',
+ 'OS_AMIGA' => 'Amiga',
+ 'OS_VMS' => 'VMS',
+ 'OS_UNIX' => 'Unix',
+ 'OS_ATARI' => 'Atari',
+ 'OS_OS2' => 'OS/2',
+ 'OS_MACOS' => 'MacOS',
+ 'OS_TOPS20' => 'TOPS20',
+ 'OS_WIN32' => 'Win32',
+ 'OS_VMCMS' => 'VM/CMS',
+ 'OS_ZSYSTEM' => 'Z-System',
+ 'OS_CPM' => 'CP/M',
+ 'OS_QDOS' => 'QDOS',
+ 'OS_RISCOS' => 'RISCOS',
+ 'OS_UNKNOWN' => 'Unknown',
+ }
+ unless OS_NAMES.key? os_code then
+ puts "invalid OS_CODE `#{os_code}'"
+ exit
+ end
+ message "#{OS_NAMES[os_code]}\n"
+ defines << "OS_CODE=#{os_code}"
+
+ $defs.concat(defines.collect{|d|' -D'+d})
+
+ create_makefile('zlib')
+
+end
View
3,552 lib/18/zlib/ext/zlib.c
3,552 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
1 lib/19/zlib.rb
@@ -0,0 +1 @@
+require "zlib/ext/zlib"
View
1,633 lib/19/zlib.rb.ffi
0 additions, 1,633 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 lib/19/zlib/ext/.gitignore
@@ -0,0 +1,2 @@
+mkmf.log
+Makefile
View
64 lib/19/zlib/ext/extconf.rb
@@ -0,0 +1,64 @@
+#
+# extconf.rb
+#
+# $Id: extconf.rb 26353 2010-01-19 05:14:29Z usa $
+#
+
+require 'mkmf'
+require 'rbconfig'
+
+dir_config 'zlib'
+
+
+if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and
+ have_header('zlib.h') then
+
+ defines = []
+
+ message 'checking for kind of operating system... '
+ os_code = with_config('os-code') ||
+ case RUBY_PLATFORM.split('-',2)[1]
+ when 'amigaos' then
+ os_code = 'AMIGA'
+ when /\Aos2[\-_]emx\z/ then
+ os_code = 'OS2'
+ when /mswin|mingw|bccwin/ then
+ # NOTE: cygwin should be regarded as Unix.
+ os_code = 'WIN32'
+ else
+ os_code = 'UNIX'
+ end
+ os_code = 'OS_' + os_code.upcase
+
+ OS_NAMES = {
+ 'OS_MSDOS' => 'MS-DOS',
+ 'OS_AMIGA' => 'Amiga',
+ 'OS_VMS' => 'VMS',
+ 'OS_UNIX' => 'Unix',
+ 'OS_ATARI' => 'Atari',
+ 'OS_OS2' => 'OS/2',
+ 'OS_MACOS' => 'MacOS',
+ 'OS_TOPS20' => 'TOPS20',
+ 'OS_WIN32' => 'Win32',
+ 'OS_VMCMS' => 'VM/CMS',
+ 'OS_ZSYSTEM' => 'Z-System',
+ 'OS_CPM' => 'CP/M',
+ 'OS_QDOS' => 'QDOS',
+ 'OS_RISCOS' => 'RISCOS',
+ 'OS_UNKNOWN' => 'Unknown',
+ }
+ unless OS_NAMES.key? os_code then
+ puts "invalid OS_CODE `#{os_code}'"
+ exit
+ end
+ message "#{OS_NAMES[os_code]}\n"
+ defines << "OS_CODE=#{os_code}"
+
+ $defs.concat(defines.collect{|d|' -D'+d})
+
+ have_func('crc32_combine', 'zlib.h')
+ have_func('adler32_combine', 'zlib.h')
+
+ create_makefile('zlib')
+
+end
View
4,383 lib/19/zlib/ext/zlib.c
4,383 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
6 rakelib/extensions.rake
@@ -162,6 +162,9 @@ if enabled_18
compile_ext "18/pty", :deps => ["Makefile"],
:dir => "lib/18/pty/ext",
:env => "-X18"
+ compile_ext "18/zlib", :deps => ["Makefile", "extconf.rb"],
+ :dir => "lib/18/zlib/ext",
+ :env => "-X18"
end
if enabled_19
@@ -194,6 +197,9 @@ if enabled_19
compile_ext "19/pty", :deps => ["Makefile"],
:dir => "lib/19/pty/ext",
:env => "-X19"
+ compile_ext "19/zlib", :deps => ["Makefile", "extconf.rb"],
+ :dir => "lib/19/zlib/ext",
+ :env => "-X19"
end
compile_ext "dbm", :ignore_fail => true, :deps => ["Makefile"], :dir => "lib/dbm/ext"
View
2 rakelib/vm.rake
@@ -146,8 +146,6 @@ namespace :build do
namespace :ffi do
FFI_PREPROCESSABLES = %w[ lib/fcntl.rb
- lib/18/zlib.rb
- lib/19/zlib.rb
]
unless BUILD_CONFIG[:windows]
View
11 vm/capi/19/include/ruby/encoding.h
@@ -32,6 +32,17 @@ typedef OnigEncodingType rb_encoding;
#define rb_enc_mbcput(c, buf, enc) ONIGENC_CODE_TO_MBC(enc,c,(UChar*)(buf))
#define rb_enc_asciicompat(enc) (rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc))
+/* start, ptr, end, encoding -> prev_char */
+#define rb_enc_prev_char(s,p,e,enc) \
+ ((char *)onigenc_get_prev_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
+/* start, ptr, end, encoding -> next_char */
+#define rb_enc_left_char_head(s,p,e,enc) \
+ ((char *)onigenc_get_left_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
+#define rb_enc_right_char_head(s,p,e,enc) \
+ ((char *)onigenc_get_right_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
+#define rb_enc_step_back(s,p,e,n,enc) \
+ ((char *)onigenc_step_back((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e),(int)(n)))
+
#define rb_enc_name(enc) (enc)->name
#define rb_enc_mbminlen(enc) (enc)->min_enc_len
#define rb_enc_mbmaxlen(enc) (enc)->max_enc_len
View
4 vm/capi/19/include/ruby/ruby.h
@@ -652,8 +652,10 @@ VALUE rb_uint2big(unsigned long number);
/** The pointer to the string str's data. */
#ifdef RUBY_READONLY_STRING
#define RSTRING_PTR(str) rb_str_ptr_readonly(str)
+#define RSTRING_END(str) rb_str_ptr_readonly_end(str)
#else
#define RSTRING_PTR(str) (RSTRING(str)->ptr)
+#define RSTRING_END(str) (RSTRING(str)->ptr + RSTRING(str)->len)
#endif
/** The pointer to the data. */
@@ -1605,6 +1607,8 @@ VALUE rb_uint2big(unsigned long number);
char* rb_str_ptr_readonly(VALUE self);
#define HAVE_RB_STR_PTR_READONLY 1
+ char* rb_str_ptr_readonly_end(VALUE self);
+
/** Appends other String to self and returns the modified self. */
VALUE rb_str_append(VALUE self, VALUE other);
View
8 vm/capi/string.cpp
@@ -428,6 +428,14 @@ extern "C" {
return rstr->ptr;
}
+ char* rb_str_ptr_readonly_end(VALUE self) {
+ NativeMethodEnvironment* env = NativeMethodEnvironment::get();
+
+ RString* rstr = Handle::from(self)->as_rstring(env, RSTRING_CACHE_SAFE);
+
+ return rstr->ptr + rstr->len;
+ }
+
long rb_str_len(VALUE self) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();

0 comments on commit 9aa6a37

Please sign in to comment.
Something went wrong with that request. Please try again.