Skip to content

Commit fb1b5ba

Browse files
authored
Merge pull request #85 from ngoldbaum/alloc-refactor
Use PyMem allocators
2 parents 018ab87 + 58ac555 commit fb1b5ba

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

asciidtype/tests/test_asciidtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_casting_safety():
157157
def test_unicode_to_ascii_to_unicode():
158158
arr = np.array(["hello", "this", "is", "an", "array"])
159159
ascii_arr = arr.astype(ASCIIDType(5))
160-
for dtype in ["U5", np.unicode_, np.str_]:
160+
for dtype in ["U5", np.str_]:
161161
round_trip_arr = ascii_arr.astype(dtype)
162162
np.testing.assert_array_equal(arr, round_trip_arr)
163163

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;

stringdtype/stringdtype/src/static_string.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ ssnewlen(const char *init, size_t len, ss *to_init);
2222
void
2323
ssfree(ss *str);
2424

25-
// copies the contents out *in* into *out*. Allocates a new string buffer for
26-
// *out*. Returns -1 if malloc fails and -2 if *out* is not empty. Returns 0 on
27-
// success.
25+
// Copies the contents of *in* into *out*. Allocates a new string buffer for
26+
// *out* and assumes that *out* is uninitialized. Returns -1 if malloc fails
27+
// and -2 if *out* is not initialized. ssfree *must* be called before this is
28+
// called if *in* points to an existing string. Returns 0 on success.
2829
int
2930
ssdup(const ss *in, ss *out);
3031

stringdtype/tests/test_char.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def string_array():
1414

1515
@pytest.fixture
1616
def unicode_array():
17-
return np.array(TEST_DATA, dtype=np.unicode_)
17+
return np.array(TEST_DATA, dtype=np.str_)
1818

1919

2020
UNARY_FUNCTIONS = [

stringdtype/tests/test_stringdtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_scalars_string_conversion(data, dtype):
145145
],
146146
)
147147
def test_unicode_casts(dtype, strings):
148-
arr = np.array(strings, dtype=np.unicode_).astype(dtype)
148+
arr = np.array(strings, dtype=np.str_).astype(dtype)
149149
expected = np.array(strings, dtype=dtype)
150150
np.testing.assert_array_equal(arr, expected)
151151

0 commit comments

Comments
 (0)