Home  >  Article  >  Backend Development  >  PHP extension development notes (6) ZVAL_STRING and ZVAL_STRINGL

PHP extension development notes (6) ZVAL_STRING and ZVAL_STRINGL

WBOY
WBOYOriginal
2016-08-08 09:22:361695browse

String processing is a commonly used operation for us, and zend encapsulates many macros related to string operations. Let’s take a look at ZVAL_STRING and ZVAL_STRINGL first.

<code><span>#define ZVAL_STRING(z, s, duplicate) do {   \</span><span>const</span><span>char</span> *__s=(s);                \
         zval *__z = (z);                    \
         Z_STRLEN_P(__z) = <span>strlen</span>(__s);      \
         Z_STRVAL_P(__z) = (duplicate?estrndup(__s, Z_STRLEN_P(__z)):(<span>char</span>*)__s);\
         Z_TYPE_P(__z) = IS_STRING;          \
     } <span>while</span> (<span>0</span>)

 <span>#define ZVAL_STRINGL(z, s, l, duplicate) do {   \</span><span>const</span><span>char</span> *__s=(s); <span>int</span> __l=l;         \
         zval *__z = (z);                        \
         Z_STRLEN_P(__z) = __l;                  \
         Z_STRVAL_P(__z) = (duplicate?estrndup(__s, __l):(<span>char</span>*)__s);\
         Z_TYPE_P(__z) = IS_STRING;              \
     } <span>while</span> (<span>0</span>)</code>

Because many string operations (such as substr) inside PHP end up like this Macros are used to operate, so it is very important to understand these two macros here.

ZVAL_STRINGL When processing, because the length parameter is given, there is no need to use strlen to find the length of the string, and the performance is improved.

I should have mentioned it before in the commonly used Zend API. Regarding estrndup, it is also encapsulated. When developing PHP extensions, try to use system encapsulated functions. This can optimize memory and reduce risks such as memory leaks. There are several functions developed by e*, you can read the previous related articles.

estrndup definition

<code><span>#define estrndup(s, length) _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)</span></code>

_estrndup definition

<code> ZEND_API <span>char</span> *_estrndup(<span>const</span><span>char</span> *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
 {
     <span>char</span> *p;
 <span>#ifdef ZEND_SIGNALS</span>
     TSRMLS_FETCH();
 <span>#endif</span>     HANDLE_BLOCK_INTERRUPTIONS();

     p = (<span>char</span> *) _emalloc(length+<span>1</span> ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
     <span>if</span> (UNEXPECTED(p == NULL)) {
         HANDLE_UNBLOCK_INTERRUPTIONS();
         <span>return</span> p;
     }
     <span>memcpy</span>(p, s, length);
     p[length] = <span>0</span>;
     HANDLE_UNBLOCK_INTERRUPTIONS();
     <span>return</span> p;
 }</code>

You can search for related function definitions such as _emalloc by yourself.

The above introduces the PHP extension development notes (6) ZVAL_STRING and ZVAL_STRINGL, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn