Php extension development notes (6) ZVAL_STRING and ZVAL_STRINGL

Source: Internet
Author: User
: This article mainly introduces php extension development notes (6) ZVAL_STRING and ZVAL_STRINGL. if you are interested in PHP tutorials, refer to it. String Processing is a common operation. zend encapsulates many macros related to string operations. let's take a look at ZVAL_STRING and ZVAL_STRINGL.

#define ZVAL_STRING(z, s, duplicate) do {   \constchar *__s=(s);                \         zval *__z = (z);                    \         Z_STRLEN_P(__z) = strlen(__s);      \         Z_STRVAL_P(__z) = (duplicate?estrndup(__s, Z_STRLEN_P(__z)):(char*)__s);\         Z_TYPE_P(__z) = IS_STRING;          \     } while (0) #define ZVAL_STRINGL(z, s, l, duplicate) do {   \constchar *__s=(s); int __l=l;         \         zval *__z = (z);                        \         Z_STRLEN_P(__z) = __l;                  \         Z_STRVAL_P(__z) = (duplicate?estrndup(__s, __l):(char*)__s);\         Z_TYPE_P(__z) = IS_STRING;              \     } while (0)

Because many character string operations (such as substr) in php are finally performed on these macros, it is very important to understand these two macros here.

ZVAL_STRINGL does not need to use strlen to obtain the length of the string because the length parameter is given, which improves the performance.

I have mentioned this in the commonly used zend api. estrndup also encapsulates a layer. during php extension development, try to use system-encapsulated functions to optimize the memory, reduce memory leakage and other risks. There are several functions developed by e *. you can refer to the previous articles.

Estrndup definition

#define estrndup(s, length) _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)

_ Estrndup definition

 ZEND_API char *_estrndup(constchar *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {     char *p; #ifdef ZEND_SIGNALS     TSRMLS_FETCH(); #endif     HANDLE_BLOCK_INTERRUPTIONS();     p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);     if (UNEXPECTED(p == NULL)) {         HANDLE_UNBLOCK_INTERRUPTIONS();         return p;     }     memcpy(p, s, length);     p[length] = 0;     HANDLE_UNBLOCK_INTERRUPTIONS();     return p; }

You can search for related function definitions such as _ emalloc.

The above introduces the php extension development notes (6) ZVAL_STRING and ZVAL_STRINGL, including some content, and hope to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.