protobuf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. #include <zend_hash.h>
  32. ZEND_DECLARE_MODULE_GLOBALS(protobuf)
  33. static PHP_GINIT_FUNCTION(protobuf);
  34. static PHP_GSHUTDOWN_FUNCTION(protobuf);
  35. static PHP_RINIT_FUNCTION(protobuf);
  36. static PHP_RSHUTDOWN_FUNCTION(protobuf);
  37. static PHP_MINIT_FUNCTION(protobuf);
  38. static PHP_MSHUTDOWN_FUNCTION(protobuf);
  39. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  40. // instances.
  41. static HashTable* upb_def_to_php_obj_map;
  42. // Global map from message/enum's php class entry to corresponding wrapper
  43. // Descriptor/EnumDescriptor instances.
  44. static HashTable* ce_to_php_obj_map;
  45. // -----------------------------------------------------------------------------
  46. // Global maps.
  47. // -----------------------------------------------------------------------------
  48. static void add_to_table(HashTable* t, const void* def, void* value) {
  49. uint nIndex = (ulong)def & t->nTableMask;
  50. zval* pDest = NULL;
  51. php_proto_zend_hash_index_update(t, (zend_ulong)def, &value, sizeof(zval*),
  52. (void**)&pDest);
  53. }
  54. static void* get_from_table(const HashTable* t, const void* def) {
  55. void** value;
  56. if (php_proto_zend_hash_index_find(t, (zend_ulong)def, (void**)&value) ==
  57. FAILURE) {
  58. zend_error(E_ERROR, "PHP object not found for given definition.\n");
  59. return NULL;
  60. }
  61. return *value;
  62. }
  63. static bool exist_in_table(const HashTable* t, const void* def) {
  64. void** value;
  65. return (php_proto_zend_hash_index_find(t, (zend_ulong)def, (void**)&value) ==
  66. SUCCESS);
  67. }
  68. static void add_to_list(HashTable* t, void* value) {
  69. zval* pDest = NULL;
  70. php_proto_zend_hash_next_index_insert(t, &value, sizeof(void*),
  71. (void**)&pDest);
  72. }
  73. void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value) {
  74. #if PHP_MAJOR_VERSION < 7
  75. Z_ADDREF_P(value);
  76. #else
  77. ++GC_REFCOUNT(value);
  78. #endif
  79. add_to_table(upb_def_to_php_obj_map, def, value);
  80. }
  81. PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def) {
  82. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(upb_def_to_php_obj_map, def);
  83. }
  84. void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value) {
  85. #if PHP_MAJOR_VERSION < 7
  86. Z_ADDREF_P(value);
  87. #else
  88. ++GC_REFCOUNT(value);
  89. #endif
  90. add_to_table(ce_to_php_obj_map, ce, value);
  91. }
  92. PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce) {
  93. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(ce_to_php_obj_map, ce);
  94. }
  95. bool class_added(const void* ce) {
  96. return exist_in_table(ce_to_php_obj_map, ce);
  97. }
  98. // -----------------------------------------------------------------------------
  99. // Utilities.
  100. // -----------------------------------------------------------------------------
  101. zend_function_entry protobuf_functions[] = {
  102. ZEND_FE_END
  103. };
  104. zend_module_entry protobuf_module_entry = {
  105. STANDARD_MODULE_HEADER,
  106. PHP_PROTOBUF_EXTNAME, // extension name
  107. protobuf_functions, // function list
  108. PHP_MINIT(protobuf), // process startup
  109. PHP_MSHUTDOWN(protobuf), // process shutdown
  110. PHP_RINIT(protobuf), // request shutdown
  111. PHP_RSHUTDOWN(protobuf), // request shutdown
  112. NULL, // extension info
  113. PHP_PROTOBUF_VERSION, // extension version
  114. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  115. PHP_GINIT(protobuf), // globals ctor
  116. PHP_GSHUTDOWN(protobuf), // globals dtor
  117. NULL, // post deactivate
  118. STANDARD_MODULE_PROPERTIES_EX
  119. };
  120. // install module
  121. ZEND_GET_MODULE(protobuf)
  122. // global variables
  123. static PHP_GINIT_FUNCTION(protobuf) {
  124. }
  125. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  126. }
  127. #if PHP_MAJOR_VERSION >= 7
  128. static void php_proto_hashtable_descriptor_release(zval* value) {
  129. void* ptr = Z_PTR_P(value);
  130. zend_object* object = *(zend_object**)ptr;
  131. if(--GC_REFCOUNT(object) == 0) {
  132. zend_objects_store_del(object);
  133. }
  134. efree(ptr);
  135. }
  136. #endif
  137. static PHP_RINIT_FUNCTION(protobuf) {
  138. ALLOC_HASHTABLE(upb_def_to_php_obj_map);
  139. zend_hash_init(upb_def_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  140. ALLOC_HASHTABLE(ce_to_php_obj_map);
  141. zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  142. generated_pool = NULL;
  143. generated_pool_php = NULL;
  144. return 0;
  145. }
  146. static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  147. zend_hash_destroy(upb_def_to_php_obj_map);
  148. FREE_HASHTABLE(upb_def_to_php_obj_map);
  149. zend_hash_destroy(ce_to_php_obj_map);
  150. FREE_HASHTABLE(ce_to_php_obj_map);
  151. #if PHP_MAJOR_VERSION < 7
  152. if (generated_pool_php != NULL) {
  153. zval_dtor(generated_pool_php);
  154. FREE_ZVAL(generated_pool_php);
  155. }
  156. #endif
  157. return 0;
  158. }
  159. static PHP_MINIT_FUNCTION(protobuf) {
  160. map_field_init(TSRMLS_C);
  161. map_field_iter_init(TSRMLS_C);
  162. repeated_field_init(TSRMLS_C);
  163. repeated_field_iter_init(TSRMLS_C);
  164. gpb_type_init(TSRMLS_C);
  165. message_init(TSRMLS_C);
  166. descriptor_pool_init(TSRMLS_C);
  167. descriptor_init(TSRMLS_C);
  168. enum_descriptor_init(TSRMLS_C);
  169. util_init(TSRMLS_C);
  170. return 0;
  171. }
  172. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  173. PEFREE(message_handlers);
  174. PEFREE(repeated_field_handlers);
  175. PEFREE(repeated_field_iter_handlers);
  176. PEFREE(map_field_handlers);
  177. PEFREE(map_field_iter_handlers);
  178. return 0;
  179. }