Hello Seekers of Perl Wisdom,
I try to make a perl binding to the following struct
struct _Edje_Message_String_Set
{
int count;
char *str[1];
};
At the moment my try of an implementation looks like this (only the important parts):
[...]
typedef Edje_Message_String_Set EdjeMessageStringSet;
MODULE = pEFL::Edje::Message::StringSet PACKAGE = pEFL::Edje::M
+essage::StringSet
EdjeMessageStringSet *
_new(class,count, val_arr)
char *class
int count
AV *val_arr
PREINIT:
EdjeMessageStringSet *message;
int index;
char *string;
STRLEN len;
CODE:
message = malloc(sizeof(Edje_Message_String) + count * sizeof(char
+ *));
message->count = count+1;
for (index = 0; index <= count; index++) {
SV *tmp = *av_fetch(val_arr,index,0);
string = SvPVutf8(tmp,len);
message->str[index] = savepvn(string,len);
}
RETVAL = message;
OUTPUT:
RETVAL
MODULE = pEFL::Edje::Message::StringSet PACKAGE = EdjeMessageSt
+ringSetPtr
[...]
void
str(message)
EdjeMessageStringSet *message
PREINIT:
int count;
char **vals;
int index;
PPCODE:
count = message->count;
vals = message->str;
EXTEND(SP,count);
for (index = 0; index <count; index ++) {
PUSHs( sv_2mortal( newSVpv( vals[index], 0 ) ));
}
void
DESTROY(message)
EdjeMessageStringSet *message
CODE:
free(message);
Unfortunately I get different errors (e.g. corrupted size vs. prev_size, double_free or corruption (out), segfault, invalid pointer etc.). Following a simple test code:
use pEFL::Edje::Message::StringSet;
my $i = 0;
while ($i<100) {
my @str = ("Hello", "Wordl", "from Perl");
my $str_msg = pEFL::Edje::Message::StringSet->new(@str);
my @strings = $str_msg->str();
print "COLORS @strings\n";
$i++;
}
print "The script goes to the end\n";
Where is my misunderstanding?
PS:
I tried to use perl XS memory allocation, too. But this doesn't help. For example the following doesn't work:
PREINIT:
[...]
char **val;
CODE:
New(0,val,count+1, char*);
New(0,message,1,EdjeMessageStringSet);
message->count = count+1;
for (index = 0; index <= count; index++) {
SV *tmp = *av_fetch(val_arr,index,0);
string = SvPVutf8(tmp,len);
New(0,val[index],len,char)
val[index] = savepvn(string,len);
}
Move(val,message->str,count+1,char*);
RETVAL = message;
OUTPUT:
RETVAL
btw. how can one allocate memory in Perl XS with size calculated from different types (here Edje_Message_Signal_Set and char(*))?
Thank you so much for your help!!!
Max
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.