static VALUE nst_allocate(VALUE self) { narray_t *na; char *ptr; VALUE velmsz; GetNArray(self,na); switch(NA_TYPE(na)) { case NARRAY_DATA_T: ptr = NA_DATA_PTR(na); if (na->size > 0 && ptr == NULL) { velmsz = rb_const_get(CLASS_OF(self), rb_intern("element_byte_size")); ptr = xmalloc(NUM2SIZET(velmsz) * na->size); NA_DATA_PTR(na) = ptr; } break; case NARRAY_VIEW_T: rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0); break; case NARRAY_FILEMAP_T: //ptr = ((narray_filemap_t*)na)->ptr; // to be implemented default: rb_bug("invalid narray type : %d",NA_TYPE(na)); } return self; }
char * na_get_pointer_for_write(VALUE self) { char *ptr; narray_t *na; GetNArray(self,na); if (OBJ_FROZEN(self)) { rb_raise(rb_eRuntimeError, "cannot write to frozen NArray."); } if (NA_TYPE(na) == NARRAY_DATA_T) { ptr = NA_DATA_PTR(na); if (na->size > 0 && ptr == NULL) { rb_funcall(self, id_allocate, 0); ptr = NA_DATA_PTR(na); } } else { ptr = na_get_pointer(self); if (NA_SIZE(na) > 0 && ptr == NULL) { rb_raise(rb_eRuntimeError,"cannot write to unallocated NArray"); } } //NA_SET_LOCK(na); return ptr; }
char * na_get_pointer_for_read(VALUE self) { char *ptr; narray_t *na; GetNArray(self,na); //if (NA_TEST_LOCK(na)) { // rb_raise(rb_eRuntimeError, "cannot read locked NArray."); //} if (NA_TYPE(na) == NARRAY_DATA_T) { ptr = NA_DATA_PTR(na); } else { ptr = na_get_pointer(self); } if (NA_SIZE(na) > 0 && ptr == NULL) { rb_raise(rb_eRuntimeError,"cannot read unallocated NArray"); } //NA_SET_LOCK(na); return ptr; }
char * na_get_pointer(VALUE self) { narray_t *na; GetNArray(self,na); switch(NA_TYPE(na)) { case NARRAY_DATA_T: return NA_DATA_PTR(na); case NARRAY_FILEMAP_T: return ((narray_filemap_t*)na)->ptr; case NARRAY_VIEW_T: return na_get_pointer(NA_VIEW_DATA(na)); //puts("pass NARRAY_VIEW_T in na_get_pointer_for_write"); //ptr += ((narray_view_t*)na)->offset; //ptr += NA_VIEW_OFFSET(na); break; default: rb_raise(rb_eRuntimeError,"invalid NA_TYPE"); } return NULL; }