Merge pull request #7532 from thinkerou/wrap_php7_second

PHP: use php7_wrapper to reduce duplicated codes(second part)
pull/7539/head
Stanley Cheung 8 years ago committed by GitHub
commit 31fd053180
  1. 259
      src/php/ext/grpc/call.c
  2. 30
      src/php/ext/grpc/call.h
  3. 73
      src/php/ext/grpc/call_credentials.c
  4. 19
      src/php/ext/grpc/call_credentials.h
  5. 84
      src/php/ext/grpc/channel.c
  6. 17
      src/php/ext/grpc/channel.h
  7. 53
      src/php/ext/grpc/channel_credentials.c
  8. 18
      src/php/ext/grpc/channel_credentials.h
  9. 32
      src/php/ext/grpc/php7_wrapper.h
  10. 38
      src/php/ext/grpc/server.c
  11. 17
      src/php/ext/grpc/server.h
  12. 32
      src/php/ext/grpc/server_credentials.c
  13. 19
      src/php/ext/grpc/server_credentials.h
  14. 84
      src/php/ext/grpc/timeval.c
  15. 20
      src/php/ext/grpc/timeval.h

@ -90,34 +90,54 @@ zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type
return retval;
}
/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct
should be destroyed at the end of the object's lifecycle */
zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
zval *call_object;
MAKE_STD_ZVAL(call_object);
object_init_ex(call_object, grpc_ce_call);
wrapped_grpc_call *call =
(wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC);
call->wrapped = wrapped;
call->owned = owned;
return call_object;
#else
static zend_object_handlers call_ce_handlers;
/* Frees and destroys an instance of wrapped_grpc_call */
static void free_wrapped_grpc_call(zend_object *object) {
wrapped_grpc_call *call = wrapped_grpc_call_from_obj(object);
if (call->owned && call->wrapped != NULL) {
grpc_call_destroy(call->wrapped);
}
zend_object_std_dtor(&call->std);
}
/* Initializes an instance of wrapped_grpc_call to be associated with an
* object of a class specified by class_type */
zend_object *create_wrapped_grpc_call(zend_class_entry *class_type) {
wrapped_grpc_call *intern;
intern = ecalloc(1, sizeof(wrapped_grpc_call) +
zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &call_ce_handlers;
return &intern->std;
}
#endif
/* Creates and returns a PHP array object with the data in a
* grpc_metadata_array. Returns NULL on failure */
zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
zval *grpc_parse_metadata_array(grpc_metadata_array
*metadata_array TSRMLS_DC) {
int count = metadata_array->count;
grpc_metadata *elements = metadata_array->metadata;
int i;
zval *array;
zval **data = NULL;
PHP_GRPC_MAKE_STD_ZVAL(array);
array_init(array);
int i;
HashTable *array_hash;
zval *inner_array;
char *str_key;
char *str_val;
size_t key_len;
MAKE_STD_ZVAL(array);
array_init(array);
#if PHP_MAJOR_VERSION < 7
zval **data = NULL;
#else
zval *data;
#endif
array_hash = Z_ARRVAL_P(array);
grpc_metadata *elem;
for (i = 0; i < count; i++) {
@ -127,9 +147,14 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
memcpy(str_key, elem->key, key_len);
str_val = ecalloc(elem->value_length + 1, sizeof(char));
memcpy(str_val, elem->value, elem->value_length);
#if PHP_MAJOR_VERSION < 7
if (zend_hash_find(array_hash, str_key, key_len, (void **)data) ==
SUCCESS) {
if (Z_TYPE_P(*data) != IS_ARRAY) {
#else
if ((data = zend_hash_str_find(array_hash, str_key, key_len)) != NULL) {
if (Z_TYPE_P(data) != IS_ARRAY) {
#endif
zend_throw_exception(zend_exception_get_default(TSRMLS_C),
"Metadata hash somehow contains wrong types.",
1 TSRMLS_CC);
@ -137,11 +162,18 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
efree(str_val);
return NULL;
}
add_next_index_stringl(*data, str_val, elem->value_length, false);
#if PHP_MAJOR_VERSION < 7
php_grpc_add_next_index_stringl(*data, str_val, elem->value_length,
false);
#else
php_grpc_add_next_index_stringl(data, str_val, elem->value_length,
false);
#endif
} else {
MAKE_STD_ZVAL(inner_array);
PHP_GRPC_MAKE_STD_ZVAL(inner_array);
array_init(inner_array);
add_next_index_stringl(inner_array, str_val, elem->value_length, false);
php_grpc_add_next_index_stringl(inner_array, str_val,
elem->value_length, false);
add_assoc_zval(array, str_key, inner_array);
}
}
@ -151,20 +183,27 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
/* Populates a grpc_metadata_array with the data in a PHP array object.
Returns true on success and false on failure */
bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
HashTable *array_hash;
HashTable *inner_array_hash;
#if PHP_MAJOR_VERSION < 7
zval **inner_array;
zval **value;
HashTable *array_hash;
HashPosition array_pointer;
HashTable *inner_array_hash;
HashPosition inner_array_pointer;
char *key;
uint key_len;
ulong index;
#else
zval *inner_array;
zval *value;
zend_string *key;
#endif
if (Z_TYPE_P(array) != IS_ARRAY) {
return false;
}
grpc_metadata_array_init(metadata);
array_hash = Z_ARRVAL_P(array);
#if PHP_MAJOR_VERSION < 7
for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
&array_pointer) == SUCCESS;
@ -179,7 +218,22 @@ bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
inner_array_hash = Z_ARRVAL_P(*inner_array);
metadata->capacity += zend_hash_num_elements(inner_array_hash);
}
#else
ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) {
if (key == NULL) {
return false;
}
if (Z_TYPE_P(inner_array) != IS_ARRAY) {
return false;
}
inner_array_hash = HASH_OF(inner_array);
metadata->capacity += zend_hash_num_elements(inner_array_hash);
} ZEND_HASH_FOREACH_END();
#endif
metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
#if PHP_MAJOR_VERSION < 7
for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
&array_pointer) == SUCCESS;
@ -203,113 +257,7 @@ bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
metadata->count += 1;
}
}
return true;
}
#else
static zend_object_handlers call_ce_handlers;
/* Frees and destroys an instance of wrapped_grpc_call */
static void free_wrapped_grpc_call(zend_object *object) {
wrapped_grpc_call *call = wrapped_grpc_call_from_obj(object);
if (call->owned && call->wrapped != NULL) {
grpc_call_destroy(call->wrapped);
}
zend_object_std_dtor(&call->std);
}
/* Initializes an instance of wrapped_grpc_call to be associated with an
* object of a class specified by class_type */
zend_object *create_wrapped_grpc_call(zend_class_entry *class_type) {
wrapped_grpc_call *intern;
intern = ecalloc(1, sizeof(wrapped_grpc_call) +
zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &call_ce_handlers;
return &intern->std;
}
/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
struct should be destroyed at the end of the object's lifecycle */
void grpc_php_wrap_call(grpc_call *wrapped, bool owned, zval *call_object) {
object_init_ex(call_object, grpc_ce_call);
wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object);
call->wrapped = wrapped;
call->owned = owned;
}
/* Creates and returns a PHP array object with the data in a
* grpc_metadata_array. Returns NULL on failure */
void grpc_parse_metadata_array(grpc_metadata_array *metadata_array,
zval *array) {
int count = metadata_array->count;
grpc_metadata *elements = metadata_array->metadata;
int i;
zval *data;
HashTable *array_hash;
zval inner_array;
char *str_key;
char *str_val;
size_t key_len;
array_init(array);
array_hash = HASH_OF(array);
grpc_metadata *elem;
for (i = 0; i < count; i++) {
elem = &elements[i];
key_len = strlen(elem->key);
str_key = ecalloc(key_len + 1, sizeof(char));
memcpy(str_key, elem->key, key_len);
str_val = ecalloc(elem->value_length + 1, sizeof(char));
memcpy(str_val, elem->value, elem->value_length);
if ((data = zend_hash_str_find(array_hash, str_key, key_len)) != NULL) {
if (Z_TYPE_P(data) != IS_ARRAY) {
zend_throw_exception(zend_exception_get_default(),
"Metadata hash somehow contains wrong types.",
1);
efree(str_key);
efree(str_val);
return;
}
add_next_index_stringl(data, str_val, elem->value_length);
} else {
array_init(&inner_array);
add_next_index_stringl(&inner_array, str_val, elem->value_length);
add_assoc_zval(array, str_key, &inner_array);
}
}
}
/* Populates a grpc_metadata_array with the data in a PHP array object.
Returns true on success and false on failure */
bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
zval *inner_array;
zval *value;
HashTable *array_hash;
HashTable *inner_array_hash;
zend_string *key;
if (Z_TYPE_P(array) != IS_ARRAY) {
return false;
}
grpc_metadata_array_init(metadata);
array_hash = HASH_OF(array);
ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) {
if (key == NULL) {
return false;
}
if (Z_TYPE_P(inner_array) != IS_ARRAY) {
return false;
}
inner_array_hash = HASH_OF(inner_array);
metadata->capacity += zend_hash_num_elements(inner_array_hash);
}
ZEND_HASH_FOREACH_END();
metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) {
if (key == NULL) {
return false;
@ -326,10 +274,21 @@ bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
metadata->count += 1;
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
#endif
return true;
}
#endif
/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
struct should be destroyed at the end of the object's lifecycle */
zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
zval *call_object;
PHP_GRPC_MAKE_STD_ZVAL(call_object);
object_init_ex(call_object, grpc_ce_call);
wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object);
call->wrapped = wrapped;
call->owned = owned;
return call_object;
}
/**
* Constructs a new instance of the Call class.
@ -379,6 +338,10 @@ PHP_METHOD(Call, __construct) {
* @return object Object with results of all actions
*/
PHP_METHOD(Call, startBatch) {
zval *result;
PHP_GRPC_MAKE_STD_ZVAL(result);
object_init(result);
php_grpc_ulong index;
#if PHP_MAJOR_VERSION < 7
zval **value;
zval **inner_value;
@ -387,20 +350,14 @@ PHP_METHOD(Call, startBatch) {
zval **message_flags;
char *key;
uint key_len;
ulong index;
zval *result;
zval *recv_status;
MAKE_STD_ZVAL(result);
object_init(result);
#else
zval *value;
zval *inner_value;
zval *message_value;
zval *message_flags;
zend_string *key;
zend_ulong index;
zval recv_status;
object_init(return_value);
#endif
wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
@ -574,7 +531,7 @@ PHP_METHOD(Call, startBatch) {
#else
array_hash = HASH_OF(array);
array_hash = HASH_OF(array);
ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) {
if (key) {
zend_throw_exception(spl_ce_InvalidArgumentException,
@ -692,8 +649,7 @@ array_hash = HASH_OF(array);
ops[op_num].flags = 0;
ops[op_num].reserved = NULL;
op_num++;
}
ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();
#endif
@ -755,43 +711,44 @@ array_hash = HASH_OF(array);
}
}
#else
zval recv_md;
for (int i = 0; i < op_num; i++) {
switch(ops[i].op) {
case GRPC_OP_SEND_INITIAL_METADATA:
add_property_bool(return_value, "send_metadata", true);
add_property_bool(result, "send_metadata", true);
break;
case GRPC_OP_SEND_MESSAGE:
add_property_bool(return_value, "send_message", true);
add_property_bool(result, "send_message", true);
break;
case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
add_property_bool(return_value, "send_close", true);
add_property_bool(result, "send_close", true);
break;
case GRPC_OP_SEND_STATUS_FROM_SERVER:
add_property_bool(return_value, "send_status", true);
add_property_bool(result, "send_status", true);
break;
case GRPC_OP_RECV_INITIAL_METADATA:
grpc_parse_metadata_array(&recv_metadata, array);
add_property_zval(return_value, "metadata", array);
recv_md = *grpc_parse_metadata_array(&recv_metadata);
add_property_zval(result, "metadata", &recv_md);
break;
case GRPC_OP_RECV_MESSAGE:
byte_buffer_to_string(message, &message_str, &message_len);
if (message_str == NULL) {
add_property_null(return_value, "message");
add_property_null(result, "message");
} else {
add_property_stringl(return_value, "message", message_str,
add_property_stringl(result, "message", message_str,
message_len);
}
break;
case GRPC_OP_RECV_STATUS_ON_CLIENT:
object_init(&recv_status);
grpc_parse_metadata_array(&recv_trailing_metadata, array);
add_property_zval(&recv_status, "metadata", array);
recv_md = *grpc_parse_metadata_array(&recv_trailing_metadata);
add_property_zval(&recv_status, "metadata", &recv_md);
add_property_long(&recv_status, "code", status);
add_property_string(&recv_status, "details", status_details);
add_property_zval(return_value, "status", &recv_status);
add_property_zval(result, "status", &recv_status);
break;
case GRPC_OP_RECV_CLOSE_ON_SERVER:
add_property_bool(return_value, "cancelled", cancelled);
add_property_bool(result, "cancelled", cancelled);
break;
default:
break;
@ -815,11 +772,7 @@ cleanup:
grpc_byte_buffer_destroy(message);
}
}
#if PHP_MAJOR_VERSION < 7
RETURN_DESTROY_ZVAL(result);
#else
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**

@ -48,34 +48,19 @@
/* Class entry for the Call PHP class */
extern zend_class_entry *grpc_ce_call;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_call that can be associated with a PHP object */
typedef struct wrapped_grpc_call {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call)
bool owned;
grpc_call *wrapped;
} wrapped_grpc_call;
/* Creates a Call object that wraps the given grpc_call struct */
zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC);
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call)
/* Creates and returns a PHP associative array of metadata from a C array of
* call metadata */
zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC);
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CALL_P(zv) \
(wrapped_grpc_call *)zend_object_store_get_object(zv TSRMLS_CC)
#else
/* Wrapper struct for grpc_call that can be associated with a PHP object */
typedef struct wrapped_grpc_call {
bool owned;
grpc_call *wrapped;
zend_object std;
} wrapped_grpc_call;
static inline wrapped_grpc_call
*wrapped_grpc_call_from_obj(zend_object *obj) {
return (wrapped_grpc_call*)((char*)(obj) -
@ -84,15 +69,14 @@ static inline wrapped_grpc_call
#define Z_WRAPPED_GRPC_CALL_P(zv) wrapped_grpc_call_from_obj(Z_OBJ_P((zv)))
/* Creates a Call object that wraps the given grpc_call struct */
void grpc_php_wrap_call(grpc_call *wrapped, bool owned, zval *call_object);
#endif /* PHP_MAJOR_VERSION */
/* Creates and returns a PHP associative array of metadata from a C array of
* call metadata */
void grpc_parse_metadata_array(grpc_metadata_array *metadata_array,
zval *array);
zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC);
#endif /* PHP_MAJOR_VERSION */
/* Creates a Call object that wraps the given grpc_call struct */
zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC);
/* Initializes the Call PHP class */
void grpc_init_call(TSRMLS_D);

@ -86,17 +86,6 @@ zend_object_value create_wrapped_grpc_call_credentials(
return retval;
}
zval *grpc_php_wrap_call_credentials(grpc_call_credentials *wrapped TSRMLS_DC) {
zval *credentials_object;
MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_call_credentials);
wrapped_grpc_call_credentials *credentials =
(wrapped_grpc_call_credentials *)zend_object_store_get_object(
credentials_object TSRMLS_CC);
credentials->wrapped = wrapped;
return credentials_object;
}
#else
static zend_object_handlers call_credentials_ce_handlers;
@ -124,16 +113,19 @@ zend_object *create_wrapped_grpc_call_credentials(zend_class_entry
return &intern->std;
}
void grpc_php_wrap_call_credentials(grpc_call_credentials *wrapped,
zval *credentials_object) {
#endif
zval *grpc_php_wrap_call_credentials(grpc_call_credentials
*wrapped TSRMLS_DC) {
zval *credentials_object;
PHP_GRPC_MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_call_credentials);
wrapped_grpc_call_credentials *credentials =
Z_WRAPPED_GRPC_CALL_CREDS_P(credentials_object);
credentials->wrapped = wrapped;
return credentials_object;
}
#endif
/**
* Create composite credentials from two existing credentials.
* @param CallCredentials cred1 The first credential
@ -160,13 +152,10 @@ PHP_METHOD(CallCredentials, createComposite) {
grpc_call_credentials *creds =
grpc_composite_call_credentials_create(cred1->wrapped, cred2->wrapped,
NULL);
#if PHP_MAJOR_VERSION < 7
zval *creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_php_wrap_call_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -207,13 +196,10 @@ PHP_METHOD(CallCredentials, createFromPlugin) {
grpc_call_credentials *creds =
grpc_metadata_credentials_create_from_plugin(plugin, NULL);
#if PHP_MAJOR_VERSION < 7
zval *creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_php_wrap_call_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/* Callback function for plugin creds API */
@ -226,37 +212,28 @@ void plugin_get_metadata(void *ptr, grpc_auth_metadata_context context,
/* prepare to call the user callback function with info from the
* grpc_auth_metadata_context */
#if PHP_MAJOR_VERSION < 7
zval **params[1];
zval *arg;
zval *retval;
MAKE_STD_ZVAL(arg);
PHP_GRPC_MAKE_STD_ZVAL(arg);
object_init(arg);
add_property_string(arg, "service_url", context.service_url, true);
add_property_string(arg, "method_name", context.method_name, true);
php_grpc_add_property_string(arg, "service_url", context.service_url, true);
php_grpc_add_property_string(arg, "method_name", context.method_name, true);
zval *retval;
PHP_GRPC_MAKE_STD_ZVAL(retval);
#if PHP_MAJOR_VERSION < 7
zval **params[1];
params[0] = &arg;
state->fci->param_count = 1;
state->fci->params = params;
state->fci->retval_ptr_ptr = &retval;
#else
zval arg;
zval retval;
object_init(&arg);
add_property_string(&arg, "service_url", context.service_url);
add_property_string(&arg, "method_name", context.method_name);
state->fci->param_count = 1;
state->fci->params = &arg;
state->fci->retval = &retval;
state->fci->params = arg;
state->fci->retval = retval;
#endif
state->fci->param_count = 1;
/* call the user callback function */
zend_call_function(state->fci, state->fci_cache TSRMLS_CC);
#if PHP_MAJOR_VERSION < 7
if (Z_TYPE_P(retval) != IS_ARRAY) {
#else
if (Z_TYPE_P(&retval) != IS_ARRAY) {
#endif
zend_throw_exception(spl_ce_InvalidArgumentException,
"plugin callback must return metadata array",
1 TSRMLS_CC);
@ -264,11 +241,7 @@ void plugin_get_metadata(void *ptr, grpc_auth_metadata_context context,
}
grpc_metadata_array metadata;
#if PHP_MAJOR_VERSION < 7
if (!create_metadata_array(retval, &metadata)) {
#else
if (!create_metadata_array(&retval, &metadata)) {
#endif
zend_throw_exception(spl_ce_InvalidArgumentException,
"invalid metadata", 1 TSRMLS_CC);
grpc_metadata_array_destroy(&metadata);

@ -49,25 +49,18 @@
/* Class entry for the CallCredentials PHP class */
extern zend_class_entry *grpc_ce_call_credentials;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_call_credentials that can be associated
* with a PHP object */
typedef struct wrapped_grpc_call_credentials {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call_credentials)
grpc_call_credentials *wrapped;
} wrapped_grpc_call_credentials;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call_credentials)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
(wrapped_grpc_call_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
#else
/* Wrapper struct for grpc_call_credentials that can be associated
* with a PHP object */
typedef struct wrapped_grpc_call_credentials {
grpc_call_credentials *wrapped;
zend_object std;
} wrapped_grpc_call_credentials;
#else
static inline wrapped_grpc_call_credentials
*wrapped_grpc_call_creds_from_obj(zend_object *obj) {
@ -77,7 +70,7 @@ static inline wrapped_grpc_call_credentials
std));
}
#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
wrapped_grpc_call_creds_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */

@ -86,19 +86,58 @@ zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type
return retval;
}
#else
static zend_object_handlers channel_ce_handlers;
/* Frees and destroys an instance of wrapped_grpc_channel */
static void free_wrapped_grpc_channel(zend_object *object) {
wrapped_grpc_channel *channel = wrapped_grpc_channel_from_obj(object);
if (channel->wrapped != NULL) {
grpc_channel_destroy(channel->wrapped);
}
zend_object_std_dtor(&channel->std);
}
/* Initializes an instance of wrapped_grpc_channel to be associated with an
* object of a class specified by class_type */
zend_object *create_wrapped_grpc_channel(zend_class_entry *class_type) {
wrapped_grpc_channel *intern;
intern = ecalloc(1, sizeof(wrapped_grpc_channel) +
zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &channel_ce_handlers;
return &intern->std;
}
#endif
void php_grpc_read_args_array(zval *args_array,
grpc_channel_args *args TSRMLS_DC) {
HashTable *array_hash;
HashPosition array_pointer;
int args_index;
#if PHP_MAJOR_VERSION < 7
HashPosition array_pointer;
zval **data;
char *key;
uint key_len;
ulong index;
#else
zval *data;
zend_string *key;
#endif
array_hash = Z_ARRVAL_P(args_array);
if (!array_hash) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"array_hash is NULL", 1);
return;
}
args->num_args = zend_hash_num_elements(array_hash);
args->args = ecalloc(args->num_args, sizeof(grpc_arg));
args_index = 0;
#if PHP_MAJOR_VERSION < 7
for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
zend_hash_get_current_data_ex(array_hash, (void **)&data,
&array_pointer) == SUCCESS;
@ -126,47 +165,7 @@ void php_grpc_read_args_array(zval *args_array,
}
args_index++;
}
}
#else
static zend_object_handlers channel_ce_handlers;
/* Frees and destroys an instance of wrapped_grpc_channel */
static void free_wrapped_grpc_channel(zend_object *object) {
wrapped_grpc_channel *channel = wrapped_grpc_channel_from_obj(object);
if (channel->wrapped != NULL) {
grpc_channel_destroy(channel->wrapped);
}
zend_object_std_dtor(&channel->std);
}
/* Initializes an instance of wrapped_grpc_channel to be associated with an
* object of a class specified by class_type */
zend_object *create_wrapped_grpc_channel(zend_class_entry *class_type) {
wrapped_grpc_channel *intern;
intern = ecalloc(1, sizeof(wrapped_grpc_channel) +
zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &channel_ce_handlers;
return &intern->std;
}
void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
HashTable *array_hash;
int args_index;
zval *data;
zend_string *key;
array_hash = HASH_OF(args_array);
if (!array_hash) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"array_hash is NULL", 1);
return;
}
args->num_args = zend_hash_num_elements(array_hash);
args->args = ecalloc(args->num_args, sizeof(grpc_arg));
args_index = 0;
ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, data) {
if (key == NULL) {
zend_throw_exception(spl_ce_InvalidArgumentException,
@ -189,9 +188,8 @@ void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
}
args_index++;
} ZEND_HASH_FOREACH_END();
}
#endif
}
/**
* Construct an instance of the Channel class. If the $args array contains a

@ -48,32 +48,25 @@
/* Class entry for the PHP Channel class */
extern zend_class_entry *grpc_ce_channel;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_channel that can be associated with a PHP object */
typedef struct wrapped_grpc_channel {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel)
grpc_channel *wrapped;
} wrapped_grpc_channel;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
(wrapped_grpc_channel *)zend_object_store_get_object(zv TSRMLS_CC)
#else
/* Wrapper struct for grpc_channel that can be associated with a PHP object */
typedef struct wrapped_grpc_channel {
grpc_channel *wrapped;
zend_object std;
} wrapped_grpc_channel;
static inline wrapped_grpc_channel
*wrapped_grpc_channel_from_obj(zend_object *obj) {
return (wrapped_grpc_channel*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_channel, std));
}
#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
wrapped_grpc_channel_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */

@ -96,18 +96,6 @@ zend_object_value create_wrapped_grpc_channel_credentials(
return retval;
}
zval *grpc_php_wrap_channel_credentials(grpc_channel_credentials
*wrapped TSRMLS_DC) {
zval *credentials_object;
MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_channel_credentials);
wrapped_grpc_channel_credentials *credentials =
(wrapped_grpc_channel_credentials *)zend_object_store_get_object(
credentials_object TSRMLS_CC);
credentials->wrapped = wrapped;
return credentials_object;
}
#else
static zend_object_handlers channel_credentials_ce_handlers;
@ -135,16 +123,19 @@ zend_object *create_wrapped_grpc_channel_credentials(zend_class_entry
return &intern->std;
}
void grpc_php_wrap_channel_credentials(grpc_channel_credentials *wrapped,
zval *credentials_object) {
#endif
zval *grpc_php_wrap_channel_credentials(grpc_channel_credentials
*wrapped TSRMLS_DC) {
zval *credentials_object;
PHP_GRPC_MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_channel_credentials);
wrapped_grpc_channel_credentials *credentials =
Z_WRAPPED_GRPC_CHANNEL_CREDS_P(credentials_object);
credentials->wrapped = wrapped;
return credentials_object;
}
#endif
/**
* Set default roots pem.
* @param string pem_roots PEM encoding of the server root certificates
@ -171,13 +162,10 @@ PHP_METHOD(ChannelCredentials, setDefaultRootsPem) {
*/
PHP_METHOD(ChannelCredentials, createDefault) {
grpc_channel_credentials *creds = grpc_google_default_credentials_create();
#if PHP_MAJOR_VERSION < 7
zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_php_wrap_channel_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -213,13 +201,10 @@ PHP_METHOD(ChannelCredentials, createSsl) {
grpc_channel_credentials *creds = grpc_ssl_credentials_create(
pem_root_certs,
pem_key_cert_pair.private_key == NULL ? NULL : &pem_key_cert_pair, NULL);
#if PHP_MAJOR_VERSION < 7
zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_php_wrap_channel_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -244,19 +229,13 @@ PHP_METHOD(ChannelCredentials, createComposite) {
Z_WRAPPED_GRPC_CHANNEL_CREDS_P(cred1_obj);
wrapped_grpc_call_credentials *cred2 =
Z_WRAPPED_GRPC_CALL_CREDS_P(cred2_obj);
#if PHP_MAJOR_VERSION < 7
grpc_channel_credentials *creds =
grpc_composite_channel_credentials_create(cred1->wrapped, cred2->wrapped,
NULL);
zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_channel_credentials *creds =
grpc_composite_channel_credentials_create(cred1->wrapped,
cred2->wrapped, NULL);
grpc_php_wrap_channel_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**

@ -49,27 +49,19 @@
/* Class entry for the ChannelCredentials PHP class */
extern zend_class_entry *grpc_ce_channel_credentials;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_channel_credentials that can be associated
* with a PHP object */
typedef struct wrapped_grpc_channel_credentials {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel_credentials)
grpc_channel_credentials *wrapped;
} wrapped_grpc_channel_credentials;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel_credentials)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
(wrapped_grpc_channel_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
#else
/* Wrapper struct for grpc_channel_credentials that can be associated
* with a PHP object */
typedef struct wrapped_grpc_channel_credentials {
grpc_channel_credentials *wrapped;
zend_object std;
} wrapped_grpc_channel_credentials;
static inline wrapped_grpc_channel_credentials
*wrapped_grpc_channel_creds_from_obj(zend_object *obj) {
return
@ -78,7 +70,7 @@ static inline wrapped_grpc_channel_credentials
XtOffsetOf(wrapped_grpc_channel_credentials, std));
}
#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
wrapped_grpc_channel_creds_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */

@ -39,13 +39,45 @@
#define php_grpc_int int
#define php_grpc_long long
#define php_grpc_ulong ulong
#define php_grpc_add_property_string(arg, name, context, b) \
add_property_string(arg, name, context, b)
#define php_grpc_add_property_stringl(res, name, str, len, b) \
add_property_stringl(res, name, str, len, b)
#define php_grpc_add_next_index_stringl(data, str, len, b) \
add_next_index_stringl(data, str, len, b)
#define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val, dup)
#define PHP_GRPC_MAKE_STD_ZVAL(pzv) MAKE_STD_ZVAL(pzv)
#define PHP_GRPC_WRAP_OBJECT_START(name) \
typedef struct name { \
zend_object std;
#define PHP_GRPC_WRAP_OBJECT_END(name) \
} name;
#else
#define php_grpc_int size_t
#define php_grpc_long zend_long
#define php_grpc_ulong zend_ulong
#define php_grpc_add_property_string(arg, name, context, b) \
add_property_string(arg, name, context)
#define php_grpc_add_property_stringl(res, name, str, len, b) \
add_property_stringl(res, name, str, len)
#define php_grpc_add_next_index_stringl(data, str, len, b) \
add_next_index_stringl(data, str, len)
#define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val)
#define PHP_GRPC_MAKE_STD_ZVAL(pzv) \
zval _stack_zval_##pzv; \
pzv = &(_stack_zval_##pzv)
#define PHP_GRPC_WRAP_OBJECT_START(name) \
typedef struct name {
#define PHP_GRPC_WRAP_OBJECT_END(name) \
zend_object std; \
} name;
#endif /* PHP_MAJOR_VERSION */

@ -168,13 +168,9 @@ PHP_METHOD(Server, requestCall) {
grpc_event event;
wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis());
#if PHP_MAJOR_VERSION < 7
zval *result;
MAKE_STD_ZVAL(result);
PHP_GRPC_MAKE_STD_ZVAL(result);
object_init(result);
#else
object_init(return_value);
#endif
grpc_call_details_init(&details);
grpc_metadata_array_init(&metadata);
@ -195,40 +191,32 @@ PHP_METHOD(Server, requestCall) {
1 TSRMLS_CC);
goto cleanup;
}
php_grpc_add_property_string(result, "method", details.method, true);
php_grpc_add_property_string(result, "host", details.host, true);
#if PHP_MAJOR_VERSION < 7
add_property_zval(result, "call", grpc_php_wrap_call(call, true TSRMLS_CC));
add_property_string(result, "method", details.method, true);
add_property_string(result, "host", details.host, true);
add_property_zval(result, "absolute_deadline",
grpc_php_wrap_timeval(details.deadline TSRMLS_CC));
add_property_zval(result, "metadata", grpc_parse_metadata_array(&metadata
TSRMLS_CC));
cleanup:
grpc_call_details_destroy(&details);
grpc_metadata_array_destroy(&metadata);
RETURN_DESTROY_ZVAL(result);
#else
zval zv_call;
zval zv_timeval;
zval zv_md;
grpc_php_wrap_call(call, true, &zv_call);
grpc_php_wrap_timeval(details.deadline, &zv_timeval);
grpc_parse_metadata_array(&metadata, &zv_md);
add_property_zval(return_value, "call", &zv_call);
add_property_string(return_value, "method", details.method);
add_property_string(return_value, "host", details.host);
add_property_zval(return_value, "absolute_deadline", &zv_timeval);
add_property_zval(return_value, "metadata", &zv_md);
//TODO(thinkerou): why use zval* to unit test error?
zv_call = *grpc_php_wrap_call(call, true);
zv_timeval = *grpc_php_wrap_timeval(details.deadline);
zv_md = *grpc_parse_metadata_array(&metadata);
add_property_zval(result, "call", &zv_call);
add_property_zval(result, "absolute_deadline", &zv_timeval);
add_property_zval(result, "metadata", &zv_md);
#endif
cleanup:
grpc_call_details_destroy(&details);
grpc_metadata_array_destroy(&metadata);
RETURN_DESTROY_ZVAL(return_value);
#endif
RETURN_DESTROY_ZVAL(result);
}
/**

@ -48,32 +48,25 @@
/* Class entry for the Server PHP class */
extern zend_class_entry *grpc_ce_server;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_server that can be associated with a PHP object */
typedef struct wrapped_grpc_server {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server)
grpc_server *wrapped;
} wrapped_grpc_server;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_SERVER_P(zv) \
(wrapped_grpc_server *)zend_object_store_get_object(zv TSRMLS_CC)
#else
/* Wrapper struct for grpc_server that can be associated with a PHP object */
typedef struct wrapped_grpc_server {
grpc_server *wrapped;
zend_object std;
} wrapped_grpc_server;
static inline wrapped_grpc_server
*wrapped_grpc_server_from_obj(zend_object *obj) {
return (wrapped_grpc_server*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_server, std));
}
#define Z_WRAPPED_GRPC_SERVER_P(zv) \
#define Z_WRAPPED_GRPC_SERVER_P(zv) \
wrapped_grpc_server_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */

