8000 Merge pull request #28032 from seberg/alloc-fixup · numpy/numpy@35b2c4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 35b2c4a

Browse files
authored
Merge pull request #28032 from seberg/alloc-fixup
BUG,MAINT: Fix size bug in new alloc helper and use it in one more place
2 parents 5a2da5e + 14f38e4 commit 35b2c4a

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

numpy/_core/src/multiarray/alloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static inline void
6161
_npy_init_workspace(
6262
void **buf, void *static_buf, size_t static_buf_size, size_t elsize, size_t size)
6363
{
64-
if (NPY_LIKELY(size <= static_buf_size / elsize)) {
64+
if (NPY_LIKELY(size <= static_buf_size)) {
6565
*buf = static_buf;
6666
}
6767
else {

numpy/_core/src/multiarray/common_dtype.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "numpy/npy_common.h"
88
#include "numpy/arrayobject.h"
99

10+
#include "alloc.h"
1011
#include "convert_datatype.h"
1112
#include "dtypemeta.h"
1213
#include "abstractdtypes.h"
@@ -211,19 +212,10 @@ PyArray_PromoteDTypeSequence(
211212
PyArray_DTypeMeta *result = NULL;
212213

213214
/* Copy dtypes so that we can reorder them (only allocate when many) */
214-
PyObject *_scratch_stack[NPY_MAXARGS];
215-
PyObject **_scratch_heap = NULL;
216-
PyArray_DTypeMeta **dtypes = (PyArray_DTypeMeta **)_scratch_stack;
217-
218-
if (length > NPY_MAXARGS) {
219-
_scratch_heap = PyMem_Malloc(length * sizeof(PyObject *));
220-
if (_scratch_heap == NULL) {
221-
PyErr_NoMemory();
222-
return NULL;
223-
}
224-
dtypes = (PyArray_DTypeMeta **)_scratch_heap;
215+
NPY_ALLOC_WORKSPACE(dtypes, PyArray_DTypeMeta *, 16, length);
216+
if (dtypes == NULL) {
217+
return NULL;
225218
}
226-
227219
memcpy(dtypes, dtypes_in, length * sizeof(PyObject *));
228220

229221
/*
@@ -311,6 +303,6 @@ PyArray_PromoteDTypeSequence(
311303
}
312304

313305
finish:
314-
PyMem_Free(_scratch_heap);
306+
npy_free_workspace(dtypes);
315307
return result;
316308
}

0 commit comments

Comments
 (0)
0