note
MaxPerl
<p>Dear Rob,</p>
<p>Thank you so much for your hint with Inline:C. I will try this at the next opportunity</p>
<p>I don't know why, but I solved my problem with the following:</p>
<code>
_new(class,count, val_arr)
char *class
int count
AV *val_arr
PREINIT:
EdjeMessageStringSet *message;
int index;
SV *tmp;
char *string;
STRLEN len;
CODE:
Newx(message,1,EdjeMessageStringSet);
Renewc(message,count+2, char*,EdjeMessageStringSet);
if (message == NULL)
croak("Failed to allocate memory in _new function\n");
message->count = count+1;
for (index = 0; index <= count; index++) {
tmp = *av_fetch(val_arr,index,0);
string = SvPVutf8(tmp,len);
message->str[index] = savepvn(string,len);
}
RETVAL = message;
OUTPUT:
RETVAL
</code>
<p>I have to allocate memory for the Edje_Message_String_Set struct and for the array of strings, because it is only at runtime visible, how many strings are in the array. In C this goes through <c>malloc(sizeof(Edje_Message_String_Set) + count * sizeof(char*))</c>. In Perl this works with the Renewc function. The only thing I don't understand is, why I had to reallocate count+1 items of char*. It should be count+1 :-S (because count starts at 1, and the index of the given Perl array starts at 0). (in a C example there is even malloced count-1 * sizeof(char*)...). But whatever, I am happy that it works now...</p>
11148268
11148270