Skip to content

Commit 18b755a

Browse files
committed
removed seperate memory alloc for strides
1 parent b1b8724 commit 18b755a

3 files changed

Lines changed: 9 additions & 19 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14461446
if (stgdict->format == NULL)
14471447
goto error;
14481448
stgdict->ndim = itemdict->ndim + 1;
1449-
stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t) * stgdict->ndim);
1449+
stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t) * stgdict->ndim * 2);
14501450
if (stgdict->shape == NULL) {
14511451
PyErr_NoMemory();
14521452
goto error;
@@ -1456,11 +1456,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14561456
memmove(&stgdict->shape[1], itemdict->shape,
14571457
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
14581458
}
1459-
stgdict->strides = PyMem_Malloc(sizeof(Py_ssize_t) * stgdict->ndim);
1460-
if (stgdict->strides == NULL) {
1461-
PyErr_NoMemory();
1462-
goto error;
1463-
}
1459+
stgdict->strides = stgdict->shape + stgdict->ndim;
14641460
if (stgdict->ndim > 1) {
14651461
memmove(&stgdict->strides[1], itemdict->strides,
14661462
sizeof(Py_ssize_t) * (stgdict->ndim - 1));

Modules/_ctypes/ctypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ typedef struct {
208208
PyObject *checker;
209209
int flags; /* calling convention and such */
210210

211-
/* pep3118 fields, pointers neeed PyMem_Free */
211+
/* pep3118 fields, pointers need PyMem_Free */
212212
char *format;
213213
int ndim;
214214
Py_ssize_t *shape;
215-
Py_ssize_t *strides;
215+
Py_ssize_t *strides; /* offset from *shape, not necessary to PyMem_Free */
216216
/* Py_ssize_t *suboffsets; */ /* unused in ctypes */
217217

218218
} StgDictObject;

Modules/_ctypes/stgdict.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ PyCStgDict_dealloc(StgDictObject *self)
4545
PyCStgDict_clear(self);
4646
PyMem_Free(self->format);
4747
PyMem_Free(self->shape);
48-
PyMem_Free(self->strides);
4948
PyMem_Free(self->ffi_type_pointer.elements);
5049
PyDict_Type.tp_dealloc((PyObject *)self);
5150
}
@@ -77,7 +76,6 @@ PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
7776
dst->format = NULL;
7877
PyMem_Free(dst->shape);
7978
dst->shape = NULL;
80-
PyMem_Free(dst->strides);
8179
dst->strides = NULL;
8280
dst->ffi_type_pointer.elements = NULL;
8381

@@ -102,22 +100,18 @@ PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
102100
strcpy(dst->format, src->format);
103101
}
104102
if (src->shape) {
105-
dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
103+
dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim * 2);
106104
if (dst->shape == NULL) {
107105
PyErr_NoMemory();
108106
return -1;
109107
}
110108
memcpy(dst->shape, src->shape,
111109
sizeof(Py_ssize_t) * src->ndim);
112-
}
113-
if (src->strides) {
114-
dst->strides = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
115-
if (dst->strides == NULL) {
116-
PyErr_NoMemory();
117-
return -1;
110+
if(src->strides) {
111+
dst->strides = dst->shape + src->ndim;
112+
memcpy(dst->strides, src->strides,
113+
sizeof(Py_ssize_t) * src->ndim);
118114
}
119-
memcpy(dst->strides, src->strides,
120-
sizeof(Py_ssize_t) * src->ndim);
121115
}
122116

123117
if (src->ffi_type_pointer.elements == NULL)

0 commit comments

Comments
 (0)