class Fiddle::MemoryView

Public Class Methods

export(p1) click to toggle source
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);
}
new(p1) click to toggle source
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;
}

Public Instance Methods

[](*args) click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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?() click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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() click to toggle source
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);
}