Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^9: Perl XS binding to a struct with an array of chars*

by Marshall (Canon)
on Nov 25, 2022 at 05:37 UTC ( [id://11148372]=note: print w/replies, xml ) Need Help??


in reply to Re^8: Perl XS binding to a struct with an array of chars*
in thread Perl XS binding to a struct with an array of chars*

There is also safemalloc(). This function also has the warning that only Safefree() should be used to free pointers allocated with safemalloc(). I remember reading somewhere that the reason for Safefree(), safemalloc() and the other XS memory functions has something to do with issues involving threads. Evidently malloc() and free() can get you into trouble with threaded code. Whether or not that also means trouble with even just using a multi-threaded version of Perl albeit without user threads, I don't know.

I used malloc in both test programs just to make them as similar as possible. The Perl XS version should use safemalloc() instead.

  • Comment on Re^9: Perl XS binding to a struct with an array of chars*

Replies are listed 'Best First'.
Re^10: Perl XS binding to a struct with an array of chars*
by syphilis (Archbishop) on Nov 25, 2022 at 08:42 UTC
    There is also safemalloc()

    Yes, I've only recently become aware of its existence, and I haven't used it at all.
    I'm guessing it's just Newx() refactored to mimic the way that malloc() is called.
    If I ever see anything to suggest that it offers a significant performance improvement over Newx() then I'll probably switch to it.

    Cheers,
    Rob
      Well, these are two completely different functions that have completely different purposes and uses.

      void Newx (void* ptr, int nitems, type) void* safemalloc(size_t size)
      I show an application for safemalloc() at Allocate a variable length array inside of a struct where Newx() is just not appropriate. Newx() is only appropriate if the sizeof the type is a constant.
        Newx() is only appropriate if the sizeof the type is a constant

        That's a good demo of when it makes better sense to use safemalloc() - but it seems that you can still use Newx() if you want.
        Instead of doing:
        message = (EdjeMessageStringSet*) safemalloc( sizeof(EdjeMessageString +Set) + (count+1)*sizeof(char*) );
        I think the following Newx() rendition does the same thing (and does it portably):
        Newx( message, (sizeof(EdjeMessageStringSet) + (count+1)*sizeof(char*) +) / sizeof(char), char );
        Of course, that Newx() approach relies on (sizeof(EdjeMessageStringSet) + (count+1)*sizeof(char*)) being an exact multiple of sizeof(char), which is a pretty safe bet whenever sizeof(char) is 1.

        TBH, I had never considered the possibility of Newx() being called with the first arg being a pointer to a certain type && the final arg specifying a different type.
        I wonder if there's a problem with doing that ... of which I'm currently unaware.
        Live and learn .... ;-)

        UPDATE:
        Another thing that had been nagging at me was "Why does the memory have to be allocated in one hit ?".
        The answer, of course, is "It doesn't". It's quite ok (and makes better sense to me) to do the memory allocations separately:
        char ** p; EdjeMessageStringSet* message; ... Newx(message, 1, EdjeMessageStringSet; Newx(p, count + 1, char*); ...
        and that works quite well.

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found