static void sampling_job_handler(void *data_unused) { int start = 0; int lines[BUF_SIZE]; VALUE buff[BUF_SIZE]; VALUE rb_method_name, rb_label, rb_file, rb_singleton_method; int i, is_singleton; char *method_name, *label, *file; unsigned long line, thread_id; rbkit_cpu_sample *sample = malloc(sizeof(rbkit_cpu_sample)); int collected_size = rb_profile_frames(start, sizeof(buff) / sizeof(VALUE), buff, lines); rbkit_frame_data *frame_data = malloc(sizeof(rbkit_frame_data) * collected_size); sample->frames = frame_data; sample->frame_count = collected_size; for (i=0; i<collected_size; i++) { rb_method_name = rb_profile_frame_method_name(buff[i]); if(NIL_P(rb_method_name)) { method_name = NULL; } else { method_name = StringValueCStr(rb_method_name); } frame_data[i].method_name = method_name; rb_label = rb_profile_frame_full_label(buff[i]); label = StringValueCStr(rb_label); frame_data[i].label = label; rb_file = rb_profile_frame_absolute_path(buff[i]); if(NIL_P(rb_file)) rb_file = rb_profile_frame_path(buff[i]); file = StringValueCStr(rb_file); frame_data[i].file = file; line = FIX2ULONG(rb_profile_frame_first_lineno(buff[i])); frame_data[i].line = line; rb_singleton_method = rb_profile_frame_singleton_method_p(buff[i]); is_singleton = rb_singleton_method == Qtrue; frame_data[i].is_singleton_method = is_singleton; thread_id = FIX2ULONG(rb_obj_id(rb_thread_current())); frame_data[i].thread_id = thread_id; } queue_cpu_sample_for_sending(sample); free(frame_data); free(sample); }
static VALUE test_fix2ulong(VALUE obj, VALUE num) { char buf[128]; sprintf(buf, "%lu", FIX2ULONG(num)); return rb_str_new_cstr(buf); }
static VALUE objectid_generate(VALUE self) { VALUE oid, digest; char hostname[MAX_HOSTNAME_LENGTH]; unsigned char oid_bytes[12]; unsigned long t, inc; unsigned short pid; int i; t = htonl(time(NULL)); MEMCPY(&oid_bytes, &t, unsigned char, 4); if (gethostname(hostname, MAX_HOSTNAME_LENGTH) != 0) { rb_raise(rb_eRuntimeError, "failed to get hostname"); } digest = rb_funcall(DigestMD5, rb_intern("digest"), 1, rb_str_new2(hostname)); MEMCPY(&oid_bytes[4], RSTRING_PTR(digest), unsigned char, 3); pid = htons(getpid()); MEMCPY(&oid_bytes[7], &pid, unsigned char, 2); inc = htonl(FIX2ULONG(rb_funcall(self, rb_intern("get_inc"), 0))); MEMCPY(&oid_bytes[9], ((unsigned char*)&inc + 1), unsigned char, 3); oid = rb_ary_new2(12); for(i = 0; i < 12; i++) { rb_ary_store(oid, i, INT2FIX((unsigned int)oid_bytes[i])); } return oid; }
static VALUE rb_cogl_rectangle (VALUE self, VALUE x, VALUE y, VALUE width, VALUE height) { /* If all of the values are fixed nums then use the integer version */ if (FIXNUM_P (x) && FIXNUM_P (y) && FIXNUM_P (width) && FIXNUM_P (height)) cogl_rectangle (FIX2LONG (x), FIX2LONG (y), FIX2ULONG (width), FIX2ULONG (height)); else cogl_rectanglex (rbclt_num_to_fixed (x), rbclt_num_to_fixed (y), rbclt_num_to_fixed (width), rbclt_num_to_fixed (height)); return Qnil; }
static VALUE Buffer_read(int argc, VALUE* argv, VALUE self) { VALUE out = Qnil; unsigned long n = -1; bool all = false; switch(argc) { case 2: out = argv[1]; /* pass through */ case 1: n = FIX2ULONG(argv[0]); break; case 0: all = true; break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc); } BUFFER(self, b); if(out != Qnil) { CHECK_STRING_TYPE(out); } if(all) { return read_all(b, out); } if(n == 0) { /* do nothing */ MAKE_EMPTY_STRING(out); return out; } #ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE if(!msgpack_buffer_has_io(b) && out == Qnil && msgpack_buffer_all_readable_size(b) <= n) { /* same as to_s && clear; optimize */ VALUE str = msgpack_buffer_all_as_string(b); msgpack_buffer_clear(b); if(RSTRING_LEN(str) == 0) { return Qnil; } else { return str; } } #endif MAKE_EMPTY_STRING(out); read_until_eof(b, out, n); if(RSTRING_LEN(out) == 0) { return Qnil; } else { return out; } }
static inline unsigned long rb_data_view_assert_offset(rb_data_view_t *view, VALUE idx) { unsigned long index; index = FIX2ULONG(idx); \ if (index > view->byte_length) rb_raise(rb_eRangeError, "Offset out of range."); return index; }
/* * call-seq: * DataView.new(buf) => DataView * DataView.new("buffer") => DataView * * Creates a new DataView instance. Both ArrayBuffer and data (String) constructors are supported. * * === Examples * buf = ArrayBuffer.new(8) => ArrayBuffer * view = DataView.new(buf) => DataView * * view = DataView.new(buf, 2) => DataView * view.byte_offset => 2 * view.byte_length => 6 * * view = DataView.new(buf, 2, 4) => DataView * view.byte_offset => 2 * view.byte_length => 2 * * view = DataView.new("buffer") => DataView * */ static VALUE rb_data_view_s_new(int argc, VALUE *argv, VALUE klass) { VALUE data_view; unsigned long new_byte_length; rb_data_view_t *view = NULL; VALUE obj, byte_offset, byte_length, buffer; new_byte_length = 0; rb_scan_args(argc, argv, "12", &obj, &byte_offset, &byte_length); data_view = Data_Make_Struct(klass, rb_data_view_t, rb_mark_data_view, rb_free_data_view, view); view->byte_offset = 0; view->byte_length = 0; if (rb_class_of(obj) == rb_cArrayBuffer) { GetArrayBuffer(obj); view->byte_length = buf->length; view->buf = obj; } else if (rb_type(obj) == T_STRING) { ArrayBufferEncode(obj); buffer = rb_alloc_array_buffer((unsigned long)RSTRING_LEN(obj), (void *)RSTRING_PTR(obj)); GetArrayBuffer(buffer); view->byte_length = buf->length; view->buf = buffer; } else { rb_raise(rb_eTypeError, "DataView constructor %s not supported.", RSTRING_PTR(rb_obj_as_string(obj))); } if (!NIL_P(byte_offset)) { Check_Type(byte_offset, T_FIXNUM); view->byte_offset = FIX2ULONG(byte_offset); if (view->byte_offset >= view->byte_length) rb_raise(rb_eRangeError, "Byte offset out of range."); } if (!NIL_P(byte_length)) { Check_Type(byte_length, T_FIXNUM); new_byte_length = FIX2ULONG(byte_length); if (new_byte_length > view->byte_length) rb_raise(rb_eRangeError, "Byte length out of range."); if ((view->byte_offset + new_byte_length) > view->byte_length) rb_raise(rb_eRangeError, "Byte offset / length is not aligned."); view->byte_length = new_byte_length; } view->byte_length -= view->byte_offset; rb_obj_call_init(data_view, 0, NULL); return data_view; }
void rstat2stat(VALUE rstat, struct stat *statbuf) { statbuf->st_dev = FIX2ULONG(rb_funcall(rstat,rb_intern("dev"),0)); statbuf->st_ino = FIX2ULONG(rb_funcall(rstat,rb_intern("ino"),0)); statbuf->st_mode = FIX2UINT(rb_funcall(rstat,rb_intern("mode"),0)); statbuf->st_nlink = FIX2UINT(rb_funcall(rstat,rb_intern("nlink"),0)); statbuf->st_uid = FIX2UINT(rb_funcall(rstat,rb_intern("uid"),0)); statbuf->st_gid = FIX2UINT(rb_funcall(rstat,rb_intern("gid"),0)); statbuf->st_rdev = FIX2ULONG(rb_funcall(rstat,rb_intern("rdev"),0)); statbuf->st_size = FIX2ULONG(rb_funcall(rstat,rb_intern("size"),0)); statbuf->st_blksize = NUM2ULONG(rb_funcall(rstat,rb_intern("blksize"),0)); statbuf->st_blocks = NUM2ULONG(rb_funcall(rstat,rb_intern("blocks"),0)); statbuf->st_atime = NUM2ULONG(rb_funcall(rb_funcall(rstat,rb_intern("atime"),0),rb_intern("to_i"),0)); statbuf->st_mtime = NUM2ULONG(rb_funcall(rb_funcall(rstat,rb_intern("mtime"),0),rb_intern("to_i"),0)); statbuf->st_ctime = NUM2ULONG(rb_funcall(rb_funcall(rstat,rb_intern("ctime"),0),rb_intern("to_i"),0)); }
static unsigned long *addr_of(VALUE self, VALUE index) { struct raindrops *r = get(self); unsigned long off = FIX2ULONG(index) * raindrop_size; if (off >= raindrop_size * r->size) rb_raise(rb_eArgError, "offset overrun"); return (unsigned long *)((unsigned long)r->drops + off); }
/* * call-seq: * ArrayBuffer.new(8) => ArrayBuffer * ArrayBuffer.new("buffer") => ArrayBuffer * * Creates a new ArrayBuffer instance. Both length and data (String) constructors are supported. * * === Examples * buf = ArrayBuffer.new(8) => ArrayBuffer * buf.byte_length => 8 * * buf = ArrayBuffer.new("buffer") => ArrayBuffer * buf.byte_length => 6 * buf.to_s => "buffer" * */ static VALUE rb_array_buffer_s_new(VALUE klass, VALUE obj) { VALUE buffer; if (FIXNUM_P(obj)) { buffer = rb_alloc_array_buffer(FIX2ULONG(obj), NULL); } else if (rb_type(obj) == T_STRING) { ArrayBufferEncode(obj); buffer = rb_alloc_array_buffer((unsigned long)RSTRING_LEN(obj), (void *)RSTRING_PTR(obj)); } else { rb_raise(rb_eTypeError, "ArrayBuffer constructor %s not supported.", RSTRING_PTR(rb_obj_as_string(obj))); } return buffer; }
static VALUE Buffer_skip(VALUE self, VALUE sn) { BUFFER(self, b); unsigned long n = FIX2ULONG(sn); /* do nothing */ if(n == 0) { return ULONG2NUM(0); } size_t sz = read_until_eof(b, Qnil, n); return ULONG2NUM(sz); }
// Refer Ruby source ext/objspace/object_tracing.c::freeobj_i static void freeobj_i(VALUE tpval, void *data) { rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); rbkit_obj_destroyed_event *event; VALUE obj = rb_tracearg_object(tparg); rbkit_logger *arg = (rbkit_logger *)data; // Delete allocation info of freed object delete_rbkit_allocation_info(tparg, obj, arg->str_table, arg->object_table); event = new_rbkit_obj_destroyed_event(FIX2ULONG(rb_obj_id(obj))); pack_event((rbkit_event_header *)event, arg->msgpacker); free(event); send_message(arg->sbuf); }
// Refer Ruby source ext/objspace/object_tracing.c::newobj_i static void newobj_i(VALUE tpval, void *data) { rbkit_obj_created_event *event; rbkit_logger * arg = (rbkit_logger *)data; rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); rbkit_allocation_info *info = new_rbkit_allocation_info(tparg, arg->str_table, arg->object_table); VALUE obj = rb_tracearg_object(tparg); VALUE klass = RBASIC_CLASS(obj); const char *class_name = NULL; if (!NIL_P(klass) && BUILTIN_TYPE(obj) != T_NONE && BUILTIN_TYPE(obj) != T_ZOMBIE && BUILTIN_TYPE(obj) != T_ICLASS) class_name = rb_class2name(klass); event = new_rbkit_obj_created_event(FIX2ULONG(rb_obj_id(obj)), class_name, info); pack_event((rbkit_event_header *)event, arg->msgpacker); free(event); send_message(arg->sbuf); }
static VALUE Buffer_read_all(int argc, VALUE* argv, VALUE self) { VALUE out = Qnil; unsigned long n = 0; bool all = false; switch(argc) { case 2: out = argv[1]; /* pass through */ case 1: n = FIX2ULONG(argv[0]); break; case 0: all = true; break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc); } BUFFER(self, b); if(out != Qnil) { CHECK_STRING_TYPE(out); } if(all) { return read_all(b, out); } if(n == 0) { /* do nothing */ MAKE_EMPTY_STRING(out); return out; } if(!msgpack_buffer_ensure_readable(b, n)) { rb_raise(rb_eEOFError, "end of buffer reached"); } MAKE_EMPTY_STRING(out); size_t sz = msgpack_buffer_read_to_string_nonblock(b, out, n); return out; }
static VALUE Buffer_skip_all(VALUE self, VALUE sn) { BUFFER(self, b); unsigned long n = FIX2ULONG(sn); /* do nothing */ if(n == 0) { return self; } if(!msgpack_buffer_ensure_readable(b, n)) { rb_raise(rb_eEOFError, "end of buffer reached"); } msgpack_buffer_skip_nonblock(b, n); return self; }
static inline void* get_pointer_value(VALUE value) { const int type = TYPE(value); if (type == T_DATA && rb_obj_is_kind_of(value, rbffi_PointerClass)) { return memory_address(value); } else if (type == T_NIL) { return NULL; } else if (type == T_FIXNUM) { return (void *) (uintptr_t) FIX2ULONG(value); } else if (type == T_BIGNUM) { return (void *) (uintptr_t) NUM2ULL(value); } else if (rb_respond_to(value, id_to_ptr)) { return MEMORY_PTR(rb_funcall2(value, id_to_ptr, 0, NULL)); } else { rb_raise(rb_eArgError, "value is not a pointer"); return NULL; } }
/* * call-seq: * Int32Array.new(100) => Int32Array * Int32Array.new("01234567") => Int32Array * Int32Array.new(buf, 20) => Int32Array * Int32Array.new(buf, 0, 20) => Int32Array * Int32Array.new(buf, 20, 20) => Int32Array * * Creates a new TypeArray instance. ArrayBuffer, data (String) and length constructors are supported. * * === Examples * buf = ArrayBuffer.new(100) => ArrayBuffer * * ary = Int32Array.new(buf, 20) => Int32Array * ary.length => 20 * ary.byte_length => 80 * ary.byte_offset => 20 * * ary = Int32Array.new(buf, 0, 20) => Int32Array * ary.length => 20 * ary.byte_length => 80 * ary.byte_offset => 0 * * ary = Int32Array.new(buf, 20, 20) => Int32Array * ary.length => 20 * ary.byte_length => 80 * ary.byte_offset => 20 * * ary = Int32Array.new("01234567") => Int32Array * ary.byte_length => 8 * ary.to_s => "01234567" * * ary = Int32Array.new(100) => Int32Array * ary.length => 100 * ary.byte_length => 400 */ static VALUE rb_type_array_s_new(int argc, VALUE *argv, VALUE klass) { VALUE type_array; VALUE obj, byte_offset, length; rb_type_array_t *array = NULL; unsigned long buffer_length, offset; if (klass == rb_cTypeArray) rb_raise(rb_eTypeError, "TypeArray cannot be instantiated directly."); rb_scan_args(argc, argv, "12", &obj, &byte_offset, &length); type_array = Data_Make_Struct(klass, rb_type_array_t, rb_mark_type_array, rb_free_type_array, array); if (klass != rb_cStructArray) { array->size = FIX2ULONG(rb_const_get(klass, rb_intern("BYTES_PER_ELEMENT"))); } array->byte_offset = 0; array->length = 0; if (klass == rb_cInt8Array) { array->aref_fn = rb_type_array_aref_int8; array->aset_fn = rb_type_array_aset_int8; array->mul_fn = rb_type_array_mul_int8; array->plus_fn = rb_type_array_plus_int8; array->minus_fn = rb_type_array_minus_int8; array->div_fn = rb_type_array_div_int8; array->eql_fn = rb_type_array_eql_int8; } else if (klass == rb_cUInt8Array) { array->aref_fn = rb_type_array_aref_uint8; array->aset_fn = rb_type_array_aset_uint8; array->mul_fn = rb_type_array_mul_uint8; array->plus_fn = rb_type_array_plus_uint8; array->minus_fn = rb_type_array_minus_uint8; array->div_fn = rb_type_array_div_uint8; array->eql_fn = rb_type_array_eql_uint8; } else if (klass == rb_cInt16Array) { array->aref_fn = rb_type_array_aref_int16; array->aset_fn = rb_type_array_aset_int16; array->mul_fn = rb_type_array_mul_int16; array->plus_fn = rb_type_array_plus_int16; array->minus_fn = rb_type_array_minus_int16; array->div_fn = rb_type_array_div_int16; array->eql_fn = rb_type_array_eql_int16; } else if (klass == rb_cUInt16Array) { array->aref_fn = rb_type_array_aref_uint16; array->aset_fn = rb_type_array_aset_uint16; array->mul_fn = rb_type_array_mul_uint16; array->plus_fn = rb_type_array_plus_uint16; array->minus_fn = rb_type_array_minus_uint16; array->div_fn = rb_type_array_div_uint16; array->eql_fn = rb_type_array_eql_uint16; } else if (klass == rb_cInt32Array) { array->aref_fn = rb_type_array_aref_int32; array->aset_fn = rb_type_array_aset_int32; array->mul_fn = rb_type_array_mul_int32; array->plus_fn = rb_type_array_plus_int32; array->minus_fn = rb_type_array_minus_int32; array->div_fn = rb_type_array_div_int32; array->eql_fn = rb_type_array_eql_int32; } else if (klass == rb_cUInt32Array) { array->aref_fn = rb_type_array_aref_uint32; array->aset_fn = rb_type_array_aset_uint32; array->mul_fn = rb_type_array_mul_uint32; array->plus_fn = rb_type_array_plus_uint32; array->minus_fn = rb_type_array_minus_uint32; array->div_fn = rb_type_array_div_uint32; array->eql_fn = rb_type_array_eql_uint32; } else if (klass == rb_cFloat32Array) { array->aref_fn = rb_type_array_aref_float32; array->aset_fn = rb_type_array_aset_float32; array->mul_fn = rb_type_array_mul_float32; array->plus_fn = rb_type_array_plus_float32; array->minus_fn = rb_type_array_minus_float32; array->div_fn = rb_type_array_div_float32; array->eql_fn = rb_type_array_eql_float32; } else if (klass == rb_cFloat64Array) { array->aref_fn = rb_type_array_aref_float64; array->aset_fn = rb_type_array_aset_float64; array->mul_fn = rb_type_array_mul_float64; array->plus_fn = rb_type_array_plus_float64; array->minus_fn = rb_type_array_minus_float64; array->div_fn = rb_type_array_div_float64; array->eql_fn = rb_type_array_eql_float64; } else if (klass == rb_cStructArray) { array->aref_fn = rb_type_array_aref_struct; array->aset_fn = rb_type_array_aset_struct; } if (FIXNUM_P(obj)) { array->length = FIX2ULONG(obj); array->byte_length = (array->length * array->size); array->buf = rb_alloc_array_buffer(array->byte_length, NULL); } else if (rb_type(obj) == T_STRING) { array->byte_length = (unsigned long)RSTRING_LEN(obj); array->length = (array->byte_length / array->size); ArrayBufferEncode(obj); array->buf = rb_alloc_array_buffer(array->byte_length, (void *)RSTRING_PTR(obj)); } else if (rb_class_of(obj) == rb_cArrayBuffer) { GetArrayBuffer(obj); if (!NIL_P(byte_offset)) { Check_Type(byte_offset, T_FIXNUM); array->byte_offset = FIX2ULONG(byte_offset); if (!rb_type_array_assert_alignment(array->byte_offset, array->size)) rb_raise(rb_eRangeError, "Byte offset is not aligned."); } buffer_length = buf->length; if (!NIL_P(length)) { Check_Type(length, T_FIXNUM); array->length = FIX2ULONG(length); array->byte_length = array->length * array->size; } else { array->byte_length = buffer_length - array->byte_offset; } if ((array->byte_offset + array->byte_length) > buffer_length) rb_raise(rb_eRangeError, "Byte offset / length is not aligned."); if (array->length == 0) array->length = array->byte_length / array->size; if (array->byte_offset > buffer_length || array->byte_offset + array->length > buffer_length || array->byte_offset + array->length * array->size > buffer_length) { rb_raise(rb_eRangeError, "Length is out of range."); } array->buf = obj; } else if (rb_obj_is_kind_of(obj, rb_cTypeArray) == Qtrue) { GetTypeArray(obj); array->length = ary->length; array->byte_length = (array->size * array->length); array->buf = rb_alloc_array_buffer(array->byte_length, NULL); array->byte_offset = 0; for (offset = 0; offset < array->length; ++offset) { VALUE offs = INT2FIX(offset); VALUE val = rb_funcall(obj, rb_type_array_intern_aget, 1, offs); rb_funcall(type_array, rb_type_array_intern_aset, 2, offs, val); } } else if (rb_respond_to(obj, rb_type_array_intern_superclass) && (rb_funcall(obj, rb_type_array_intern_superclass, 0) == rb_cStructType)) { array->struct_type = obj; array->size = FIX2ULONG(rb_const_get(obj, rb_intern("BYTES_PER_ELEMENT"))); if (!NIL_P(byte_offset)) { Check_Type(byte_offset, T_FIXNUM); array->byte_offset = FIX2ULONG(byte_offset); if (!rb_type_array_assert_alignment(array->byte_offset, array->size)) rb_raise(rb_eRangeError, "Byte offset is not aligned."); } if (!NIL_P(length)) { Check_Type(length, T_FIXNUM); array->length = FIX2ULONG(length); } else { array->length = 1; } array->byte_length = (array->size * array->length); array->buf = rb_alloc_array_buffer(array->byte_length, NULL); } else { rb_raise(rb_eTypeError, "TypeArray constructor %s not supported.", RSTRING_PTR(rb_obj_as_string(obj))); } rb_obj_call_init(type_array, 0, NULL); return type_array; }
// a macro helper to get the server pointer embeded in an object inline static char *get_str(VALUE obj) { VALUE i = rb_ivar_get(obj, io_id); return (char *)FIX2ULONG(i); }
inline static size_t get_end(VALUE obj) { VALUE i = rb_ivar_get(obj, end_id); return (size_t)FIX2ULONG(i); }
inline static intptr_t get_uuid(VALUE obj) { VALUE i = rb_ivar_get(obj, fd_var_id); return (intptr_t)FIX2ULONG(i); }
VALUE na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv) { int ndim, ndim0; int row_major; int i, r; size_t j; size_t len; ssize_t beg, step; VALUE v; narray_t *na; size_t m; VALUE reduce; if (naryc<1) { rb_raise(rb_eRuntimeError,"must be positive: naryc=%d", naryc); } GetNArray(naryv[0],na); reduce = na->reduce; if (argc==0) { //printf("pass argc=0 reduce=%d\n",NUM2INT(reduce)); return reduce; } ndim = ndim0 = na->ndim; row_major = TEST_COLUMN_MAJOR(naryv[0]); for (i=1; i<naryc; i++) { GetNArray(naryv[i],na); if (TEST_COLUMN_MAJOR(naryv[i]) != row_major) { rb_raise(nary_eDimensionError,"dimension order is different"); } if (na->ndim > ndim) { ndim = na->ndim; } } if (ndim != ndim0) { j = FIX2ULONG(reduce) << (ndim-ndim0); reduce = ULONG2NUM(j); if (!FIXNUM_P(reduce)) { rb_raise(nary_eDimensionError,"reduce has too many bits"); } } //printf("argc=%d\n",argc); m = 0; reduce = Qnil; for (i=0; i<argc; i++) { v = argv[i]; //printf("argv[%d]=",i);rb_p(v); if (TYPE(v)==T_FIXNUM) { beg = FIX2INT(v); if (beg<0) beg+=ndim; if (beg>=ndim || beg<0) { rb_raise(nary_eDimensionError,"dimension is out of range"); } len = 1; step = 0; //printf("beg=%d step=%d len=%d\n",beg,step,len); } else if (rb_obj_is_kind_of(v,rb_cRange) || rb_obj_is_kind_of(v,na_cStep)) { nary_step_array_index( v, ndim, &len, &beg, &step ); } else { rb_raise(nary_eDimensionError, "invalid dimension argument %s", rb_obj_classname(v)); } for (j=0; j<len; j++) { r = beg + step*j; if (row_major) r = ndim-1-r; if (reduce==Qnil) { if ( r < (ssize_t)sizeof(size_t) ) { m |= ((size_t)1) << r; continue; } else { reduce = SIZE2NUM(m); } } v = rb_funcall( INT2FIX(1), rb_intern("<<"), 1, INT2FIX(r) ); reduce = rb_funcall( reduce, rb_intern("|"), 1, v ); } } if (reduce==Qnil) reduce = SIZE2NUM(m); return reduce; }
/* * invokeMethod */ static CMPIStatus invokeMethod( CMPIMethodMI* self, const CMPIContext* ctx, const CMPIResult* rslt, const CMPIObjectPath* objName, const char* method, const CMPIArgs* in, CMPIArgs* out) { Target_Type _ctx; Target_Type _objName; CMPIStatus status = {CMPI_RC_ERR_NOT_SUPPORTED, NULL}; _SBLIM_TRACE(1,("invokeMethod() called, ctx %p, rslt %p, objName %p, method %s, in %p, out %p", ctx, rslt, objName, method, in, out)); TARGET_THREAD_BEGIN_BLOCK; _ctx = SWIG_NewPointerObj((void*) ctx, SWIGTYPE_p__CMPIContext, 0); _objName = SWIG_NewPointerObj((void*) objName, SWIGTYPE_p__CMPIObjectPath, 0); /* Ruby style method invocation */ #if defined(SWIGRUBY) /* de-camelize method name, might need 2time length */ char *methodname = alloca(strlen(method) * 2 + 1); decamelize(method, methodname); /* access method arguments information via <decamelized>_args */ int argsnamesize = strlen(methodname) + 5 + 1; char *argsname = alloca(argsnamesize); /* "<name>_args" */ snprintf(argsname, argsnamesize, "%s_args", methodname); /* get the args array, gives names of input and output arguments */ VALUE args = rb_funcall(((ProviderMIHandle*)self->hdl)->implementation, rb_intern(argsname), 0); if (check_ruby_type(args, T_ARRAY, "invoke: <method>_args must be Array", &status, (ProviderMIHandle*)self->hdl ) < 0) return status; VALUE argsin = rb_ary_entry(args, 0); /* array of input arg names */ if (check_ruby_type(argsin, T_ARRAY, "invoke: Input arguments must be Array", &status, (ProviderMIHandle*)self->hdl ) < 0) return status; int number_of_arguments = RARRAY_LEN(argsin) / 2; _SBLIM_TRACE(1,("%s -> %d input args", argsname, number_of_arguments)); /* 3 args will be added by TargetCall, 2 args are added here, others are input args to function */ VALUE *input = alloca((3 + 2 + number_of_arguments) * sizeof(VALUE)); input[3] = _ctx; input[4] = _objName; /* loop over input arg names and types and get CMPIData via CMGetArg() */ int i; for (i = 0; i < number_of_arguments; ++i) { const char *argname; CMPIData data; argname = target_charptr(rb_ary_entry(argsin, i*2)); data = CMGetArg(in, argname, &status); if (status.rc != CMPI_RC_OK) { if ((data.state & CMPI_nullValue) ||(data.state & CMPI_notFound)) { input[5+i] = Target_Null; continue; } _SBLIM_TRACE(1,("Failed (rc %d) to get input arg %d:%s for %s", status.rc, i>>1, argname, method)); return status; } input[5+i] = data_value(&data); } /* actual provider call, passes output args and return value via 'result' */ VALUE result = TargetCall((ProviderMIHandle*)self->hdl, &status, methodname, -(2+number_of_arguments), input); /* argsout is array of [<return_type>, <output_arg_name>, <output_arg_type>, ... */ VALUE argsout = rb_ary_entry(args, 1); CMPIValue value; CMPIType expected_type; CMPIType actual_type; if (check_ruby_type(argsout, T_ARRAY, "invoke: Output arguments must be Array", &status, (ProviderMIHandle*)self->hdl) < 0) return status; number_of_arguments = (RARRAY_LEN(argsout) - 1); if (number_of_arguments > 0) { /* if output args are defined, result must be an array * result[0] is the return value * result[1..n] are the output args in argsout order */ if (check_ruby_type(result, T_ARRAY, "invoke: function with output arguments must return Array", &status, (ProviderMIHandle*)self->hdl) < 0) return status; /* loop over output arg names and types and set CMPIData via CMSetArg() */ for (i = 0; i < number_of_arguments; i += 2) { const char *argname; argname = target_charptr(rb_ary_entry(argsout, i+1)); expected_type = FIX2ULONG(rb_ary_entry(argsout, i+2)); actual_type = target_to_value(rb_ary_entry(result, (i >> 1) + 1), &value, expected_type); status = CMAddArg(out, argname, &value, actual_type); if (status.rc != CMPI_RC_OK) { _SBLIM_TRACE(1,("Failed (rc %d) to set output arg %d:%s for %s; expected type %x, actual type %x", status.rc, i>>1, argname, method, expected_type, actual_type)); return status; } } /* result[0] is the return value */ result = rb_ary_entry(result, 0); }
for (i = 0; i < number_of_arguments; i += 2) { const char *argname; argname = target_charptr(rb_ary_entry(argsout, i+1)); expected_type = FIX2ULONG(rb_ary_entry(argsout, i+2)); actual_type = target_to_value(rb_ary_entry(result, (i >> 1) + 1), &value, expected_type); status = CMAddArg(out, argname, &value, actual_type); if (status.rc != CMPI_RC_OK) { _SBLIM_TRACE(1,("Failed (rc %d) to set output arg %d:%s for %s; expected type %x, actual type %x", status.rc, i>>1, argname, method, expected_type, actual_type)); return status; } } /* result[0] is the return value */ result = rb_ary_entry(result, 0); } expected_type = FIX2ULONG(rb_ary_entry(argsout, 0)); actual_type = target_to_value(result, &value, expected_type); CMReturnData(rslt, &value, actual_type); CMReturnDone(rslt); #else Target_Type _rslt; Target_Type _in; Target_Type _out; _rslt = SWIG_NewPointerObj((void*) rslt, SWIGTYPE_p__CMPIResult, 0); _in = SWIG_NewPointerObj((void*) in, SWIGTYPE_p__CMPIArgs, 0); _out = SWIG_NewPointerObj((void*) out, SWIGTYPE_p__CMPIArgs, 0); Target_Type _method; _method = string2target(method); TargetCall((ProviderMIHandle*)self->hdl, &status, "invoke_method", 6, _ctx,
VALUE image_open(VALUE self, VALUE filename_location, VALUE disk_type_flag) { char * filename; int dtype; struct tsk4r_img_wrapper * ptr; Data_Get_Struct(self, struct tsk4r_img_wrapper, ptr); VALUE img_size; VALUE img_sector_size; VALUE description = Qnil; VALUE name = Qnil; dtype = FIX2ULONG(disk_type_flag); TSK_IMG_TYPE_ENUM * type_flag_num = get_img_flag(disk_type_flag); if (rb_obj_is_kind_of(filename_location, rb_cString)) { fprintf(stdout, "opening %s. (flag=%d)\n", StringValuePtr(filename_location), dtype); rb_str_modify(filename_location); filename=StringValuePtr(filename_location); ptr->image = tsk_img_open_sing(filename, (TSK_IMG_TYPE_ENUM)type_flag_num, 0); // 0=default sector size if (ptr->image == NULL) rb_warn("unable to open image %s.\n", StringValuePtr(filename_location)); } else if (rb_obj_is_kind_of(filename_location, rb_cArray)) { long i; typedef TSK_TCHAR * split_list; split_list images[255]; // to do: make array length reflect list's length for (i=0; i < RARRAY_LEN(filename_location); i++) { VALUE rstring = rb_ary_entry(filename_location, i); images[i] = StringValuePtr(rstring); } int count = (int)RARRAY_LEN(filename_location); ptr->image = tsk_img_open(count, (const TSK_TCHAR **)images, (TSK_IMG_TYPE_ENUM)type_flag_num, 0); // 0=default sector size VALUE arr_to_s = rb_funcall(filename_location, rb_intern("to_s"), 0, NULL); if (ptr->image == NULL) rb_warn("unable to open images %s.\n", StringValuePtr(arr_to_s)); } else { rb_raise(rb_eArgError, "Arg1 should be String or Array of strings."); } if (ptr->image == NULL) { rb_funcall(self, rb_intern("taint"), 0, NULL); return Qnil; } else { TSK_IMG_INFO *image = ptr->image; img_size = LONG2NUM(image->size); img_sector_size = INT2NUM((int)image->sector_size); TSK_IMG_TYPE_ENUM typenum = image->itype; description = image_type_to_desc(self, INT2NUM(typenum)); name = image_type_to_name(self, INT2NUM(typenum)); rb_iv_set(self, "@size", img_size); rb_iv_set(self, "@sector_size", img_sector_size); rb_iv_set(self, "@type", INT2NUM((int)typenum)); rb_iv_set(self, "@description", description); rb_iv_set(self, "@name", name); return self; } }
void rstatvfs2statvfs(VALUE rstatvfs,struct statvfs *statvfsbuf) { statvfsbuf->f_bsize = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_bsize"),0)); statvfsbuf->f_frsize = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_frsize"),0)); statvfsbuf->f_blocks = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_blocks"),0)); statvfsbuf->f_bfree = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_bfree"),0)); statvfsbuf->f_bavail = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_bavail"),0)); statvfsbuf->f_files = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_files"),0)); statvfsbuf->f_ffree = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_ffree"),0)); statvfsbuf->f_favail = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_favail"),0)); statvfsbuf->f_fsid = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_fsid"),0)); statvfsbuf->f_flag = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_flag"),0)); statvfsbuf->f_namemax = FIX2ULONG(rb_funcall(rstatvfs,rb_intern("f_namemax"),0)); }