[Python-3000] cleaning up different ways to free an object
Adam Olsen
rhamph at gmail.com
Sun Aug 19 23:33:48 CEST 2007
On 8/19/07, Neal Norwitz <nnorwitz at gmail.com> wrote:
> I just fixed a bug in the new memoryview that used PyObject_DEL which
> caused a problem in debug mode. I had to change it to a Py_DECREF.
> It seems we have a lot of spellings of ways to free an object and I
> wonder if there are more problems lurking in there.
>
> $ cat */*.c | grep -c PyObject_Del
> 103
> $ cat */*.c | grep -c PyObject_DEL
> 16
> $ cat */*.c | grep -c PyObject_Free
> 16
> $ cat */*.c | grep -c PyObject_FREE
> 19
>
> I don't know how many of these are correct or incorrect.
>
> Note in Include/objimpl, the Del and Free variants are the same. I
> plan to get rid of one of them.
>
> #define PyObject_Del PyObject_Free
> #define PyObject_DEL PyObject_FREE
>
> PyObject_{MALLOC,REALLOC,FREE} depend upon whether python is compiled
> with debug mode, pymalloc, or not.
>
> What are the rules for when a particular API should be used (or not
> used) to free an object?
Going from the lowest level to the highest level we have:
{malloc,realloc,free} - libc's functions, arguments are bytes.
PyMem_{Malloc,Realloc,Free} - Simple wrapper of {malloc,realloc,free}
or PyObject_{Malloc,Realloc,Free}, but guarantees 0-byte allocations
will always succeed. Do we really need this? At best it seems
synonymous with PyObject_{Malloc,Realloc,Free}. It is a better name
though.
PyObject_{Malloc,Realloc,Free} - obmalloc.c's functions, arguments are
bytes. Despite the name, I believe it can be used for arbitrary
allocations (not just PyObjects.) Probably shouldn't be in Objects/.
configure calls these pymalloc and they are controlled by the
WITH_PYMALLOC define. Also guarantees 0-byte allocations will
succeed.
_PyObject_{New,NewVar} - object.c's functions, arguments are a
PyTypeObject and optionally a size. Determines the number of bytes
automatically, initializes ob_type and ob_refcnt fields.
_PyObject_Del - Does nothing in particular (wraps free/Free), but the
argument is intended to be a PyObject returned by
_PyObject_{New,NewVar}. Exists only to complement other functions.
Currently only a macro. Could be extended to sanity-check ob_refcnt
field on debug builds.
_PyObject_GC_{New,NewVar,Del} - As _PyObject_{New,NewVar,Del}, but
adds hidden accounting info needed by cycle GC.
PyObject{,_GC}_{New,NewVar,Del} - Macros that add typecasting to the above.
--
Adam Olsen, aka Rhamphoryncus
More information about the Python-3000
mailing list