byte_buffer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +4 -0
- data/bin/benchmarks +105 -0
- data/bin/console +7 -0
- data/byte_buffer.gemspec +41 -0
- data/ext/byte_buffer_ext/byte_buffer.c +671 -0
- data/ext/byte_buffer_ext/extconf.rb +2 -0
- data/ext/byte_buffer_ext/portable_endian.h +118 -0
- data/lib/byte_buffer.rb +5 -0
- data/lib/byte_buffer/buffer.rb +26 -0
- data/lib/byte_buffer/version.rb +3 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NjExMjYwYzZjYjdhNGU2M2NhNWJlM2U1MjBjNGRkODRkMDcwNDgzMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjM4NTRhMWVlOGY3NTNlNmYwNTkwNzU2YThkNWRiMGJiMWFlMjYwNw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmFhZjhkODQyZjAzNTk0YjQxMzVjZGMyMjFhNDFkNTJkN2UxYmRkNDdjYzA3
|
10
|
+
NzAyMzZkMzcyYTNjMTQ0ZjA4MTA1ZDg4ODgyODllZmE3MGVlYWIzYzFhNDcx
|
11
|
+
MjIyNmIyYzE2MGRmNmI5NmFkY2IxMGVmYzNlYTkwNDE0NDU4Y2E=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MmI5YWM4OGYyYTdkYmI1YTlhODIzNTcyYzM4ODU2YjEzMzM2NGJkNGVjOTc4
|
14
|
+
YjM3NTY4ODEwZWU5ZDZiMjlmY2MzZWM1ZTI5ODI1ODkxMDdlNjcyN2FjNjQ5
|
15
|
+
MmE5M2UxZjk0NTdkNDE5ODQwZmU1Y2U4ZGVlYmUwZDk1ODk3OGM=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Apptopia Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# ByteBuffer
|
2
|
+
|
3
|
+
Fast native implementation of binary buffer for MRI.
|
4
|
+
|
5
|
+
ByteBuffer is designed to be a drop in replacement of https://github.com/iconara/ione `Ione::ByteBuffer`. It implements the same interface, extending it with support for additional numeric types and array operations.
|
6
|
+
|
7
|
+
It can be used for marshalling/demarshalling of binary frame PDUs. A prime example of such use case is code for handling cassandra native protocol which can be found in `Cassandra::Protocol::CqlNativeByteBuffer` from https://github.com/apptopia/ruby-driver/tree/perf-tweaks
|
8
|
+
|
9
|
+
## Benchmarks
|
10
|
+
|
11
|
+
As hinted by the description above, main motivation for creating this gem is pursuit of performance. `ByteBuffer::Buffer` is designed to be faster than or even with implementations based on ruby strings and `String#pack`. Here are results of benchmarks taken with ruby 1.9.3 on Mac OS X:
|
12
|
+
|
13
|
+
```
|
14
|
+
$ ./bin/benchmarks | grep slower
|
15
|
+
bm_allocation - Ione::ByteBuffer: 1317389.4 i/s - 1.14x slower
|
16
|
+
bm_churn - Ione::ByteBuffer space reuse, read/write interleaved: 6894.2 i/s - 3.32x slower
|
17
|
+
bm_append_big - ByteBuffer::Buffer big strings: 7546.2 i/s - 1.03x slower
|
18
|
+
bm_append_small - Ione::ByteBuffer small strings: 21482.4 i/s - 2.57x slower
|
19
|
+
bm_append_int - Cassandra::Protocol::CqlByteBuffer: 11363.9 i/s - 10.46x slower
|
20
|
+
bm_append_long - Cassandra::Protocol::CqlByteBuffer: 5554.0 i/s - 17.85x slower
|
21
|
+
bm_append_double - Cassandra::Protocol::CqlByteBuffer: 8569.1 i/s - 11.54x slower
|
22
|
+
bm_read - Cassandra::Protocol::CqlByteBuffer: 26853.6 i/s - 2.28x slower
|
23
|
+
bm_read_int - Cassandra::Protocol::CqlByteBuffer: 17687.1 i/s - 5.64x slower
|
24
|
+
bm_read_long - Cassandra::Protocol::CqlByteBuffer: 12658.3 i/s - 4.32x slower
|
25
|
+
bm_read_double - Cassandra::Protocol::CqlByteBuffer: 15554.9 i/s - 3.94x slower
|
26
|
+
```
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'byte_buffer'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install byte_buffer
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
TODO: Write usage instructions here
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, install dependencies by running:
|
51
|
+
|
52
|
+
```
|
53
|
+
$ bundle install
|
54
|
+
```
|
55
|
+
|
56
|
+
Native module can be compiled by running:
|
57
|
+
|
58
|
+
```
|
59
|
+
$ bundle exec rake compile
|
60
|
+
```
|
61
|
+
|
62
|
+
This should be enough to run tests successfully with:
|
63
|
+
|
64
|
+
```
|
65
|
+
$ bundle exec rspec
|
66
|
+
```
|
67
|
+
|
68
|
+
Also, there's a script which runs benchmarks against `Ione::ByteBuffer` and `Cassandra::Protocol::CqlNativeByteBuffer`, here's how to run it:
|
69
|
+
|
70
|
+
```
|
71
|
+
$ ./bin/benchmarks
|
72
|
+
```
|
data/Rakefile
ADDED
data/bin/benchmarks
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "byte_buffer"
|
5
|
+
require "benchmark/ips"
|
6
|
+
require "ione"
|
7
|
+
require "cassandra"
|
8
|
+
|
9
|
+
def bm_allocation(buffer_class)
|
10
|
+
buffer_class.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def bm_churn(buffer_class)
|
14
|
+
b = buffer_class.new
|
15
|
+
s = 'X' * 200
|
16
|
+
100.times {
|
17
|
+
b.append(s)
|
18
|
+
b.read(s.size)
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def bm_append_big(buffer_class)
|
23
|
+
buffer = buffer_class.new
|
24
|
+
100.times {
|
25
|
+
buffer.append("HELLO" * 100)
|
26
|
+
}
|
27
|
+
buffer.to_str
|
28
|
+
end
|
29
|
+
|
30
|
+
def bm_append_small(buffer_class)
|
31
|
+
buffer = buffer_class.new
|
32
|
+
100.times {
|
33
|
+
buffer.append("HELLO")
|
34
|
+
}
|
35
|
+
buffer.to_str
|
36
|
+
end
|
37
|
+
|
38
|
+
def bm_append_int(buffer_class)
|
39
|
+
buffer = buffer_class.new
|
40
|
+
100.times {
|
41
|
+
buffer.append_int(101)
|
42
|
+
}
|
43
|
+
buffer.to_str
|
44
|
+
end
|
45
|
+
|
46
|
+
def bm_append_long(buffer_class)
|
47
|
+
buffer = buffer_class.new
|
48
|
+
100.times {
|
49
|
+
buffer.append_long(101)
|
50
|
+
}
|
51
|
+
buffer.to_str
|
52
|
+
end
|
53
|
+
|
54
|
+
def bm_append_double(buffer_class)
|
55
|
+
buffer = buffer_class.new
|
56
|
+
100.times {
|
57
|
+
buffer.append_double(101.101)
|
58
|
+
}
|
59
|
+
buffer.to_str
|
60
|
+
end
|
61
|
+
|
62
|
+
def bm_read(buffer_class)
|
63
|
+
buffer = buffer_class.new("ABCD" * 100)
|
64
|
+
100.times {buffer.read(4)}
|
65
|
+
end
|
66
|
+
|
67
|
+
def bm_read_int(buffer_class)
|
68
|
+
buffer = buffer_class.new("ABCD" * 100)
|
69
|
+
100.times {buffer.read_int}
|
70
|
+
end
|
71
|
+
|
72
|
+
def bm_read_long(buffer_class)
|
73
|
+
buffer = buffer_class.new("ABCDEFGH" * 100)
|
74
|
+
100.times {buffer.read_long}
|
75
|
+
end
|
76
|
+
|
77
|
+
def bm_read_double(buffer_class)
|
78
|
+
buffer = buffer_class.new("ABCDEFGH" * 100)
|
79
|
+
100.times {buffer.read_double}
|
80
|
+
end
|
81
|
+
|
82
|
+
def run_benchmark(method, other_klass = Cassandra::Protocol::CqlByteBuffer, name_suffix = nil)
|
83
|
+
name_a = ["#{method} - ByteBuffer::Buffer", name_suffix].compact.join(' ')
|
84
|
+
name_b = ["#{method} - #{other_klass}", name_suffix].compact.join(' ')
|
85
|
+
|
86
|
+
Benchmark.ips do |x|
|
87
|
+
x.report(name_a) {send(method, ByteBuffer::Buffer)}
|
88
|
+
x.report(name_b) {send(method, other_klass)}
|
89
|
+
x.compare!
|
90
|
+
end
|
91
|
+
|
92
|
+
puts "\n\n"
|
93
|
+
end
|
94
|
+
|
95
|
+
run_benchmark(:bm_allocation, Ione::ByteBuffer)
|
96
|
+
run_benchmark(:bm_churn, Ione::ByteBuffer, 'space reuse, read/write interleaved')
|
97
|
+
run_benchmark(:bm_append_big, Ione::ByteBuffer, 'big strings')
|
98
|
+
run_benchmark(:bm_append_small, Ione::ByteBuffer, 'small strings')
|
99
|
+
run_benchmark(:bm_append_int)
|
100
|
+
run_benchmark(:bm_append_long)
|
101
|
+
run_benchmark(:bm_append_double)
|
102
|
+
run_benchmark(:bm_read)
|
103
|
+
run_benchmark(:bm_read_int)
|
104
|
+
run_benchmark(:bm_read_long)
|
105
|
+
run_benchmark(:bm_read_double)
|
data/bin/console
ADDED
data/byte_buffer.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
require "#{lib}/byte_buffer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "byte_buffer"
|
7
|
+
spec.version = ByteBuffer::VERSION
|
8
|
+
spec.authors = ["Serge Balyuk"]
|
9
|
+
spec.email = ["sergeb@apptopia.com", "bgipsy@gmail.com"]
|
10
|
+
spec.homepage = "http://github.com/apptopia/byte_buffer"
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{
|
14
|
+
Fast native implementation of byte buffer intended to be used as part
|
15
|
+
of frame based protocol PDU handling code. Can serve as drop in replacement for Ione::ByteBuffer
|
16
|
+
and used in DataStax Cassandra Ruby driver.
|
17
|
+
}
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
20
|
+
# delete this section to allow pushing this gem to any host.
|
21
|
+
# if spec.respond_to?(:metadata)
|
22
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
23
|
+
# else
|
24
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
spec.extensions = ["ext/byte_buffer_ext/extconf.rb"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "pry", "~> 0.10.1"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
37
|
+
spec.add_development_dependency "rake-compiler", "~> 0.9.5"
|
38
|
+
spec.add_development_dependency "benchmark-ips", "~> 2.2.0"
|
39
|
+
spec.add_development_dependency "ione", "~> 1.2.0"
|
40
|
+
spec.add_development_dependency "cassandra-driver", "~> 2.1.3"
|
41
|
+
end
|
@@ -0,0 +1,671 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2015 Apptopia Inc.
|
3
|
+
* You may redistribute this under the terms of the MIT license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "ruby.h"
|
8
|
+
#include <string.h>
|
9
|
+
#include <inttypes.h>
|
10
|
+
#include "portable_endian.h"
|
11
|
+
|
12
|
+
#define BYTE_BUFFER_EMBEDDED_SIZE 512
|
13
|
+
|
14
|
+
static VALUE rb_byte_buffer_allocate(VALUE klass);
|
15
|
+
static VALUE rb_byte_buffer_initialize(int argc, VALUE *argv, VALUE self);
|
16
|
+
static VALUE rb_byte_buffer_capacity(VALUE self);
|
17
|
+
static VALUE rb_byte_buffer_length(VALUE self);
|
18
|
+
static VALUE rb_byte_buffer_append(VALUE self, VALUE str);
|
19
|
+
static VALUE rb_byte_buffer_append_long(VALUE self, VALUE i);
|
20
|
+
static VALUE rb_byte_buffer_append_int(VALUE self, VALUE i);
|
21
|
+
static VALUE rb_byte_buffer_append_byte(VALUE self, VALUE i);
|
22
|
+
static VALUE rb_byte_buffer_append_short(VALUE self, VALUE i);
|
23
|
+
static VALUE rb_byte_buffer_append_double(VALUE self, VALUE i);
|
24
|
+
static VALUE rb_byte_buffer_append_float(VALUE self, VALUE i);
|
25
|
+
static VALUE rb_byte_buffer_append_byte_array(VALUE self, VALUE ary);
|
26
|
+
static VALUE rb_byte_buffer_discard(VALUE self, VALUE n);
|
27
|
+
static VALUE rb_byte_buffer_read(VALUE self, VALUE n);
|
28
|
+
static VALUE rb_byte_buffer_read_long(int argc, VALUE *argv, VALUE self);
|
29
|
+
static VALUE rb_byte_buffer_read_int(int argc, VALUE *argv, VALUE self);
|
30
|
+
static VALUE rb_byte_buffer_read_short(int argc, VALUE *argv, VALUE self);
|
31
|
+
static VALUE rb_byte_buffer_read_byte(int argc, VALUE *argv, VALUE self);
|
32
|
+
static VALUE rb_byte_buffer_read_double(VALUE self);
|
33
|
+
static VALUE rb_byte_buffer_read_float(VALUE self);
|
34
|
+
static VALUE rb_byte_buffer_read_byte_array(int argc, VALUE *argv, VALUE self);
|
35
|
+
static VALUE rb_byte_buffer_index(int argc, VALUE *argv, VALUE self);
|
36
|
+
static VALUE rb_byte_buffer_update(VALUE self, VALUE location, VALUE bytes);
|
37
|
+
static VALUE rb_byte_buffer_to_str(VALUE self);
|
38
|
+
static VALUE rb_byte_buffer_inspect(VALUE self);
|
39
|
+
|
40
|
+
static void byte_buffer_free(void *ptr);
|
41
|
+
static size_t byte_buffer_memsize(const void *ptr);
|
42
|
+
|
43
|
+
static const rb_data_type_t buffer_data_type = {
|
44
|
+
"byte_buffer/buffer",
|
45
|
+
{NULL, byte_buffer_free, byte_buffer_memsize}
|
46
|
+
};
|
47
|
+
|
48
|
+
typedef struct {
|
49
|
+
size_t size;
|
50
|
+
size_t write_pos;
|
51
|
+
size_t read_pos;
|
52
|
+
char embedded_buffer[BYTE_BUFFER_EMBEDDED_SIZE];
|
53
|
+
char *b_ptr;
|
54
|
+
} buffer_t;
|
55
|
+
|
56
|
+
#define READ_PTR(buffer_ptr) \
|
57
|
+
(buffer_ptr->b_ptr + buffer_ptr->read_pos)
|
58
|
+
|
59
|
+
#define READ_SIZE(buffer_ptr) \
|
60
|
+
(buffer_ptr->write_pos - buffer_ptr->read_pos)
|
61
|
+
|
62
|
+
#define WRITE_PTR(buffer_ptr) \
|
63
|
+
(buffer_ptr->b_ptr + buffer_ptr->write_pos)
|
64
|
+
|
65
|
+
#define ENSURE_WRITE_CAPACITY(buffer_ptr,len) \
|
66
|
+
{ if (buffer_ptr->write_pos + len > buffer_ptr->size) grow_buffer(buffer_ptr, len); }
|
67
|
+
|
68
|
+
#define ENSURE_READ_CAPACITY(buffer_ptr,len) \
|
69
|
+
{ if (buffer_ptr->read_pos + len > buffer_ptr->write_pos) \
|
70
|
+
rb_raise(rb_eRangeError, "%zu bytes requred, but only %zu available", (size_t)len, READ_SIZE(buffer_ptr)); }
|
71
|
+
|
72
|
+
static int32_t value_to_int32(VALUE x);
|
73
|
+
static int64_t value_to_int64(VALUE x);
|
74
|
+
static double value_to_dbl(VALUE x);
|
75
|
+
static void grow_buffer(buffer_t* buffer_ptr, size_t len);
|
76
|
+
|
77
|
+
static VALUE rb_cBuffer = 0;
|
78
|
+
|
79
|
+
void
|
80
|
+
Init_byte_buffer_ext()
|
81
|
+
{
|
82
|
+
VALUE rb_mByteBuffer;
|
83
|
+
|
84
|
+
rb_mByteBuffer = rb_define_module("ByteBuffer");
|
85
|
+
rb_cBuffer = rb_define_class_under(rb_mByteBuffer, "Buffer", rb_cObject);
|
86
|
+
|
87
|
+
rb_define_alloc_func(rb_cBuffer, rb_byte_buffer_allocate);
|
88
|
+
rb_define_const(rb_cBuffer, "DEFAULT_PREALLOC_SIZE", INT2FIX(BYTE_BUFFER_EMBEDDED_SIZE));
|
89
|
+
rb_define_method(rb_cBuffer, "initialize", rb_byte_buffer_initialize, -1);
|
90
|
+
rb_define_method(rb_cBuffer, "capacity", rb_byte_buffer_capacity, 0);
|
91
|
+
rb_define_method(rb_cBuffer, "length", rb_byte_buffer_length, 0);
|
92
|
+
rb_define_method(rb_cBuffer, "append", rb_byte_buffer_append, 1);
|
93
|
+
rb_define_method(rb_cBuffer, "append_long", rb_byte_buffer_append_long, 1);
|
94
|
+
rb_define_method(rb_cBuffer, "append_int", rb_byte_buffer_append_int, 1);
|
95
|
+
rb_define_method(rb_cBuffer, "append_byte", rb_byte_buffer_append_byte, 1);
|
96
|
+
rb_define_method(rb_cBuffer, "append_short", rb_byte_buffer_append_short, 1);
|
97
|
+
rb_define_method(rb_cBuffer, "append_double", rb_byte_buffer_append_double, 1);
|
98
|
+
rb_define_method(rb_cBuffer, "append_float", rb_byte_buffer_append_float, 1);
|
99
|
+
rb_define_method(rb_cBuffer, "append_byte_array", rb_byte_buffer_append_byte_array, 1);
|
100
|
+
rb_define_method(rb_cBuffer, "discard", rb_byte_buffer_discard, 1);
|
101
|
+
rb_define_method(rb_cBuffer, "read", rb_byte_buffer_read, 1);
|
102
|
+
rb_define_method(rb_cBuffer, "read_long", rb_byte_buffer_read_long, -1);
|
103
|
+
rb_define_method(rb_cBuffer, "read_int", rb_byte_buffer_read_int, -1);
|
104
|
+
rb_define_method(rb_cBuffer, "read_short", rb_byte_buffer_read_short, -1);
|
105
|
+
rb_define_method(rb_cBuffer, "read_byte", rb_byte_buffer_read_byte, -1);
|
106
|
+
rb_define_method(rb_cBuffer, "read_double", rb_byte_buffer_read_double, 0);
|
107
|
+
rb_define_method(rb_cBuffer, "read_float", rb_byte_buffer_read_float, 0);
|
108
|
+
rb_define_method(rb_cBuffer, "read_byte_array", rb_byte_buffer_read_byte_array, -1);
|
109
|
+
rb_define_method(rb_cBuffer, "index", rb_byte_buffer_index, -1);
|
110
|
+
rb_define_method(rb_cBuffer, "update", rb_byte_buffer_update, 2);
|
111
|
+
rb_define_method(rb_cBuffer, "to_str", rb_byte_buffer_to_str, 0);
|
112
|
+
rb_define_method(rb_cBuffer, "inspect", rb_byte_buffer_inspect, 0);
|
113
|
+
}
|
114
|
+
|
115
|
+
VALUE
|
116
|
+
rb_byte_buffer_allocate(VALUE klass)
|
117
|
+
{
|
118
|
+
buffer_t *b;
|
119
|
+
VALUE obj = TypedData_Make_Struct(klass, buffer_t, &buffer_data_type, b);
|
120
|
+
b->b_ptr = b->embedded_buffer;
|
121
|
+
b->size = BYTE_BUFFER_EMBEDDED_SIZE;
|
122
|
+
|
123
|
+
return obj;
|
124
|
+
}
|
125
|
+
|
126
|
+
VALUE
|
127
|
+
rb_byte_buffer_initialize(int argc, VALUE *argv, VALUE self)
|
128
|
+
{
|
129
|
+
VALUE str, prealloc_size;
|
130
|
+
|
131
|
+
rb_scan_args(argc, argv, "02", &str, &prealloc_size);
|
132
|
+
|
133
|
+
if (!NIL_P(prealloc_size)) {
|
134
|
+
buffer_t *b;
|
135
|
+
long len;
|
136
|
+
|
137
|
+
Check_Type(prealloc_size, T_FIXNUM);
|
138
|
+
len = FIX2LONG(prealloc_size);
|
139
|
+
if (len < 0) rb_raise(rb_eRangeError, "prealloc size can't be negative");
|
140
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
141
|
+
|
142
|
+
if ((size_t)len > b->size) {
|
143
|
+
if (b->b_ptr != b->embedded_buffer) xfree(b->b_ptr);
|
144
|
+
b->b_ptr = ALLOC_N(char, len);
|
145
|
+
b->size = len;
|
146
|
+
b->read_pos = b->write_pos = 0;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
if (!NIL_P(str))
|
151
|
+
rb_byte_buffer_append(self, str);
|
152
|
+
|
153
|
+
return self;
|
154
|
+
}
|
155
|
+
|
156
|
+
VALUE
|
157
|
+
rb_byte_buffer_capacity(VALUE self)
|
158
|
+
{
|
159
|
+
buffer_t *b;
|
160
|
+
|
161
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
162
|
+
|
163
|
+
return UINT2NUM(b->size);
|
164
|
+
}
|
165
|
+
|
166
|
+
VALUE
|
167
|
+
rb_byte_buffer_length(VALUE self)
|
168
|
+
{
|
169
|
+
buffer_t *b;
|
170
|
+
|
171
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
172
|
+
|
173
|
+
return UINT2NUM(READ_SIZE(b));
|
174
|
+
}
|
175
|
+
|
176
|
+
VALUE
|
177
|
+
rb_byte_buffer_append(VALUE self, VALUE str)
|
178
|
+
{
|
179
|
+
char *c_str;
|
180
|
+
size_t len;
|
181
|
+
buffer_t *b;
|
182
|
+
|
183
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
184
|
+
|
185
|
+
if (CLASS_OF(str) == rb_cString) {
|
186
|
+
c_str = RSTRING_PTR(str);
|
187
|
+
len = RSTRING_LEN(str);
|
188
|
+
} else if (rb_cBuffer && rb_obj_is_kind_of(str, rb_cBuffer)) {
|
189
|
+
buffer_t *other_b;
|
190
|
+
TypedData_Get_Struct(str, buffer_t, &buffer_data_type, other_b);
|
191
|
+
c_str = READ_PTR(other_b);
|
192
|
+
len = READ_SIZE(other_b);
|
193
|
+
} else {
|
194
|
+
VALUE s = rb_funcall(str, rb_intern("to_s"), 0, 0);
|
195
|
+
c_str = RSTRING_PTR(s);
|
196
|
+
len = RSTRING_LEN(s);
|
197
|
+
}
|
198
|
+
|
199
|
+
ENSURE_WRITE_CAPACITY(b, len);
|
200
|
+
memcpy(WRITE_PTR(b), c_str, len);
|
201
|
+
b->write_pos += len;
|
202
|
+
|
203
|
+
return self;
|
204
|
+
}
|
205
|
+
|
206
|
+
VALUE
|
207
|
+
rb_byte_buffer_append_long(VALUE self, VALUE i)
|
208
|
+
{
|
209
|
+
buffer_t *b;
|
210
|
+
int64_t i64 = value_to_int64(i);
|
211
|
+
|
212
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
213
|
+
ENSURE_WRITE_CAPACITY(b, 8);
|
214
|
+
i64 = htobe64(i64);
|
215
|
+
*((int64_t*)WRITE_PTR(b)) = i64;
|
216
|
+
b->write_pos += 8;
|
217
|
+
|
218
|
+
return self;
|
219
|
+
}
|
220
|
+
|
221
|
+
VALUE
|
222
|
+
rb_byte_buffer_append_int(VALUE self, VALUE i)
|
223
|
+
{
|
224
|
+
buffer_t *b;
|
225
|
+
int32_t i32 = value_to_int32(i);
|
226
|
+
|
227
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
228
|
+
ENSURE_WRITE_CAPACITY(b, 4);
|
229
|
+
i32 = htobe32(i32);
|
230
|
+
*((int32_t*)WRITE_PTR(b)) = i32;
|
231
|
+
b->write_pos += 4;
|
232
|
+
|
233
|
+
return self;
|
234
|
+
}
|
235
|
+
|
236
|
+
VALUE
|
237
|
+
rb_byte_buffer_append_byte(VALUE self, VALUE i)
|
238
|
+
{
|
239
|
+
buffer_t *b;
|
240
|
+
int32_t i32 = value_to_int32(i);
|
241
|
+
int8_t i8 = (int8_t)i32;
|
242
|
+
|
243
|
+
if (i32 > 0xFF || -i32 > 0x80)
|
244
|
+
rb_raise(rb_eRangeError, "Number %d doesn't fit into byte", i32);
|
245
|
+
|
246
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
247
|
+
ENSURE_WRITE_CAPACITY(b, 1);
|
248
|
+
*((int8_t*)WRITE_PTR(b)) = i8;
|
249
|
+
b->write_pos += 1;
|
250
|
+
|
251
|
+
return self;
|
252
|
+
}
|
253
|
+
|
254
|
+
VALUE
|
255
|
+
rb_byte_buffer_append_short(VALUE self, VALUE i)
|
256
|
+
{
|
257
|
+
buffer_t *b;
|
258
|
+
int32_t i32 = value_to_int32(i);
|
259
|
+
int16_t i16 = (int16_t)i32;
|
260
|
+
|
261
|
+
if (i32 > 0xFFFF || -i32 > 0x8000)
|
262
|
+
rb_raise(rb_eRangeError, "Number %d doesn't fit into 2 bytes", i32);
|
263
|
+
|
264
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
265
|
+
ENSURE_WRITE_CAPACITY(b, 2);
|
266
|
+
i16 = htobe16(i16);
|
267
|
+
*((int16_t*)WRITE_PTR(b)) = i16;
|
268
|
+
b->write_pos += 2;
|
269
|
+
|
270
|
+
return self;
|
271
|
+
}
|
272
|
+
|
273
|
+
VALUE
|
274
|
+
rb_byte_buffer_append_double(VALUE self, VALUE i)
|
275
|
+
{
|
276
|
+
buffer_t *b;
|
277
|
+
union {double d; uint64_t i64;} ucast;
|
278
|
+
|
279
|
+
ucast.d = value_to_dbl(i);
|
280
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
281
|
+
ENSURE_WRITE_CAPACITY(b, 8);
|
282
|
+
ucast.i64 = htobe64(ucast.i64);
|
283
|
+
*(int64_t*)WRITE_PTR(b) = ucast.i64;
|
284
|
+
b->write_pos += 8;
|
285
|
+
|
286
|
+
return self;
|
287
|
+
}
|
288
|
+
|
289
|
+
VALUE
|
290
|
+
rb_byte_buffer_append_float(VALUE self, VALUE i)
|
291
|
+
{
|
292
|
+
buffer_t *b;
|
293
|
+
union {float f; uint32_t i32;} ucast;
|
294
|
+
|
295
|
+
ucast.f = (float)value_to_dbl(i);
|
296
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
297
|
+
ENSURE_WRITE_CAPACITY(b, 4);
|
298
|
+
ucast.i32 = htobe32(ucast.i32);
|
299
|
+
*(int32_t*)WRITE_PTR(b) = ucast.i32;
|
300
|
+
b->write_pos += 4;
|
301
|
+
|
302
|
+
return self;
|
303
|
+
}
|
304
|
+
|
305
|
+
VALUE
|
306
|
+
rb_byte_buffer_append_byte_array(VALUE self, VALUE maybe_ary)
|
307
|
+
{
|
308
|
+
VALUE ary = rb_check_array_type(maybe_ary);
|
309
|
+
VALUE *ary_ptr;
|
310
|
+
long len, i;
|
311
|
+
buffer_t *b;
|
312
|
+
|
313
|
+
if (NIL_P(ary))
|
314
|
+
rb_raise(rb_eTypeError, "expected Array, got %s", rb_obj_classname(maybe_ary));
|
315
|
+
|
316
|
+
len = RARRAY_LEN(ary);
|
317
|
+
ary_ptr = RARRAY_PTR(ary);
|
318
|
+
|
319
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
320
|
+
ENSURE_WRITE_CAPACITY(b, len);
|
321
|
+
for (i = 0; i < RARRAY_LEN(ary); ++i) {
|
322
|
+
VALUE n = ary_ptr[i];
|
323
|
+
int32_t i32 = value_to_int32(n);
|
324
|
+
int8_t i8 = (int8_t)i32;
|
325
|
+
|
326
|
+
if (i32 > 0xFF || -i32 > 0x80)
|
327
|
+
rb_raise(rb_eRangeError, "Number %d doesn't fit into byte", i32);
|
328
|
+
|
329
|
+
((int8_t*)WRITE_PTR(b))[i] = i8;
|
330
|
+
}
|
331
|
+
|
332
|
+
b->write_pos += len;
|
333
|
+
|
334
|
+
return self;
|
335
|
+
}
|
336
|
+
|
337
|
+
VALUE
|
338
|
+
rb_byte_buffer_discard(VALUE self, VALUE n)
|
339
|
+
{
|
340
|
+
buffer_t *b;
|
341
|
+
long len;
|
342
|
+
|
343
|
+
Check_Type(n, T_FIXNUM);
|
344
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
345
|
+
len = FIX2LONG(n);
|
346
|
+
if (len < 0) rb_raise(rb_eRangeError, "Cannot discard a negative number of bytes");
|
347
|
+
ENSURE_READ_CAPACITY(b, len);
|
348
|
+
b->read_pos += len;
|
349
|
+
|
350
|
+
return self;
|
351
|
+
}
|
352
|
+
|
353
|
+
VALUE
|
354
|
+
rb_byte_buffer_read(VALUE self, VALUE n)
|
355
|
+
{
|
356
|
+
buffer_t *b;
|
357
|
+
long len;
|
358
|
+
VALUE str;
|
359
|
+
|
360
|
+
Check_Type(n, T_FIXNUM);
|
361
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
362
|
+
len = FIX2LONG(n);
|
363
|
+
if (len < 0) rb_raise(rb_eRangeError, "Cannot read a negative number of bytes");
|
364
|
+
ENSURE_READ_CAPACITY(b, len);
|
365
|
+
str = rb_str_new(READ_PTR(b), len);
|
366
|
+
b->read_pos += len;
|
367
|
+
|
368
|
+
return str;
|
369
|
+
}
|
370
|
+
|
371
|
+
VALUE
|
372
|
+
rb_byte_buffer_read_long(int argc, VALUE *argv, VALUE self)
|
373
|
+
{
|
374
|
+
VALUE f_signed;
|
375
|
+
buffer_t *b;
|
376
|
+
uint64_t i64;
|
377
|
+
|
378
|
+
rb_scan_args(argc, argv, "01", &f_signed);
|
379
|
+
|
380
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
381
|
+
ENSURE_READ_CAPACITY(b, 8);
|
382
|
+
i64 = be64toh(*((uint64_t*)READ_PTR(b)));
|
383
|
+
b->read_pos += 8;
|
384
|
+
|
385
|
+
if (RTEST(f_signed))
|
386
|
+
return LONG2NUM((int64_t)i64);
|
387
|
+
else
|
388
|
+
return ULONG2NUM(i64);
|
389
|
+
}
|
390
|
+
|
391
|
+
VALUE
|
392
|
+
rb_byte_buffer_read_int(int argc, VALUE *argv, VALUE self)
|
393
|
+
{
|
394
|
+
VALUE f_signed;
|
395
|
+
buffer_t *b;
|
396
|
+
uint32_t i32;
|
397
|
+
|
398
|
+
rb_scan_args(argc, argv, "01", &f_signed);
|
399
|
+
|
400
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
401
|
+
ENSURE_READ_CAPACITY(b, 4);
|
402
|
+
i32 = be32toh(*((uint32_t*)READ_PTR(b)));
|
403
|
+
b->read_pos += 4;
|
404
|
+
|
405
|
+
if (RTEST(f_signed))
|
406
|
+
return INT2NUM((int32_t)i32);
|
407
|
+
else
|
408
|
+
return UINT2NUM(i32);
|
409
|
+
}
|
410
|
+
|
411
|
+
VALUE
|
412
|
+
rb_byte_buffer_read_short(int argc, VALUE *argv, VALUE self)
|
413
|
+
{
|
414
|
+
VALUE f_signed;
|
415
|
+
buffer_t *b;
|
416
|
+
uint16_t i16;
|
417
|
+
|
418
|
+
rb_scan_args(argc, argv, "01", &f_signed);
|
419
|
+
|
420
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
421
|
+
ENSURE_READ_CAPACITY(b, 2);
|
422
|
+
i16 = be16toh(*((uint16_t*)READ_PTR(b)));
|
423
|
+
b->read_pos += 2;
|
424
|
+
|
425
|
+
if (RTEST(f_signed))
|
426
|
+
return INT2NUM((int16_t)i16);
|
427
|
+
else
|
428
|
+
return UINT2NUM(i16);
|
429
|
+
}
|
430
|
+
|
431
|
+
VALUE
|
432
|
+
rb_byte_buffer_read_byte(int argc, VALUE *argv, VALUE self)
|
433
|
+
{
|
434
|
+
VALUE f_signed;
|
435
|
+
buffer_t *b;
|
436
|
+
uint8_t i8;
|
437
|
+
|
438
|
+
rb_scan_args(argc, argv, "01", &f_signed);
|
439
|
+
|
440
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
441
|
+
ENSURE_READ_CAPACITY(b, 1);
|
442
|
+
i8 = *((uint8_t*)READ_PTR(b));
|
443
|
+
b->read_pos += 1;
|
444
|
+
|
445
|
+
if (RTEST(f_signed))
|
446
|
+
return INT2NUM((int8_t)i8);
|
447
|
+
else
|
448
|
+
return UINT2NUM(i8);
|
449
|
+
}
|
450
|
+
|
451
|
+
VALUE
|
452
|
+
rb_byte_buffer_read_double(VALUE self)
|
453
|
+
{
|
454
|
+
buffer_t *b;
|
455
|
+
union {uint64_t i64; double d;} ucast;
|
456
|
+
|
457
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
458
|
+
ENSURE_READ_CAPACITY(b, 8);
|
459
|
+
ucast.i64 = be64toh(*(uint64_t*)READ_PTR(b));
|
460
|
+
b->read_pos += 8;
|
461
|
+
|
462
|
+
return DBL2NUM(ucast.d);
|
463
|
+
}
|
464
|
+
|
465
|
+
VALUE
|
466
|
+
rb_byte_buffer_read_float(VALUE self)
|
467
|
+
{
|
468
|
+
buffer_t *b;
|
469
|
+
union {float d; uint32_t i32;} ucast;
|
470
|
+
|
471
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
472
|
+
ENSURE_READ_CAPACITY(b, 4);
|
473
|
+
ucast.i32 = be32toh(*(uint32_t*)READ_PTR(b));
|
474
|
+
b->read_pos += 4;
|
475
|
+
|
476
|
+
return DBL2NUM((double)ucast.d);
|
477
|
+
}
|
478
|
+
|
479
|
+
VALUE
|
480
|
+
rb_byte_buffer_read_byte_array(int argc, VALUE *argv, VALUE self)
|
481
|
+
{
|
482
|
+
buffer_t *b;
|
483
|
+
VALUE n;
|
484
|
+
VALUE f_signed;
|
485
|
+
VALUE ary;
|
486
|
+
long len, i;
|
487
|
+
int b_signed;
|
488
|
+
|
489
|
+
rb_scan_args(argc, argv, "11", &n, &f_signed);
|
490
|
+
|
491
|
+
Check_Type(n, T_FIXNUM);
|
492
|
+
len = FIX2LONG(n);
|
493
|
+
if (len < 0) rb_raise(rb_eRangeError, "Cannot read a negative number of bytes");
|
494
|
+
b_signed = RTEST(f_signed);
|
495
|
+
|
496
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
497
|
+
ENSURE_READ_CAPACITY(b, len);
|
498
|
+
|
499
|
+
ary = rb_ary_new();
|
500
|
+
for (i=0; i<len; ++i) {
|
501
|
+
uint8_t i8 = *((uint8_t*)READ_PTR(b));
|
502
|
+
b->read_pos += 1;
|
503
|
+
|
504
|
+
if (b_signed)
|
505
|
+
rb_ary_push(ary, INT2NUM((int8_t)i8));
|
506
|
+
else
|
507
|
+
rb_ary_push(ary, UINT2NUM(i8));
|
508
|
+
}
|
509
|
+
|
510
|
+
return ary;
|
511
|
+
}
|
512
|
+
|
513
|
+
VALUE
|
514
|
+
rb_byte_buffer_index(int argc, VALUE *argv, VALUE self)
|
515
|
+
{
|
516
|
+
VALUE substr;
|
517
|
+
VALUE voffset;
|
518
|
+
size_t offset;
|
519
|
+
buffer_t *b;
|
520
|
+
|
521
|
+
rb_scan_args(argc, argv, "11", &substr, &voffset);
|
522
|
+
Check_Type(substr, T_STRING);
|
523
|
+
if (!NIL_P(voffset)) {
|
524
|
+
long l = NUM2LONG(voffset);
|
525
|
+
if (l < 0) rb_raise(rb_eRangeError, "offset can't be negative");
|
526
|
+
offset = l;
|
527
|
+
} else
|
528
|
+
offset = 0;
|
529
|
+
|
530
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
531
|
+
if (offset >= READ_SIZE(b) || offset + RSTRING_LEN(substr) > READ_SIZE(b))
|
532
|
+
return Qnil;
|
533
|
+
else {
|
534
|
+
char* pos = memmem(READ_PTR(b) + offset, READ_SIZE(b), RSTRING_PTR(substr), RSTRING_LEN(substr));
|
535
|
+
if (pos)
|
536
|
+
return UINT2NUM(pos - READ_PTR(b));
|
537
|
+
else
|
538
|
+
return Qnil;
|
539
|
+
}
|
540
|
+
}
|
541
|
+
|
542
|
+
VALUE
|
543
|
+
rb_byte_buffer_update(VALUE self, VALUE location, VALUE bytes)
|
544
|
+
{
|
545
|
+
long offset;
|
546
|
+
long copy_bytes_len;
|
547
|
+
buffer_t *b;
|
548
|
+
|
549
|
+
Check_Type(location, T_FIXNUM);
|
550
|
+
Check_Type(bytes, T_STRING);
|
551
|
+
offset = NUM2LONG(location);
|
552
|
+
if (offset < 0) rb_raise(rb_eRangeError, "location can't be negative");
|
553
|
+
|
554
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
555
|
+
if ((size_t)offset >= READ_SIZE(b))
|
556
|
+
return self;
|
557
|
+
|
558
|
+
copy_bytes_len = RSTRING_LEN(bytes);
|
559
|
+
if ((size_t)(offset + copy_bytes_len) > READ_SIZE(b))
|
560
|
+
copy_bytes_len = READ_SIZE(b) - offset;
|
561
|
+
memcpy(READ_PTR(b) + offset, RSTRING_PTR(bytes), copy_bytes_len);
|
562
|
+
|
563
|
+
return self;
|
564
|
+
}
|
565
|
+
|
566
|
+
VALUE
|
567
|
+
rb_byte_buffer_to_str(VALUE self)
|
568
|
+
{
|
569
|
+
buffer_t *b;
|
570
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
571
|
+
|
572
|
+
return rb_str_new(READ_PTR(b), READ_SIZE(b));
|
573
|
+
}
|
574
|
+
|
575
|
+
VALUE
|
576
|
+
rb_byte_buffer_inspect(VALUE self)
|
577
|
+
{
|
578
|
+
buffer_t *b;
|
579
|
+
VALUE str;
|
580
|
+
|
581
|
+
TypedData_Get_Struct(self, buffer_t, &buffer_data_type, b);
|
582
|
+
str = rb_sprintf("#<%s:%p read_pos:%zu write_pos:%zu len:%zu capacity:%zu>",
|
583
|
+
rb_obj_classname(self), (void*)self, b->read_pos, b->write_pos, READ_SIZE(b), b->size);
|
584
|
+
|
585
|
+
return str;
|
586
|
+
}
|
587
|
+
|
588
|
+
int32_t
|
589
|
+
value_to_int32(VALUE x)
|
590
|
+
{
|
591
|
+
if (FIXNUM_P(x)) {
|
592
|
+
int64_t i64 = FIX2LONG(x);
|
593
|
+
|
594
|
+
if (i64 > 0xFFFFFFFF || -i64 > 0x80000000)
|
595
|
+
rb_raise(rb_eRangeError, "Number %" PRId64 " is too big", i64);
|
596
|
+
|
597
|
+
return (int32_t)i64;
|
598
|
+
} else if (TYPE(x) == T_BIGNUM)
|
599
|
+
rb_raise(rb_eRangeError, "expected `interger', got `bignum'");
|
600
|
+
else
|
601
|
+
rb_raise(rb_eTypeError, "expected `interger', got %s ", rb_obj_classname(x));
|
602
|
+
|
603
|
+
return 0;
|
604
|
+
}
|
605
|
+
|
606
|
+
int64_t
|
607
|
+
value_to_int64(VALUE x)
|
608
|
+
{
|
609
|
+
if (FIXNUM_P(x))
|
610
|
+
return FIX2LONG(x);
|
611
|
+
else if (TYPE(x) == T_BIGNUM && RBIGNUM_SIGN(x))
|
612
|
+
return (int64_t)rb_big2ull(x);
|
613
|
+
else if (TYPE(x) == T_BIGNUM && !RBIGNUM_SIGN(x))
|
614
|
+
return rb_big2ll(x);
|
615
|
+
else
|
616
|
+
rb_raise(rb_eTypeError, "expected `interger' or `bignum', got %s ", rb_obj_classname(x));
|
617
|
+
|
618
|
+
return 0;
|
619
|
+
}
|
620
|
+
|
621
|
+
double
|
622
|
+
value_to_dbl(VALUE x)
|
623
|
+
{
|
624
|
+
if (FIXNUM_P(x))
|
625
|
+
return ((double)FIX2LONG(x));
|
626
|
+
else if (TYPE(x) == T_FLOAT)
|
627
|
+
return RFLOAT_VALUE(x);
|
628
|
+
else if (TYPE(x) == T_BIGNUM)
|
629
|
+
return rb_big2dbl(x);
|
630
|
+
else
|
631
|
+
rb_raise(rb_eTypeError, "expected a numeric type, got %s ", rb_obj_classname(x));
|
632
|
+
|
633
|
+
return 0.0;
|
634
|
+
}
|
635
|
+
|
636
|
+
void
|
637
|
+
grow_buffer(buffer_t* buffer_ptr, size_t len)
|
638
|
+
{
|
639
|
+
size_t new_size = buffer_ptr->write_pos - buffer_ptr->read_pos + len;
|
640
|
+
|
641
|
+
if (new_size <= buffer_ptr->size) {
|
642
|
+
memmove(buffer_ptr->b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
643
|
+
buffer_ptr->write_pos -= buffer_ptr->read_pos;
|
644
|
+
buffer_ptr->read_pos = 0;
|
645
|
+
} else {
|
646
|
+
char *new_b_ptr;
|
647
|
+
|
648
|
+
new_size += new_size / 2;
|
649
|
+
new_b_ptr = ALLOC_N(char, new_size);
|
650
|
+
memcpy(new_b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
651
|
+
if (buffer_ptr->b_ptr != buffer_ptr->embedded_buffer) xfree(buffer_ptr->b_ptr);
|
652
|
+
buffer_ptr->b_ptr = new_b_ptr;
|
653
|
+
buffer_ptr->size = new_size;
|
654
|
+
buffer_ptr->write_pos -= buffer_ptr->read_pos;
|
655
|
+
buffer_ptr->read_pos = 0;
|
656
|
+
}
|
657
|
+
}
|
658
|
+
|
659
|
+
void
|
660
|
+
byte_buffer_free(void *ptr)
|
661
|
+
{
|
662
|
+
buffer_t *b = ptr;
|
663
|
+
if (b->b_ptr != b->embedded_buffer) xfree(b->b_ptr);
|
664
|
+
xfree(b);
|
665
|
+
}
|
666
|
+
|
667
|
+
size_t
|
668
|
+
byte_buffer_memsize(const void *ptr)
|
669
|
+
{
|
670
|
+
return ptr ? sizeof(buffer_t) : 0;
|
671
|
+
}
|
@@ -0,0 +1,118 @@
|
|
1
|
+
// "License": Public Domain
|
2
|
+
// I, Mathias PanzenbГ¶ck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
|
3
|
+
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
|
4
|
+
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
|
5
|
+
// an example on how to get the endian conversion functions on different platforms.
|
6
|
+
|
7
|
+
#ifndef PORTABLE_ENDIAN_H__
|
8
|
+
#define PORTABLE_ENDIAN_H__
|
9
|
+
|
10
|
+
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
|
11
|
+
|
12
|
+
# define __WINDOWS__
|
13
|
+
|
14
|
+
#endif
|
15
|
+
|
16
|
+
#if defined(__linux__) || defined(__CYGWIN__)
|
17
|
+
|
18
|
+
# include <endian.h>
|
19
|
+
|
20
|
+
#elif defined(__APPLE__)
|
21
|
+
|
22
|
+
# include <libkern/OSByteOrder.h>
|
23
|
+
|
24
|
+
# define htobe16(x) OSSwapHostToBigInt16(x)
|
25
|
+
# define htole16(x) OSSwapHostToLittleInt16(x)
|
26
|
+
# define be16toh(x) OSSwapBigToHostInt16(x)
|
27
|
+
# define le16toh(x) OSSwapLittleToHostInt16(x)
|
28
|
+
|
29
|
+
# define htobe32(x) OSSwapHostToBigInt32(x)
|
30
|
+
# define htole32(x) OSSwapHostToLittleInt32(x)
|
31
|
+
# define be32toh(x) OSSwapBigToHostInt32(x)
|
32
|
+
# define le32toh(x) OSSwapLittleToHostInt32(x)
|
33
|
+
|
34
|
+
# define htobe64(x) OSSwapHostToBigInt64(x)
|
35
|
+
# define htole64(x) OSSwapHostToLittleInt64(x)
|
36
|
+
# define be64toh(x) OSSwapBigToHostInt64(x)
|
37
|
+
# define le64toh(x) OSSwapLittleToHostInt64(x)
|
38
|
+
|
39
|
+
# define __BYTE_ORDER BYTE_ORDER
|
40
|
+
# define __BIG_ENDIAN BIG_ENDIAN
|
41
|
+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
|
42
|
+
# define __PDP_ENDIAN PDP_ENDIAN
|
43
|
+
|
44
|
+
#elif defined(__OpenBSD__)
|
45
|
+
|
46
|
+
# include <sys/endian.h>
|
47
|
+
|
48
|
+
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
|
49
|
+
|
50
|
+
# include <sys/endian.h>
|
51
|
+
|
52
|
+
# define be16toh(x) betoh16(x)
|
53
|
+
# define le16toh(x) letoh16(x)
|
54
|
+
|
55
|
+
# define be32toh(x) betoh32(x)
|
56
|
+
# define le32toh(x) letoh32(x)
|
57
|
+
|
58
|
+
# define be64toh(x) betoh64(x)
|
59
|
+
# define le64toh(x) letoh64(x)
|
60
|
+
|
61
|
+
#elif defined(__WINDOWS__)
|
62
|
+
|
63
|
+
# include <winsock2.h>
|
64
|
+
# include <sys/param.h>
|
65
|
+
|
66
|
+
# if BYTE_ORDER == LITTLE_ENDIAN
|
67
|
+
|
68
|
+
# define htobe16(x) htons(x)
|
69
|
+
# define htole16(x) (x)
|
70
|
+
# define be16toh(x) ntohs(x)
|
71
|
+
# define le16toh(x) (x)
|
72
|
+
|
73
|
+
# define htobe32(x) htonl(x)
|
74
|
+
# define htole32(x) (x)
|
75
|
+
# define be32toh(x) ntohl(x)
|
76
|
+
# define le32toh(x) (x)
|
77
|
+
|
78
|
+
# define htobe64(x) htonll(x)
|
79
|
+
# define htole64(x) (x)
|
80
|
+
# define be64toh(x) ntohll(x)
|
81
|
+
# define le64toh(x) (x)
|
82
|
+
|
83
|
+
# elif BYTE_ORDER == BIG_ENDIAN
|
84
|
+
|
85
|
+
/* that would be xbox 360 */
|
86
|
+
# define htobe16(x) (x)
|
87
|
+
# define htole16(x) __builtin_bswap16(x)
|
88
|
+
# define be16toh(x) (x)
|
89
|
+
# define le16toh(x) __builtin_bswap16(x)
|
90
|
+
|
91
|
+
# define htobe32(x) (x)
|
92
|
+
# define htole32(x) __builtin_bswap32(x)
|
93
|
+
# define be32toh(x) (x)
|
94
|
+
# define le32toh(x) __builtin_bswap32(x)
|
95
|
+
|
96
|
+
# define htobe64(x) (x)
|
97
|
+
# define htole64(x) __builtin_bswap64(x)
|
98
|
+
# define be64toh(x) (x)
|
99
|
+
# define le64toh(x) __builtin_bswap64(x)
|
100
|
+
|
101
|
+
# else
|
102
|
+
|
103
|
+
# error byte order not supported
|
104
|
+
|
105
|
+
# endif
|
106
|
+
|
107
|
+
# define __BYTE_ORDER BYTE_ORDER
|
108
|
+
# define __BIG_ENDIAN BIG_ENDIAN
|
109
|
+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
|
110
|
+
# define __PDP_ENDIAN PDP_ENDIAN
|
111
|
+
|
112
|
+
#else
|
113
|
+
|
114
|
+
# error platform not supported
|
115
|
+
|
116
|
+
#endif
|
117
|
+
|
118
|
+
#endif
|
data/lib/byte_buffer.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module ByteBuffer
|
2
|
+
class Buffer
|
3
|
+
alias_method :size, :length
|
4
|
+
alias_method :bytesize, :length
|
5
|
+
alias_method :<<, :append
|
6
|
+
alias_method :cheap_peek, :to_str
|
7
|
+
alias_method :to_s, :to_str
|
8
|
+
|
9
|
+
def empty?
|
10
|
+
self.length == 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def eql?(other)
|
14
|
+
self.to_str.eql?(other.to_str)
|
15
|
+
end
|
16
|
+
alias_method :==, :eql?
|
17
|
+
|
18
|
+
def hash
|
19
|
+
to_str.hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def dup
|
23
|
+
self.class.new(self.to_str)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: byte_buffer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Serge Balyuk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.10.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.10.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.2.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake-compiler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.9.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.5
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: benchmark-ips
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.2.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.2.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: ione
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.2.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.2.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: cassandra-driver
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.1.3
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ~>
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 2.1.3
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- sergeb@apptopia.com
|
128
|
+
- bgipsy@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions:
|
131
|
+
- ext/byte_buffer_ext/extconf.rb
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- .gitignore
|
135
|
+
- .rspec
|
136
|
+
- .travis.yml
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/benchmarks
|
142
|
+
- bin/console
|
143
|
+
- byte_buffer.gemspec
|
144
|
+
- ext/byte_buffer_ext/byte_buffer.c
|
145
|
+
- ext/byte_buffer_ext/extconf.rb
|
146
|
+
- ext/byte_buffer_ext/portable_endian.h
|
147
|
+
- lib/byte_buffer.rb
|
148
|
+
- lib/byte_buffer/buffer.rb
|
149
|
+
- lib/byte_buffer/version.rb
|
150
|
+
homepage: http://github.com/apptopia/byte_buffer
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ! '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ! '>='
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.4.7
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Fast native implementation of byte buffer intended to be used as part of
|
174
|
+
frame based protocol PDU handling code. Can serve as drop in replacement for Ione::ByteBuffer
|
175
|
+
and used in DataStax Cassandra Ruby driver.
|
176
|
+
test_files: []
|