data-buffer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98569d8254905d930614b2f4f46ebc1292b998539c2f8185e2a1d86bca5c4f61
4
+ data.tar.gz: 4305e5842428a9d346248fcadd49d962ccd13f579ff15ac74e888031d303b6d0
5
+ SHA512:
6
+ metadata.gz: 286b60992a8264c16fa5258cb461b1dc98407c0bdd63288a2927ff565590a37a3b1607e357b3487784c2beba77835d10fc04fd97d8bc35ae2ea00003d79e8f9f
7
+ data.tar.gz: 9491986ca17d2762c148433e1f9accfedd22ce1bc50ba747e5441e5f419991de116a9b9dd5b62725e4d3ea11da48727ab8774b56372d1d0c7464a9eb0e969a13
@@ -0,0 +1,58 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+ /lib/*.bundle
58
+ /lib/*.so
@@ -0,0 +1,25 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm: 2.7.1
5
+ matrix:
6
+ include:
7
+ - rvm: 2.7.1
8
+ os: linux
9
+ - rvm: 2.7.1
10
+ os: osx
11
+ - rvm: ruby-head
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ fast_finish: true
15
+
16
+ before_install: gem install bundler
17
+ script:
18
+ - gem list -l
19
+ - bundle exec rake compile
20
+ - bundle exec rake
21
+
22
+ bundler_args: --jobs 1 --retry 3
23
+
24
+ notifications:
25
+ email: false
@@ -0,0 +1,20 @@
1
+ {
2
+ "configurations": [
3
+ {
4
+ "name": "Mac",
5
+ "includePath": [
6
+ "${workspaceFolder}/**",
7
+ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/Headers"
8
+ ],
9
+ "defines": [],
10
+ "macFrameworkPath": [
11
+ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
12
+ ],
13
+ "compilerPath": "/usr/bin/clang",
14
+ "cStandard": "c11",
15
+ "cppStandard": "c++17",
16
+ "intelliSenseMode": "clang-x64"
17
+ }
18
+ ],
19
+ "version": 4
20
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "files.associations": {
3
+ "__functional_03": "cpp",
4
+ "functional": "cpp",
5
+ "__hash_table": "cpp",
6
+ "__split_buffer": "cpp",
7
+ "array": "cpp",
8
+ "bitset": "cpp",
9
+ "initializer_list": "cpp",
10
+ "iterator": "cpp",
11
+ "string": "cpp",
12
+ "string_view": "cpp",
13
+ "unordered_map": "cpp",
14
+ "utility": "cpp",
15
+ "vector": "cpp",
16
+ "sstream": "cpp",
17
+ "__functional_base": "cpp",
18
+ "__functional_base_03": "cpp",
19
+ "__tuple": "cpp",
20
+ "algorithm": "cpp",
21
+ "chrono": "cpp",
22
+ "type_traits": "cpp",
23
+ "limits": "cpp",
24
+ "memory": "cpp",
25
+ "ratio": "cpp",
26
+ "tuple": "cpp"
27
+ }
28
+ }
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evt.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data-buffer (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.2)
10
+ rake (12.3.3)
11
+ rake-compiler (1.1.1)
12
+ rake
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ data-buffer!
19
+ minitest (~> 5.0)
20
+ rake (~> 12.0)
21
+ rake-compiler (~> 1.0)
22
+
23
+ BUNDLED WITH
24
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020, Delton Ding
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,34 @@
1
+ # buffer
2
+ The gem to operate low level I/O data buffer
3
+
4
+ [![Build Status](https://travis-ci.org/dsh0416/buffer.svg?branch=master)](https://travis-ci.org/dsh0416/buffer)
5
+
6
+ ## Example
7
+
8
+ ```
9
+ gem install 'data-buffer'
10
+ ```
11
+
12
+ ```ruby
13
+ require 'buffer'
14
+
15
+ buf = Buffer.new(1024) # Create char* buffer with size of 1024
16
+ buf.clear # memset 0 to buffer size
17
+ buf.size # return buffer size
18
+ buf.to_s # convert to string. WARNING: make sure the string has NUL terminator
19
+ buf.bytes # convert to char byte array
20
+
21
+ buf.resize(2048) # realloc the buffer to 2048
22
+
23
+ buf = Buffer.from('Hello World') # Create char* from existing string
24
+ buf = Buffer.from([1, 2, 3, 244]) # Create char* from existing char array
25
+
26
+ buf.data # Get raw data struct
27
+
28
+ # struct buffer_data {
29
+ # size_t buffer_size;
30
+ # char* buffer;
31
+ # };
32
+ #
33
+ # buffer_data* data = (buffer_data*) ((struct RData *)obj)->data;
34
+ ```
@@ -0,0 +1,17 @@
1
+
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+ require 'rake/extensiontask'
5
+
6
+ spec = Gem::Specification.load('buffer.gemspec')
7
+ Rake::ExtensionTask.new('buffer_ext', spec) do |ext|
8
+ ext.ext_dir = "ext/buffer"
9
+ end
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << "test"
13
+ t.libs << "lib"
14
+ t.test_files = FileList["test/**/*_test.rb"]
15
+ end
16
+
17
+ task :default => :test
@@ -0,0 +1,28 @@
1
+
2
+ require_relative 'lib/buffer/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "data-buffer"
6
+ spec.version = Buffer::VERSION
7
+ spec.authors = ["Delton Ding"]
8
+ spec.email = ["dsh0416@gmail.com"]
9
+
10
+ spec.summary = "The gem to operate low level I/O data buffer."
11
+ spec.description = "The gem to operate low level I/O data buffer."
12
+ spec.homepage = "https://github.com/dsh0416/buffer"
13
+ spec.license = 'BSD-3-Clause'
14
+ spec.required_ruby_version = '>= 2.7.1'
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/dsh0416/buffer"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.require_paths = ["lib"]
25
+ spec.extensions = ['ext/buffer/extconf.rb']
26
+
27
+ spec.add_development_dependency 'rake-compiler', '~> 1.0'
28
+ end
@@ -0,0 +1,113 @@
1
+ #include "buffer.h"
2
+
3
+ void Init_buffer_ext() {
4
+ kBuffer = rb_define_class("Buffer", rb_cObject);
5
+ kBufferPayload = rb_define_class_under(kBuffer, "BufferPayload", rb_cObject);
6
+ rb_define_singleton_method(kBuffer, "from", method_buffer_from, 1);
7
+ rb_define_method(kBuffer, "initialize", method_buffer_init, 1);
8
+ rb_define_method(kBuffer, "clear", method_buffer_clear, 0);
9
+ rb_define_method(kBuffer, "resize", method_buffer_resize, 1);
10
+ rb_define_method(kBuffer, "to_s", method_buffer_to_s, 0);
11
+ rb_define_method(kBuffer, "bytes", method_buffer_bytes, 0);
12
+ rb_define_method(kBuffer, "size", method_buffer_buffer_size, 0);
13
+ }
14
+
15
+ VALUE method_buffer_from(VALUE klass, VALUE val) {
16
+ if (TYPE(val) == T_STRING) {
17
+ char* ptr = StringValueCStr(val);
18
+ VALUE obj = rb_funcall(kBuffer, rb_intern("new"), 1, SIZET2NUM(strlen(ptr) + 1));
19
+ VALUE data = rb_iv_get(obj, "@data");
20
+ char* buf_ptr = internal_buffer_data_get(data)->buffer;
21
+ memcpy(buf_ptr, ptr, strlen(ptr) + 1);
22
+ return obj;
23
+ }
24
+
25
+ if (TYPE(val) == T_ARRAY) {
26
+ long len = RARRAY_LEN(val);
27
+ VALUE obj = rb_funcall(kBuffer, rb_intern("new"), 1, LONG2NUM(len));
28
+ VALUE data = rb_iv_get(obj, "@data");
29
+ char* buf_ptr = internal_buffer_data_get(data)->buffer;
30
+ for (long i = 0; i < len; i++) {
31
+ VALUE num = rb_ary_entry(val, i);
32
+ buf_ptr[i] = NUM2CHR(num);
33
+ }
34
+ return obj;
35
+ }
36
+
37
+ return Qnil;
38
+ }
39
+
40
+ VALUE method_buffer_init(VALUE self, VALUE size) {
41
+ VALUE data = internal_buffer_data_malloc(NUM2SIZET(size));
42
+ rb_iv_set(self, "@data", data);
43
+ return self;
44
+ }
45
+
46
+ VALUE method_buffer_clear(VALUE self) {
47
+ VALUE payload = rb_iv_get(self, "@data");
48
+ struct buffer_data* data = internal_buffer_data_get(payload);
49
+ memset(data->buffer, 0, data->buffer_size);
50
+ return self;
51
+ }
52
+
53
+ VALUE method_buffer_resize(VALUE self, VALUE size) {
54
+ VALUE data = rb_iv_get(self, "@data");
55
+ internal_buffer_data_realloc(data, NUM2SIZET(size));
56
+ return self;
57
+ }
58
+
59
+ VALUE method_buffer_to_s(VALUE self) {
60
+ // FIXME: What if a buffer doesn't have a NUL terminator?
61
+ VALUE data = rb_iv_get(self, "@data");
62
+
63
+ return rb_str_buf_new2(internal_buffer_data_get(data)->buffer);
64
+ }
65
+
66
+ VALUE method_buffer_bytes(VALUE self) {
67
+ VALUE data = rb_iv_get(self, "@data");
68
+ struct buffer_data* buffer = internal_buffer_data_get(data);
69
+ VALUE result = rb_ary_new2(buffer->buffer_size);
70
+ for (size_t i = 0; i < buffer->buffer_size; i++) {
71
+ rb_ary_store(result, i, CHR2FIX(buffer->buffer[i])); // # TODO: Why there is no CHR2NUM?
72
+ }
73
+ return result;
74
+ }
75
+
76
+ VALUE method_buffer_buffer_size(VALUE self) {
77
+ VALUE data = rb_iv_get(self, "@data");
78
+
79
+ return SIZET2NUM(internal_buffer_data_get(data)->buffer_size);
80
+ }
81
+
82
+ // internal methods
83
+ VALUE internal_buffer_data_malloc(size_t size) {
84
+ struct buffer_data* data = xmalloc(sizeof(struct buffer_data));
85
+ data->buffer_size = size;
86
+ data->buffer = xmalloc(sizeof(char) * size);
87
+ return TypedData_Wrap_Struct(kBufferPayload, &type_buffer_data, data);
88
+ }
89
+
90
+ VALUE internal_buffer_data_realloc(VALUE wrapped, size_t size) {
91
+ struct buffer_data* ptr;
92
+ TypedData_Get_Struct(wrapped, struct buffer_data, &type_buffer_data, ptr);
93
+ ptr->buffer_size = size;
94
+ ptr->buffer = xrealloc(ptr->buffer, sizeof(char) * size);
95
+ return wrapped;
96
+ }
97
+
98
+ struct buffer_data* internal_buffer_data_get(VALUE wrapped) {
99
+ struct buffer_data* ptr;
100
+ TypedData_Get_Struct(wrapped, struct buffer_data, &type_buffer_data, ptr);
101
+ return ptr;
102
+ }
103
+
104
+ void internal_buffer_data_free(void* ptr) {
105
+ struct buffer_data* t = (struct buffer_data*) ptr;
106
+ xfree(t->buffer);
107
+ xfree(t);
108
+ }
109
+
110
+ size_t internal_buffer_data_size(const void* ptr) {
111
+ struct buffer_data* t = (struct buffer_data*) ptr;
112
+ return sizeof(struct buffer_data) + t->buffer_size;
113
+ }
@@ -0,0 +1,36 @@
1
+ #include <ruby.h>
2
+
3
+ VALUE kBuffer = Qnil;
4
+ VALUE kBufferPayload = Qnil;
5
+
6
+ void Init_buffer_ext();
7
+ VALUE method_buffer_from(VALUE klass, VALUE val);
8
+ VALUE method_buffer_init(VALUE self, VALUE size);
9
+ VALUE method_buffer_clear(VALUE self);
10
+ VALUE method_buffer_resize(VALUE self, VALUE size);
11
+ VALUE method_buffer_to_s(VALUE self);
12
+ VALUE method_buffer_bytes(VALUE self);
13
+ VALUE method_buffer_buffer_size(VALUE self);
14
+
15
+ // internal methods
16
+ VALUE internal_buffer_data_malloc(size_t size);
17
+ VALUE internal_buffer_data_realloc(VALUE wrapped, size_t size);
18
+ struct buffer_data* internal_buffer_data_get(VALUE wrapped);
19
+ void internal_buffer_data_free(void* ptr);
20
+ size_t internal_buffer_data_size(const void* ptr);
21
+
22
+ struct buffer_data {
23
+ size_t buffer_size;
24
+ char* buffer;
25
+ };
26
+
27
+ static const rb_data_type_t type_buffer_data = {
28
+ .wrap_struct_name = "buffer_data",
29
+ .function = {
30
+ .dmark = NULL,
31
+ .dfree = internal_buffer_data_free,
32
+ .dsize = internal_buffer_data_size,
33
+ },
34
+ .data = NULL,
35
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
36
+ };
@@ -0,0 +1,6 @@
1
+ require 'mkmf'
2
+ extension_name = 'buffer_ext'
3
+ dir_config(extension_name)
4
+
5
+ create_header
6
+ create_makefile(extension_name)
@@ -0,0 +1,5 @@
1
+ require 'buffer_ext'
2
+
3
+ class Buffer
4
+ attr_reader :data
5
+ end
@@ -0,0 +1,3 @@
1
+ class Buffer
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data-buffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Delton Ding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: The gem to operate low level I/O data buffer.
28
+ email:
29
+ - dsh0416@gmail.com
30
+ executables: []
31
+ extensions:
32
+ - ext/buffer/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".travis.yml"
37
+ - ".vscode/c_cpp_properties.json"
38
+ - ".vscode/settings.json"
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - buffer.gemspec
45
+ - ext/buffer/buffer.c
46
+ - ext/buffer/buffer.h
47
+ - ext/buffer/extconf.rb
48
+ - lib/buffer.rb
49
+ - lib/buffer/version.rb
50
+ homepage: https://github.com/dsh0416/buffer
51
+ licenses:
52
+ - BSD-3-Clause
53
+ metadata:
54
+ homepage_uri: https://github.com/dsh0416/buffer
55
+ source_code_uri: https://github.com/dsh0416/buffer
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.7.1
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.2
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: The gem to operate low level I/O data buffer.
75
+ test_files: []