[Python-Dev] Problem with the memory docs

Tim Peters tim.one@comcast.net
Thu, 04 Apr 2002 15:48:52 -0500


[/F]
> umm.  I have to admit that it's rather ugly, but I'm pretty sure the
> following guarantees that requesting zero bytes will never return a
> NULL pointer:
>
> static char nullbyte = MAGIC;
>
> void* malloc(int bytes)
> {
>     if (bytes == 0) {
> #if DEBUG_MODE
>         if (*nullbyte != MAGIC)
>             uh-oh
> #endif
>         return &nullbyte;
>     }
>     ...
>
> void free(void* ptr)
> {
>     if (ptr == &nullbyte) {
> #if DEBUG_MODE
>         if (*nullbyte != MAGIC)
>             uh-oh
> #endif
>         return; /* nothing to do */
>     }
>     ...

Don't forget realloc too, and that malloc hasn't taken an int argument since
before 1989 <wink>.

Sorry, but this is nuts, and Martin is right that I don't want Python to
offer a "malloc wrapper" that doesn't meet the standard rules for malloc.
Instead I intend to change the docs to say that Py_Malloc(0) acts like
platform malloc(1), and leave it at that.  I expect the _PyMem_EXTRA hack to
go away too.