: 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.