protobuf.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef PHP_PROTOBUF_H_
  31. #define PHP_PROTOBUF_H_
  32. #include <php.h>
  33. #include <stdbool.h>
  34. #include "php-upb.h"
  35. const zval *get_generated_pool();
  36. #if PHP_VERSION_ID < 70300
  37. #define GC_ADDREF(h) ++GC_REFCOUNT(h)
  38. #define GC_DELREF(h) --GC_REFCOUNT(h)
  39. #endif
  40. // Since php 7.4, the write_property() object handler now returns the assigned
  41. // value (after possible type coercions) rather than void.
  42. // https://github.com/php/php-src/blob/PHP-7.4.0/UPGRADING.INTERNALS#L171-L173
  43. #if PHP_VERSION_ID < 70400
  44. #define PROTO_RETURN_VAL void
  45. #else
  46. #define PROTO_RETURN_VAL zval*
  47. #endif
  48. // Sine php 8.0, the Object Handlers API was changed to receive zend_object*
  49. // instead of zval* and zend_string* instead of zval* for property names.
  50. // https://github.com/php/php-src/blob/php-8.0.0beta1/UPGRADING.INTERNALS#L37-L39
  51. #if PHP_VERSION_ID < 80000
  52. #define PROTO_VAL zval
  53. #define PROTO_STR zval
  54. #define PROTO_MSG_P(obj) (Message*)Z_OBJ_P(obj)
  55. #define PROTO_STRVAL_P(obj) Z_STRVAL_P(obj)
  56. #define PROTO_STRLEN_P(obj) Z_STRLEN_P(obj)
  57. #else
  58. #define PROTO_VAL zend_object
  59. #define PROTO_STR zend_string
  60. #define PROTO_MSG_P(obj) (Message*)(obj)
  61. #define PROTO_STRVAL_P(obj) ZSTR_VAL(obj)
  62. #define PROTO_STRLEN_P(obj) ZSTR_LEN(obj)
  63. #endif
  64. #define PHP_PROTOBUF_VERSION "3.13.0.1"
  65. // ptr -> PHP object cache. This is a weak map that caches lazily-created
  66. // wrapper objects around upb types:
  67. // * upb_msg* -> Message
  68. // * upb_array* -> RepeatedField
  69. // * upb_map*, -> MapField
  70. // * upb_msgdef* -> Descriptor
  71. // * upb_enumdef* -> EnumDescriptor
  72. // * zend_class_entry* -> Descriptor
  73. //
  74. // Each wrapped object should add itself to the map when it is constructed, and
  75. // remove itself from the map when it is destroyed. This is how we ensure that
  76. // the map only contains live objects. The map is weak so it does not actually
  77. // take references to the cached objects.
  78. void ObjCache_Add(const void *key, zend_object *php_obj);
  79. void ObjCache_Delete(const void *key);
  80. bool ObjCache_Get(const void *key, zval *val);
  81. // PHP class name map. This is necessary because the pb_name->php_class_name
  82. // transformation is non-reversible, so when we need to look up a msgdef or
  83. // enumdef by PHP class, we can't turn the class name into a pb_name.
  84. // * php_class_name -> upb_msgdef*
  85. // * php_class_name -> upb_enumdef*
  86. void NameMap_AddMessage(const upb_msgdef *m);
  87. void NameMap_AddEnum(const upb_enumdef *m);
  88. const upb_msgdef *NameMap_GetMessage(zend_class_entry *ce);
  89. const upb_enumdef *NameMap_GetEnum(zend_class_entry *ce);
  90. // We need our own assert() because PHP takes control of NDEBUG in its headers.
  91. #ifdef PBPHP_ENABLE_ASSERTS
  92. #define PBPHP_ASSERT(x) \
  93. do { \
  94. if (!(x)) { \
  95. fprintf(stderr, "Assertion failure at %s:%d %s", __FILE__, __LINE__, \
  96. #x); \
  97. abort(); \
  98. } \
  99. } while (false)
  100. #else
  101. #define PBPHP_ASSERT(x) \
  102. do { \
  103. } while (false && (x))
  104. #endif
  105. #endif // PHP_PROTOBUF_H_