aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/ext/google/protobuf/protobuf.c
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-08-25 08:49:34 -0700
committerGravatar GitHub <noreply@github.com>2017-08-25 08:49:34 -0700
commitc7457ef65a7a8584b1e3bd396c401ccf8e275ffa (patch)
treea142f1ed092807b1bd38683f57e37bcf852f665a /php/ext/google/protobuf/protobuf.c
parent98a3734b5aa680f565af10a5fd4430baa4b4aa10 (diff)
Add any support in php runtime. (#3486)
* Add any support in php runtime. * Remove unused file in config.m4 * Fix comments * Fix error for tsrmls build * Add newly added file to Makefile.am
Diffstat (limited to 'php/ext/google/protobuf/protobuf.c')
-rw-r--r--php/ext/google/protobuf/protobuf.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/php/ext/google/protobuf/protobuf.c b/php/ext/google/protobuf/protobuf.c
index dc730030..abdf14b9 100644
--- a/php/ext/google/protobuf/protobuf.c
+++ b/php/ext/google/protobuf/protobuf.c
@@ -46,6 +46,9 @@ static HashTable* upb_def_to_php_obj_map;
// Global map from message/enum's php class entry to corresponding wrapper
// Descriptor/EnumDescriptor instances.
static HashTable* ce_to_php_obj_map;
+// Global map from message/enum's proto fully-qualified name to corresponding
+// wrapper Descriptor/EnumDescriptor instances.
+static HashTable* proto_to_php_obj_map;
// -----------------------------------------------------------------------------
// Global maps.
@@ -80,6 +83,22 @@ static void add_to_list(HashTable* t, void* value) {
(void**)&pDest);
}
+static void add_to_strtable(HashTable* t, const char* key, int key_size,
+ void* value) {
+ zval* pDest = NULL;
+ php_proto_zend_hash_update_mem(t, key, key_size, &value, sizeof(void*),
+ (void**)&pDest);
+}
+
+static void* get_from_strtable(const HashTable* t, const char* key, int key_size) {
+ void** value;
+ if (php_proto_zend_hash_find_mem(t, key, key_size, (void**)&value) ==
+ FAILURE) {
+ return NULL;
+ }
+ return *value;
+}
+
void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value) {
#if PHP_MAJOR_VERSION < 7
Z_ADDREF_P(value);
@@ -110,6 +129,20 @@ bool class_added(const void* ce) {
return exist_in_table(ce_to_php_obj_map, ce);
}
+void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value) {
+#if PHP_MAJOR_VERSION < 7
+ Z_ADDREF_P(value);
+#else
+ ++GC_REFCOUNT(value);
+#endif
+ add_to_strtable(proto_to_php_obj_map, proto, strlen(proto), value);
+}
+
+PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto) {
+ return (PHP_PROTO_HASHTABLE_VALUE)get_from_strtable(proto_to_php_obj_map,
+ proto, strlen(proto));
+}
+
// -----------------------------------------------------------------------------
// Utilities.
// -----------------------------------------------------------------------------
@@ -163,6 +196,9 @@ static PHP_RINIT_FUNCTION(protobuf) {
ALLOC_HASHTABLE(ce_to_php_obj_map);
zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
+ ALLOC_HASHTABLE(proto_to_php_obj_map);
+ zend_hash_init(proto_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
+
generated_pool = NULL;
generated_pool_php = NULL;
internal_generated_pool_php = NULL;
@@ -177,6 +213,9 @@ static PHP_RSHUTDOWN_FUNCTION(protobuf) {
zend_hash_destroy(ce_to_php_obj_map);
FREE_HASHTABLE(ce_to_php_obj_map);
+ zend_hash_destroy(proto_to_php_obj_map);
+ FREE_HASHTABLE(proto_to_php_obj_map);
+
#if PHP_MAJOR_VERSION < 7
if (generated_pool_php != NULL) {
zval_dtor(generated_pool_php);
@@ -217,6 +256,7 @@ static PHP_MINIT_FUNCTION(protobuf) {
repeated_field_init(TSRMLS_C);
repeated_field_iter_init(TSRMLS_C);
util_init(TSRMLS_C);
+ any_init(TSRMLS_C);
return 0;
}