Skip to content

Commit 7785ba9

Browse files
authored
ENH: Extern memory management to Cython (numpy#27630)
Add NumPy's memory management functionality to numpy.pxd so Cython users can leverage it.
1 parent 65d5b86 commit 7785ba9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

numpy/__init__.cython-30.pxd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,23 @@ cdef extern from "numpy/arrayobject.h":
757757
npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
758758
int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
759759

760+
# The memory handler functions require the NumPy 1.22 API
761+
# and may require defining NPY_TARGET_VERSION
762+
ctypedef struct PyDataMemAllocator:
763+
void *ctx
764+
void* (*malloc) (void *ctx, size_t size)
765+
void* (*calloc) (void *ctx, size_t nelem, size_t elsize)
766+
void* (*realloc) (void *ctx, void *ptr, size_t new_size)
767+
void (*free) (void *ctx, void *ptr, size_t size)
768+
769+
ctypedef struct PyDataMem_Handler:
770+
char* name
771+
npy_uint8 version
772+
PyDataMemAllocator allocator
773+
774+
object PyDataMem_SetHandler(object handler)
775+
object PyDataMem_GetHandler()
776+
760777
# additional datetime related functions are defined below
761778

762779

numpy/__init__.pxd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,23 @@ cdef extern from "numpy/arrayobject.h":
672672
npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
673673
int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
674674

675+
# The memory handler functions require the NumPy 1.22 API
676+
# and may require defining NPY_TARGET_VERSION
677+
ctypedef struct PyDataMemAllocator:
678+
void *ctx
679+
void* (*malloc) (void *ctx, size_t size)
680+
void* (*calloc) (void *ctx, size_t nelem, size_t elsize)
681+
void* (*realloc) (void *ctx, void *ptr, size_t new_size)
682+
void (*free) (void *ctx, void *ptr, size_t size)
683+
684+
ctypedef struct PyDataMem_Handler:
685+
char* name
686+
npy_uint8 version
687+
PyDataMemAllocator allocator
688+
689+
object PyDataMem_SetHandler(object handler)
690+
object PyDataMem_GetHandler()
691+
675692
# additional datetime related functions are defined below
676693

677694

0 commit comments

Comments
 (0)