Re: Help with resources management
| From: | Sara Golemon | Date: | Mon, 01 Nov 2004 15:35:28 +0000 |
| Subject: | Re: Help with resources management | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-1560@lists.php.net to get a copy of this message | ||
> 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);
> }
>
return_value is the zval* here, your options variable should be of type
SSH_OPTIONS*, but in this case a pointer is a pointer is a pointer so it
doesn't matter so much.
> More or less, malloc a variable (in the function you have
> declared a pointer), register the variable and you get zval.
>
When Andrey said "malloc a variable" he meant either directly (e)malloc() a
struct and populate it, or indirectly by way of an API function (such as
options_new()) which does the mallocing and populating for you. Since
options_new() uses the system malloc() call (I'm assuming it doesn't provide
malloc callbacks -- like libz or libbz2 do) rather than Zend's emalloc(),
you'll have to make sure you've registered a dtor function to clean it up
(you'd want to do it anyway of course, but this makes it even more critical
to avoid memleaks)... but one thing at a time....
> 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);
> }
>
Again, your first implementation was better, just change the variable type
on options to SSH_OPTIONS* and you're good to go. As to this
implementation.......when does malloc() ever take two parameters? :)
The tricky part is reading it back in whatever function uses the SSH_OPTIONS
parameter later on. What are you doing for reading that back? And oh, btw,
I assume you're registering the le_ssh resource type in your MINIT()
function.... Come to that, a more descriptive name for this might be
le_ssh_options (you need a different list entry for each datatype you're
going to tuck into a resource).
> [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.
>
That was me, and the mail scanner was php.net's spam blocker, it should have
provided you with a token to email back so that your sending address will be
recognized....
In any event libssh seems to be permanently 404 on that intraweb thingy...
-Sara