Skip to content
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

Make array data allocator configurable #5470

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions numpy/core/code_generators/cversions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
# The interface has not changed, but the hash is different due to
# the annotations, so keep the previous version number.
0x00000009 = 982c4ebb6e7e4c194bf46b1535b4ef1b

# Version 10 (NumPy 1.10) Added configurable array data allocator.
0x0000000a = 7c6ff1780a699fd67e75dc0bc180a994
2 changes: 2 additions & 0 deletions numpy/core/code_generators/numpy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@
# End 1.9 API
'PyArray_CheckAnyScalarExact': (300, NonNull(1)),
# End 1.10 API
'PyDataMem_GetAllocator': (301,),
'PyDataMem_SetAllocator': (302,),
}

ufunc_types_api = {
Expand Down
35 changes: 28 additions & 7 deletions numpy/core/include/numpy/ndarraytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,26 @@ typedef struct _arr_descr {
PyObject *shape; /* a tuple */
} PyArray_ArrayDescr;

/*
* This is a function for hooking into the PyDataMem_NEW/FREE/RENEW functions.
* See the documentation for PyDataMem_SetEventHook.
*/
typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
void *user_data);

/* Allocator structure for array data */
typedef void *(PyDataMem_AllocFunc)(size_t size);
typedef void *(PyDataMem_ZeroedAllocFunc)(size_t nelems, size_t elsize);
typedef void (PyDataMem_FreeFunc)(void *ptr);
typedef void *(PyDataMem_ReallocFunc)(void *ptr, size_t size);

typedef struct {
PyDataMem_AllocFunc *alloc;
PyDataMem_ZeroedAllocFunc *zeroed_alloc;
PyDataMem_FreeFunc *free;
PyDataMem_ReallocFunc *realloc;
} PyDataMem_Allocator;

/*
* The main array object structure.
*
Expand Down Expand Up @@ -675,6 +695,8 @@ typedef struct tagPyArrayObject_fields {
int flags;
/* For weak references */
PyObject *weakreflist;
/* For resizes and deallocation */
const PyDataMem_Allocator *allocator;
} PyArrayObject_fields;

/*
Expand Down Expand Up @@ -1536,6 +1558,12 @@ PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v)
v, itemptr, arr);
}

static NPY_INLINE const PyDataMem_Allocator *
PyArray_ALLOCATOR(const PyArrayObject *arr)
{
return ((PyArrayObject_fields *)arr)->allocator;
}

#else

/* These macros are deprecated as of NumPy 1.7. */
Expand Down Expand Up @@ -1778,13 +1806,6 @@ typedef struct {
*/
} PyArrayInterface;

/*
* This is a function for hooking into the PyDataMem_NEW/FREE/RENEW functions.
* See the documentation for PyDataMem_SetEventHook.
*/
typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
void *user_data);

/*
* Use the keyword NPY_DEPRECATED_INCLUDES to ensure that the header files
* npy_*_*_deprecated_api.h are only included from here and nowhere else.
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/setup_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
# 0x00000008 - 1.7.x
# 0x00000009 - 1.8.x
# 0x00000009 - 1.9.x
C_API_VERSION = 0x00000009
# 0x0000000A - 1.10.x
C_API_VERSION = 0x0000000A

class MismatchCAPIWarning(Warning):
pass
Expand Down