hikari_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.
@@ -0,0 +1,12 @@
1
+ #include "hikari_buffer.h"
2
+ #include "buffer.h"
3
+ VALUE rb_mHikari;
4
+ VALUE rb_cBuffer;
5
+
6
+ RUBY_FUNC_EXPORTED void
7
+ Init_hikari_buffer(void)
8
+ {
9
+ rb_mHikari = rb_define_module("Hikari");
10
+ Init_hikari_buffer_class();
11
+ }
12
+
@@ -0,0 +1,9 @@
1
+ #ifndef HIKARI_BUFFER_H
2
+ #define HIKARI_BUFFER_H 1
3
+ #include <stdint.h>
4
+ #include "ruby.h"
5
+
6
+ extern VALUE rb_mHikari;
7
+ extern VALUE rb_cBuffer;
8
+
9
+ #endif /* HIKARI_BUFFER_H */
@@ -0,0 +1,64 @@
1
+ #include "io.h"
2
+
3
+ void buffer_ensure_capacity(Buffer *buffer, size_t additional)
4
+ {
5
+ size_t required = buffer->cursor + additional;
6
+ if (required <= buffer->capacity)
7
+ return;
8
+
9
+ while (buffer->capacity < required)
10
+ {
11
+ buffer->capacity *= 2;
12
+ }
13
+ // Reallocate the buffer's data to the new capacity
14
+
15
+ REALLOC_N(
16
+ buffer->data,
17
+ uint8_t,
18
+ buffer->capacity);
19
+ }
20
+ static inline void
21
+ buffer_commit_write(Buffer *buffer, size_t bytes)
22
+ {
23
+ buffer->cursor += bytes;
24
+
25
+ if (buffer->cursor > buffer->size)
26
+ {
27
+ buffer->size = buffer->cursor;
28
+ }
29
+ }
30
+ void buffer_write(Buffer *buffer, const void *data, size_t length)
31
+ {
32
+ buffer_ensure_capacity(buffer, length);
33
+ memcpy(buffer->data + buffer->cursor, data, length);
34
+ buffer_commit_write(buffer, length);
35
+ }
36
+ void buffer_check_read_bounds(Buffer *buffer, size_t length)
37
+ {
38
+ if (buffer->cursor + length > buffer->size)
39
+ {
40
+ rb_raise(rb_eEOFError, "end of buffer reached while reading");
41
+ }
42
+ }
43
+
44
+ void buffer_read(Buffer *buffer, void *dst, size_t length)
45
+ {
46
+ buffer_check_read_bounds(buffer, length);
47
+
48
+ memcpy(
49
+ dst,
50
+ buffer->data + buffer->cursor,
51
+ length);
52
+ buffer->cursor += length;
53
+ }
54
+ void buffer_peek(
55
+ Buffer *buffer,
56
+ void *dst,
57
+ size_t length)
58
+ {
59
+ size_t cursor = buffer->cursor;
60
+
61
+ buffer_read(buffer, dst, length);
62
+
63
+ buffer->cursor = cursor;
64
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef HIKARI_IO_H
2
+ #define HIKARI_IO_H
3
+
4
+ #include "buffer.h"
5
+
6
+ void buffer_write(Buffer *buffer, const void *src, size_t length);
7
+ void buffer_read(Buffer *buffer, void *dst, size_t length);
8
+ void buffer_check_read_bounds(Buffer *buffer, size_t length);
9
+ void buffer_ensure_capacity(Buffer *buffer, size_t additional);
10
+
11
+ void buffer_peek(Buffer *buffer, void *dst, size_t length);
12
+ #endif
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HikariBuffer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "hikari_buffer/version"
4
+ require "hikari_buffer/hikari_buffer"
5
+
6
+ module Hikari
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,4 @@
1
+ module HikariBuffer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hikari_buffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Antar Mukhopadhyaya
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A native C extension providing fast binary serialization and deserialization
13
+ for primitive data types, strings, and raw bytes.
14
+ email:
15
+ - opensource.antar@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/hikari_buffer/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bench/growth_stress.rb
27
+ - bench/marshal.rb
28
+ - bench/mixed_workload.rb
29
+ - bench/protocol.rb
30
+ - bench/read_bytes.rb
31
+ - bench/read_string.rb
32
+ - bench/sequential_packet.rb
33
+ - bench/stringio.rb
34
+ - bench/write_bytes.rb
35
+ - bench/write_string.rb
36
+ - bench/write_u32.rb
37
+ - ext/hikari_buffer/buffer.c
38
+ - ext/hikari_buffer/buffer.h
39
+ - ext/hikari_buffer/convert.c
40
+ - ext/hikari_buffer/convert.h
41
+ - ext/hikari_buffer/extconf.rb
42
+ - ext/hikari_buffer/hikari_buffer.c
43
+ - ext/hikari_buffer/hikari_buffer.h
44
+ - ext/hikari_buffer/io.c
45
+ - ext/hikari_buffer/io.h
46
+ - lib/hikari_buffer.rb
47
+ - lib/hikari_buffer/version.rb
48
+ - sig/hikari_buffer.rbs
49
+ homepage: https://github.com/AntarMukhopadhyaya/hikari-buffer
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org
54
+ homepage_uri: https://github.com/AntarMukhopadhyaya/hikari-buffer
55
+ source_code_uri: https://github.com/AntarMukhopadhyaya/hikari-buffer
56
+ changelog_uri: https://github.com/AntarMukhopadhyaya/hikari-buffer/blob/main/CHANGELOG.md
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: 3.2.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 4.0.17
72
+ specification_version: 4
73
+ summary: Fast binary buffer for Ruby.
74
+ test_files: []