arena.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <Zend/zend_API.h>
  31. #include "php-upb.h"
  32. // -----------------------------------------------------------------------------
  33. // Arena
  34. // -----------------------------------------------------------------------------
  35. typedef struct Arena {
  36. zend_object std;
  37. upb_arena* arena;
  38. } Arena;
  39. zend_class_entry *Arena_class_entry;
  40. static zend_object_handlers Arena_object_handlers;
  41. // PHP Object Handlers /////////////////////////////////////////////////////////
  42. static zend_object* Arena_Create(zend_class_entry *class_type) {
  43. Arena *intern = emalloc(sizeof(Arena));
  44. zend_object_std_init(&intern->std, class_type);
  45. intern->std.handlers = &Arena_object_handlers;
  46. intern->arena = upb_arena_new();
  47. // Skip object_properties_init(), we don't allow derived classes.
  48. return &intern->std;
  49. }
  50. static void Arena_Free(zend_object* obj) {
  51. Arena* intern = (Arena*)obj;
  52. upb_arena_free(intern->arena);
  53. zend_object_std_dtor(&intern->std);
  54. }
  55. // C Functions from arena.h ////////////////////////////////////////////////////
  56. void Arena_Init(zval* val) {
  57. ZVAL_OBJ(val, Arena_Create(Arena_class_entry));
  58. }
  59. upb_arena *Arena_Get(zval *val) {
  60. Arena *a = (Arena*)Z_OBJ_P(val);
  61. return a->arena;
  62. }
  63. // -----------------------------------------------------------------------------
  64. // Module init.
  65. // -----------------------------------------------------------------------------
  66. // No public methods.
  67. static const zend_function_entry Arena_methods[] = {
  68. ZEND_FE_END
  69. };
  70. void Arena_ModuleInit() {
  71. zend_class_entry tmp_ce;
  72. INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\Arena", Arena_methods);
  73. Arena_class_entry = zend_register_internal_class(&tmp_ce);
  74. Arena_class_entry->create_object = Arena_Create;
  75. Arena_class_entry->ce_flags |= ZEND_ACC_FINAL;
  76. memcpy(&Arena_object_handlers, &std_object_handlers,
  77. sizeof(zend_object_handlers));
  78. Arena_object_handlers.free_obj = Arena_Free;
  79. }