Skip to content

Commit 53bc8d6

Browse files
authored
replace m_new with m_new0, wherever reasonable (v923z#521)
replace m_new with m_new0, wherever reasonable, and remove dangling memory fragments created by m_new0
1 parent da33f62 commit 53bc8d6

File tree

10 files changed

+94
-104
lines changed

10 files changed

+94
-104
lines changed

code/ndarray.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ void ndarray_fill_array_iterable(mp_float_t *array, mp_obj_t iterable) {
163163

164164
#if ULAB_HAS_FUNCTION_ITERATOR
165165
size_t *ndarray_new_coords(uint8_t ndim) {
166-
size_t *coords = m_new(size_t, ndim);
167-
memset(coords, 0, ndim*sizeof(size_t));
166+
size_t *coords = m_new0(size_t, ndim);
168167
return coords;
169168
}
170169

@@ -618,10 +617,9 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
618617

619618
// if the length is 0, still allocate a single item, so that contractions can be handled
620619
size_t len = ndarray->itemsize * MAX(1, ndarray->len);
621-
uint8_t *array = m_new(byte, len);
620+
uint8_t *array = m_new0(byte, len);
622621
// this should set all elements to 0, irrespective of the of the dtype (all bits are zero)
623622
// we could, perhaps, leave this step out, and initialise the array only, when needed
624-
memset(array, 0, len);
625623
ndarray->array = array;
626624
ndarray->origin = array;
627625
return ndarray;
@@ -1060,8 +1058,7 @@ bool ndarray_can_broadcast(ndarray_obj_t *lhs, ndarray_obj_t *rhs, uint8_t *ndim
10601058
//
10611059
// 1. the two shapes are either equal
10621060
// 2. one of the shapes is 1
1063-
memset(lstrides, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1064-
memset(rstrides, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1061+
10651062
lstrides[ULAB_MAX_DIMS - 1] = lhs->strides[ULAB_MAX_DIMS - 1];
10661063
rstrides[ULAB_MAX_DIMS - 1] = rhs->strides[ULAB_MAX_DIMS - 1];
10671064
for(uint8_t i=ULAB_MAX_DIMS; i > 0; i--) {
@@ -1094,7 +1091,7 @@ bool ndarray_can_broadcast_inplace(ndarray_obj_t *lhs, ndarray_obj_t *rhs, int32
10941091
//
10951092
// 1. the two shapes are either equal
10961093
// 2. the shapes on the right hand side is 1
1097-
memset(rstrides, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1094+
10981095
rstrides[ULAB_MAX_DIMS - 1] = rhs->strides[ULAB_MAX_DIMS - 1];
10991096
for(uint8_t i=ULAB_MAX_DIMS; i > 0; i--) {
11001097
if((lhs->shape[i-1] == rhs->shape[i-1]) || (rhs->shape[i-1] == 0) || (rhs->shape[i-1] == 1)) {
@@ -1142,10 +1139,8 @@ static mp_bound_slice_t generate_slice(mp_int_t n, mp_obj_t index) {
11421139
}
11431140

11441141
static ndarray_obj_t *ndarray_view_from_slices(ndarray_obj_t *ndarray, mp_obj_tuple_t *tuple) {
1145-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1146-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1147-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
1148-
memset(strides, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1142+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
1143+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
11491144

11501145
uint8_t ndim = ndarray->ndim;
11511146

@@ -1187,9 +1182,9 @@ void ndarray_assign_view(ndarray_obj_t *view, ndarray_obj_t *values) {
11871182
return;
11881183
}
11891184
uint8_t ndim = 0;
1190-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1191-
int32_t *lstrides = m_new(int32_t, ULAB_MAX_DIMS);
1192-
int32_t *rstrides = m_new(int32_t, ULAB_MAX_DIMS);
1185+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
1186+
int32_t *lstrides = m_new0(int32_t, ULAB_MAX_DIMS);
1187+
int32_t *rstrides = m_new0(int32_t, ULAB_MAX_DIMS);
11931188
if(!ndarray_can_broadcast(view, values, &ndim, shape, lstrides, rstrides)) {
11941189
mp_raise_ValueError(translate("operands could not be broadcast together"));
11951190
m_del(size_t, shape, ULAB_MAX_DIMS);
@@ -1846,9 +1841,9 @@ mp_obj_t ndarray_binary_op(mp_binary_op_t _op, mp_obj_t lobj, mp_obj_t robj) {
18461841
}
18471842

18481843
uint8_t ndim = 0;
1849-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1850-
int32_t *lstrides = m_new(int32_t, ULAB_MAX_DIMS);
1851-
int32_t *rstrides = m_new(int32_t, ULAB_MAX_DIMS);
1844+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
1845+
int32_t *lstrides = m_new0(int32_t, ULAB_MAX_DIMS);
1846+
int32_t *rstrides = m_new0(int32_t, ULAB_MAX_DIMS);
18521847
uint8_t broadcastable;
18531848
if((op == MP_BINARY_OP_INPLACE_ADD) || (op == MP_BINARY_OP_INPLACE_MULTIPLY) || (op == MP_BINARY_OP_INPLACE_POWER) ||
18541849
(op == MP_BINARY_OP_INPLACE_SUBTRACT) || (op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE)) {
@@ -2157,8 +2152,8 @@ mp_obj_t ndarray_reshape_core(mp_obj_t oin, mp_obj_t _shape, bool inplace) {
21572152
if(shape->len > ULAB_MAX_DIMS) {
21582153
mp_raise_ValueError(translate("maximum number of dimensions is 4"));
21592154
}
2160-
size_t *new_shape = m_new(size_t, ULAB_MAX_DIMS);
2161-
memset(new_shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
2155+
size_t *new_shape = m_new0(size_t, ULAB_MAX_DIMS);
2156+
21622157
size_t new_length = 1;
21632158
for(uint8_t i=0; i < shape->len; i++) {
21642159
new_shape[ULAB_MAX_DIMS - i - 1] = mp_obj_get_int(shape->items[shape->len - i - 1]);

code/numpy/create.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ static mp_obj_t create_zeros_ones_full(mp_obj_t oshape, uint8_t dtype, mp_obj_t
3535
if(len > ULAB_MAX_DIMS) {
3636
mp_raise_TypeError(translate("too many dimensions"));
3737
}
38-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
39-
memset(shape, 0, ULAB_MAX_DIMS * sizeof(size_t));
38+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
39+
4040
size_t i = 0;
4141
mp_obj_iter_buf_t iter_buf;
4242
mp_obj_t item, iterable = mp_getiter(oshape, &iter_buf);
@@ -245,8 +245,7 @@ mp_obj_t create_concatenate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
245245
mp_raise_TypeError(translate("first argument must be a tuple of ndarrays"));
246246
}
247247
int8_t axis = (int8_t)args[1].u_int;
248-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
249-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
248+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
250249
mp_obj_tuple_t *ndarrays = MP_OBJ_TO_PTR(args[0].u_obj);
251250

252251
// first check, whether the arrays are compatible

code/numpy/io/io.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ static mp_obj_t io_load(mp_obj_t file) {
131131

132132
io_read_(stream, stream_p, buffer, "', 'fortran_order': False, 'shape': (", 37, &error);
133133

134-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
135-
memset(shape, 0, sizeof(size_t) * ULAB_MAX_DIMS);
134+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
136135

137136
uint16_t bytes_to_read = MIN(ULAB_IO_BUFFER_SIZE, header_length - 51);
138137
// bytes_to_read is 128 at most. This should be enough to contain a
@@ -230,6 +229,8 @@ static mp_obj_t io_load(mp_obj_t file) {
230229
m_del(char, tmpbuff, sz);
231230
}
232231

232+
m_del(size_t, shape, ULAB_MAX_DIMS);
233+
233234
return MP_OBJ_FROM_PTR(ndarray);
234235
}
235236

@@ -386,8 +387,7 @@ static mp_obj_t io_loadtxt(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
386387
mp_raise_ValueError(translate("usecols is too high"));
387388
}
388389

389-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
390-
memset(shape, 0, sizeof(size_t) * ULAB_MAX_DIMS);
390+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
391391

392392
#if ULAB_MAX_DIMS == 1
393393
shape[0] = rows;
@@ -480,6 +480,8 @@ static mp_obj_t io_loadtxt(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
480480
} while((read > 0) && (rows < max_rows));
481481

482482
stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
483+
484+
m_del(size_t, shape, ULAB_MAX_DIMS);
483485
m_del(char, buffer, ULAB_IO_BUFFER_SIZE);
484486
m_del(char, clipboard, ULAB_IO_CLIPBOARD_SIZE);
485487
m_del(uint16_t, cols, used_columns);

code/numpy/linalg/linalg_tools.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#include "linalg_tools.h"
1616

17-
/*
18-
* The following function inverts a matrix, whose entries are given in the input array
17+
/*
18+
* The following function inverts a matrix, whose entries are given in the input array
1919
* The function has no dependencies beyond micropython itself (for the definition of mp_float_t),
2020
* and can be used independent of ulab.
2121
*/
@@ -26,10 +26,9 @@ bool linalg_invert_matrix(mp_float_t *data, size_t N) {
2626

2727
// initially, this is the unit matrix: the contents of this matrix is what
2828
// will be returned after all the transformations
29-
mp_float_t *unit = m_new(mp_float_t, N*N);
29+
mp_float_t *unit = m_new0(mp_float_t, N*N);
3030
mp_float_t elem = 1.0;
31-
// initialise the unit matrix
32-
memset(unit, 0, sizeof(mp_float_t)*N*N);
31+
3332
for(size_t m=0; m < N; m++) {
3433
memcpy(&unit[m * (N+1)], &elem, sizeof(mp_float_t));
3534
}
@@ -78,9 +77,9 @@ bool linalg_invert_matrix(mp_float_t *data, size_t N) {
7877
return true;
7978
}
8079

81-
/*
82-
* The following function calculates the eigenvalues and eigenvectors of a symmetric
83-
* real matrix, whose entries are given in the input array.
80+
/*
81+
* The following function calculates the eigenvalues and eigenvectors of a symmetric
82+
* real matrix, whose entries are given in the input array.
8483
* The function has no dependencies beyond micropython itself (for the definition of mp_float_t),
8584
* and can be used independent of ulab.
8685
*/
@@ -166,6 +165,6 @@ size_t linalg_jacobi_rotations(mp_float_t *array, mp_float_t *eigvectors, size_t
166165
eigvectors[m * S + N] = s * vm + c * vn;
167166
}
168167
} while(iterations > 0);
169-
168+
170169
return iterations;
171170
}

code/numpy/numerical.c

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,9 @@ static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t
515515
int8_t ax = tools_get_axis(axis, ndarray->ndim);
516516

517517
uint8_t *array = (uint8_t *)ndarray->array;
518-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
519-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
520-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
521-
memset(strides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
518+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
519+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
520+
522521
numerical_reduce_axes(ndarray, ax, shape, strides);
523522
uint8_t index = ULAB_MAX_DIMS - ndarray->ndim + ax;
524523

@@ -543,6 +542,9 @@ static mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t
543542
} else {
544543
RUN_ARGMIN(ndarray, mp_float_t, array, results, rarray, shape, strides, index, optype);
545544
}
545+
546+
m_del(int32_t, strides, ULAB_MAX_DIMS);
547+
546548
if(results->len == 1) {
547549
return mp_binary_get_val_array(results->dtype, results->array, 0);
548550
}
@@ -636,10 +638,9 @@ static mp_obj_t numerical_sort_helper(mp_obj_t oin, mp_obj_t axis, uint8_t inpla
636638
ax = tools_get_axis(axis, ndarray->ndim);
637639
}
638640

639-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
640-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
641-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
642-
memset(strides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
641+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
642+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
643+
643644
numerical_reduce_axes(ndarray, ax, shape, strides);
644645
ax = ULAB_MAX_DIMS - ndarray->ndim + ax;
645646
// we work with the typed array, so re-scale the stride
@@ -655,6 +656,9 @@ static mp_obj_t numerical_sort_helper(mp_obj_t oin, mp_obj_t axis, uint8_t inpla
655656
HEAPSORT(ndarray, mp_float_t, array, shape, strides, ax, increment, ndarray->shape[ax]);
656657
}
657658
}
659+
660+
m_del(int32_t, strides, ULAB_MAX_DIMS);
661+
658662
if(inplace == 1) {
659663
return mp_const_none;
660664
} else {
@@ -733,17 +737,15 @@ mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
733737
}
734738
int8_t ax = tools_get_axis(args[1].u_obj, ndarray->ndim);
735739

736-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
737-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
738-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
739-
memset(strides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
740+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
741+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
740742
numerical_reduce_axes(ndarray, ax, shape, strides);
741743

742744
// We could return an NDARRAY_UINT8 array, if all lengths are shorter than 256
743745
ndarray_obj_t *indices = ndarray_new_ndarray(ndarray->ndim, ndarray->shape, NULL, NDARRAY_UINT16);
744-
int32_t *istrides = m_new(int32_t, ULAB_MAX_DIMS);
745-
memset(istrides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
746+
int32_t *istrides = m_new0(int32_t, ULAB_MAX_DIMS);
746747
numerical_reduce_axes(indices, ax, shape, istrides);
748+
747749
for(uint8_t i=0; i < ULAB_MAX_DIMS; i++) {
748750
istrides[i] /= sizeof(uint16_t);
749751
}
@@ -804,6 +806,11 @@ mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
804806
HEAP_ARGSORT(ndarray, mp_float_t, array, shape, strides, ax, increment, ndarray->shape[ax], iarray, istrides, iincrement);
805807
}
806808
}
809+
810+
m_del(size_t, shape, ULAB_MAX_DIMS);
811+
m_del(int32_t, strides, ULAB_MAX_DIMS);
812+
m_del(int32_t, istrides, ULAB_MAX_DIMS);
813+
807814
return MP_OBJ_FROM_PTR(indices);
808815
}
809816

@@ -931,13 +938,12 @@ mp_obj_t numerical_diff(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
931938

932939
int8_t *stencil = m_new(int8_t, N+1);
933940
stencil[0] = 1;
934-
for(uint8_t i=1; i < N+1; i++) {
941+
for(uint8_t i = 1; i < N+1; i++) {
935942
stencil[i] = -stencil[i-1]*(N-i+1)/i;
936943
}
937944

938-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
939-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
940-
for(uint8_t i=0; i < ULAB_MAX_DIMS; i++) {
945+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
946+
for(uint8_t i = 0; i < ULAB_MAX_DIMS; i++) {
941947
shape[i] = ndarray->shape[i];
942948
if(i == index) {
943949
shape[i] -= N;
@@ -948,8 +954,7 @@ mp_obj_t numerical_diff(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
948954
uint8_t *rarray = (uint8_t *)results->array;
949955

950956
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
951-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
952-
memset(strides, 0, sizeof(int32_t)*ULAB_MAX_DIMS);
957+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
953958
numerical_reduce_axes(ndarray, ax, shape, strides);
954959

955960
if(ndarray->dtype == NDARRAY_UINT8) {
@@ -1083,13 +1088,14 @@ mp_obj_t numerical_median(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
10831088
} else {
10841089
int8_t ax = tools_get_axis(args[1].u_obj, ndarray->ndim);
10851090

1086-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1087-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1088-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
1089-
memset(strides, 0, sizeof(uint32_t)*ULAB_MAX_DIMS);
1091+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
1092+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
10901093
numerical_reduce_axes(ndarray, ax, shape, strides);
1094+
10911095
ax = ULAB_MAX_DIMS - ndarray->ndim + ax;
10921096
ndarray_obj_t *results = ndarray_new_dense_ndarray(ndarray->ndim-1, shape, NDARRAY_FLOAT);
1097+
m_del(size_t, shape, ULAB_MAX_DIMS);
1098+
10931099
mp_float_t *rarray = (mp_float_t *)results->array;
10941100

10951101
uint8_t *array = (uint8_t *)ndarray->array;
@@ -1237,16 +1243,12 @@ mp_obj_t numerical_roll(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
12371243
} else if(mp_obj_is_int(args[2].u_obj)){
12381244
int8_t ax = tools_get_axis(args[2].u_obj, ndarray->ndim);
12391245

1240-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
1241-
memset(shape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1242-
int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
1243-
memset(strides, 0, sizeof(int32_t)*ULAB_MAX_DIMS);
1246+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
1247+
int32_t *strides = m_new0(int32_t, ULAB_MAX_DIMS);
12441248
numerical_reduce_axes(ndarray, ax, shape, strides);
12451249

1246-
size_t *rshape = m_new(size_t, ULAB_MAX_DIMS);
1247-
memset(rshape, 0, sizeof(size_t)*ULAB_MAX_DIMS);
1248-
int32_t *rstrides = m_new(int32_t, ULAB_MAX_DIMS);
1249-
memset(rstrides, 0, sizeof(int32_t)*ULAB_MAX_DIMS);
1250+
size_t *rshape = m_new0(size_t, ULAB_MAX_DIMS);
1251+
int32_t *rstrides = m_new0(int32_t, ULAB_MAX_DIMS);
12501252
numerical_reduce_axes(results, ax, rshape, rstrides);
12511253

12521254
ax = ULAB_MAX_DIMS - ndarray->ndim + ax;
@@ -1307,9 +1309,16 @@ mp_obj_t numerical_roll(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
13071309
i++;
13081310
} while(i < shape[ULAB_MAX_DIMS - 3]);
13091311
#endif
1312+
1313+
m_del(size_t, shape, ULAB_MAX_DIMS);
1314+
m_del(int32_t, strides, ULAB_MAX_DIMS);
1315+
m_del(size_t, rshape, ULAB_MAX_DIMS);
1316+
m_del(int32_t, rstrides, ULAB_MAX_DIMS);
1317+
13101318
} else {
13111319
mp_raise_TypeError(translate("wrong axis index"));
13121320
}
1321+
13131322
return results;
13141323
}
13151324

0 commit comments

Comments
 (0)