diff --git a/Rakefile b/Rakefile index 2050f4c..33c7df3 100644 --- a/Rakefile +++ b/Rakefile @@ -31,11 +31,6 @@ def configure_cross_compilation(ext) end end -Rake::ExtensionTask.new('iobuffer_ext', spec) do |ext| - ext.ext_dir = 'ext/iobuffer' - configure_cross_compilation(ext) -end - Rake::ExtensionTask.new('cool.io_ext', spec) do |ext| ext.ext_dir = 'ext/cool.io' configure_cross_compilation(ext) diff --git a/cool.io.gemspec b/cool.io.gemspec index e0bb210..9baf947 100644 --- a/cool.io.gemspec +++ b/cool.io.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |s| s.homepage = "https://github.com/socketry/cool.io" s.summary = "A cool framework for doing high performance I/O in Ruby" s.description = "Cool.io provides a high performance event framework for Ruby which uses the libev C library" - s.extensions = ["ext/cool.io/extconf.rb", "ext/iobuffer/extconf.rb"] + s.extensions = ["ext/cool.io/extconf.rb"] s.licenses = ["MIT"] s.files = `git ls-files`.split("\n") diff --git a/ext/iobuffer/iobuffer.c b/ext/cool.io/buffer.c similarity index 79% rename from ext/iobuffer/iobuffer.c rename to ext/cool.io/buffer.c index 07ed3c0..0b0a28d 100644 --- a/ext/iobuffer/iobuffer.c +++ b/ext/cool.io/buffer.c @@ -43,25 +43,26 @@ struct buffer_node { unsigned char data[0]; }; -static VALUE cIO_Buffer = Qnil; - -static VALUE IO_Buffer_allocate(VALUE klass); -static void IO_Buffer_mark(struct buffer *); -static void IO_Buffer_free(struct buffer *); - -static VALUE IO_Buffer_default_node_size(VALUE klass); -static VALUE IO_Buffer_set_default_node_size(VALUE klass, VALUE size); -static VALUE IO_Buffer_initialize(int argc, VALUE * argv, VALUE self); -static VALUE IO_Buffer_clear(VALUE self); -static VALUE IO_Buffer_size(VALUE self); -static VALUE IO_Buffer_empty(VALUE self); -static VALUE IO_Buffer_append(VALUE self, VALUE data); -static VALUE IO_Buffer_prepend(VALUE self, VALUE data); -static VALUE IO_Buffer_read(int argc, VALUE * argv, VALUE self); -static VALUE IO_Buffer_read_frame(VALUE self, VALUE data, VALUE mark); -static VALUE IO_Buffer_to_str(VALUE self); -static VALUE IO_Buffer_read_from(VALUE self, VALUE io); -static VALUE IO_Buffer_write_to(VALUE self, VALUE io); +static VALUE mCoolio = Qnil; +static VALUE cCoolio_Buffer = Qnil; + +static VALUE Coolio_Buffer_allocate(VALUE klass); +static void Coolio_Buffer_mark(struct buffer *); +static void Coolio_Buffer_free(struct buffer *); + +static VALUE Coolio_Buffer_default_node_size(VALUE klass); +static VALUE Coolio_Buffer_set_default_node_size(VALUE klass, VALUE size); +static VALUE Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self); +static VALUE Coolio_Buffer_clear(VALUE self); +static VALUE Coolio_Buffer_size(VALUE self); +static VALUE Coolio_Buffer_empty(VALUE self); +static VALUE Coolio_Buffer_append(VALUE self, VALUE data); +static VALUE Coolio_Buffer_prepend(VALUE self, VALUE data); +static VALUE Coolio_Buffer_read(int argc, VALUE * argv, VALUE self); +static VALUE Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark); +static VALUE Coolio_Buffer_to_str(VALUE self); +static VALUE Coolio_Buffer_read_from(VALUE self, VALUE io); +static VALUE Coolio_Buffer_write_to(VALUE self, VALUE io); static struct buffer *buffer_new(void); static void buffer_clear(struct buffer * buf); @@ -83,60 +84,63 @@ static int buffer_write_to(struct buffer * buf, int fd); * Ruby IO objects. */ void -Init_iobuffer_ext() +Init_coolio_buffer() { - cIO_Buffer = rb_define_class_under(rb_cIO, "Buffer", rb_cObject); - rb_define_alloc_func(cIO_Buffer, IO_Buffer_allocate); - - rb_define_singleton_method(cIO_Buffer, "default_node_size", - IO_Buffer_default_node_size, 0); - rb_define_singleton_method(cIO_Buffer, "default_node_size=", - IO_Buffer_set_default_node_size, 1); - - rb_define_method(cIO_Buffer, "initialize", IO_Buffer_initialize, -1); - rb_define_method(cIO_Buffer, "clear", IO_Buffer_clear, 0); - rb_define_method(cIO_Buffer, "size", IO_Buffer_size, 0); - rb_define_method(cIO_Buffer, "empty?", IO_Buffer_empty, 0); - rb_define_method(cIO_Buffer, "<<", IO_Buffer_append, 1); - rb_define_method(cIO_Buffer, "append", IO_Buffer_append, 1); - rb_define_method(cIO_Buffer, "write", IO_Buffer_append, 1); - rb_define_method(cIO_Buffer, "prepend", IO_Buffer_prepend, 1); - rb_define_method(cIO_Buffer, "read", IO_Buffer_read, -1); - rb_define_method(cIO_Buffer, "read_frame", IO_Buffer_read_frame, 2); - rb_define_method(cIO_Buffer, "to_str", IO_Buffer_to_str, 0); - rb_define_method(cIO_Buffer, "read_from", IO_Buffer_read_from, 1); - rb_define_method(cIO_Buffer, "write_to", IO_Buffer_write_to, 1); - - rb_define_const(cIO_Buffer, "MAX_SIZE", INT2NUM(MAX_BUFFER_SIZE)); + VALUE cCoolio_IO; + + mCoolio = rb_define_module("Coolio"); + cCoolio_Buffer = rb_define_class_under(mCoolio, "Buffer", rb_cObject); + rb_define_alloc_func(cCoolio_Buffer, Coolio_Buffer_allocate); + + rb_define_singleton_method(cCoolio_Buffer, "default_node_size", + Coolio_Buffer_default_node_size, 0); + rb_define_singleton_method(cCoolio_Buffer, "default_node_size=", + Coolio_Buffer_set_default_node_size, 1); + + rb_define_method(cCoolio_Buffer, "initialize", Coolio_Buffer_initialize, -1); + rb_define_method(cCoolio_Buffer, "clear", Coolio_Buffer_clear, 0); + rb_define_method(cCoolio_Buffer, "size", Coolio_Buffer_size, 0); + rb_define_method(cCoolio_Buffer, "empty?", Coolio_Buffer_empty, 0); + rb_define_method(cCoolio_Buffer, "<<", Coolio_Buffer_append, 1); + rb_define_method(cCoolio_Buffer, "append", Coolio_Buffer_append, 1); + rb_define_method(cCoolio_Buffer, "write", Coolio_Buffer_append, 1); + rb_define_method(cCoolio_Buffer, "prepend", Coolio_Buffer_prepend, 1); + rb_define_method(cCoolio_Buffer, "read", Coolio_Buffer_read, -1); + rb_define_method(cCoolio_Buffer, "read_frame", Coolio_Buffer_read_frame, 2); + rb_define_method(cCoolio_Buffer, "to_str", Coolio_Buffer_to_str, 0); + rb_define_method(cCoolio_Buffer, "read_from", Coolio_Buffer_read_from, 1); + rb_define_method(cCoolio_Buffer, "write_to", Coolio_Buffer_write_to, 1); + + rb_define_const(cCoolio_Buffer, "MAX_SIZE", INT2NUM(MAX_BUFFER_SIZE)); } static VALUE -IO_Buffer_allocate(VALUE klass) +Coolio_Buffer_allocate(VALUE klass) { - return Data_Wrap_Struct(klass, IO_Buffer_mark, IO_Buffer_free, buffer_new()); + return Data_Wrap_Struct(klass, Coolio_Buffer_mark, Coolio_Buffer_free, buffer_new()); } static void -IO_Buffer_mark(struct buffer * buf) +Coolio_Buffer_mark(struct buffer * buf) { /* Naively discard the memory pool whenever Ruby garbage collects */ buffer_free_pool(buf); } static void -IO_Buffer_free(struct buffer * buf) +Coolio_Buffer_free(struct buffer * buf) { buffer_free(buf); } /** * call-seq: - * IO_Buffer.default_node_size -> 4096 + * Coolio::Buffer.default_node_size -> 4096 * * Retrieves the current value of the default node size. */ static VALUE -IO_Buffer_default_node_size(VALUE klass) +Coolio_Buffer_default_node_size(VALUE klass) { return UINT2NUM(default_node_size); } @@ -159,12 +163,12 @@ convert_node_size(VALUE size) /** * call-seq: - * IO_Buffer.default_node_size = 16384 + * Coolio::Buffer.default_node_size = 16384 * - * Sets the default node size for calling IO::Buffer.new with no arguments. + * Sets the default node size for calling Coolio::Buffer.new with no arguments. */ static VALUE -IO_Buffer_set_default_node_size(VALUE klass, VALUE size) +Coolio_Buffer_set_default_node_size(VALUE klass, VALUE size) { default_node_size = convert_node_size(size); @@ -173,12 +177,12 @@ IO_Buffer_set_default_node_size(VALUE klass, VALUE size) /** * call-seq: - * IO_Buffer.new(size = IO::Buffer.default_node_size) -> IO_Buffer + * Coolio::Buffer.new(size = Coolio::Buffer.default_node_size) -> Coolio::Buffer * - * Create a new IO_Buffer with linked segments of the given size + * Create a new Coolio::Buffer with linked segments of the given size */ static VALUE -IO_Buffer_initialize(int argc, VALUE * argv, VALUE self) +Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self) { VALUE node_size_obj; struct buffer *buf; @@ -200,12 +204,12 @@ IO_Buffer_initialize(int argc, VALUE * argv, VALUE self) /** * call-seq: - * IO_Buffer#clear -> nil + * Coolio::Buffer#clear -> nil * - * Clear all data from the IO_Buffer + * Clear all data from the Coolio::Buffer */ static VALUE -IO_Buffer_clear(VALUE self) +Coolio_Buffer_clear(VALUE self) { struct buffer *buf; Data_Get_Struct(self, struct buffer, buf); @@ -217,12 +221,12 @@ IO_Buffer_clear(VALUE self) /** * call-seq: - * IO_Buffer#size -> Integer + * Coolio::Buffer#size -> Integer * * Return the size of the buffer in bytes */ static VALUE -IO_Buffer_size(VALUE self) +Coolio_Buffer_size(VALUE self) { struct buffer *buf; Data_Get_Struct(self, struct buffer, buf); @@ -232,12 +236,12 @@ IO_Buffer_size(VALUE self) /** * call-seq: - * IO_Buffer#empty? -> Boolean + * Coolio::Buffer#empty? -> Boolean * * Is the buffer empty? */ static VALUE -IO_Buffer_empty(VALUE self) +Coolio_Buffer_empty(VALUE self) { struct buffer *buf; Data_Get_Struct(self, struct buffer, buf); @@ -247,12 +251,12 @@ IO_Buffer_empty(VALUE self) /** * call-seq: - * IO_Buffer#append(data) -> String + * Coolio::Buffer#append(data) -> String * * Append the given data to the end of the buffer */ static VALUE -IO_Buffer_append(VALUE self, VALUE data) +Coolio_Buffer_append(VALUE self, VALUE data) { struct buffer *buf; Data_Get_Struct(self, struct buffer, buf); @@ -266,12 +270,12 @@ IO_Buffer_append(VALUE self, VALUE data) /** * call-seq: - * IO_Buffer#prepend(data) -> String + * Coolio::Buffer#prepend(data) -> String * * Prepend the given data to the beginning of the buffer */ static VALUE -IO_Buffer_prepend(VALUE self, VALUE data) +Coolio_Buffer_prepend(VALUE self, VALUE data) { struct buffer *buf; Data_Get_Struct(self, struct buffer, buf); @@ -284,7 +288,7 @@ IO_Buffer_prepend(VALUE self, VALUE data) /** * call-seq: - * IO_Buffer#read(length = nil) -> String + * Coolio::Buffer#read(length = nil) -> String * * Read the specified abount of data from the buffer. If no value * is given the entire contents of the buffer are returned. Any data @@ -294,7 +298,7 @@ IO_Buffer_prepend(VALUE self, VALUE data) * the given length). */ static VALUE -IO_Buffer_read(int argc, VALUE * argv, VALUE self) +Coolio_Buffer_read(int argc, VALUE * argv, VALUE self) { VALUE length_obj, str; int length; @@ -322,7 +326,7 @@ IO_Buffer_read(int argc, VALUE * argv, VALUE self) /** * call-seq: - * IO_Buffer#read_frame(str, mark) -> boolean + * Coolio::Buffer#read_frame(str, mark) -> boolean * * Read up to and including the given frame marker (expressed a a * Fixnum 0-255) byte, copying into the supplied string object. If the mark is @@ -331,7 +335,7 @@ IO_Buffer_read(int argc, VALUE * argv, VALUE self) * */ static VALUE -IO_Buffer_read_frame(VALUE self, VALUE data, VALUE mark) +Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark) { char mark_c = (char) NUM2INT(mark); struct buffer *buf; @@ -347,12 +351,12 @@ IO_Buffer_read_frame(VALUE self, VALUE data, VALUE mark) /** * call-seq: - * IO_Buffer#to_str -> String + * Coolio::Buffer#to_str -> String * * Convert the Buffer to a String. The original buffer is unmodified. */ static VALUE -IO_Buffer_to_str(VALUE self) +Coolio_Buffer_to_str(VALUE self) { VALUE str; struct buffer *buf; @@ -367,14 +371,14 @@ IO_Buffer_to_str(VALUE self) /** * call-seq: - * IO_Buffer#read_from(io) -> Integer + * Coolio::Buffer#read_from(io) -> Integer * * Perform a nonblocking read of the the given IO object and fill * the buffer with any data received. The call will read as much * data as it can until the read would block. */ static VALUE -IO_Buffer_read_from(VALUE self, VALUE io) +Coolio_Buffer_read_from(VALUE self, VALUE io) { struct buffer *buf; int ret; @@ -399,14 +403,14 @@ IO_Buffer_read_from(VALUE self, VALUE io) /** * call-seq: - * IO_Buffer#write_to(io) -> Integer + * Coolio::Buffer#write_to(io) -> Integer * * Perform a nonblocking write of the buffer to the given IO object. * As much data as possible is written until the call would block. * Any data which is written is removed from the buffer. */ static VALUE -IO_Buffer_write_to(VALUE self, VALUE io) +Coolio_Buffer_write_to(VALUE self, VALUE io) { struct buffer *buf; #if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR) diff --git a/ext/cool.io/cool.io.h b/ext/cool.io/cool.io.h index 06f73cf..025e15d 100644 --- a/ext/cool.io/cool.io.h +++ b/ext/cool.io/cool.io.h @@ -60,6 +60,7 @@ struct Coolio_Watcher void Coolio_Loop_process_event(VALUE watcher, int revents); +void Init_coolio_buffer(); void Init_coolio_loop(); void Init_coolio_watcher(); void Init_coolio_iowatcher(); diff --git a/ext/cool.io/cool.io_ext.c b/ext/cool.io/cool.io_ext.c index 57092f3..223e756 100644 --- a/ext/cool.io/cool.io_ext.c +++ b/ext/cool.io/cool.io_ext.c @@ -16,6 +16,7 @@ static VALUE mCoolio = Qnil; void Init_cool() { /* Initializers for other modules */ + Init_coolio_buffer(); Init_coolio_loop(); Init_coolio_watcher(); Init_coolio_iowatcher(); diff --git a/ext/cool.io/extconf.rb b/ext/cool.io/extconf.rb index f990899..6062bcb 100644 --- a/ext/cool.io/extconf.rb +++ b/ext/cool.io/extconf.rb @@ -11,6 +11,12 @@ have_func('rb_str_set_len') have_library('rt', 'clock_gettime') +have_func("rb_io_descriptor") +have_library("c", "main") +if have_macro("HAVE_RB_IO_T", "ruby/io.h") + have_struct_member("rb_io_t", "fd", "ruby/io.h") +end + if have_header('ruby/io.h') $defs << '-DHAVE_RUBY_IO_H' end @@ -72,4 +78,3 @@ makefile_contents.gsub! /LIBS = (.*) (\S*ws2_32\S*)/i, 'LIBS = \\2 \\1' File.open('Makefile', 'w') { |f| f.write makefile_contents } end - diff --git a/ext/iobuffer/extconf.rb b/ext/iobuffer/extconf.rb deleted file mode 100644 index e725c81..0000000 --- a/ext/iobuffer/extconf.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'mkmf' - -dir_config("iobuffer") -have_func("rb_io_descriptor") -have_library("c", "main") -if have_macro("HAVE_RB_IO_T", "ruby/io.h") - have_struct_member("rb_io_t", "fd", "ruby/io.h") -end - -create_makefile("iobuffer_ext") diff --git a/lib/cool.io.rb b/lib/cool.io.rb index 5248951..c758906 100644 --- a/lib/cool.io.rb +++ b/lib/cool.io.rb @@ -6,7 +6,6 @@ require "cool.io/version" require "cool.io/custom_require" -cool_require "iobuffer_ext" cool_require "cool.io_ext" require "cool.io/loop" diff --git a/lib/cool.io/io.rb b/lib/cool.io/io.rb index b59e1f4..1ca7ebd 100644 --- a/lib/cool.io/io.rb +++ b/lib/cool.io/io.rb @@ -21,7 +21,7 @@ class IO def initialize(io) @_io = io - @_write_buffer ||= ::IO::Buffer.new + @_write_buffer ||= ::Coolio::Buffer.new @_read_watcher = Watcher.new(io, self, :r) @_write_watcher = Watcher.new(io, self, :w) end diff --git a/lib/cool.io/socket.rb b/lib/cool.io/socket.rb index 013694f..1ae9282 100644 --- a/lib/cool.io/socket.rb +++ b/lib/cool.io/socket.rb @@ -130,7 +130,7 @@ def self.connect(addr, port, *args) # Called by precreate during asyncronous DNS resolution def preinitialize(addr, port, *args) - @_write_buffer = ::IO::Buffer.new # allow for writing BEFORE DNS has resolved + @_write_buffer = ::Coolio::Buffer.new # allow for writing BEFORE DNS has resolved @remote_host, @remote_addr, @remote_port = addr, addr, port @_resolver = TCPConnectResolver.new(self, addr, port, *args) end diff --git a/spec/iobuffer_spec.rb b/spec/iobuffer_spec.rb index 1c2ff3c..a6cadf3 100644 --- a/spec/iobuffer_spec.rb +++ b/spec/iobuffer_spec.rb @@ -1,10 +1,10 @@ require File.expand_path('../spec_helper', __FILE__) -describe IO::Buffer do +describe Cool.io::Buffer do let :buffer do - IO::Buffer.new + Cool.io::Buffer.new end it "provides a subset of the methods available in Strings" do