Help with resources management
| From: | Yannick Warnier | Date: | Mon, 01 Nov 2004 12:08:16 +0000 |
| Subject: | Help with resources management | ||
| Groups: | php.pecl.dev | ||
| Request: | Send a blank email to pecl-dev+get-1557@lists.php.net to get a copy of this message | ||
Hi there,
A while ago, I asked for some links on how to manage resources when
creating an extension from an existing C library. I got answers from
some of, who I thank a lot [1].
To remind you of my problem, I would like to write an extension that
uses functions returning funny types like, for example:
SSH_OPTIONS *options_new();
Where SSH_OPTIONS is a structure declared in my ssh library.
I have come to this, which seems a lot nicer than before and even
compiles, but doesn't return anything other than NULL:
ZEND_FUNCTION(ssh_options_new)
{
zval *options;
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
options = (zval *) options_new();
ZEND_REGISTER_RESOURCE(return_value, &options, le_ssh);
}
Now I just had time now to try this again and I am having problem
understanding the answers I received back then, mostly because I am not
C proficient anymore. For example, Andrey Hristov wrote:
"
More or less, malloc a variable (in the function you have
declared a pointer), register the variable and you get zval.
Return this zval to the user. When the user passes back the
zval you can check whether it is a resource (IS_RESOURCE or
with zend_parse_parameters()) and with the resource you can
get back a pointer to the memory previously allocated. Another
detail is that you have to provide a pointer to a function
which is used by ZE for automatic deallocation (destruction)
of the resource, whenever that is needed. This function is
usually short and is more or less a cast and efree().
"
So applying this answer, how should I cope with this? I'm thinking about
something like:
ZEND_FUNCTION(ssh_options_new)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
options = malloc(options_new(),sizeof(???));
ZEND_REGISTER_RESOURCE(return_value, &options, le_ssh);
}
But honestly I really doubt it could be correct.
Could somebody give me a hint about this?
Thanks in advance,
Yannick Warnier
[1] I also tried to send the ssh library to someone that asked me (don't
remember who) but was blocked by some kind of mail scanner. I can try
again if the person asks.