Skip to content

Commit 42c8ced

Browse files
committed
Merge pull request numpy#4177 from juliantaylor/objmalloc
ENH: replace a few small allocations with PyObject_Malloc
2 parents e58dc05 + 6834b3a commit 42c8ced

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

numpy/core/src/multiarray/arrayobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ static PyObject *
16781678
array_alloc(PyTypeObject *type, Py_ssize_t NPY_UNUSED(nitems))
16791679
{
16801680
/* nitems will always be 0 */
1681-
PyObject *obj = PyArray_malloc(type->tp_basicsize);
1681+
PyObject *obj = PyObject_Malloc(type->tp_basicsize);
16821682
PyObject_Init(obj, type);
16831683
return obj;
16841684
}

numpy/core/src/multiarray/multiarraymodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4017,7 +4017,7 @@ PyMODINIT_FUNC initmultiarray(void) {
40174017
if (!d) {
40184018
goto err;
40194019
}
4020-
PyArray_Type.tp_free = PyArray_free;
4020+
PyArray_Type.tp_free = PyObject_Free;
40214021
if (PyType_Ready(&PyArray_Type) < 0) {
40224022
return RETVAL;
40234023
}

numpy/core/src/multiarray/nditer_constr.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags,
194194

195195
/* Allocate memory for the iterator */
196196
iter = (NpyIter*)
197-
PyArray_malloc(NIT_SIZEOF_ITERATOR(itflags, ndim, nop));
197+
PyObject_Malloc(NIT_SIZEOF_ITERATOR(itflags, ndim, nop));
198198

199199
NPY_IT_TIME_POINT(c_malloc);
200200

@@ -217,7 +217,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags,
217217
flags,
218218
op_flags, op_itflags,
219219
&NIT_MASKOP(iter))) {
220-
PyArray_free(iter);
220+
PyObject_Free(iter);
221221
return NULL;
222222
}
223223
/* Set resetindex to zero as well (it's just after the resetdataptr) */
@@ -549,7 +549,7 @@ NpyIter_Copy(NpyIter *iter)
549549

550550
/* Allocate memory for the new iterator */
551551
size = NIT_SIZEOF_ITERATOR(itflags, ndim, nop);
552-
newiter = (NpyIter*)PyArray_malloc(size);
552+
newiter = (NpyIter*)PyObject_Malloc(size);
553553

554554
/* Copy the raw values to the new iterator */
555555
memcpy(newiter, iter, size);
@@ -664,9 +664,7 @@ NpyIter_Deallocate(NpyIter *iter)
664664
/* buffers */
665665
buffers = NBF_BUFFERS(bufferdata);
666666
for(iop = 0; iop < nop; ++iop, ++buffers) {
667-
if (*buffers) {
668-
PyArray_free(*buffers);
669-
}
667+
PyArray_free(*buffers);
670668
}
671669
/* read bufferdata */
672670
transferdata = NBF_READTRANSFERDATA(bufferdata);
@@ -691,7 +689,7 @@ NpyIter_Deallocate(NpyIter *iter)
691689
}
692690

693691
/* Deallocate the iterator memory */
694-
PyArray_free(iter);
692+
PyObject_Free(iter);
695693

696694
return NPY_SUCCEED;
697695
}

numpy/core/src/multiarray/scalartypes.c.src

+5-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ gentype_alloc(PyTypeObject *type, Py_ssize_t nitems)
111111
PyObject *obj;
112112
const size_t size = _PyObject_VAR_SIZE(type, nitems + 1);
113113

114-
obj = (PyObject *)PyArray_malloc(size);
114+
obj = (PyObject *)PyObject_Malloc(size);
115115
memset(obj, 0, size);
116116
if (type->tp_itemsize == 0) {
117117
PyObject_INIT(obj, type);
@@ -1540,7 +1540,7 @@ gentype_byteswap(PyObject *self, PyObject *args)
15401540

15411541
gentype_getreadbuf(self, 0, (void **)&data);
15421542
descr = PyArray_DescrFromScalar(self);
1543-
newmem = PyArray_malloc(descr->elsize);
1543+
newmem = PyObject_Malloc(descr->elsize);
15441544
if (newmem == NULL) {
15451545
Py_DECREF(descr);
15461546
return PyErr_NoMemory();
@@ -1549,7 +1549,7 @@ gentype_byteswap(PyObject *self, PyObject *args)
15491549
descr->f->copyswap(newmem, data, 1, NULL);
15501550
}
15511551
new = PyArray_Scalar(newmem, descr, NULL);
1552-
PyArray_free(newmem);
1552+
PyObject_Free(newmem);
15531553
Py_DECREF(descr);
15541554
return new;
15551555
}
@@ -3985,7 +3985,7 @@ initialize_numeric_types(void)
39853985
PyGenericArrType_Type.tp_getset = gentype_getsets;
39863986
PyGenericArrType_Type.tp_new = NULL;
39873987
PyGenericArrType_Type.tp_alloc = gentype_alloc;
3988-
PyGenericArrType_Type.tp_free = PyArray_free;
3988+
PyGenericArrType_Type.tp_free = PyObject_Free;
39893989
PyGenericArrType_Type.tp_repr = gentype_repr;
39903990
PyGenericArrType_Type.tp_str = gentype_str;
39913991
PyGenericArrType_Type.tp_richcompare = gentype_richcompare;
@@ -4009,7 +4009,7 @@ initialize_numeric_types(void)
40094009
PyBoolArrType_Type.tp_as_number->nb_index = (unaryfunc)bool_index;
40104010

40114011
PyStringArrType_Type.tp_alloc = NULL;
4012-
PyStringArrType_Type.tp_free = NULL;
4012+
PyStringArrType_Type.tp_free = PyObject_Free;
40134013

40144014
PyStringArrType_Type.tp_repr = stringtype_repr;
40154015
PyStringArrType_Type.tp_str = stringtype_str;

0 commit comments

Comments
 (0)