Class: Fiddle::MemoryView
- Inherits:
-
Object
- Object
- Fiddle::MemoryView
- Defined in:
- ext/fiddle/memory_view.c
Class Method Summary collapse
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #byte_size ⇒ Object
- #format ⇒ Object
- #initialize(target) ⇒ Object constructor
- #item_size ⇒ Object
- #ndim ⇒ Object
- #obj ⇒ Object
- #readonly? ⇒ Boolean
- #release ⇒ Object
- #shape ⇒ Object
- #strides ⇒ Object
- #sub_offsets ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(target) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'ext/fiddle/memory_view.c', line 108
static VALUE
rb_fiddle_memview_initialize(VALUE obj, VALUE target)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (!rb_memory_view_get(target, &data->view, 0)) {
data->view.obj = Qnil;
rb_raise(rb_eArgError, "Unable to get a memory view from %+"PRIsVALUE, target);
}
return Qnil;
}
|
Class Method Details
.export(target) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'ext/fiddle/memory_view.c', line 99
static VALUE
rb_fiddle_memview_s_export(VALUE klass, VALUE target)
{
ID id_new;
CONST_ID(id_new, "new");
VALUE memview = rb_funcall(klass, id_new, 1, target);
return rb_ensure(rb_yield, memview, rb_fiddle_memview_release, memview);
}
|
Instance Method Details
#[](*args) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'ext/fiddle/memory_view.c', line 235
static VALUE
rb_fiddle_memview_aref(int argc, VALUE *argv, VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
const ssize_t ndim = data->view.ndim;
if (argc != ndim) {
rb_raise(rb_eIndexError, "wrong number of index (%d for %"PRIdSIZE")", argc, ndim);
}
VALUE indices_v = 0;
ssize_t *indices = ALLOCV_N(ssize_t, indices_v, ndim);
ssize_t i;
for (i = 0; i < ndim; ++i) {
ssize_t x = NUM2SSIZET(argv[i]);
indices[i] = x;
}
uint8_t *ptr = rb_memory_view_get_item_pointer(&data->view, indices);
ALLOCV_END(indices_v);
if (data->view.format == NULL) {
return INT2FIX(*ptr);
}
if (!data->members) {
const char *err;
if (rb_memory_view_parse_item_format(data->view.format, &data->members, &data->n_members, &err) < 0) {
rb_raise(rb_eRuntimeError, "Unable to recognize item format at %"PRIdSIZE" in \"%s\"",
err - data->view.format, data->view.format);
}
}
return rb_memory_view_extract_item_members(ptr, data->members, data->n_members);
}
|
#byte_size ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'ext/fiddle/memory_view.c', line 131
static VALUE
rb_fiddle_memview_get_byte_size(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
return SSIZET2NUM(data->view.byte_size);
}
|
#format ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'ext/fiddle/memory_view.c', line 151
static VALUE
rb_fiddle_memview_get_format(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
return data->view.format == NULL ? Qnil : rb_str_new_cstr(data->view.format);
}
|
#item_size ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'ext/fiddle/memory_view.c', line 161
static VALUE
rb_fiddle_memview_get_item_size(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
return SSIZET2NUM(data->view.item_size);
}
|
#ndim ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'ext/fiddle/memory_view.c', line 171
static VALUE
rb_fiddle_memview_get_ndim(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
return SSIZET2NUM(data->view.ndim);
}
|
#obj ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'ext/fiddle/memory_view.c', line 122
static VALUE
rb_fiddle_memview_get_obj(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
return data->view.obj;
}
|
#readonly? ⇒ Boolean
141 142 143 144 145 146 147 148 149 |
# File 'ext/fiddle/memory_view.c', line 141
static VALUE
rb_fiddle_memview_get_readonly(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
return data->view.readonly ? Qtrue : Qfalse;
}
|
#release ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'ext/fiddle/memory_view.c', line 88
static VALUE
rb_fiddle_memview_release(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
fiddle_memview_release(data);
return Qnil;
}
|
#shape ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'ext/fiddle/memory_view.c', line 181
static VALUE
rb_fiddle_memview_get_shape(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
if (data->view.shape == NULL) return Qnil;
const ssize_t ndim = data->view.ndim;
VALUE shape = rb_ary_new_capa(ndim);
ssize_t i;
for (i = 0; i < ndim; ++i) {
rb_ary_push(shape, SSIZET2NUM(data->view.shape[i]));
}
return shape;
}
|
#strides ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'ext/fiddle/memory_view.c', line 199
static VALUE
rb_fiddle_memview_get_strides(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
if (data->view.strides == NULL) return Qnil;
const ssize_t ndim = data->view.ndim;
VALUE strides = rb_ary_new_capa(ndim);
ssize_t i;
for (i = 0; i < ndim; ++i) {
rb_ary_push(strides, SSIZET2NUM(data->view.strides[i]));
}
return strides;
}
|
#sub_offsets ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'ext/fiddle/memory_view.c', line 217
static VALUE
rb_fiddle_memview_get_sub_offsets(VALUE obj)
{
struct memview_data *data;
TypedData_Get_Struct(obj, struct memview_data, &fiddle_memview_data_type, data);
if (NIL_P(data->view.obj)) return Qnil;
if (data->view.sub_offsets == NULL) return Qnil;
const ssize_t ndim = data->view.ndim;
VALUE sub_offsets = rb_ary_new_capa(ndim);
ssize_t i;
for (i = 0; i < ndim; ++i) {
rb_ary_push(sub_offsets, SSIZET2NUM(data->view.sub_offsets[i]));
}
return sub_offsets;
}
|
#to_s ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'ext/fiddle/memory_view.c', line 275
static VALUE
rb_fiddle_memview_to_s(VALUE self)
{
struct memview_data *data;
const char *raw_data;
long byte_size;
VALUE string;
TypedData_Get_Struct(self,
struct memview_data,
&fiddle_memview_data_type,
data);
if (NIL_P(data->view.obj)) {
raw_data = NULL;
byte_size = 0;
} else {
raw_data = data->view.data;
byte_size = data->view.byte_size;
}
string = rb_enc_str_new_static(raw_data, byte_size, rb_ascii8bit_encoding());
{
ID id_memory_view;
CONST_ID(id_memory_view, "memory_view");
rb_ivar_set(string, id_memory_view, self);
}
return rb_obj_freeze(string);
}
|