@@ -26,7 +26,6 @@ typedef union
26
26
27
27
static int get_number (ErlNifEnv * env , ERL_NIF_TERM term , double * dp );
28
28
static Matrix * alloc_matrix (ErlNifEnv * env , unsigned nrows , unsigned ncols );
29
- static Matrix * realloc_matrix (ErlNifEnv * env , Matrix * mx , unsigned nrows , unsigned ncols );
30
29
static void matrix_destr (ErlNifEnv * env , void * obj );
31
30
32
31
static ErlNifResourceType * resource_type = NULL ;
@@ -193,25 +192,32 @@ static ERL_NIF_TERM matrix_to_lists(ErlNifEnv* env, int argc, const ERL_NIF_TERM
193
192
194
193
static ERL_NIF_TERM append_to_matrix (ErlNifEnv * env , int argc , const ERL_NIF_TERM argv [])
195
194
{
196
- mx_t mx ;
197
- unsigned j ;
195
+ Matrix * mx_ret = NULL ;
196
+ unsigned j , it_i , it_j ;
198
197
unsigned nrows , ncols ;
199
198
ERL_NIF_TERM list , row , ret ;
199
+ mx_t mx ;
200
200
201
201
if (!enif_get_resource (env , argv [0 ], resource_type , & mx .vp )) return enif_make_badarg (env );
202
202
203
203
nrows = mx .p -> nrows ;
204
204
ncols = mx .p -> ncols ;
205
205
206
- mx .p = realloc_matrix (env , mx .p , nrows + 1 , ncols );
206
+ mx_ret = alloc_matrix (env , nrows + 1 , ncols );
207
+ for (it_i = 0 ; it_i < nrows ; it_i ++ ) {
208
+ for (it_j = 0 ; it_j < ncols ; it_j ++ ){
209
+ POS (mx_ret , it_i , it_j ) = POS (mx .p , it_i , it_j );
210
+ }
211
+ }
212
+
207
213
list = argv [1 ];
208
214
if (!enif_get_list_cell (env , list , & row , & list )) {
209
215
goto badarg ;
210
216
}
211
217
for (j = 0 ; j < ncols ; j ++ ) {
212
218
ERL_NIF_TERM v ;
213
219
if (!enif_get_list_cell (env , row , & v , & row ) ||
214
- !get_number (env , v , & POS (mx . p ,nrows ,j ))) {
220
+ !get_number (env , v , & POS (mx_ret ,nrows ,j ))) {
215
221
goto badarg ;
216
222
}
217
223
}
@@ -223,8 +229,8 @@ static ERL_NIF_TERM append_to_matrix(ErlNifEnv* env, int argc, const ERL_NIF_TER
223
229
goto badarg ;
224
230
}
225
231
226
- ret = enif_make_resource (env , mx . p );
227
- enif_release_resource (mx . p );
232
+ ret = enif_make_resource (env , mx_ret );
233
+ enif_release_resource (mx_ret );
228
234
return ret ;
229
235
230
236
badarg :
@@ -250,14 +256,6 @@ static Matrix* alloc_matrix(ErlNifEnv* env, unsigned nrows, unsigned ncols)
250
256
return mx ;
251
257
}
252
258
253
- static Matrix * realloc_matrix (ErlNifEnv * env , Matrix * mx , unsigned nrows , unsigned ncols )
254
- {
255
- mx -> nrows = nrows ;
256
- mx -> ncols = ncols ;
257
- mx -> data = enif_realloc (mx -> data , nrows * ncols * sizeof (double ));
258
- return mx ;
259
- }
260
-
261
259
static void matrix_destr (ErlNifEnv * env , void * obj )
262
260
{
263
261
Matrix * mx = (Matrix * ) obj ;
@@ -495,8 +493,14 @@ static ERL_NIF_TERM float64_tensor(ErlNifEnv *env, int argc, const ERL_NIF_TERM
495
493
}
496
494
}
497
495
498
- tensor = TF_NewTensor (TF_DOUBLE , dims , ndims , mx1 .p -> data , (size_alloc ) * sizeof (double ), tensor_deallocator , 0 );
496
+ double * data = enif_alloc ((mx1 .p -> nrows )* (mx1 .p -> ncols )* sizeof (double ));
497
+ for (i = 0 ; i < mx1 .p -> nrows ; i ++ ) {
498
+ for (j = 0 ; j < mx1 .p -> ncols ; j ++ ) {
499
+ data [(i )* (mx1 .p -> ncols ) + (j )] = (double ) POS (mx1 .p , i , j );
500
+ }
501
+ }
499
502
503
+ tensor = TF_NewTensor (TF_DOUBLE , dims , ndims , data , (size_alloc ) * sizeof (double ), tensor_deallocator , 0 );
500
504
}
501
505
502
506
memcpy ((void * ) tensor_resource_alloc , (void * ) & tensor , sizeof (TF_Tensor * ));
@@ -775,7 +779,12 @@ static ERL_NIF_TERM load_image_as_tensor(ErlNifEnv *env, int argc, const ERL_NIF
775
779
const int size_alloc = output_size * sizeof (unsigned char );
776
780
int64_t dims [3 ] = {width , height , num_pixels };
777
781
778
- tensor = TF_NewTensor (TF_UINT8 , dims , 3 , output , size_alloc , tensor_deallocator , 0 );
782
+ uint8_t * data = enif_alloc (output_size * sizeof (uint8_t ));
783
+ for (int it = 0 ; it < output_size ; it ++ ){
784
+ data [it ] = (uint8_t )output [it ];
785
+ }
786
+
787
+ tensor = TF_NewTensor (TF_UINT8 , dims , 3 , data , size_alloc , tensor_deallocator , 0 );
779
788
memcpy ((void * ) tensor_resource_alloc , (void * ) & tensor , sizeof (TF_Tensor * ));
780
789
ERL_NIF_TERM new_tensor = enif_make_resource (env , tensor_resource_alloc );
781
790
enif_release_resource (tensor_resource_alloc );
0 commit comments