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.