Issue #21233: Rename the C structure "PyMemAllocator" to "PyMemAllocatorEx" to

make sure that the code using it will be adapted for the new "calloc" field
(instead of crashing).
This commit is contained in:
Victor Stinner 2014-06-02 21:57:10 +02:00
parent aa0e7afa43
commit d8f0d922d5
6 changed files with 32 additions and 29 deletions

View File

@ -232,7 +232,7 @@ Customize Memory Allocators
.. versionadded:: 3.4
.. c:type:: PyMemAllocator
.. c:type:: PyMemAllocatorEx
Structure used to describe a memory block allocator. The structure has
four fields:
@ -253,7 +253,9 @@ Customize Memory Allocators
+----------------------------------------------------------+---------------------------------------+
.. versionchanged:: 3.5
Add a new field ``calloc``.
The :c:type:`PyMemAllocator` structure was renamed to
:c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.
.. c:type:: PyMemAllocatorDomain
@ -267,12 +269,12 @@ Customize Memory Allocators
:c:func:`PyObject_Realloc` and :c:func:`PyObject_Free`
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Get the memory block allocator of the specified domain.
.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Set the memory block allocator of the specified domain.

View File

@ -271,4 +271,5 @@ Changes in the Python API
Changes in the C API
--------------------
* The :c:type:`PyMemAllocator` structure has a new ``calloc`` field.
* The :c:type:`PyMemAllocator` structure was renamed to
:c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.

View File

@ -128,7 +128,7 @@ typedef enum {
} PyMemAllocatorDomain;
typedef struct {
/* user context passed as the first argument to the 3 functions */
/* user context passed as the first argument to the 4 functions */
void *ctx;
/* allocate a memory block */
@ -142,11 +142,11 @@ typedef struct {
/* release a memory block */
void (*free) (void *ctx, void *ptr);
} PyMemAllocator;
} PyMemAllocatorEx;
/* Get the memory block allocator of the specified domain. */
PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
PyMemAllocator *allocator);
PyMemAllocatorEx *allocator);
/* Set the memory block allocator of the specified domain.
@ -160,7 +160,7 @@ PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
on top on the new allocator. */
PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
PyMemAllocator *allocator);
PyMemAllocatorEx *allocator);
/* Setup hooks to detect bugs in the following Python memory allocator
functions:

View File

@ -2756,7 +2756,7 @@ test_pymem_alloc0(PyObject *self)
}
typedef struct {
PyMemAllocator alloc;
PyMemAllocatorEx alloc;
size_t malloc_size;
size_t calloc_nelem;
@ -2802,7 +2802,7 @@ test_setallocators(PyMemAllocatorDomain domain)
PyObject *res = NULL;
const char *error_msg;
alloc_hook_t hook;
PyMemAllocator alloc;
PyMemAllocatorEx alloc;
size_t size, size2, nelem, elsize;
void *ptr, *ptr2;

View File

@ -18,9 +18,9 @@ static void raw_free(void *ptr);
/* Protected by the GIL */
static struct {
PyMemAllocator mem;
PyMemAllocator raw;
PyMemAllocator obj;
PyMemAllocatorEx mem;
PyMemAllocatorEx raw;
PyMemAllocatorEx obj;
} allocators;
static struct {
@ -475,7 +475,7 @@ tracemalloc_remove_trace(void *ptr)
static void*
tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
void *ptr;
assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize);
@ -501,7 +501,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
static void*
tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
{
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
void *ptr2;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
@ -546,7 +546,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
static void
tracemalloc_free(void *ctx, void *ptr)
{
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (ptr == NULL)
return;
@ -567,7 +567,7 @@ tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
void *ptr;
if (get_reentrant()) {
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (use_calloc)
return alloc->calloc(alloc->ctx, nelem, elsize);
else
@ -607,7 +607,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
Example: PyMem_RawRealloc() is called internally by pymalloc
(_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new
arena (new_arena()). */
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
if (ptr2 != NULL && ptr != NULL) {
@ -639,7 +639,7 @@ tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
void *ptr;
if (get_reentrant()) {
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (use_calloc)
return alloc->calloc(alloc->ctx, nelem, elsize);
else
@ -685,7 +685,7 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
if (get_reentrant()) {
/* Reentrant call to PyMem_RawRealloc(). */
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
@ -863,7 +863,7 @@ tracemalloc_deinit(void)
static int
tracemalloc_start(int max_nframe)
{
PyMemAllocator alloc;
PyMemAllocatorEx alloc;
size_t size;
if (tracemalloc_init() < 0)

View File

@ -151,7 +151,7 @@ _PyObject_ArenaFree(void *ctx, void *ptr, size_t size)
typedef struct {
/* We tag each block with an API ID in order to tag API violations */
char api_id;
PyMemAllocator alloc;
PyMemAllocatorEx alloc;
} debug_alloc_api_t;
static struct {
debug_alloc_api_t raw;
@ -166,7 +166,7 @@ static struct {
#define PYDBG_FUNCS _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree
#endif
static PyMemAllocator _PyMem_Raw = {
static PyMemAllocatorEx _PyMem_Raw = {
#ifdef PYMALLOC_DEBUG
&_PyMem_Debug.raw, PYDBG_FUNCS
#else
@ -174,7 +174,7 @@ static PyMemAllocator _PyMem_Raw = {
#endif
};
static PyMemAllocator _PyMem = {
static PyMemAllocatorEx _PyMem = {
#ifdef PYMALLOC_DEBUG
&_PyMem_Debug.mem, PYDBG_FUNCS
#else
@ -182,7 +182,7 @@ static PyMemAllocator _PyMem = {
#endif
};
static PyMemAllocator _PyObject = {
static PyMemAllocatorEx _PyObject = {
#ifdef PYMALLOC_DEBUG
&_PyMem_Debug.obj, PYDBG_FUNCS
#else
@ -209,7 +209,7 @@ void
PyMem_SetupDebugHooks(void)
{
#ifdef PYMALLOC_DEBUG
PyMemAllocator alloc;
PyMemAllocatorEx alloc;
alloc.malloc = _PyMem_DebugMalloc;
alloc.calloc = _PyMem_DebugCalloc;
@ -237,7 +237,7 @@ PyMem_SetupDebugHooks(void)
}
void
PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
{
switch(domain)
{
@ -255,7 +255,7 @@ PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
}
void
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
{
switch(domain)
{