protobuf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_mem(t, (zend_ulong)def, &value,
  52. sizeof(zval*), (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_mem(t, (zend_ulong)def, (void**)&value) ==
  57. FAILURE) {
  58. return NULL;
  59. }
  60. return *value;
  61. }
  62. static bool exist_in_table(const HashTable* t, const void* def) {
  63. void** value;
  64. return (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def,
  65. (void**)&value) == SUCCESS);
  66. }
  67. static void add_to_list(HashTable* t, void* value) {
  68. zval* pDest = NULL;
  69. php_proto_zend_hash_next_index_insert_mem(t, &value, sizeof(void*),
  70. (void**)&pDest);
  71. }
  72. void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value) {
  73. #if PHP_MAJOR_VERSION < 7
  74. Z_ADDREF_P(value);
  75. #else
  76. ++GC_REFCOUNT(value);
  77. #endif
  78. add_to_table(upb_def_to_php_obj_map, def, value);
  79. }
  80. PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def) {
  81. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(upb_def_to_php_obj_map, def);
  82. }
  83. void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value) {
  84. #if PHP_MAJOR_VERSION < 7
  85. Z_ADDREF_P(value);
  86. #else
  87. ++GC_REFCOUNT(value);
  88. #endif
  89. add_to_table(ce_to_php_obj_map, ce, value);
  90. }
  91. PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce) {
  92. return (PHP_PROTO_HASHTABLE_VALUE)get_from_table(ce_to_php_obj_map, ce);
  93. }
  94. bool class_added(const void* ce) {
  95. return exist_in_table(ce_to_php_obj_map, ce);
  96. }
  97. // -----------------------------------------------------------------------------
  98. // Utilities.
  99. // -----------------------------------------------------------------------------
  100. zend_function_entry protobuf_functions[] = {
  101. ZEND_FE_END
  102. };
  103. zend_module_entry protobuf_module_entry = {
  104. STANDARD_MODULE_HEADER,
  105. PHP_PROTOBUF_EXTNAME, // extension name
  106. protobuf_functions, // function list
  107. PHP_MINIT(protobuf), // process startup
  108. PHP_MSHUTDOWN(protobuf), // process shutdown
  109. PHP_RINIT(protobuf), // request shutdown
  110. PHP_RSHUTDOWN(protobuf), // request shutdown
  111. NULL, // extension info
  112. PHP_PROTOBUF_VERSION, // extension version
  113. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  114. PHP_GINIT(protobuf), // globals ctor
  115. PHP_GSHUTDOWN(protobuf), // globals dtor
  116. NULL, // post deactivate
  117. STANDARD_MODULE_PROPERTIES_EX
  118. };
  119. // install module
  120. ZEND_GET_MODULE(protobuf)
  121. // global variables
  122. static PHP_GINIT_FUNCTION(protobuf) {
  123. }
  124. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  125. }
  126. #if PHP_MAJOR_VERSION >= 7
  127. static void php_proto_hashtable_descriptor_release(zval* value) {
  128. void* ptr = Z_PTR_P(value);
  129. zend_object* object = *(zend_object**)ptr;
  130. if(--GC_REFCOUNT(object) == 0) {
  131. zend_objects_store_del(object);
  132. }
  133. efree(ptr);
  134. }
  135. #endif
  136. static PHP_RINIT_FUNCTION(protobuf) {
  137. ALLOC_HASHTABLE(upb_def_to_php_obj_map);
  138. zend_hash_init(upb_def_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  139. ALLOC_HASHTABLE(ce_to_php_obj_map);
  140. zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
  141. generated_pool = NULL;
  142. generated_pool_php = NULL;
  143. internal_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. if (internal_generated_pool_php != NULL) {
  157. zval_dtor(internal_generated_pool_php);
  158. FREE_ZVAL(internal_generated_pool_php);
  159. }
  160. #else
  161. if (generated_pool_php != NULL) {
  162. zval tmp;
  163. ZVAL_OBJ(&tmp, generated_pool_php);
  164. zval_dtor(&tmp);
  165. }
  166. if (internal_generated_pool_php != NULL) {
  167. zval tmp;
  168. ZVAL_OBJ(&tmp, internal_generated_pool_php);
  169. zval_dtor(&tmp);
  170. }
  171. #endif
  172. return 0;
  173. }
  174. static PHP_MINIT_FUNCTION(protobuf) {
  175. descriptor_pool_init(TSRMLS_C);
  176. descriptor_init(TSRMLS_C);
  177. enum_descriptor_init(TSRMLS_C);
  178. enum_value_descriptor_init(TSRMLS_C);
  179. field_descriptor_init(TSRMLS_C);
  180. gpb_type_init(TSRMLS_C);
  181. internal_descriptor_pool_init(TSRMLS_C);
  182. map_field_init(TSRMLS_C);
  183. map_field_iter_init(TSRMLS_C);
  184. message_init(TSRMLS_C);
  185. oneof_descriptor_init(TSRMLS_C);
  186. repeated_field_init(TSRMLS_C);
  187. repeated_field_iter_init(TSRMLS_C);
  188. util_init(TSRMLS_C);
  189. return 0;
  190. }
  191. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  192. PEFREE(message_handlers);
  193. PEFREE(repeated_field_handlers);
  194. PEFREE(repeated_field_iter_handlers);
  195. PEFREE(map_field_handlers);
  196. PEFREE(map_field_iter_handlers);
  197. return 0;
  198. }