Re: zval_copy_ctor

From: Date: Sat, 12 May 2001 09:12:14 +0000
Subject: Re: zval_copy_ctor
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-54987@lists.php.net to get a copy of this message
At 02:42 AM 5/12/2001 +0100, Wez Furlong wrote:
Hi, In my mailparse extension I am building up an array to contain the headers while parsing the message. The array is held in a zval in the internal C structure, one for each message part. When the "user space" code requests info for a particular message part it is returned as an assoc. array. One of the keys contains the headers; eg: $info => array( "content-type" => "text/plain", "headers" => array(
       "to" => "wez@php.net",
       "from" => php-dev@lists.php.net"
) ); So far, to do this I am using this code: zval * headers; MAKE_STD_ZVAL(headers); *headers = *rfcbuf->headers; if (zval_copy_ctor(headers) == SUCCESS) { zend_hash_update(HASH_OF(return_value), "headers",
       strlen("headers") + 1, &headers, sizeof(headers)
       NULL);
} where rfcbuf->headers is a "zval *" created using MAKE_STD_ZVAL and array_init. It seems to work OK. Is it correct? The ZendAPI docs aren't 100% clear about using the copy constructor, and I am experiencing intermittent segfaults in a specific part of my "application" that uses this code.
The code is almost OK. The only problem is that the reference count and is_ref from rfcbuf->headers are also copied to *headers. So what you should be doing (if you only add it once to the return_value) is to do INIT_PZVAL(headers) before the hash_update so that they are reset to refcount=1 is_ref=0. Also a small tip, use sizeof("headers") instead of strlen("headers")+1. It is much faster as it is evaluated at compile-time. And by the way, zval_copy_ctor does recursive copy constructing so your code is OK. Andi

« previous php.dev (#54987) next »