PHP Extension Development Notes (6) zval_string and Zval_stringl

Source: Internet
Author: User
String processing is a common operation, and Zend encapsulates a lot of macros related to string manipulation, first look at zval_string and Zval_stringl

#define ZVAL_STRING(z, s, duplicate) do {   \constchar *__s=(s);                \         zval *__z = (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 {   \constcharint __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 string operations inside PHP, such as SUBSTR, are finally given to such macros, it is important to understand the two macros here.

Zval_stringl in the process, because given the length parameter, so do not need to use strlen to find the length of the string, performance improved.

In front of the commonly used Zend API should have said, about Estrndup are also encapsulated a layer, in the development of PHP extension, as far as possible to use the system encapsulated functions, so that can optimize memory, reduce memory leaks and other risks, there are several e* development functions, you can see the relevant articles before.

Estrndup definition

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

_estrndup definition

char *_estrndup(constchar *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {     char#ifdef ZEND_SIGNALS     #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);     0;     HANDLE_UNBLOCK_INTERRUPTIONS();     return p; }

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

The above describes the PHP extension development Note (6) zval_string and Zval_stringl, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.