VALUE string_spec_rb_alloc_tmp_buffer(VALUE self, VALUE len) { VALUE str; char* s = (char*)rb_alloc_tmp_buffer(&str, FIX2INT(len)); return str; }
static VALUE inst_put_sarray(VALUE self, VALUE arg, const char *field) { struct ngraph_instance *inst; ngraph_value ary; int num, i; VALUE str, tmpstr; inst = check_id(self); if (inst == NULL) { return Qnil; } if (NIL_P(arg)) { num = 0; } else { if (!RB_TYPE_P(arg, T_ARRAY)) { rb_raise(rb_eArgError, "%s#%s: the argument must be an Array", rb_obj_classname(self), field); } num = RARRAY_LEN(arg); } ary.ary = NULL; if (num > 0) { ary.ary = rb_alloc_tmp_buffer(&tmpstr, sizeof(*ary.ary) + sizeof(ngraph_value) * num); ary.ary->num = num; if (ary.ary) { for (i = 0; i < num; i++) { str = rb_ary_entry(arg, i); ary.ary->ary[i].str = StringValueCStr(str); } } } inst->rcode = ngraph_object_put(inst->obj, field, inst->id, &ary); if (ary.ary) { rb_free_tmp_buffer(&tmpstr); } if (inst->rcode < 0) { return Qnil; } return arg; }
static struct ngraph_array * allocate_darray(VALUE self, volatile VALUE *tmpstr, VALUE arg, const char *field) { struct ngraph_array *narray; int i, num; VALUE ary; ary = get_array_arg(self, field, arg, &num); narray = rb_alloc_tmp_buffer(tmpstr, sizeof(*narray) + sizeof(ngraph_value) * num); if (narray == NULL) { return NULL; } narray->num = num; for (i = 0; i < num; i++) { narray->ary[i].d = NUM2DBL(rb_ary_entry(ary, i)); } return narray; }