[Python-Dev] pymemcompat.h & PyMem_New and friends

Tim Peters tim.one@comcast.net
Mon, 10 Jun 2002 08:07:08 -0400


[Michael Hudson]
> ...
> There is at least one other family in fairly widespread use in the
> Python core; the "typed memory allocator", PyMem_New, PyMem_Resize and
> PyMem_Del.  Should this family be listed in pyemcompat.h or subtly
> discouraged? (I don't think there are any other options).

I left it out of the "recommended" memory API, so "subtly discouraged" gets
my vote.  I think the current comment in pymem.h shows this <wink>:

 * These are carried along for historical reasons.  There's rarely a good
 * reason to use them anymore (you can just as easily do the multiply and
 * cast yourself).

> I think it should be subtly discouraged, for a couple of reasons:
>
> a) three is a smaller number than four.
> b) there is a non-analogy:
>
>        PyMem_Malloc ---> PyMem_New
>        PyObject_Malloc ---> PyObject_New
>
>    They do rather different things.
> c) I don't think omitting a cast and a sizeof is that much of a win.

It also hides a multiply, and that's a Bad Idea because callers almost never
first check that the hidden multiply doesn't overflow a size_t -- and
neither do the macros.  There are a few calls in the Python core that do,
but only because I slammed those checks in when a real-life overflow bug
surfaced.  If the PyMem_XYZ family were reworked to detect overflow, it may
become valuable again.

> I'm not proposing actually taking these interfaces away.

I suppose that explains why you're still breathing <wink>.

> (as a special bonus I won't even mention the fact that we have
> PyMem_Resize, PyObject_GC_Resize (only used in listobject.c)

No, it's not used in listobject.c -- there's no need for it there, as list
guts are stored separately from the list object.  It is used in
tupleobject.c and in frameobject.c, as tuples and frames are the only
container types in Python that *embed* a variable amount of data in the
object and may participate in cycles.

> but not PyObject_Resize...)

That seems a curious omission, but it would only be useful for a variable-
size object that doesn't participate in cyclic gc.  8-bit strings are the
only type that come to mind.