OLD | NEW |
| 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 |
1 #include "protobuf.h" | 31 #include "protobuf.h" |
2 | 32 |
3 #include <zend_hash.h> | 33 #include <zend_hash.h> |
4 | 34 |
5 ZEND_DECLARE_MODULE_GLOBALS(protobuf) | 35 ZEND_DECLARE_MODULE_GLOBALS(protobuf) |
6 static PHP_GINIT_FUNCTION(protobuf); | 36 static PHP_GINIT_FUNCTION(protobuf); |
7 static PHP_GSHUTDOWN_FUNCTION(protobuf); | 37 static PHP_GSHUTDOWN_FUNCTION(protobuf); |
| 38 static PHP_RINIT_FUNCTION(protobuf); |
| 39 static PHP_RSHUTDOWN_FUNCTION(protobuf); |
| 40 static PHP_MINIT_FUNCTION(protobuf); |
| 41 static PHP_MSHUTDOWN_FUNCTION(protobuf); |
| 42 |
| 43 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor |
| 44 // instances. |
| 45 static HashTable* upb_def_to_php_obj_map; |
| 46 // Global map from message/enum's php class entry to corresponding wrapper |
| 47 // Descriptor/EnumDescriptor instances. |
| 48 static HashTable* ce_to_php_obj_map; |
8 | 49 |
9 // ----------------------------------------------------------------------------- | 50 // ----------------------------------------------------------------------------- |
10 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor | 51 // Global maps. |
11 // instances. | |
12 // ----------------------------------------------------------------------------- | 52 // ----------------------------------------------------------------------------- |
13 | 53 |
14 void add_def_obj(const void* def, zval* value) { | 54 static void add_to_table(HashTable* t, const void* def, void* value) { |
15 uint nIndex = (ulong)def & PROTOBUF_G(upb_def_to_php_obj_map).nTableMask; | 55 uint nIndex = (ulong)def & t->nTableMask; |
16 | 56 |
17 zval* pDest = NULL; | 57 zval* pDest = NULL; |
18 Z_ADDREF_P(value); | 58 zend_hash_index_update(t, (zend_ulong)def, &value, sizeof(zval*), (void**)&pDe
st); |
19 zend_hash_index_update(&PROTOBUF_G(upb_def_to_php_obj_map), (zend_ulong)def, | |
20 &value, sizeof(zval*), &pDest); | |
21 } | 59 } |
22 | 60 |
23 zval* get_def_obj(const void* def) { | 61 static void* get_from_table(const HashTable* t, const void* def) { |
24 zval** value; | 62 void** value; |
25 if (zend_hash_index_find(&PROTOBUF_G(upb_def_to_php_obj_map), (zend_ulong)def, | 63 if (zend_hash_index_find(t, (zend_ulong)def, (void**)&value) == FAILURE) { |
26 &value) == FAILURE) { | |
27 zend_error(E_ERROR, "PHP object not found for given definition.\n"); | 64 zend_error(E_ERROR, "PHP object not found for given definition.\n"); |
28 return NULL; | 65 return NULL; |
29 } | 66 } |
30 return *value; | 67 return *value; |
31 } | 68 } |
32 | 69 |
| 70 static void add_to_list(HashTable* t, void* value) { |
| 71 zval* pDest = NULL; |
| 72 zend_hash_next_index_insert(t, &value, sizeof(void*), (void**)&pDest); |
| 73 } |
| 74 |
| 75 void add_def_obj(const void* def, zval* value) { |
| 76 Z_ADDREF_P(value); |
| 77 add_to_table(upb_def_to_php_obj_map, def, value); |
| 78 } |
| 79 |
| 80 zval* get_def_obj(const void* def) { |
| 81 return (zval*)get_from_table(upb_def_to_php_obj_map, def); |
| 82 } |
| 83 |
| 84 void add_ce_obj(const void* ce, zval* value) { |
| 85 Z_ADDREF_P(value); |
| 86 add_to_table(ce_to_php_obj_map, ce, value); |
| 87 } |
| 88 |
| 89 zval* get_ce_obj(const void* ce) { |
| 90 return (zval*)get_from_table(ce_to_php_obj_map, ce); |
| 91 } |
| 92 |
33 // ----------------------------------------------------------------------------- | 93 // ----------------------------------------------------------------------------- |
34 // Utilities. | 94 // Utilities. |
35 // ----------------------------------------------------------------------------- | 95 // ----------------------------------------------------------------------------- |
36 | 96 |
37 // define the function(s) we want to add | |
38 zend_function_entry protobuf_functions[] = { | 97 zend_function_entry protobuf_functions[] = { |
39 ZEND_FE(get_generated_pool, NULL) | |
40 ZEND_FE_END | 98 ZEND_FE_END |
41 }; | 99 }; |
42 | 100 |
43 // "protobuf_functions" refers to the struct defined above | |
44 // we'll be filling in more of this later: you can use this to specify | |
45 // globals, php.ini info, startup and teardown functions, etc. | |
46 zend_module_entry protobuf_module_entry = { | 101 zend_module_entry protobuf_module_entry = { |
47 STANDARD_MODULE_HEADER, | 102 STANDARD_MODULE_HEADER, |
48 PHP_PROTOBUF_EXTNAME, // extension name | 103 PHP_PROTOBUF_EXTNAME, // extension name |
49 protobuf_functions, // function list | 104 protobuf_functions, // function list |
50 PHP_MINIT(protobuf), // process startup | 105 PHP_MINIT(protobuf), // process startup |
51 NULL, // process shutdown | 106 PHP_MSHUTDOWN(protobuf), // process shutdown |
52 NULL, // request startup | 107 PHP_RINIT(protobuf), // request shutdown |
53 NULL, // request shutdown | 108 PHP_RSHUTDOWN(protobuf), // request shutdown |
54 NULL, // extension info | 109 NULL, // extension info |
55 PHP_PROTOBUF_VERSION, // extension version | 110 PHP_PROTOBUF_VERSION, // extension version |
56 PHP_MODULE_GLOBALS(protobuf), // globals descriptor | 111 PHP_MODULE_GLOBALS(protobuf), // globals descriptor |
57 PHP_GINIT(protobuf), // globals ctor | 112 PHP_GINIT(protobuf), // globals ctor |
58 PHP_GSHUTDOWN(protobuf), // globals dtor | 113 PHP_GSHUTDOWN(protobuf), // globals dtor |
59 NULL, // post deactivate | 114 NULL, // post deactivate |
60 STANDARD_MODULE_PROPERTIES_EX | 115 STANDARD_MODULE_PROPERTIES_EX |
61 }; | 116 }; |
62 | 117 |
63 // install module | 118 // install module |
64 ZEND_GET_MODULE(protobuf) | 119 ZEND_GET_MODULE(protobuf) |
65 | 120 |
66 // global variables | 121 // global variables |
67 static PHP_GINIT_FUNCTION(protobuf) { | 122 static PHP_GINIT_FUNCTION(protobuf) { |
68 protobuf_globals->generated_pool = NULL; | |
69 generated_pool = NULL; | |
70 protobuf_globals->message_handlers = NULL; | |
71 zend_hash_init(&protobuf_globals->upb_def_to_php_obj_map, 16, NULL, | |
72 ZVAL_PTR_DTOR, 0); | |
73 } | 123 } |
74 | 124 |
75 static PHP_GSHUTDOWN_FUNCTION(protobuf) { | 125 static PHP_GSHUTDOWN_FUNCTION(protobuf) { |
76 if (protobuf_globals->generated_pool != NULL) { | |
77 FREE_ZVAL(protobuf_globals->generated_pool); | |
78 } | |
79 if (protobuf_globals->message_handlers != NULL) { | |
80 FREE(protobuf_globals->message_handlers); | |
81 } | |
82 zend_hash_destroy(&protobuf_globals->upb_def_to_php_obj_map); | |
83 } | 126 } |
84 | 127 |
85 PHP_MINIT_FUNCTION(protobuf) { | 128 static PHP_RINIT_FUNCTION(protobuf) { |
| 129 ALLOC_HASHTABLE(upb_def_to_php_obj_map); |
| 130 zend_hash_init(upb_def_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0); |
| 131 |
| 132 ALLOC_HASHTABLE(ce_to_php_obj_map); |
| 133 zend_hash_init(ce_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0); |
| 134 |
| 135 generated_pool = NULL; |
| 136 generated_pool_php = NULL; |
| 137 |
| 138 return 0; |
| 139 } |
| 140 |
| 141 static PHP_RSHUTDOWN_FUNCTION(protobuf) { |
| 142 zend_hash_destroy(upb_def_to_php_obj_map); |
| 143 FREE_HASHTABLE(upb_def_to_php_obj_map); |
| 144 |
| 145 zend_hash_destroy(ce_to_php_obj_map); |
| 146 FREE_HASHTABLE(ce_to_php_obj_map); |
| 147 |
| 148 if (generated_pool_php != NULL) { |
| 149 zval_dtor(generated_pool_php); |
| 150 FREE_ZVAL(generated_pool_php); |
| 151 } |
| 152 |
| 153 return 0; |
| 154 } |
| 155 |
| 156 static PHP_MINIT_FUNCTION(protobuf) { |
| 157 map_field_init(TSRMLS_C); |
| 158 repeated_field_init(TSRMLS_C); |
| 159 repeated_field_iter_init(TSRMLS_C); |
| 160 gpb_type_init(TSRMLS_C); |
| 161 message_init(TSRMLS_C); |
86 descriptor_pool_init(TSRMLS_C); | 162 descriptor_pool_init(TSRMLS_C); |
87 descriptor_init(TSRMLS_C); | 163 descriptor_init(TSRMLS_C); |
88 message_builder_context_init(TSRMLS_C); | 164 enum_descriptor_init(TSRMLS_C); |
| 165 util_init(TSRMLS_C); |
| 166 |
| 167 return 0; |
89 } | 168 } |
| 169 |
| 170 static PHP_MSHUTDOWN_FUNCTION(protobuf) { |
| 171 PEFREE(message_handlers); |
| 172 PEFREE(repeated_field_handlers); |
| 173 PEFREE(map_field_handlers); |
| 174 |
| 175 return 0; |
| 176 } |
OLD | NEW |