protobuf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. zend_hash_index_update(t, (zend_ulong)def, &value, sizeof(zval*), &pDest);
  52. }
  53. static void* get_from_table(const HashTable* t, const void* def) {
  54. void** value;
  55. if (zend_hash_index_find(t, (zend_ulong)def, (void**)&value) == FAILURE) {
  56. zend_error(E_ERROR, "PHP object not found for given definition.\n");
  57. return NULL;
  58. }
  59. return *value;
  60. }
  61. static void add_to_list(HashTable* t, void* value) {
  62. zval* pDest = NULL;
  63. zend_hash_next_index_insert(t, &value, sizeof(void*), &pDest);
  64. }
  65. void add_def_obj(const void* def, zval* value) {
  66. Z_ADDREF_P(value);
  67. add_to_table(upb_def_to_php_obj_map, def, value);
  68. }
  69. zval* get_def_obj(const void* def) {
  70. return (zval*)get_from_table(upb_def_to_php_obj_map, def);
  71. }
  72. void add_ce_obj(const void* ce, zval* value) {
  73. Z_ADDREF_P(value);
  74. add_to_table(ce_to_php_obj_map, ce, value);
  75. }
  76. zval* get_ce_obj(const void* ce) {
  77. return (zval*)get_from_table(ce_to_php_obj_map, ce);
  78. }
  79. // -----------------------------------------------------------------------------
  80. // Utilities.
  81. // -----------------------------------------------------------------------------
  82. zend_function_entry protobuf_functions[] = {
  83. ZEND_FE_END
  84. };
  85. zend_module_entry protobuf_module_entry = {
  86. STANDARD_MODULE_HEADER,
  87. PHP_PROTOBUF_EXTNAME, // extension name
  88. protobuf_functions, // function list
  89. PHP_MINIT(protobuf), // process startup
  90. PHP_MSHUTDOWN(protobuf), // process shutdown
  91. PHP_RINIT(protobuf), // request shutdown
  92. PHP_RSHUTDOWN(protobuf), // request shutdown
  93. NULL, // extension info
  94. PHP_PROTOBUF_VERSION, // extension version
  95. PHP_MODULE_GLOBALS(protobuf), // globals descriptor
  96. PHP_GINIT(protobuf), // globals ctor
  97. PHP_GSHUTDOWN(protobuf), // globals dtor
  98. NULL, // post deactivate
  99. STANDARD_MODULE_PROPERTIES_EX
  100. };
  101. // install module
  102. ZEND_GET_MODULE(protobuf)
  103. // global variables
  104. static PHP_GINIT_FUNCTION(protobuf) {
  105. }
  106. static PHP_GSHUTDOWN_FUNCTION(protobuf) {
  107. }
  108. static PHP_RINIT_FUNCTION(protobuf) {
  109. ALLOC_HASHTABLE(upb_def_to_php_obj_map);
  110. zend_hash_init(upb_def_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0);
  111. ALLOC_HASHTABLE(ce_to_php_obj_map);
  112. zend_hash_init(ce_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0);
  113. generated_pool = NULL;
  114. generated_pool_php = NULL;
  115. }
  116. static PHP_RSHUTDOWN_FUNCTION(protobuf) {
  117. zend_hash_destroy(upb_def_to_php_obj_map);
  118. FREE_HASHTABLE(upb_def_to_php_obj_map);
  119. zend_hash_destroy(ce_to_php_obj_map);
  120. FREE_HASHTABLE(ce_to_php_obj_map);
  121. if (generated_pool_php != NULL) {
  122. zval_dtor(generated_pool_php);
  123. FREE_ZVAL(generated_pool_php);
  124. }
  125. }
  126. static PHP_MINIT_FUNCTION(protobuf) {
  127. map_field_init(TSRMLS_C);
  128. repeated_field_init(TSRMLS_C);
  129. gpb_type_init(TSRMLS_C);
  130. message_init(TSRMLS_C);
  131. descriptor_pool_init(TSRMLS_C);
  132. descriptor_init(TSRMLS_C);
  133. enum_descriptor_init(TSRMLS_C);
  134. util_init(TSRMLS_C);
  135. }
  136. static PHP_MSHUTDOWN_FUNCTION(protobuf) {
  137. PEFREE(message_handlers);
  138. PEFREE(repeated_field_handlers);
  139. PEFREE(map_field_handlers);
  140. }