8000 WIP: modify for some review comments · ruby/ruby@88d54af · GitHub
[go: up one dir, main page]

Skip to content

Commit 88d54af

Browse files
committed
WIP: modify for some review comments
1 parent 5b09f17 commit 88d54af

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

buffer.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
static ID id_buffer_protocol;
1212

13-
static const rb_data_type_t random_mt_type = {
13+
static const rb_data_type_t buffer_protocol_type = {
1414
"buffer_protocol",
1515
{
1616
0,
@@ -21,8 +21,8 @@ static const rb_data_type_t random_mt_type = {
2121
};
2222

2323
/* Register buffer protocol functions for the given class */
24-
int
25-
rb_buffer_protocol_register_klass(VALUE klass, const rb_buffer_protocol_entry_t *entry) {
24+
bool
25+
rb_buffer_protocol_register(VALUE klass, const rb_buffer_protocol_entry_t *entry) {
2626
VALUE entry_obj = rb_ivar_get(klass, id_buffer_protocol);
2727
if (! NIL_P(entry_obj)) {
2828
rb_warning("Duplicated registration of buffer protocol to %"PRIsVALUE, klass);
@@ -40,11 +40,11 @@ int
4040
rb_buffer_is_row_major_contiguous(const rb_buffer_t *view)
4141
{
4242
const ssize_t ndim = view->ndim;
43-
const ssize_t* shape = view->shape;
44-
const ssize_t* strides = view->strides;
43+
const ssize_t *shape = view->shape;
44+
const ssize_t *strides = view->strides;
4545
ssize_t n = view->item_size;
4646
ssize_t i;
47-
for (i = view->ndim - 1; i >= 0; --i) {
47+
for (i = ndim - 1; i >= 0; --i) {
4848
if (strides[i] != n) return 0;
4949
n *= shape[i];
5050
}
@@ -56,8 +56,8 @@ int
5656
rb_buffer_is_column_major_contiguous(const rb_buffer_t *view)
5757
{
5858
const ssize_t ndim = view->ndim;
59-
const ssize_t* shape = view->shape;
60-
const ssize_t* strides = view->strides;
59+
const ssize_t *shape = view->shape;
60+
const ssize_t *strides = view->strides;
6161
ssize_t n = view->item_size;
6262
ssize_t i;
6363
for (i = 0; i < ndim; ++i) {
@@ -88,12 +88,11 @@ rb_buffer_fill_contiguous_strides(const int ndim, const int item_size, const ssi
8888

8989
/* Initialize view to expose a simple byte array */
9090
int
91-
rb_buffer_init_as_byte_array(rb_buffer_t *view, VALUE obj, void *data, ssize_t len, int readonly, int flags)
91+
rb_buffer_init_as_byte_array(rb_buffer_t *view, VALUE obj, void *data, ssize_t len, int read_only, int flags)
9292
{
93-
view->obj = obj;
9493
view->data = data;
9594
view->len = len;
96-
view->readonly = readonly;
95+
view->read_only = read_only;
9796
view->format = NULL;
9897
view->item_size = 1;
9998
view->ndim = 1;
@@ -226,7 +225,7 @@ rb_buffer_get_item_pointer(rb_buffer_t *view, ssize_t *indices)
226225
static rb_buffer_protocol_entry_t *
227226
lookup_buffer_protocol_entry(VALUE klass) {
228227
VALUE entry_obj = rb_ivar_get(klass, id_buffer_protocol);
229-
while (entry_obj == Qnil) {
228+
while (NIL_P(entry_obj)) {
230229
klass = rb_class_get_superclass(klass);
231230

232231
if (klass == rb_cBasicObject || klass == rb_cObject)
@@ -238,20 +237,20 @@ lookup_buffer_protocol_entry(VALUE klass) {
238237
if (! rb_typeddata_is_kind_of(entry_obj, &buffer_protocol_entry_data_type))
239238
return NULL;
240239

241-
return (rb_buffer_protocol_entry_t *)DATA_PTR(entry_obj);
240+
return (rb_buffer_protocol_entry_t *)RTYPEDDATA_PTR(entry_obj);
242241
}
243242

244243
/* Examine whether the given object supports buffer protocol. */
245244
int
246-
rb_obj_has_buffer_protocol(VALUE obj)
245+
rb_buffer_protocol_available_p(VALUE obj)
247246
{
248247
VALUE klass = CLASS_OF(obj);
249248
return lookup_buffer_protocol_entry(klass) != NULL;
250249
}
251250

252251
/* Obtain a buffer from obj, and substitute the information to view. */
253252
int
254-
rb_obj_get_buffer(VALUE obj, rb_buffer_t* view, int flags) {
253+
rb_buffer_protocol_get_buffer(VALUE obj, rb_buffer_t* view, int flags) {
255254
VALUE klass = CLASS_OF(obj);
256255
rb_buffer_protocol_entry_t *entry = lookup_buffer_protocol_entry(klass);
257256
if (entry)
@@ -262,7 +261,7 @@ rb_obj_get_buffer(VALUE obj, rb_buffer_t* view, int flags) {
262261

263262
/* Release the buffer view obtained from obj. */
264263
int
265-
rb_obj_release_buffer(VALUE obj, rb_buffer_t* view) {
264+
rb_buffer_protocol_release_buffer(VALUE obj, rb_buffer_t* view) {
266265
VALUE klass = CLASS_OF(obj);
267266
rb_buffer_protocol_entry_t *entry = lookup_buffer_protocol_entry(klass);
268267
if (entry)

include/ruby/buffer.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include "ruby/internal/dllexport.h"
14+
#include "ruby/internal/stdbool.h"
1415
#include "ruby/internal/value.h"
1516

1617
enum ruby_buffer_flags {
@@ -26,19 +27,14 @@ enum ruby_buffer_flags {
2627
};
2728

2829
typedef struct {
29-
/* The original object that have the memory exported via this buffer.
30-
* The consumer of this buffer has the responsibility to call rb_gc_mark
31-
* for preventing this obj collected by GC. */
32-
VALUE obj;
33-
3430
/* The pointer to the exported memory. */
3531
void *data;
3632

3733
/* The number of bytes in data. */
3834
ssize_t len;
3935

40-
/* 1 for readonly memory, 0 for writable memory. */
41-
int readonly;
36+
/* 1 for read-only memory, 0 for writable memory. */
37+
int read_only;
4238

4339
/* A string to describe the format of an element, or NULL for unsigned byte.
4440
* The format string is a sequence the following pack-template specifiers:
@@ -75,18 +71,18 @@ typedef struct {
7571
void *const private;
7672
} rb_buffer_t;
7773

78-
typedef int (* rb_get_buffer_func_t)(VALUE obj, rb_buffer_t *view, int flags);
79-
typedef int (* rb_release_buffer_func_t)(VALUE obj, rb_buffer_t *view);
74+
typedef int (* rb_buffer_protocol_get_buffer_func_t)(VALUE obj, rb_buffer_t *view, int flags);
75+
typedef int (* rb_buffer_protocol_release_buffer_func_t)(VALUE obj, rb_buffer_t *view);
8076

8177
typedef struct {
82-
rb_get_buffer_func_t get_buffer_func;
83-
rb_release_buffer_func_t release_buffer_func;
78+
rb_buffer_protocol_get_buffer_func_t get_buffer_func;
79+
rb_buffer_protocol_release_buffer_func_t release_buffer_func;
8480
} rb_buffer_protocol_entry_t;
8581

8682
RBIMPL_SYMBOL_EXPORT_BEGIN()
8783

8884
/* buffer.c */
89-
int rb_buffer_protocol_register_klass(VALUE klass, const buffer_protocol_entry_t *entry);
85+
bool rb_buffer_protocol_register(VALUE klass, const buffer_protocol_entry_t *entry);
9086

9187
#define rb_buffer_is_contiguous(view) ( \
9288
rb_buffer_is_row_major_contiguous(view) \
@@ -99,9 +95,9 @@ int rb_buffer_init_as_byte_array(rb_buffer_t *view, void *data, ssize_t len, int
9995
ssize_t rb_buffer_item_size_from_format(const char *format);
10096
void *rb_buffer_get_item_pointer(rb_buffer_t *view, ssize_t *indices);
10197

102-
int rb_obj_has_buffer_protocol(VALUE obj);
103-
int rb_obj_get_buffer(VALUE obj, rb_buffer_t* buffer);
104-
int rb_obj_release_buffer(VALUE obj, rb_buffer_t* buffer);
98+
int rb_buffer_protocol_available_p(VALUE obj);
99+
int rb_buffer_protocol_get_buffer(VALUE obj, rb_buffer_t* buffer);
100+
int rb_buffer_protocol_release_buffer(VALUE obj, rb_buffer_t* buffer);
105101

106102
RBIMPL_SYMBOL_EXPORT_END()
107103

0 commit comments

Comments
 (0)
0