ext/numo/narray/narray.c in numo-narray-0.9.1.2 vs ext/numo/narray/narray.c in numo-narray-0.9.1.3
- old
+ new
@@ -130,11 +130,11 @@
{
int i;
narray_t *na;
GetNArray(self,na);
- printf("%s:\n",rb_class2name(CLASS_OF(self)));
+ printf("%s:\n",rb_class2name(rb_obj_class(self)));
printf(" id = 0x%"PRI_VALUE_PREFIX"x\n", self);
printf(" type = %d\n", na->type);
printf(" flag = [%d,%d]\n", na->flag[0], na->flag[1]);
printf(" size = %"SZF"d\n", na->size);
printf(" ndim = %d\n", na->ndim);
@@ -726,10 +726,42 @@
}
return Qfalse;
}
+/*
+ Release memory for array data. Ignored for NArray-view.
+ This method is useful to free memory of referenced (i.e., GC does not work)
+ but unused NArray object.
+ @overload free
+*/
+static VALUE
+na_free(VALUE self)
+{
+ narray_t *na;
+ char *ptr;
+
+ GetNArray(self,na);
+
+ switch(NA_TYPE(na)) {
+ case NARRAY_DATA_T:
+ ptr = NA_DATA_PTR(na);
+ if (ptr != NULL) {
+ NA_DATA_PTR(na) = NULL;
+ xfree(ptr);
+ }
+ break;
+ case NARRAY_VIEW_T:
+ break;
+ case NARRAY_FILEMAP_T:
+ default:
+ rb_bug("invalid narray type : %d",NA_TYPE(na));
+ }
+ return self;
+}
+
+
/* method: shape() -- returns shape, array of the size of dimensions */
static VALUE
na_shape(VALUE self)
{
volatile VALUE v;
@@ -910,11 +942,11 @@
volatile VALUE view;
GetNArray(self,na);
nd = na->ndim;
- view = na_s_allocate_view(CLASS_OF(self));
+ view = na_s_allocate_view(rb_obj_class(self));
na_copy_flags(self, view);
GetNArrayView(view, na2);
na_setup_shape((narray_t*)na2, nd, na->shape);
@@ -963,11 +995,11 @@
* Expand the shape of an array. Insert a new axis with size=1
* at a given dimension.
* @param [Integer] dim dimension at which new axis is inserted.
* @return [Numo::NArray] result narray view.
*/
-VALUE
+static VALUE
na_expand_dims(VALUE self, VALUE vdim)
{
int i, j, nd, dim;
size_t *shape, *na_shape;
stridx_t *stridx, *na_stridx;
@@ -1022,11 +1054,11 @@
* call-seq:
* narray.reverse([dim0,dim1,..]) => narray
*
* Return reversed view along specified dimeinsion
*/
-VALUE
+static VALUE
nary_reverse(int argc, VALUE *argv, VALUE self)
{
int i, nd;
size_t j, n;
size_t offset;
@@ -1041,11 +1073,11 @@
reduce = na_reduce_dimension(argc, argv, 1, &self, 0, 0);
GetNArray(self,na);
nd = na->ndim;
- view = na_s_allocate_view(CLASS_OF(self));
+ view = na_s_allocate_view(rb_obj_class(self));
na_copy_flags(self, view);
GetNArrayView(view, na2);
na_setup_shape((narray_t*)na2, nd, na->shape);
@@ -1141,11 +1173,11 @@
static VALUE
nary_coerce(VALUE x, VALUE y)
{
VALUE type;
- type = numo_na_upcast(CLASS_OF(x), CLASS_OF(y));
+ type = numo_na_upcast(rb_obj_class(x), rb_obj_class(y));
y = rb_funcall(type,id_cast,1,y);
return rb_assoc_new(y , x);
}
@@ -1158,11 +1190,11 @@
{
VALUE velmsz;
narray_t *na;
GetNArray(self,na);
- velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size);
+ velmsz = rb_const_get(rb_obj_class(self), id_element_byte_size);
if (FIXNUM_P(velmsz)) {
return SIZET2NUM(NUM2SIZET(velmsz) * na->size);
}
return SIZET2NUM(ceil(NUM2DBL(velmsz) * na->size));
}
@@ -1281,11 +1313,11 @@
offset = 0;
}
GetNArray(self,na);
size = NA_SIZE(na);
- velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size);
+ velmsz = rb_const_get(rb_obj_class(self), id_element_byte_size);
if (FIXNUM_P(velmsz)) {
byte_size = size * NUM2SIZET(velmsz);
} else {
byte_size = ceil(size * NUM2DBL(velmsz));
}
@@ -1339,11 +1371,11 @@
a = rb_ary_new();
rb_ary_push(a, INT2FIX(1)); // version
rb_ary_push(a, na_shape(self));
rb_ary_push(a, INT2FIX(NA_FLAG0(self)));
- if (CLASS_OF(self) == numo_cRObject) {
+ if (rb_obj_class(self) == numo_cRObject) {
narray_t *na;
VALUE *ptr;
size_t offset=0;
GetNArray(self,na);
if (na->type == NARRAY_VIEW_T) {
@@ -1360,11 +1392,11 @@
}
RB_GC_GUARD(self);
return a;
}
-VALUE na_inplace( VALUE self );
+static VALUE na_inplace( VALUE self );
/*
Load marshal data.
@overload marshal_load(data)
@params [Array] Array containing marshal data.
@return [nil]
@@ -1385,11 +1417,11 @@
"(only version 1)", NUM2INT(RARRAY_AREF(a,0)));
}
na_initialize(self,RARRAY_AREF(a,1));
NA_FL0_SET(self,FIX2INT(RARRAY_AREF(a,2)));
v = RARRAY_AREF(a,3);
- if (CLASS_OF(self) == numo_cRObject) {
+ if (rb_obj_class(self) == numo_cRObject) {
narray_t *na;
char *ptr;
if (TYPE(v) != T_ARRAY) {
rb_raise(rb_eArgError,"RObject content should be array");
}
@@ -1607,22 +1639,22 @@
}
/*
Return true if column major.
*/
-VALUE na_column_major_p( VALUE self )
+static VALUE na_column_major_p( VALUE self )
{
if (TEST_COLUMN_MAJOR(self))
return Qtrue;
else
return Qfalse;
}
/*
Return true if row major.
*/
-VALUE na_row_major_p( VALUE self )
+static VALUE na_row_major_p( VALUE self )
{
if (TEST_ROW_MAJOR(self))
return Qtrue;
else
return Qfalse;
@@ -1630,21 +1662,21 @@
/*
Return true if byte swapped.
*/
-VALUE na_byte_swapped_p( VALUE self )
+static VALUE na_byte_swapped_p( VALUE self )
{
if (TEST_BYTE_SWAPPED(self))
return Qtrue;
return Qfalse;
}
/*
Return true if not byte swapped.
*/
-VALUE na_host_order_p( VALUE self )
+static VALUE na_host_order_p( VALUE self )
{
if (TEST_BYTE_SWAPPED(self))
return Qfalse;
return Qtrue;
}
@@ -1652,11 +1684,11 @@
/*
Returns view of narray with inplace flagged.
@return [Numo::NArray] view of narray with inplace flag.
*/
-VALUE na_inplace( VALUE self )
+static VALUE na_inplace( VALUE self )
{
VALUE view = self;
view = na_make_view(self);
SET_INPLACE(view);
return view;
@@ -1664,28 +1696,20 @@
/*
Set inplace flag to self.
@return [Numo::NArray] self
*/
-VALUE na_inplace_bang( VALUE self )
+static VALUE na_inplace_bang( VALUE self )
{
SET_INPLACE(self);
return self;
}
-VALUE na_inplace_store( VALUE self, VALUE val )
-{
- if (self==val)
- return self;
- else
- return na_store( self, val );
-}
-
/*
Return true if inplace flagged.
*/
-VALUE na_inplace_p( VALUE self )
+static VALUE na_inplace_p( VALUE self )
{
if (TEST_INPLACE(self))
return Qtrue;
else
return Qfalse;
@@ -1693,11 +1717,11 @@
/*
Unset inplace flag to self.
@return [Numo::NArray] self
*/
-VALUE na_out_of_place_bang( VALUE self )
+static VALUE na_out_of_place_bang( VALUE self )
{
UNSET_INPLACE(self);
return self;
}
@@ -1789,21 +1813,21 @@
i.e., both arrays have same shape and corresponding elements are equal.
@overload == other
@param [Object] other
@return [Boolean] true if self and other is equal.
*/
-VALUE
+static VALUE
na_equal(VALUE self, volatile VALUE other)
{
volatile VALUE vbool;
narray_t *na1, *na2;
int i;
GetNArray(self,na1);
if (!rb_obj_is_kind_of(other,cNArray)) {
- other = rb_funcall(CLASS_OF(self), id_cast, 1, other);
+ other = rb_funcall(rb_obj_class(self), id_cast, 1, other);
}
GetNArray(other,na2);
if (na1->ndim != na2->ndim) {
return Qfalse;
@@ -1873,9 +1897,10 @@
rb_define_alias (cNArray, "total","size");
rb_define_method(cNArray, "shape", na_shape, 0);
rb_define_method(cNArray, "ndim", na_ndim,0);
rb_define_alias (cNArray, "rank","ndim");
rb_define_method(cNArray, "empty?", na_empty_p, 0);
+ rb_define_method(cNArray, "free", na_free, 0);
rb_define_method(cNArray, "debug_info", nary_debug_info, 0);
rb_define_method(cNArray, "contiguous?", na_check_contiguous, 0);