How to Use zend when php calls C Functions

Source: Internet
Author: User
A zend_parse_parameters prototype intzend_parse_parameters (intnum_arstsrmls_dc, char * type_spec,...); the first parameter is the number of parameters passed to the function. The common practice is to pass it ZEND_NUM_ARGS (). This is a macro that indicates the total number of parameters passed to the function. The second parameter is used for thread safety,

A zend_parse_parameters prototype int zend_parse_parameters (int num_args TSRMLS_DC, char * type_spec,...); the first parameter is the number of parameters passed to the function. The common practice is to pass ZEND_NUM_ARGS (). This is a macro that indicates the total number of parameters passed to the function. The second parameter is used for thread safety,

A zend_parse_parameters prototype int zend_parse_parameters (int num_args TSRMLS_DC, char * type_spec ,...);
The first parameter is the number of parameters passed to the function. Generally, it is passed to ZEND_NUM_ARGS (). This is a macro that indicates the total number of parameters passed to the function.
The second parameter is used to ensure thread security and always transmits the TSRMLS_CC macro.
The third parameter is a string that specifies the expected parameter type of the function.
The fourth parameter is followed by the list of variables that need to be updated with the parameter value. Because PHP uses loose variable definitions and dynamic type determination, it is possible to convert parameters of different types into The expected type.
Type. We also list some types that are not discussed from integrity considerations.

The following table lists possible types.

Type specifier

Corresponding C type

Description

L

Long

Symbol integer

D

Double

Floating Point Number

S

Char *, int

Binary string, Length

B

Zend_bool

Logical type (1 or 0)

R

Zval *

Resources (File pointers, database connections, etc)

A

Zval *

Union Array

O

Zval *

Any type of object

O

Zval *

Specifies the type of object. Class type of the target object

Z

Zval *

Zval of any operation


Zval is the value container of the Zend engine [1]. Whether this variable is Boolean, string, or any other type, its information will always be contained in a zval consortium. In this chapter, we do not directly access zval, but operate through some additional macros. Below is more or less zval in C, so that we can better understand the following code.

[Cpp]View plaincopy

  1. Typedef union _ zval {
  2. Long lval;
  3. Double dval;
  4. Struct {
  5. Char * val;
  6. Int len;
  7. } Str;
  8. HashTable * ht;
  9. Zend_object_value obj;
  10. } Zval;
  11. If (zend_parse_parameters (argc TSRMLS_CC, "sl", & str, & str_len, & n) = FAILURE)
  12. Return;


It is noted that the automatically generated code checks the return value of the function FAILUER (SUCCESS is SUCCESS) to determine whether the function is successful. If it fails, return immediately, and zend_parse_parameters () triggers the warning. Because the function is intended to receive a string l and an integer n, "sl" is specified as its type indicator. S requires two parameters, So we pass the reference char * and int (str and str_len) to the zend_parse_parameters () function.

2. Memory Management
The php api used to allocate memory from the heap is almost the same as the standard c api. When writing extensions, use the following API functions that correspond to C (so you don't have to explain them:
Emalloc (size_t size );
Efree (void * ptr );
Ecalloc (size_t nmemb, size_t size );
Erealloc (void * ptr, size_t size );
Estrdup (const char * s );
Estrndup (const char * s, unsigned int length );
We recommend that you use these memory allocation functions. The advantage of these functions is that, if the function is not released by chance within any allocation, it will be released at the end of the page request. Therefore, real memory leakage will not occur. Another important reason is that you do not need to check whether the return values of these memory allocation functions are null. When memory allocation fails, they will issue an E_ERROR error and will never return to the extension.

Values returned by PHP Functions

The extended API contains a wide range of macros used to return values from functions. These macros have two main styles: the first is the RETVAL_type () form, which sets the return value but the C code continues to execute. This is usually used when you want to clean up the control before handing it over to the script engine, and then use C's return Statement "return" to return to PHP; the latter macro is more common in the form of RETURN_type (). It sets the return type and controls the return to PHP.

Set the return value and end the Function

Set Return Value

Macro return type and Parameters

RETURN_LONG (l)

RETVAL_LONG (l)

Integer

RETURN_BOOL (B)

RETVAL_BOOL (B)

Boolean (1 or 0)

RETURN_NULL ()

RETVAL_NULL ()

NULL

RETURN_DOUBLE (d)

RETVAL_DOUBLE (d)

Floating Point Number

RETURN_STRING (s, dup)

RETVAL_STRING (s, dup)

String. If dup is 1, the engine will call estrdup () to duplicate s and use copy. If dup is 0, s is used.

RETURN_STRINGL (s, l, dup)

RETVAL_STRINGL (s, l, dup)

String value with a length of l. Similar to the previous macro, but the speed is faster because the length of s is specified.

ETURN_TRUE

RETVAL_TRUE

Returns a Boolean value of true. Note that this macro has no parentheses.

RETURN_FALSE

RETVAL_FALSE

Returns a Boolean value of false. Note that this macro has no parentheses.

RETURN_RESOURCE (r)

RETVAL_RESOURCE (r)

Resource handle.

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.