diff options
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/functions.c b/functions.c index 46f9782..7c49554 100644 --- a/functions.c +++ b/functions.c @@ -145,7 +145,9 @@ runfunc(Function f, Array *left, Array *right) Datum *dfnres = evalline(code, nil, 0); popdfnframe(); - result = (*dfnres).array; /* TODO what if the evaluation failed */ + result = dfnres->array; /* TODO what if the evaluation failed */ + incarrayref(result); + freedatum(dfnres); }else if(f.type == FunctypePrim){ if(left){ fndyad d = dyadfunctiondefs[f.code]; @@ -525,7 +527,7 @@ fnMix(Array *right) for(j = 0; j < commonshape->size; j++) result->shape[i+j] = commonshape->intdata[j]; - int *index = malloc(sizeof(int) * commonrank); + int *index = emalloc(sizeof(int) * commonrank); int offset; for(i = 0; i < size/commonsize; i++){ Array *a = right->arraydata[i]; @@ -618,7 +620,7 @@ fnGradeUp(Array *right) int i,j; int len = right->shape[0]; - Array **elems = malloc(sizeof(Array *) * len); + Array **elems = emalloc(sizeof(Array *) * len); Array *index = mkscalarint(globalIO()); Array *order = allocarray(AtypeInt, 1, len); order->shape[0] = len; @@ -740,7 +742,7 @@ fnRavel(Array *right) { Array *res = duparray(right); res->rank = 1; - res->shape = realloc(res->shape, sizeof(int) * 1); + res->shape = erealloc(res->shape, sizeof(int) * 1); res->shape[0] = res->size; return res; } @@ -750,7 +752,7 @@ fnTable(Array *right) { Array *res = duparray(right); res->rank = 2; - res->shape = realloc(res->shape, sizeof(int) * 2); + res->shape = erealloc(res->shape, sizeof(int) * 2); res->shape[0] = right->rank ? res->shape[0] : 1; res->shape[1] = right->size / res->shape[0]; return res; @@ -814,8 +816,8 @@ fnTranspose(Array *right) res->shape[i] = right->shape[res->rank - 1 - i]; int from, to; - int *sizesFrom = malloc(sizeof(int) * right->rank); - int *sizesTo = malloc(sizeof(int) * right->rank); + int *sizesFrom = emalloc(sizeof(int) * right->rank); + int *sizesTo = emalloc(sizeof(int) * right->rank); int accFrom = 1, accTo = 1; for(int i = 0; i < right->rank; i++){ sizesFrom[i] = accFrom; @@ -1289,7 +1291,7 @@ fnTake(Array *left, Array *right) if(right->rank == 0){ right = duparray(right); right->rank = left->size; - right->shape = realloc(right->shape, sizeof(int) * right->rank); + right->shape = erealloc(right->shape, sizeof(int) * right->rank); for(i = 0; i < right->rank; i++) right->shape[i] = 1; }else @@ -1306,7 +1308,7 @@ fnTake(Array *left, Array *right) left->intdata[i] = old->intdata[i]; } - int *shape = malloc(sizeof(int) * left->size); + int *shape = emalloc(sizeof(int) * left->size); int size = 1; for(i = 0; i < left->size; i++){ int s = left->intdata[i]; @@ -1319,7 +1321,7 @@ fnTake(Array *left, Array *right) for(i = 0; i < right->rank; i++) result->shape[i] = shape[i]; - int *index = mallocz(sizeof(int) * left->size, 1); + int *index = emallocz(sizeof(int) * left->size, 1); int fromindex; for(i = 0; i < size; i++){ for(int j = left->size-1; index[j] == shape[j]; j--){ @@ -1378,7 +1380,7 @@ fnDrop(Array *left, Array *right) if(right->rank == 0){ right = duparray(right); right->rank = left->size; - right->shape = realloc(right->shape, sizeof(int) * right->rank); + right->shape = erealloc(right->shape, sizeof(int) * right->rank); for(i = 0; i < right->rank; i++) right->shape[i] = 1; }else @@ -1560,7 +1562,7 @@ fnIndex(Array *left, Array *right) for(i = 0; i < rank; i++) result->shape[i] = shape->intdata[i]; freearray(shape); - int *leftindex = mallocz(sizeof(int) * right->rank, 1); + int *leftindex = emallocz(sizeof(int) * right->rank, 1); for(i = 0; i < result->size; i++){ for(int j = left->size-1; leftindex[j] == left->arraydata[j]->size; j--){ leftindex[j] = 0; @@ -1925,7 +1927,7 @@ indexOfHelper(Array *left, Array *right, int interval) rightshape->intdata[rightshape->size-1-i] = right->shape[right->rank-1-i]; right = fnReshape(rightshape, right); - Array **lefts = malloc(sizeof(Array *) * n); + Array **lefts = emalloc(sizeof(Array *) * n); Array *index = mkscalarint(io); for(i = 0; i < n; i++){ index->intdata[0] = i+io; |