mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
As Guido suggested, this makes the new subclassing code substantially simpler. But the mechanics of doing it w/ C macro semantics are a mess, and _PyObject_VAR_SIZE has a new calling sequence now. Question: The PyObject_NEW_VAR macro appears to be part of the public API. Regardless of what it expands to, the notion that it has to round up the memory it allocates is new, and extensions containing the old PyObject_NEW_VAR macro expansion (which was embedded in the PyObject_NEW_VAR expansion) won't do this rounding. But the rounding isn't actually *needed* except for new-style instances with dict pointers after a variable-length blob of embedded data. So my guess is that we do not need to bump the API version for this (as the rounding isn't needed for anything an extension can do unless it's recompiled anyway). What's your guess?
This commit is contained in:
parent
406fe3b1c0
commit
6d483d3477
4 changed files with 70 additions and 61 deletions
|
@ -798,14 +798,17 @@ _PyObject_GC_UnTrack(PyObject *op)
|
|||
}
|
||||
|
||||
PyObject *
|
||||
_PyObject_GC_Malloc(PyTypeObject *tp, int nitems, size_t padding)
|
||||
_PyObject_GC_Malloc(PyTypeObject *tp, int nitems)
|
||||
{
|
||||
PyObject *op;
|
||||
size_t basicsize;
|
||||
#ifdef WITH_CYCLE_GC
|
||||
const size_t basic = (size_t)_PyObject_VAR_SIZE(tp, nitems);
|
||||
const size_t nbytes = sizeof(PyGC_Head) + basic + padding;
|
||||
size_t nbytes;
|
||||
PyGC_Head *g;
|
||||
|
||||
PyGC_Head *g = PyObject_MALLOC(nbytes);
|
||||
_PyObject_VAR_SIZE(basicsize, tp, nitems);
|
||||
nbytes = sizeof(PyGC_Head) + basicsize;
|
||||
g = PyObject_MALLOC(nbytes);
|
||||
if (g == NULL)
|
||||
return (PyObject *)PyErr_NoMemory();
|
||||
g->gc_next = NULL;
|
||||
|
@ -821,7 +824,8 @@ _PyObject_GC_Malloc(PyTypeObject *tp, int nitems, size_t padding)
|
|||
}
|
||||
op = FROM_GC(g);
|
||||
#else
|
||||
op = PyObject_MALLOC(_PyObject_VAR_SIZE(tp, nitems) + padding);
|
||||
_PyObject_VAR_SIZE(basicsize, tp, nitems);
|
||||
op = PyObject_MALLOC(basicsize);
|
||||
if (op == NULL)
|
||||
return (PyObject *)PyErr_NoMemory();
|
||||
|
||||
|
@ -832,33 +836,36 @@ _PyObject_GC_Malloc(PyTypeObject *tp, int nitems, size_t padding)
|
|||
PyObject *
|
||||
_PyObject_GC_New(PyTypeObject *tp)
|
||||
{
|
||||
PyObject *op = _PyObject_GC_Malloc(tp, 0, 0);
|
||||
PyObject *op = _PyObject_GC_Malloc(tp, 0);
|
||||
return PyObject_INIT(op, tp);
|
||||
}
|
||||
|
||||
PyVarObject *
|
||||
_PyObject_GC_NewVar(PyTypeObject *tp, int size)
|
||||
_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
|
||||
{
|
||||
PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, size, 0);
|
||||
return PyObject_INIT_VAR(op, tp, size);
|
||||
PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, nitems);
|
||||
return PyObject_INIT_VAR(op, tp, nitems);
|
||||
}
|
||||
|
||||
PyVarObject *
|
||||
_PyObject_GC_Resize(PyVarObject *op, int size)
|
||||
_PyObject_GC_Resize(PyVarObject *op, int nitems)
|
||||
{
|
||||
size_t basicsize;
|
||||
#ifdef WITH_CYCLE_GC
|
||||
PyGC_Head *g = AS_GC(op);
|
||||
g = PyObject_REALLOC(g, _PyObject_VAR_SIZE(op->ob_type, size) +
|
||||
sizeof(PyGC_Head));
|
||||
|
||||
_PyObject_VAR_SIZE(basicsize, op->ob_type, nitems);
|
||||
g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
|
||||
if (g == NULL)
|
||||
return (PyVarObject *)PyErr_NoMemory();
|
||||
op = (PyVarObject *) FROM_GC(g);
|
||||
#else
|
||||
op = PyObject_REALLOC(op, _PyObject_VAR_SIZE(op->ob_type, size));
|
||||
_PyObject_VAR_SIZE(basicsize, op->ob_type, nitems);
|
||||
op = PyObject_REALLOC(op, basicsize);
|
||||
if (op == NULL)
|
||||
return (PyVarObject *)PyErr_NoMemory();
|
||||
#endif
|
||||
op->ob_size = size;
|
||||
op->ob_size = nitems;
|
||||
return op;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue