Skip to content

Commit

Permalink
add macro PHP_GRPC_INIT_HANDLER
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerou committed Jul 28, 2016
1 parent 514d296 commit 5dafd82
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 75 deletions.
20 changes: 6 additions & 14 deletions src/php/ext/grpc/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ zval *grpc_parse_metadata_array(grpc_metadata_array
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_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data) ==
SUCCESS) {
if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data)
== SUCCESS) {
if (Z_TYPE_P(data) != IS_ARRAY) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C),
"Metadata hash somehow contains wrong types.",
Expand Down Expand Up @@ -271,7 +271,6 @@ PHP_METHOD(Call, startBatch) {
char *message_str;
size_t message_len;


grpc_metadata_array_init(&metadata);
grpc_metadata_array_init(&trailing_metadata);
grpc_metadata_array_init(&recv_metadata);
Expand All @@ -291,7 +290,7 @@ PHP_METHOD(Call, startBatch) {
char *key = NULL;
int key_type;
PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
value)
value)
if (key_type != HASH_KEY_IS_LONG || key != NULL) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"batch keys must be integers", 1 TSRMLS_CC);
Expand All @@ -304,10 +303,8 @@ PHP_METHOD(Call, startBatch) {
"Bad metadata value given", 1 TSRMLS_CC);
goto cleanup;
}
ops[op_num].data.send_initial_metadata.count =
metadata.count;
ops[op_num].data.send_initial_metadata.metadata =
metadata.metadata;
ops[op_num].data.send_initial_metadata.count = metadata.count;
ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
break;
case GRPC_OP_SEND_MESSAGE:
if (Z_TYPE_P(value) != IS_ARRAY) {
Expand Down Expand Up @@ -562,10 +559,5 @@ void grpc_init_call(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
ce.create_object = create_wrapped_grpc_call;
grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&call_ce_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std);
call_ce_handlers.free_obj = free_wrapped_grpc_call;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);
}
11 changes: 2 additions & 9 deletions src/php/ext/grpc/call_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,6 @@ void grpc_init_call_credentials(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\CallCredentials", call_credentials_methods);
ce.create_object = create_wrapped_grpc_call_credentials;
grpc_ce_call_credentials = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&call_credentials_ce_handlers,
zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
call_credentials_ce_handlers.offset =
XtOffsetOf(wrapped_grpc_call_credentials, std);
call_credentials_ce_handlers.free_obj =
free_wrapped_grpc_call_credentials;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_call_credentials,
call_credentials_ce_handlers);
}
6 changes: 2 additions & 4 deletions src/php/ext/grpc/call_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call_credentials)

static inline wrapped_grpc_call_credentials
*wrapped_grpc_call_credentials_from_obj(zend_object *obj) {
return
(wrapped_grpc_call_credentials*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_call_credentials,
std));
return (wrapped_grpc_call_credentials*)(
(char*)(obj) - XtOffsetOf(wrapped_grpc_call_credentials, std));
}

#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
Expand Down
8 changes: 1 addition & 7 deletions src/php/ext/grpc/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,5 @@ void grpc_init_channel(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
ce.create_object = create_wrapped_grpc_channel;
grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&channel_ce_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
channel_ce_handlers.offset =
XtOffsetOf(wrapped_grpc_channel, std);
channel_ce_handlers.free_obj = free_wrapped_grpc_channel;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
}
11 changes: 2 additions & 9 deletions src/php/ext/grpc/channel_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,6 @@ void grpc_init_channel_credentials(TSRMLS_D) {
grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
ce.create_object = create_wrapped_grpc_channel_credentials;
grpc_ce_channel_credentials = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&channel_credentials_ce_handlers,
zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
channel_credentials_ce_handlers.offset =
XtOffsetOf(wrapped_grpc_channel_credentials, std);
channel_credentials_ce_handlers.free_obj =
free_wrapped_grpc_channel_credentials;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel_credentials,
channel_credentials_ce_handlers);
}
5 changes: 2 additions & 3 deletions src/php/ext/grpc/channel_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel_credentials)

static inline wrapped_grpc_channel_credentials
*wrapped_grpc_channel_credentials_from_obj(zend_object *obj) {
return
(wrapped_grpc_channel_credentials *)
((char*)(obj) - XtOffsetOf(wrapped_grpc_channel_credentials, std));
return (wrapped_grpc_channel_credentials *)(
(char*)(obj) - XtOffsetOf(wrapped_grpc_channel_credentials, std));
}

#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
Expand Down
11 changes: 10 additions & 1 deletion src/php/ext/grpc/php7_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len, voi

#define PHP_GRPC_GET_CLASS_ENTRY(object) zend_get_class_entry(object TSRMLS_CC)

#define PHP_GRPC_INIT_HANDLER(class_object, handler_name)

#else

#define php_grpc_int size_t
Expand Down Expand Up @@ -187,7 +189,8 @@ static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len, voi

#define PHP_GRPC_HASH_FOREACH_END() } ZEND_HASH_FOREACH_END();

static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len, void **value) {
static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
void **value) {
zval *value_tmp = zend_hash_str_find(ht, key, len -1);
if (value_tmp == NULL) {
return FAILURE;
Expand All @@ -203,6 +206,12 @@ static inline int php_grpc_zend_hash_del(HashTable *ht, char *key, int len) {

#define PHP_GRPC_GET_CLASS_ENTRY(object) Z_OBJ_P(object)->ce

#define PHP_GRPC_INIT_HANDLER(class_object, handler_name) \
memcpy(&handler_name, zend_get_std_object_handlers(), \
sizeof(zend_object_handlers)); \
handler_name.offset = XtOffsetOf(class_object, std); \
handler_name.free_obj = free_##class_object

#endif /* PHP_MAJOR_VERSION */

#endif /* PHP7_WRAPPER_GRPC_H */
9 changes: 2 additions & 7 deletions src/php/ext/grpc/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,13 @@ static zend_function_entry server_methods[] = {
PHP_ME(Server, addHttp2Port, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Server, addSecureHttp2Port, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Server, start, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
PHP_FE_END
};

void grpc_init_server(TSRMLS_D) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Grpc\\Server", server_methods);
ce.create_object = create_wrapped_grpc_server;
grpc_ce_server = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&server_ce_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
server_ce_handlers.offset = XtOffsetOf(wrapped_grpc_server, std);
server_ce_handlers.free_obj = free_wrapped_grpc_server;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_server, server_ce_handlers);
}
3 changes: 1 addition & 2 deletions src/php/ext/grpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ static inline wrapped_grpc_server
XtOffsetOf(wrapped_grpc_server, std));
}

#define Z_WRAPPED_GRPC_SERVER_P(zv) \
wrapped_grpc_server_from_obj(Z_OBJ_P((zv)))
#define Z_WRAPPED_GRPC_SERVER_P(zv) wrapped_grpc_server_from_obj(Z_OBJ_P((zv)))

#endif /* PHP_MAJOR_VERSION */

Expand Down
13 changes: 3 additions & 10 deletions src/php/ext/grpc/server_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,14 @@ PHP_METHOD(ServerCredentials, createSsl) {
static zend_function_entry server_credentials_methods[] = {
PHP_ME(ServerCredentials, createSsl, NULL,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
PHP_FE_END
};

void grpc_init_server_credentials(TSRMLS_D) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Grpc\\ServerCredentials", server_credentials_methods);
ce.create_object = create_wrapped_grpc_server_credentials;
grpc_ce_server_credentials = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&server_credentials_ce_handlers,
zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
server_credentials_ce_handlers.offset =
XtOffsetOf(wrapped_grpc_server_credentials, std);
server_credentials_ce_handlers.free_obj =
free_wrapped_grpc_server_credentials;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_server_credentials,
server_credentials_ce_handlers);
}
4 changes: 2 additions & 2 deletions src/php/ext/grpc/server_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server_credentials)

static inline wrapped_grpc_server_credentials
*wrapped_grpc_server_credentials_from_obj(zend_object *obj) {
return (wrapped_grpc_server_credentials*)
((char*)(obj) - XtOffsetOf(wrapped_grpc_server_credentials, std));
return (wrapped_grpc_server_credentials*)(
(char*)(obj) - XtOffsetOf(wrapped_grpc_server_credentials, std));
}

#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
Expand Down
8 changes: 1 addition & 7 deletions src/php/ext/grpc/timeval.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,7 @@ void grpc_init_timeval(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Timeval", timeval_methods);
ce.create_object = create_wrapped_grpc_timeval;
grpc_ce_timeval = zend_register_internal_class(&ce TSRMLS_CC);
#if PHP_MAJOR_VERSION >= 7
memcpy(&timeval_ce_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
timeval_ce_handlers.offset =
XtOffsetOf(wrapped_grpc_timeval, std);
timeval_ce_handlers.free_obj = free_wrapped_grpc_timeval;
#endif
PHP_GRPC_INIT_HANDLER(wrapped_grpc_timeval, timeval_ce_handlers);
}

void grpc_shutdown_timeval(TSRMLS_D) {}

0 comments on commit 5dafd82

Please sign in to comment.