Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: handle possible error for PyTraceMallocTrack #27567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 4 doc/source/reference/c-api/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4044,8 +4044,8 @@ Memory management

.. c:function:: char* PyDataMem_RENEW(void * ptr, size_t newbytes)

Macros to allocate, free, and reallocate memory. These macros are used
internally to create arrays.
Functions to allocate, free, and reallocate memory. These are used
internally to manage array data memory unless overridden.

.. c:function:: npy_intp* PyDimMem_NEW(int nd)

Expand Down
38 changes: 30 additions & 8 deletions 38 numpy/_core/src/multiarray/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ PyDataMem_NEW(size_t size)

assert(size != 0);
result = malloc(size);
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
if (ret == -1) {
free(result);
return NULL;
}
return result;
}

Expand All @@ -251,7 +255,11 @@ PyDataMem_NEW_ZEROED(size_t nmemb, size_t size)
void *result;

result = calloc(nmemb, size);
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size);
if (ret == -1) {
free(result);
return NULL;
}
return result;
}

Expand All @@ -276,7 +284,11 @@ PyDataMem_RENEW(void *ptr, size_t size)
assert(size != 0);
PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr);
result = realloc(ptr, size);
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
if (ret == -1) {
free(result);
return NULL;
}
return result;
}

Expand Down Expand Up @@ -360,7 +372,11 @@ PyDataMem_UserNEW(size_t size, PyObject *mem_handler)
}
assert(size != 0);
result = handler->allocator.malloc(handler->allocator.ctx, size);
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
if (ret == -1) {
handler->allocator.free(handler->allocator.ctx, result, size);
return NULL;
}
return result;
}

Expand All @@ -374,7 +390,11 @@ PyDataMem_UserNEW_ZEROED(size_t nmemb, size_t size, PyObject *mem_handler)
return NULL;
}
result = handler->allocator.calloc(handler->allocator.ctx, nmemb, size);
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size);
if (ret == -1) {
handler->allocator.free(handler->allocator.ctx, result, size);
return NULL;
}
return result;
}

Expand Down Expand Up @@ -404,11 +424,13 @@ PyDataMem_UserRENEW(void *ptr, size_t size, PyObject *mem_handler)
}

assert(size != 0);
PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr);
result = handler->allocator.realloc(handler->allocator.ctx, ptr, size);
if (result != ptr) {
PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr);
int ret = PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
if (ret == -1) {
handler->allocator.free(handler->allocator.ctx, result, size);
return NULL;
}
PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size);
return result;
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.