@ -84,18 +84,6 @@ zend_object_value create_wrapped_grpc_server_credentials(
return retval;
}
zval *grpc_php_wrap_server_credentials(grpc_server_credentials
*wrapped TSRMLS_DC) {
zval *server_credentials_object;
MAKE_STD_ZVAL(server_credentials_object);
object_init_ex(server_credentials_object, grpc_ce_server_credentials);
wrapped_grpc_server_credentials *server_credentials =
(wrapped_grpc_server_credentials *)zend_object_store_get_object(
server_credentials_object TSRMLS_CC);
server_credentials->wrapped = wrapped;
return server_credentials_object;
}
#else
static zend_object_handlers server_credentials_ce_handlers;
@ -123,16 +111,19 @@ zend_object *create_wrapped_grpc_server_credentials(zend_class_entry
return &intern->std;
}
void grpc_php_wrap_server_credentials(grpc_server_credentials *wrapped,
zval *server_credentials_object) {
#endif
zval *grpc_php_wrap_server_credentials(grpc_server_credentials
*wrapped TSRMLS_DC) {
zval *server_credentials_object;
PHP_GRPC_MAKE_STD_ZVAL(server_credentials_object);
object_init_ex(server_credentials_object, grpc_ce_server_credentials);
wrapped_grpc_server_credentials *server_credentials =
Z_WRAPPED_GRPC_SERVER_CREDS_P(server_credentials_object);
server_credentials->wrapped = wrapped;
return server_credentials_object;
}
#endif
/**
* Create SSL credentials.
* @param string pem_root_certs PEM encoding of the server root certificates
@ -163,13 +154,10 @@ PHP_METHOD(ServerCredentials, createSsl) {
grpc_server_credentials *creds = grpc_ssl_server_credentials_create_ex(
pem_root_certs, &pem_key_cert_pair, 1,
GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, NULL);
#if PHP_MAJOR_VERSION < 7
zval *creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
zval *creds_object;
PHP_GRPC_MAKE_STD_ZVAL(creds_object);
creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
#else
grpc_php_wrap_server_credentials(creds, return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
static zend_function_entry server_credentials_methods[] = {

@ -49,33 +49,26 @@
/* Class entry for the Server_Credentials PHP class */
extern zend_class_entry *grpc_ce_server_credentials;
#if PHP_MAJOR_VERSION < 7
/* Wrapper struct for grpc_server_credentials that can be associated with a PHP
* object */
typedef struct wrapped_grpc_server_credentials {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server_credentials)
grpc_server_credentials *wrapped;
} wrapped_grpc_server_credentials;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server_credentials)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
(wrapped_grpc_server_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
#else
typedef struct wrapped_grpc_server_credentials {
grpc_server_credentials *wrapped;
zend_object std;
} wrapped_grpc_server_credentials;
static inline wrapped_grpc_server_credentials
*wrapped_grpc_server_creds_from_obj(zend_object *obj) {
return (wrapped_grpc_server_credentials*)
((char*)(obj) -
XtOffsetOf(wrapped_grpc_server_credentials, std));
((char*)(obj) - XtOffsetOf(wrapped_grpc_server_credentials, std));
}
#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
wrapped_grpc_server_creds_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */

@ -56,9 +56,9 @@ zend_class_entry *grpc_ce_timeval;
/* Frees and destroys an instance of wrapped_grpc_call */
void free_wrapped_grpc_timeval(void *object TSRMLS_DC) {
wrapped_grpc_timeval *timeval = (wrapped_grpc_timeval *)object;
zend_object_std_dtor(&timeval->std TSRMLS_CC);
efree(timeval);
wrapped_grpc_timeval *timeval = (wrapped_grpc_timeval *)object;
zend_object_std_dtor(&timeval->std TSRMLS_CC);
efree(timeval);
}
/* Initializes an instance of wrapped_grpc_timeval to be associated with an
@ -78,17 +78,6 @@ zend_object_value create_wrapped_grpc_timeval(zend_class_entry *class_type
return retval;
}
zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) {
zval *timeval_object;
MAKE_STD_ZVAL(timeval_object);
object_init_ex(timeval_object, grpc_ce_timeval);
wrapped_grpc_timeval *timeval =
(wrapped_grpc_timeval *)zend_object_store_get_object(
timeval_object TSRMLS_CC);
memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec));
return timeval_object;
}
#else
static zend_object_handlers timeval_ce_handlers;
@ -111,14 +100,17 @@ zend_object *create_wrapped_grpc_timeval(zend_class_entry *class_type) {
return &intern->std;
}
void grpc_php_wrap_timeval(gpr_timespec wrapped, zval *timeval_object) {
#endif
zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) {
zval *timeval_object;
PHP_GRPC_MAKE_STD_ZVAL(timeval_object);
object_init_ex(timeval_object, grpc_ce_timeval);
wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(timeval_object);
memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec));
return timeval_object;
}
#endif
/**
* Constructs a new instance of the Timeval class
* @param long $usec The number of microseconds in the interval
@ -156,16 +148,12 @@ PHP_METHOD(Timeval, add) {
}
wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis());
wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj);
#if PHP_MAJOR_VERSION < 7
zval *sum =
zval *sum;
PHP_GRPC_MAKE_STD_ZVAL(sum);
sum =
grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped)
TSRMLS_CC);
RETURN_DESTROY_ZVAL(sum);
#else
grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped),
return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -186,16 +174,12 @@ PHP_METHOD(Timeval, subtract) {
}
wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis());
wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj);
#if PHP_MAJOR_VERSION < 7
zval *diff =
zval *diff;
PHP_GRPC_MAKE_STD_ZVAL(diff);
diff =
grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped)
TSRMLS_CC);
RETURN_DESTROY_ZVAL(diff);
#else
grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped),
return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -255,13 +239,10 @@ PHP_METHOD(Timeval, similar) {
* @return Timeval The current time
*/
PHP_METHOD(Timeval, now) {
#if PHP_MAJOR_VERSION < 7
zval *now = grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME) TSRMLS_CC);
zval *now;
PHP_GRPC_MAKE_STD_ZVAL(now);
now = grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(now);
#else
grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME), return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -269,18 +250,13 @@ PHP_METHOD(Timeval, now) {
* @return Timeval Zero length time interval
*/
PHP_METHOD(Timeval, zero) {
#if PHP_MAJOR_VERSION < 7
zval *grpc_php_timeval_zero =
zval *grpc_php_timeval_zero;
PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_zero);
grpc_php_timeval_zero =
grpc_php_wrap_timeval(gpr_time_0(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_ZVAL(grpc_php_timeval_zero,
false, /* Copy original before returning? */
true /* Destroy original before returning */);
#else
grpc_php_wrap_timeval(gpr_time_0(GPR_CLOCK_REALTIME), return_value);
RETURN_ZVAL(return_value,
false, /* Copy original before returning? */
true /* Destroy original before returning */);
#endif
}
/**
@ -288,14 +264,11 @@ PHP_METHOD(Timeval, zero) {
* @return Timeval Infinite future time value
*/
PHP_METHOD(Timeval, infFuture) {
#if PHP_MAJOR_VERSION < 7
zval *grpc_php_timeval_inf_future =
zval *grpc_php_timeval_inf_future;
PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_inf_future);
grpc_php_timeval_inf_future =
grpc_php_wrap_timeval(gpr_inf_future(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_future);
#else
grpc_php_wrap_timeval(gpr_inf_future(GPR_CLOCK_REALTIME), return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**
@ -303,14 +276,11 @@ PHP_METHOD(Timeval, infFuture) {
* @return Timeval Infinite past time value
*/
PHP_METHOD(Timeval, infPast) {
#if PHP_MAJOR_VERSION < 7
zval *grpc_php_timeval_inf_past =
zval *grpc_php_timeval_inf_past;
PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_inf_past);
grpc_php_timeval_inf_past =
grpc_php_wrap_timeval(gpr_inf_past(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_past);
#else
grpc_php_wrap_timeval(gpr_inf_past(GPR_CLOCK_REALTIME), return_value);
RETURN_DESTROY_ZVAL(return_value);
#endif
}
/**

@ -50,30 +50,24 @@
extern zend_class_entry *grpc_ce_timeval;
/* Wrapper struct for timeval that can be associated with a PHP object */
#if PHP_MAJOR_VERSION < 7
typedef struct wrapped_grpc_timeval {
zend_object std;
PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_timeval)
gpr_timespec wrapped;
} wrapped_grpc_timeval;
PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_timeval)
#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
(wrapped_grpc_timeval *)zend_object_store_get_object(zv TSRMLS_CC)
#else
typedef struct wrapped_grpc_timeval {
gpr_timespec wrapped;
zend_object std;
} wrapped_grpc_timeval;
static inline wrapped_grpc_timeval
*wrapped_grpc_timeval_from_obj(zend_object *obj) {
return (wrapped_grpc_timeval*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_timeval, std));
}
#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
wrapped_grpc_timeval_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
@ -85,10 +79,6 @@ void grpc_init_timeval(TSRMLS_D);
void grpc_shutdown_timeval(TSRMLS_D);
/* Creates a Timeval object that wraps the given timeval struct */
#if PHP_MAJOR_VERSION < 7
zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC);
#else
void grpc_php_wrap_timeval(gpr_timespec wrapped, zval *timeval_object);
#endif /* PHP_MAJOR_VERSION */
#endif /* NET_GRPC_PHP_GRPC_TIMEVAL_H_ */

Loading…
Cancel
Save