Skip to content

Commit

Permalink
Add Coolio namespece to IO::Buffer (#82)
Browse files Browse the repository at this point in the history
* Add Coolio namespece to IO::Buffer

The `IO::Buffer` class was introduced at Ruby 3.1.
Ref: https://bugs.ruby-lang.org/issues/18020

The cool.io gem has same name class and it has been conflicted.
If you used unknowingly, it will overwrite Ruby's IO::Buffer and it might cause problems.

So, this patch will add namespece that it can be used as `Coolio::IO::Buffer`.

* Rename Coolio::IO::Buffer to Coolio::Buffer

* Move iobuffer.c into cool.io dir

* Rename the initializer function
  • Loading branch information
Watson1978 authored Oct 2, 2024
1 parent 2735948 commit 0f0e442
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 98 deletions.
5 changes: 0 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cool.io.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
156 changes: 80 additions & 76 deletions ext/iobuffer/iobuffer.c → ext/cool.io/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ext/cool.io/cool.io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions ext/cool.io/cool.io_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion ext/cool.io/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

10 changes: 0 additions & 10 deletions ext/iobuffer/extconf.rb

This file was deleted.

Loading

0 comments on commit 0f0e442

Please sign in to comment.