protobuf.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // ptr -> PHP object cache. This is a weak map that caches lazily-created
  41. // wrapper objects around upb types:
  42. // * upb_msg* -> Message
  43. // * upb_array* -> RepeatedField
  44. // * upb_map*, -> MapField
  45. // * upb_msgdef* -> Descriptor
  46. // * upb_enumdef* -> EnumDescriptor
  47. // * zend_class_entry* -> Descriptor
  48. //
  49. // Each wrapped object should add itself to the map when it is constructed, and
  50. // remove itself from the map when it is destroyed. This is how we ensure that
  51. // the map only contains live objects. The map is weak so it does not actually
  52. // take references to the cached objects.
  53. void ObjCache_Add(const void *key, zend_object *php_obj);
  54. void ObjCache_Delete(const void *key);
  55. bool ObjCache_Get(const void *key, zval *val);
  56. // PHP class name map. This is necessary because the pb_name->php_class_name
  57. // transformation is non-reversible, so when we need to look up a msgdef or
  58. // enumdef by PHP class, we can't turn the class name into a pb_name.
  59. // * php_class_name -> upb_msgdef*
  60. // * php_class_name -> upb_enumdef*
  61. void NameMap_AddMessage(const upb_msgdef *m);
  62. void NameMap_AddEnum(const upb_enumdef *m);
  63. const upb_msgdef *NameMap_GetMessage(zend_class_entry *ce);
  64. const upb_enumdef *NameMap_GetEnum(zend_class_entry *ce);
  65. // We need our own assert() because PHP takes control of NDEBUG in its headers.
  66. #ifdef PBPHP_ENABLE_ASSERTS
  67. #define PBPHP_ASSERT(x) \
  68. do { \
  69. if (!(x)) { \
  70. fprintf(stderr, "Assertion failure at %s:%d %s", __FILE__, __LINE__, \
  71. #x); \
  72. abort(); \
  73. } \
  74. } while (false)
  75. #else
  76. #define PBPHP_ASSERT(x) \
  77. do { \
  78. } while (false && (x))
  79. #endif
  80. #endif // PHP_PROTOBUF_H_