Skip to content

Commit 8ab07ef

Browse files
committed
use PyMem allocators instead of malloc/free
1 parent 7a8528a commit 8ab07ef

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ get_cast_spec(const char *name, NPY_CASTING casting,
11711171
NPY_ARRAYMETHOD_FLAGS flags, PyArray_DTypeMeta **dtypes,
11721172
PyType_Slot *slots)
11731173
{
1174-
PyArrayMethod_Spec *ret = malloc(sizeof(PyArrayMethod_Spec));
1174+
PyArrayMethod_Spec *ret = PyMem_Malloc(sizeof(PyArrayMethod_Spec));
11751175

11761176
ret->name = name;
11771177
ret->nin = 1;
@@ -1187,7 +1187,7 @@ get_cast_spec(const char *name, NPY_CASTING casting,
11871187
PyArray_DTypeMeta **
11881188
get_dtypes(PyArray_DTypeMeta *dt1, PyArray_DTypeMeta *dt2)
11891189
{
1190-
PyArray_DTypeMeta **ret = malloc(2 * sizeof(PyArray_DTypeMeta *));
1190+
PyArray_DTypeMeta **ret = PyMem_Malloc(2 * sizeof(PyArray_DTypeMeta *));
11911191

11921192
ret[0] = dt1;
11931193
ret[1] = dt2;
@@ -1297,7 +1297,7 @@ get_casts()
12971297
dt2s_dtypes, dt2s_slots);
12981298

12991299
PyArrayMethod_Spec **casts =
1300-
malloc((num_casts + 1) * sizeof(PyArrayMethod_Spec *));
1300+
PyMem_Malloc((num_casts + 1) * sizeof(PyArrayMethod_Spec *));
13011301

13021302
int cast_i = 0;
13031303

stringdtype/stringdtype/src/dtype.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,12 @@ init_string_dtype(void)
788788
StringDType.base.singleton = singleton;
789789

790790
for (int i = 0; StringDType_casts[i] != NULL; i++) {
791-
free(StringDType_casts[i]->dtypes);
792-
free(StringDType_casts[i]);
791+
PyMem_Free(StringDType_casts[i]->dtypes);
792+
PyMem_Free(StringDType_casts[i]);
793793
}
794794

795+
PyMem_Free(StringDType_casts);
796+
795797
return 0;
796798
}
797799

stringdtype/stringdtype/src/static_string.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "Python.h"
2+
13
#include "static_string.h"
24

35
// defined this way so it has an in-memory representation that is distinct
@@ -16,7 +18,7 @@ ssnewlen(const char *init, size_t len, ss *to_init)
1618
return 0;
1719
}
1820

19-
char *ret_buf = (char *)malloc(sizeof(char) * len);
21+
char *ret_buf = (char *)PyMem_RawMalloc(sizeof(char) * len);
2022

2123
if (ret_buf == NULL) {
2224
return -1;
@@ -35,7 +37,7 @@ void
3537
ssfree(ss *str)
3638
{
3739
if (str->buf != NULL && str->buf != EMPTY_STRING.buf) {
38-
free(str->buf);
40+
PyMem_RawFree(str->buf);
3941
str->buf = NULL;
4042
}
4143
str->len = 0;
@@ -68,7 +70,7 @@ ssnewemptylen(size_t num_bytes, ss *out)
6870
return 0;
6971
}
7072

71-
char *buf = (char *)malloc(sizeof(char) * num_bytes);
73+
char *buf = (char *)PyMem_RawMalloc(sizeof(char) * num_bytes);
7274

7375
if (buf == NULL) {
7476
return -1;

0 commit comments

Comments
 (0)