Steve Hay <[EMAIL PROTECTED]> writes:
>Is it safe to store a pointer to a C struct in an SV?

Yes, provided the lifetime of the data pointed to is suitable.

>
>    MY_STRUCT my_struct;
>    sv = newSVuv((unsigned long) &my_struct);
>    SvIOK_only_UV(sv);

Your my_struct is on the stack in the XS function - memory gets 
reused when XS returns.

What I tend to do is put the struct in the string part of a SvPV

     SV *sv = newSV(sizeof(MY_STRUCT));
     MY_STRUCT *my_struct = (MY_STRUCT *) SvPVX(sv);


>
>and later on my_filter() can then access:
>
>    (MY_STRUCT *) SvUVX(sv)

That would in my scheme be 

     MY_STRUCT *my_struct = (MY_STRUCT *) SvPVX(sv);




>
>However, the struct now available within my_filter() doesn't seem to be 
>working properly...
>
>If I switch to using a file scope static variable for the struct then it 
>works OK for one filtered source file, but this approach is not feasible 
>in general because if one filtered source file loads another one then 
>the second filter starts writing all over the struct that was being used 
>by the first filter!  They need to have their "own" structs available, 
>hence I wanted to store a pointer to it on the SV.
>
>Is that a "safe" thing to do in general?  Any ideas what might be going 
>wrong here?
>
>Thanks,
>- Steve
>
>
>
>------------------------------------------------
>Radan Computational Ltd.
>
>The information contained in this message and any files transmitted with it are 
>confidential and intended for the addressee(s) only.  If you have received this 
>message in error or there are any problems, please notify the sender immediately.  
>The unauthorized use, disclosure, copying or alteration of this message is strictly 
>forbidden.  Note that any views or opinions presented in this email are solely those 
>of the author and do not necessarily represent those of Radan Computational Ltd.  The 
>recipient(s) of this message should check it and any attached files for viruses: 
>Radan Computational will accept no liability for any damage caused by any virus 
>transmitted by this email.

Reply via email to