in reply to Re: XS malloc and free (whose)
in thread XS malloc and free

If I got the function pointer syntax right then something like this would be a workaround maybe?

void (perls_free)( void*p ) = &free; #undef free // free() should now refer to the crt free() and use perls_free() for +XS

Though the long term solution would be to remove the #define from the header file in favour of Safefree().

That said, the whole area of memory allocation functions and macro's in the Perl codebase is a mess. Sometime last year I did a not totally crude grep and eliminate duplicates process on the sources and discovered that between the 4 basic functions--malloc(), calloc(), realloc() & free()--there were (from memory) 114 different ways of calling them through the multiplicitous variations of functions and macros. It's truely amazing that there are many more memory problems than there are.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: XS malloc and free (whose)
by tye (Sage) on Feb 07, 2006 at 20:26 UTC

    I think you are missing a "*":

    void (*perls_fre­e)( void*p ) = &free; #undef free

    But that still risks the second half of my warning:

    (but don't use "free" any further down in your code, even indirectly via some other obscure C-preprocessor macro).

    which seems rather difficult to avoid with any certainty given your assessment of the current messiness. :)

    Update: BTW, I am aware that the "&" is "optional", but I consider this simply to be a mistake on the part of the ANSI C definers. (:

    - tye        

      I knew it didn't look right.

      How about doing things the other way around. Take an alias for the CRT free() function prior to including XSUB.h?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        First thanks for all the insight.

        That maybe a solution, but not through the preprocessor (code yet untested)

        // private external C library with one function #include <stdlib.h> void crt_free(void*p) { free(p); }
        link with this and call crt_free() instead of free().

        And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
        (Terry Pratchett, Small Gods)