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